KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > DatabaseConnectionConvertorTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24 import java.util.Collection JavaDoc;
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 JavaDoc;
39 import org.xml.sax.ErrorHandler JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.SAXParseException JavaDoc;
43
44 /**
45  *
46  * @author Andrei Badea
47  */

48 public class DatabaseConnectionConvertorTest extends TestBase {
49     
50     public DatabaseConnectionConvertorTest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     protected void setUp() throws Exception JavaDoc {
55         Util.deleteConnectionFiles();
56     }
57     
58     public void testReadXml() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc doc = null;
80         InputStream JavaDoc input = fo.getInputStream();
81         try {
82             doc = XMLUtil.parse(new InputSource JavaDoc(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 JavaDoc goldenDoc = null;
90         input = getClass().getResourceAsStream("bar-connection.xml");
91         try {
92             goldenDoc = XMLUtil.parse(new InputSource JavaDoc(input), true, true, null, EntityCatalog.getDefault());
93         } finally {
94             input.close();
95         }
96         
97         assertTrue(DOMCompare.compareDocuments(doc, goldenDoc));
98     }
99     
100     /**
101      * Tests that the instance retrieved from the DO created by DCC.create(DCI dbconn) is the same object as dbconn.
102      */

103     public void testSameInstanceAfterCreate() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc e) { }
122         
123         FileObject fo = Util.getConnectionsFolder().getChildren()[0];
124         
125         ErrorHandlerImpl errHandler = new ErrorHandlerImpl();
126         Document JavaDoc doc = null;
127         InputStream JavaDoc input = fo.getInputStream();
128         try {
129             doc = XMLUtil.parse(new InputSource JavaDoc(input), true, true, errHandler, EntityCatalog.getDefault());
130         } finally {
131             input.close();
132         }
133         
134         Document JavaDoc goldenDoc = null;
135         input = getClass().getResourceAsStream("bar-connection.xml");
136         try {
137             goldenDoc = XMLUtil.parse(new InputSource JavaDoc(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 JavaDoc {
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 JavaDoc instances = result.allInstances();
151         assertEquals(1, instances.size());
152     }
153     
154     public void testImportOldConnections() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc name, FileObject folder) throws Exception JavaDoc {
173         FileObject fo = folder.createData(name);
174         FileLock lock = fo.lock();
175         try {
176             OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(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 JavaDoc {
197         
198         public boolean error = false;
199         
200         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
201         }
202
203         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
204             error = true;
205         }
206
207         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
208             error = true;
209         }
210     }
211 }
212
Popular Tags