1 19 20 package org.openide.loaders; 21 22 import java.io.File ; 23 import java.io.FileWriter ; 24 import org.netbeans.junit.NbTestCase; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileSystem; 27 import org.openide.filesystems.FileUtil; 28 import org.openide.filesystems.MultiFileSystem; 29 import org.openide.filesystems.XMLFileSystem; 30 31 32 35 public class InstanceDataObjectGetNameTest extends NbTestCase { 36 private DataObject obj; 37 private FileSystem fs; 38 39 40 public InstanceDataObjectGetNameTest(String name) { 41 super (name); 42 } 43 44 private static String name; 45 private static int cnt; 46 public static String computeName() { 47 cnt++; 48 return name; 49 } 50 51 protected void setUp () throws Exception { 52 53 cnt = 0; 54 55 File f = new File (getWorkDir(), "layer.xml"); 56 FileWriter w = new FileWriter (f); 57 w.write("<filesystem><file name='x.instance'> "); 58 w.write(" <attr name='name' methodvalue='" + InstanceDataObjectGetNameTest.class.getName() + ".computeName'/> "); 59 w.write("</file></filesystem> "); 60 w.close(); 61 62 fs = new MultiFileSystem(new FileSystem[] { 63 FileUtil.createMemoryFileSystem(), 64 new XMLFileSystem(f.toURL()) 65 }); 66 FileObject fo = fs.findResource("x.instance"); 67 assertNotNull(fo); 68 69 assertNull(fo.getAttribute("name")); 70 assertEquals("One call", 1, cnt); 71 cnt = 0; 73 74 obj = DataObject.find(fo); 75 76 assertEquals("No calls now", 0, cnt); 77 } 78 79 public void testNameIsCached() throws Exception { 80 if (!(obj instanceof InstanceDataObject)) { 81 fail("We need IDO : " + obj); 82 } 83 84 name = "Ahoj"; 85 assertEquals("We can influence a name", "Ahoj", obj.getName()); 86 assertEquals("one call", 1, cnt); 87 assertEquals("Name stays the same", "Ahoj", obj.getName()); 88 assertEquals("no new call", 1, cnt); 89 90 name = "kuk"; 91 assertEquals("Name stays the same", "Ahoj", obj.getName()); 92 assertEquals("no new call", 1, cnt); 93 94 obj.getPrimaryFile().setAttribute("someattr", "new"); 95 96 assertEquals("Name changes as attribute changes fired", "kuk", obj.getName()); 97 assertEquals("of course new call is there", 2, cnt); 98 99 } 100 } 101 | Popular Tags |