1 19 20 package org.netbeans.modules.db.explorer; 21 22 import java.io.InputStream ; 23 import java.io.OutputStreamWriter ; 24 import java.util.Collection ; 25 import org.netbeans.modules.db.explorer.nodes.RootNode; 26 import org.netbeans.modules.db.test.DOMCompare; 27 import org.netbeans.modules.db.test.TestBase; 28 import org.netbeans.modules.db.test.Util; 29 import org.openide.cookies.InstanceCookie; 30 import org.openide.filesystems.FileLock; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataFolder; 33 import org.openide.loaders.DataObject; 34 import org.openide.loaders.FolderLookup; 35 import org.openide.util.Lookup; 36 import org.openide.xml.EntityCatalog; 37 import org.openide.xml.XMLUtil; 38 import org.w3c.dom.Document ; 39 import org.xml.sax.ErrorHandler ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.SAXParseException ; 43 44 48 public class DatabaseConnectionConvertorTest extends TestBase { 49 50 public DatabaseConnectionConvertorTest(String testName) { 51 super(testName); 52 } 53 54 protected void setUp() throws Exception { 55 Util.deleteConnectionFiles(); 56 } 57 58 public void testReadXml() throws Exception { 59 FileObject fo = createConnectionFile("connection.xml", Util.getConnectionsFolder()); 60 DataObject dobj = DataObject.find(fo); 61 InstanceCookie ic = (InstanceCookie)dobj.getCookie(InstanceCookie.class); 62 assertNotNull(ic); 63 64 DatabaseConnection conn = (DatabaseConnection)ic.instanceCreate(); 65 assertEquals("org.foo.FooDriver", conn.getDriver()); 66 assertEquals("foo_driver", conn.getDriverName()); 67 assertEquals("jdbc:foo:localhost", conn.getDatabase()); 68 assertEquals("schema", conn.getSchema()); 69 assertEquals("user", conn.getUser()); 70 } 71 72 public void testWriteXml() throws Exception { 73 DatabaseConnection conn = new DatabaseConnection("org.bar.BarDriver", "bar_driver", "jdbc:bar:localhost", "schema", "user", null); 74 DatabaseConnectionConvertor.create(conn); 75 76 FileObject fo = Util.getConnectionsFolder().getChildren()[0]; 77 78 ErrorHandlerImpl errHandler = new ErrorHandlerImpl(); 79 Document doc = null; 80 InputStream input = fo.getInputStream(); 81 try { 82 doc = XMLUtil.parse(new InputSource (input), true, true, errHandler, EntityCatalog.getDefault()); 83 } finally { 84 input.close(); 85 } 86 87 assertFalse("DatabaseConnectionConvertor generates invalid XML acc to the DTD!", errHandler.error); 88 89 Document goldenDoc = null; 90 input = getClass().getResourceAsStream("bar-connection.xml"); 91 try { 92 goldenDoc = XMLUtil.parse(new InputSource (input), true, true, null, EntityCatalog.getDefault()); 93 } finally { 94 input.close(); 95 } 96 97 assertTrue(DOMCompare.compareDocuments(doc, goldenDoc)); 98 } 99 100 103 public void testSameInstanceAfterCreate() throws Exception { 104 DatabaseConnection dbconn = new DatabaseConnection("org.bar.BarDriver", "bar_driver", "jdbc:bar:localhost", "schema", "user", null); 105 DataObject dobj = DatabaseConnectionConvertor.create(dbconn); 106 assertSame(dbconn, ((InstanceCookie)dobj.getCookie(InstanceCookie.class)).instanceCreate()); 107 } 108 109 public void testSaveOnPropertyChange() throws Exception { 110 DatabaseConnection dbconn = new DatabaseConnection("a", "b", "c", "d", "e", null); 111 DatabaseConnectionConvertor.create(dbconn); 112 113 dbconn.setDriver("org.bar.BarDriver"); 114 dbconn.setDriverName("bar_driver"); 115 dbconn.setDatabase("jdbc:bar:localhost"); 116 dbconn.setSchema("schema"); 117 dbconn.setUser("user"); 118 119 try { 120 Thread.sleep(2500); 121 } catch (InterruptedException e) { } 122 123 FileObject fo = Util.getConnectionsFolder().getChildren()[0]; 124 125 ErrorHandlerImpl errHandler = new ErrorHandlerImpl(); 126 Document doc = null; 127 InputStream input = fo.getInputStream(); 128 try { 129 doc = XMLUtil.parse(new InputSource (input), true, true, errHandler, EntityCatalog.getDefault()); 130 } finally { 131 input.close(); 132 } 133 134 Document goldenDoc = null; 135 input = getClass().getResourceAsStream("bar-connection.xml"); 136 try { 137 goldenDoc = XMLUtil.parse(new InputSource (input), true, true, null, EntityCatalog.getDefault()); 138 } finally { 139 input.close(); 140 } 141 142 assertTrue(DOMCompare.compareDocuments(doc, goldenDoc)); 143 } 144 145 public void testLookup() throws Exception { 146 FileObject parent = Util.getConnectionsFolder(); 147 createConnectionFile("connection.xml", parent); 148 FolderLookup lookup = new FolderLookup(DataFolder.findFolder(parent)); 149 Lookup.Result result = lookup.getLookup().lookup(new Lookup.Template(DatabaseConnection.class)); 150 Collection instances = result.allInstances(); 151 assertEquals(1, instances.size()); 152 } 153 154 public void testImportOldConnections() throws Exception { 155 DatabaseConnection conn = new DatabaseConnection("org.foo.FooDriver", "foo_driver", "jdbc:foo:localhost", "schema", "user", null); 156 RootNode.getOption().getConnections().add(conn); 157 158 DatabaseConnectionConvertor.importOldConnections(); 159 160 FileObject root = Util.getConnectionsFolder(); 161 Collection instances = new FolderLookup(DataFolder.findFolder(root)).getLookup().lookup(new Lookup.Template(DatabaseConnection.class)).allInstances(); 162 assertEquals(1, instances.size()); 163 164 DatabaseConnection importedConn = (DatabaseConnection)instances.iterator().next(); 165 assertEquals(conn.getDriver(), importedConn.getDriver()); 166 assertEquals(conn.getDriverName(), importedConn.getDriverName()); 167 assertEquals(conn.getDatabase(), importedConn.getDatabase()); 168 assertEquals(conn.getSchema(), importedConn.getSchema()); 169 assertEquals(conn.getUser(), importedConn.getUser()); 170 } 171 172 private static FileObject createConnectionFile(String name, FileObject folder) throws Exception { 173 FileObject fo = folder.createData(name); 174 FileLock lock = fo.lock(); 175 try { 176 OutputStreamWriter writer = new OutputStreamWriter (fo.getOutputStream(lock), "UTF-8"); 177 try { 178 writer.write("<?xml version='1.0' encoding='UTF-8'?>"); 179 writer.write("<!DOCTYPE connection PUBLIC '-//NetBeans//DTD Database Connection 1.0//EN' 'http://www.netbeans.org/dtds/connection-1_0.dtd'>"); 180 writer.write("<connection>"); 181 writer.write("<driver-class value='org.foo.FooDriver'/>"); 182 writer.write("<driver-name value='foo_driver'/>"); 183 writer.write("<database-url value='jdbc:foo:localhost'/>"); 184 writer.write("<schema value='schema'/>"); 185 writer.write("<user value='user'/>"); 186 writer.write("</connection>"); 187 } finally { 188 writer.close(); 189 } 190 } finally { 191 lock.releaseLock(); 192 } 193 return fo; 194 } 195 196 private static final class ErrorHandlerImpl implements ErrorHandler { 197 198 public boolean error = false; 199 200 public void warning(SAXParseException exception) throws SAXException { 201 } 202 203 public void fatalError(SAXParseException exception) throws SAXException { 204 error = true; 205 } 206 207 public void error(SAXParseException exception) throws SAXException { 208 error = true; 209 } 210 } 211 } 212 | Popular Tags |