1 19 20 package org.netbeans.api.project; 21 22 import java.net.URL ; 23 import java.util.Date ; 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 34 public class TestUtilTest extends NbTestCase { 35 36 public TestUtilTest(String name) { 37 super(name); 38 } 39 40 protected void tearDown() throws Exception { 41 TestUtil.setLookup(new Object [0]); 42 super.tearDown(); 43 } 44 45 public void testSetLookup() throws Exception { 46 TestUtil.setLookup(Lookups.singleton("hello")); 47 assertEquals("initial lookup works", "hello", Lookup.getDefault().lookup(String .class)); 48 TestUtil.setLookup(Lookups.singleton("goodbye")); 49 assertEquals("modified lookup works", "goodbye", Lookup.getDefault().lookup(String .class)); 50 TestUtil.setLookup(Lookup.EMPTY); 51 assertEquals("cleared lookup works", null, Lookup.getDefault().lookup(String .class)); 52 } 53 54 public void testCreateFileFromContent() throws Exception { 55 URL 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 created = b.lastModified(); 71 Thread.sleep(1500); assertEquals("got same b back", b, TestUtil.createFileFromContent(null, scratch, "d2/b")); 73 Date modified = b.lastModified(); 74 assertTrue("touched and changed timestamp", modified.after(created)); 75 } 76 77 } 78 | Popular Tags |