1 19 20 package org.netbeans.core.registry.enabledisabletest; 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.TestMFS; 26 import org.netbeans.junit.NbTestCase; 27 import org.netbeans.junit.NbTestSuite; 28 import org.netbeans.spi.registry.SpiUtils; 29 import org.openide.cookies.InstanceCookie; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileSystem; 32 import org.openide.filesystems.LocalFileSystem; 33 import org.openide.filesystems.XMLFileSystem; 34 import org.openide.loaders.DataObject; 35 import org.openide.loaders.FolderLookup; 36 import org.openide.util.Lookup; 37 import org.xml.sax.SAXException ; 38 39 import java.beans.PropertyVetoException ; 40 import java.io.IOException ; 41 import java.net.URL ; 42 43 public class CompatibilityTest extends NbTestCase { 44 private static final String LAYER = "data/compatible_layer.xml"; 45 private FileSystem mfs; 46 47 private static final String DOC_NAME = "module"; 48 private static Context docElementCtx; 49 50 private static Exception moduleInitExc; 51 52 static { 53 try { 54 ModuleUtils.DEFAULT.install(); 55 ModuleUtils.DEFAULT.enableBookModule(true); 56 ModuleUtils.DEFAULT.enableCDModule(true); 57 } catch (Exception e) { 58 moduleInitExc = e; 59 } 60 } 61 62 public CompatibilityTest(String s) { 63 super(s); 64 } 65 public static void main(String [] args) { 66 TestRunner.run(new NbTestSuite(CompatibilityTest.class)); 67 } 68 69 public void testMethodValue1 () throws Exception { 70 getAndTestObject("methodvalue1"); 71 } 72 73 public void testMethodValue2 () throws Exception { 74 getAndTestObject("methodvalue2"); 75 76 } 77 78 public void testMethodValue3 () throws Exception { 79 Object book = getAndTestObject("methodvalue3"); 80 81 assertTrue("Author should be Tom. See: " + book.toString(),book.toString().indexOf("Tom") > 0); 82 assertTrue("Title should be Sorry.See: " + book.toString(),book.toString().indexOf("Sorry") > 0); 83 } 84 85 public void testNewValue1 () throws Exception { 86 getAndTestObject("newvalue1"); 87 } 88 89 public void testBookTest () throws Exception { 90 getAndTestObject("bookTest"); 91 } 92 93 94 private Object getAndTestObject(final String bindingName) throws Exception { 95 if (moduleInitExc != null) 96 throw moduleInitExc; 97 Object retVal = getContext().getSubcontext(bindingName).getObject(bindingName, null); 98 assertNotNull(retVal); 99 100 FileObject fileObject = getFileObject(bindingName); 101 DataObject dataObject = DataObject.find(fileObject); 102 assertNotNull(dataObject); 103 104 InstanceCookie cookie = (InstanceCookie)dataObject.getCookie(InstanceCookie.class); 105 assertNotNull(cookie); 106 107 Object instance = cookie.instanceCreate(); 108 assertNotNull(instance); 109 110 Lookup lkp = new FolderLookup(dataObject.getFolder()).getLookup(); 111 Object lookupItem = lkp.lookup(instance.getClass()); 112 assertNotNull(lookupItem); 113 assertTrue(lookupItem == instance); 114 115 119 return retVal; 120 } 121 122 123 private Context getContext() throws PropertyVetoException , IOException , SAXException { 124 if (docElementCtx == null) { 125 docElementCtx = SpiUtils.createContext(FileSystemContextFactory.createContext(getFileSystem().getRoot())); 126 docElementCtx = docElementCtx.getSubcontext(DOC_NAME); 127 } 128 129 return docElementCtx; 130 } 131 132 private FileSystem getFileSystem() throws PropertyVetoException , IOException , SAXException { 133 if (mfs == null) { 134 LocalFileSystem lfs = new LocalFileSystem(); 135 lfs.setRootDirectory(getWorkDir()); 136 137 URL u1 = getClass().getResource(LAYER); 138 139 FileSystem xfs1 = new XMLFileSystem( u1 ); 140 mfs = new TestMFS( new FileSystem[] { lfs, xfs1 } ); 141 } 142 return mfs; 143 } 144 145 146 private FileObject getFileObject(final String bindingName) throws PropertyVetoException , IOException , SAXException { 147 final String absolutePath = getContext().getAbsoluteContextName() + "/" + bindingName + "/" + bindingName; 148 final String [] extensions = new String [] {"settings", "instance", "xml"}; 149 150 FileObject fo = null; 151 for (int i = 0; i < extensions.length; i++) { 152 String name = absolutePath + "." + extensions[i]; 153 fo = getFileSystem().findResource(name); 154 if (fo != null) break; 155 } 156 157 assertNotNull(fo); 158 return fo; 159 } 160 } 161 | Popular Tags |