KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > project > TestUtilTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.project;
21
22 import java.net.URL JavaDoc;
23 import java.util.Date JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.FileUtil;
27 import org.openide.util.Lookup;
28 import org.openide.util.lookup.Lookups;
29
30 /**
31  * Test functionality of TestUtil.
32  * @author Jesse Glick
33  */

34 public class TestUtilTest extends NbTestCase {
35
36     public TestUtilTest(String JavaDoc name) {
37         super(name);
38     }
39
40     protected void tearDown() throws Exception JavaDoc {
41         TestUtil.setLookup(new Object JavaDoc[0]);
42         super.tearDown();
43     }
44     
45     public void testSetLookup() throws Exception JavaDoc {
46         TestUtil.setLookup(Lookups.singleton("hello"));
47         assertEquals("initial lookup works", "hello", Lookup.getDefault().lookup(String JavaDoc.class));
48         TestUtil.setLookup(Lookups.singleton("goodbye"));
49         assertEquals("modified lookup works", "goodbye", Lookup.getDefault().lookup(String JavaDoc.class));
50         TestUtil.setLookup(Lookup.EMPTY);
51         assertEquals("cleared lookup works", null, Lookup.getDefault().lookup(String JavaDoc.class));
52     }
53     
54     public void testCreateFileFromContent() throws Exception JavaDoc {
55         URL JavaDoc content = TestUtilTest.class.getResource("TestUtilTest.class");
56         assertNotNull("have TestUtilTest.class", content);
57         int length = content.openConnection().getContentLength();
58         assertTrue("have some length", length > 0);
59         FileObject scratch = TestUtil.makeScratchDir(this);
60         assertTrue("scratch is a dir", scratch.isFolder());
61         assertEquals("scratch is empty", 0, scratch.getChildren().length);
62         FileObject a = TestUtil.createFileFromContent(content, scratch, "d/a");
63         assertTrue("a is a file", a.isData());
64         assertEquals("right path", "d/a", FileUtil.getRelativePath(scratch, a));
65         assertEquals("right length", length, (int)a.getSize());
66         FileObject b = TestUtil.createFileFromContent(null, scratch, "d2/b");
67         assertTrue("b is a file", b.isData());
68         assertEquals("right path", "d2/b", FileUtil.getRelativePath(scratch, b));
69         assertEquals("b is empty", 0, (int)b.getSize());
70         Date JavaDoc created = b.lastModified();
71         Thread.sleep(1500); // Unix has coarse timestamp marking
72
assertEquals("got same b back", b, TestUtil.createFileFromContent(null, scratch, "d2/b"));
73         Date JavaDoc modified = b.lastModified();
74         assertTrue("touched and changed timestamp", modified.after(created));
75     }
76     
77 }
78
Popular Tags