1 19 20 package org.netbeans.modules.db.explorer.driver; 21 22 import java.io.InputStream ; 23 import java.io.OutputStreamWriter ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.net.URLEncoder ; 27 import java.util.Collection ; 28 import org.netbeans.api.db.explorer.JDBCDriver; 29 import org.netbeans.modules.db.test.DOMCompare; 30 import org.netbeans.modules.db.test.TestBase; 31 import org.netbeans.modules.db.test.Util; 32 import org.openide.cookies.InstanceCookie; 33 import org.openide.filesystems.FileLock; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileSystem; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.Repository; 38 import org.openide.loaders.DataFolder; 39 import org.openide.loaders.DataObject; 40 import org.openide.loaders.FolderLookup; 41 import org.openide.util.Lookup; 42 import org.openide.xml.EntityCatalog; 43 import org.openide.xml.XMLUtil; 44 import org.w3c.dom.Document ; 45 import org.xml.sax.ErrorHandler ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.SAXParseException ; 49 50 54 public class JDBCDriverConvertorTest extends TestBase { 55 56 public JDBCDriverConvertorTest(String testName) { 57 super(testName); 58 } 59 60 protected void setUp() throws Exception { 61 Util.deleteDriverFiles(); 62 } 63 64 public void testReadXml() throws Exception { 65 FileObject fo = createDriverFile10("org_foo_FooDriver_10.xml", Util.getDriversFolder()); 67 DataObject dobj = DataObject.find(fo); 68 InstanceCookie ic = (InstanceCookie)dobj.getCookie(InstanceCookie.class); 69 assertNotNull(ic); 70 71 JDBCDriver driver = (JDBCDriver)ic.instanceCreate(); 72 assertEquals("foo_driver", driver.getName()); 73 assertEquals("org.foo.FooDriver", driver.getClassName()); 74 assertEquals(2, driver.getURLs().length); 75 assertEquals(new URL ("file:///foo1.jar"), driver.getURLs()[0]); 76 assertEquals(new URL ("file:///foo2.jar"), driver.getURLs()[1]); 77 78 fo = createDriverFile11("org_foo_FooDriver_11.xml", Util.getDriversFolder()); 80 dobj = DataObject.find(fo); 81 ic = (InstanceCookie)dobj.getCookie(InstanceCookie.class); 82 assertNotNull(ic); 83 84 driver = (JDBCDriver)ic.instanceCreate(); 85 assertEquals("foo_driver", driver.getName()); 86 assertEquals("Foo Driver", driver.getDisplayName()); 87 assertEquals("org.foo.FooDriver", driver.getClassName()); 88 assertEquals(2, driver.getURLs().length); 89 assertEquals(new URL ("file:///foo1.jar"), driver.getURLs()[0]); 90 assertEquals(new URL ("file:///foo2.jar"), driver.getURLs()[1]); 91 } 92 93 public void testWriteXml() throws Exception { 94 JDBCDriver driver = JDBCDriver.create("bar_driver", "Bar Driver", "org.bar.BarDriver", new URL [] { new URL ("file:///bar1.jar"), new URL ("file:///bar2.jar") }); 95 JDBCDriverConvertor.create(driver); 96 97 FileObject fo = Util.getDriversFolder().getFileObject("org_bar_BarDriver.xml"); 98 99 ErrorHandlerImpl errHandler = new ErrorHandlerImpl(); 100 Document doc = null; 101 InputStream input = fo.getInputStream(); 102 try { 103 doc = XMLUtil.parse(new InputSource (input), true, true, errHandler, EntityCatalog.getDefault()); 104 } finally { 105 input.close(); 106 } 107 108 assertFalse("JDBCDriverConvertor generates invalid XML acc to the DTD!", errHandler.error); 109 110 Document goldenDoc = null; 111 input = getClass().getResourceAsStream("bar-driver.xml"); 112 try { 113 goldenDoc = XMLUtil.parse(new InputSource (input), true, true, null, EntityCatalog.getDefault()); 114 } finally { 115 input.close(); 116 } 117 118 assertTrue(DOMCompare.compareDocuments(doc, goldenDoc)); 119 } 120 121 127 public void testSameInstanceAfterCreate() throws Exception { 128 JDBCDriver driver1 = JDBCDriver.create("bar_driver", "Bar Driver", "org.bar.BarDriver", new URL [] { new URL ("file:///bar1.jar"), new URL ("file:///bar2.jar") }); 129 DataObject dobj1 = JDBCDriverConvertor.create(driver1); 130 JDBCDriver driver2 = JDBCDriver.create("foo_driver", "Foo Driver", "org.foo.FooDriver", new URL [] { new URL ("file:///foo1.jar"), new URL ("file:///foo2.jar") }); 131 DataObject dobj2 = JDBCDriverConvertor.create(driver2); 132 assertSame(driver1, ((InstanceCookie)dobj1.getCookie(InstanceCookie.class)).instanceCreate()); 133 assertSame(driver2, ((InstanceCookie)dobj2.getCookie(InstanceCookie.class)).instanceCreate()); 134 } 135 136 public void testLookup() throws Exception { 137 FileObject parent = Util.getDriversFolder(); 138 createDriverFile11("org_foo_FooDriver.xml", parent); 139 FolderLookup lookup = new FolderLookup(DataFolder.findFolder(parent)); 140 Lookup.Result result = lookup.getLookup().lookup(new Lookup.Template(JDBCDriver.class)); 141 Collection instances = result.allInstances(); 142 assertEquals(1, instances.size()); 143 } 144 145 public void testEncodeURL() throws Exception { 146 assertEquals(new URL ("file:///test%20file#fragment"), JDBCDriverConvertor.encodeURL(new URL ("file:///test file#fragment"))); 147 assertEquals(new URL ("file:///test%20file"), JDBCDriverConvertor.encodeURL(new URL ("file:///test file"))); 148 } 149 150 public void testImportOldDrivers() throws Exception { 151 final String UNENCODED_URL = "file:///foo 1.jar"; 152 153 FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); 154 FileObject oldRoot = sfs.findResource(JDBCDriverConvertor.OLD_DRIVERS_PATH); 155 if (oldRoot == null) { 156 oldRoot = FileUtil.createFolder(sfs.getRoot(), JDBCDriverConvertor.OLD_DRIVERS_PATH); 157 } 158 URL [] urls = new URL [] { new URL (UNENCODED_URL) }; 159 createDriverFile10("testdriver.xml", oldRoot, urls); 160 161 JDBCDriverConvertor.importOldDrivers(); 162 163 assertEquals(0, oldRoot.getChildren().length); 164 165 FileObject newRoot = Util.getDriversFolder(); 166 assertEquals(1, newRoot.getChildren().length); 167 168 FolderLookup lookup = new FolderLookup(DataFolder.findFolder(newRoot)); 169 Lookup.Result result = lookup.getLookup().lookup(new Lookup.Template(JDBCDriver.class)); 170 Collection instances = result.allInstances(); 171 JDBCDriver drv = (JDBCDriver)instances.iterator().next(); 172 assertEquals(JDBCDriverConvertor.encodeURL(new URL (UNENCODED_URL)), drv.getURLs()[0]); 173 assertEquals(drv.getName(), drv.getDisplayName()); 175 } 176 177 private static FileObject createDriverFile10(String fileName, FileObject folder) throws Exception { 178 URL [] urls = new URL [] { 179 new URL ("file:///foo1.jar"), 180 new URL ("file:///foo2.jar"), 181 }; 182 return createDriverFile10(fileName, folder, urls); 183 } 184 185 private static FileObject createDriverFile10(String fileName, FileObject folder, URL [] urls) throws Exception { 186 return createDriverFile(10, fileName, folder, urls); 187 } 188 189 private static FileObject createDriverFile11(String fileName, FileObject folder) throws Exception { 190 URL [] urls = new URL [] { 191 new URL ("file:///foo1.jar"), 192 new URL ("file:///foo2.jar"), 193 }; 194 return createDriverFile(11, fileName, folder, urls); 195 } 196 197 private static FileObject createDriverFile(int version, String fileName, FileObject folder, URL [] urls) throws Exception { 198 String publicIdVer = version == 10 ? "1.0" : "1.1"; 199 String systemIdVer = version == 10 ? "1_0" : "1_1"; 200 201 FileObject fo = folder.createData(fileName); 202 FileLock lock = fo.lock(); 203 try { 204 OutputStreamWriter writer = new OutputStreamWriter (fo.getOutputStream(lock), "UTF-8"); 205 try { 206 writer.write("<?xml version='1.0' encoding='UTF-8'?>"); 207 writer.write("<!DOCTYPE driver PUBLIC '-//NetBeans//DTD JDBC Driver " + publicIdVer + "//EN' 'http://www.netbeans.org/dtds/jdbc-driver-" + systemIdVer + ".dtd'>"); 208 writer.write("<driver>"); 209 writer.write("<name value='foo_driver'/>"); 210 if (version == 11) { 211 writer.write("<display-name value='Foo Driver'/>"); 212 } 213 writer.write("<class value='org.foo.FooDriver'/>"); 214 writer.write("<urls>"); 215 for (int i = 0; i < urls.length; i++) { 216 writer.write("<url value='" + urls[i].toExternalForm() + "'/>"); 217 } 218 writer.write("</urls>"); 219 writer.write("</driver>"); 220 } finally { 221 writer.close(); 222 } 223 } finally { 224 lock.releaseLock(); 225 } 226 return fo; 227 } 228 229 private static final class ErrorHandlerImpl implements ErrorHandler { 230 231 public boolean error = false; 232 233 public void warning(SAXParseException exception) throws SAXException { 234 } 235 236 public void fatalError(SAXParseException exception) throws SAXException { 237 error = true; 238 } 239 240 public void error(SAXParseException exception) throws SAXException { 241 error = true; 242 } 243 } 244 } 245 | Popular Tags |