1 19 20 package org.netbeans.core.registry; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.api.registry.Context; 24 import org.netbeans.api.registry.fs.FileSystemContextFactory; 25 import org.netbeans.core.registry.convertors.TestBean; 26 import org.netbeans.core.registry.convertors.TestBeanConvertor; 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.junit.NbTestSuite; 29 import org.netbeans.spi.registry.BasicContext; 30 import org.netbeans.spi.registry.SpiUtils; 31 import org.openide.filesystems.*; 32 import org.openide.xml.XMLUtil; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 import org.xml.sax.InputSource ; 36 37 import java.io.InputStream ; 38 import java.net.URL ; 39 import java.util.Arrays ; 40 import java.util.Collections ; 41 import java.util.List ; 42 43 47 public class ConvertedObjectTest extends NbTestCase { 48 49 public ConvertedObjectTest(String name) { 50 super(name); 51 } 52 53 public static void main(String [] args) { 54 TestRunner.run(new NbTestSuite(ConvertedObjectTest.class)); 55 } 56 57 private Context c; 58 private FileSystem fs; 59 60 protected void setUp() throws Exception { 61 super.setUp(); 62 URL u = ConvertedObjectTest.class.getResource("data/object-layer.xml"); 63 FileSystem xfs = new XMLFileSystem(u); 64 LocalFileSystem lfs = new LocalFileSystem(); 65 clearWorkDir(); 66 lfs.setRootDirectory(getWorkDir()); 67 fs = new MultiFileSystem(new FileSystem[] {lfs, xfs}); 68 BasicContext rootCtx = FileSystemContextFactory.createContext(fs.getRoot()); 69 Context root = SpiUtils.createContext(rootCtx); 70 c = root.createSubcontext("test-context"); 71 } 72 76 77 78 public void testRead() throws Exception { 79 TestBean t1 = (TestBean)c.getObject("binding1", null); 80 assertNotNull(t1); 81 assertEquals("val1", t1.getProp1()); 82 assertEquals("val2", t1.getProp2()); 83 TestBean t2 = (TestBean)c.getObject("binding2", null); 84 assertNotNull(t1); 85 assertEquals("val3", t2.getProp1()); 86 assertEquals("val4", t2.getProp2()); 87 List l = c.getOrderedNames(); 88 Collections.sort(l); 89 assertEquals(Arrays.asList(new String [] {"binding1", "binding2"}), l); 90 } 91 92 93 public void testWriteNew() throws Exception { 94 TestBean t = new TestBean(); 95 t.setProp1("gen1"); 96 t.setProp2("gen2"); 97 c.putObject("binding3", t); 98 FileObject fo = fs.findResource("test-context/binding3.xml"); 99 assertNotNull(fo); 100 InputStream is = fo.getInputStream(); 101 try { 102 Document d = XMLUtil.parse(new InputSource (is), false, true, null, null); 103 Element e = d.getDocumentElement(); 104 assertEquals(TestBeanConvertor.NS, e.getNamespaceURI()); 105 assertEquals("test-bean", e.getLocalName()); 106 assertEquals("gen1", e.getAttribute("prop1")); 107 assertEquals("gen2", e.getAttribute("prop2")); 108 } finally { 109 is.close(); 110 } 111 } 112 113 114 public void testOverwrite() throws Exception { 115 TestBean t = (TestBean)c.getObject("binding1", null); 116 assertNotNull(t); 117 assertEquals("val1", t.getProp1()); 118 assertEquals("val2", t.getProp2()); 119 t.setProp1("val1-modified"); 120 c.putObject("binding1", t); 121 assertEquals(t, c.getObject("binding1", null)); 122 FileObject fo = fs.findResource("test-context/binding1.xml"); 123 assertNotNull(fo); 124 InputStream is = fo.getInputStream(); 125 try { 126 Document d = XMLUtil.parse(new InputSource (is), false, true, null, null); 127 Element e = d.getDocumentElement(); 128 assertEquals(TestBeanConvertor.NS, e.getNamespaceURI()); 129 assertEquals("test-bean", e.getLocalName()); 130 assertEquals("val1-modified", e.getAttribute("prop1")); 131 assertEquals("val2", e.getAttribute("prop2")); 132 } finally { 133 is.close(); 134 } 135 } 136 137 } 138 | Popular Tags |