KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > driver > JDBCDriverConvertorTest


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.driver;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLEncoder JavaDoc;
27 import java.util.Collection JavaDoc;
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 JavaDoc;
45 import org.xml.sax.ErrorHandler JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48 import org.xml.sax.SAXParseException JavaDoc;
49
50 /**
51  *
52  * @author Andrei Badea
53  */

54 public class JDBCDriverConvertorTest extends TestBase {
55     
56     public JDBCDriverConvertorTest(String JavaDoc testName) {
57         super(testName);
58     }
59     
60     protected void setUp() throws Exception JavaDoc {
61         Util.deleteDriverFiles();
62     }
63     
64     public void testReadXml() throws Exception JavaDoc {
65         // DTD version 1.0
66
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 JavaDoc("file:///foo1.jar"), driver.getURLs()[0]);
76         assertEquals(new URL JavaDoc("file:///foo2.jar"), driver.getURLs()[1]);
77         
78         // DTD version 1.1
79
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 JavaDoc("file:///foo1.jar"), driver.getURLs()[0]);
90         assertEquals(new URL JavaDoc("file:///foo2.jar"), driver.getURLs()[1]);
91     }
92     
93     public void testWriteXml() throws Exception JavaDoc {
94         JDBCDriver driver = JDBCDriver.create("bar_driver", "Bar Driver", "org.bar.BarDriver", new URL JavaDoc[] { new URL JavaDoc("file:///bar1.jar"), new URL JavaDoc("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 JavaDoc doc = null;
101         InputStream JavaDoc input = fo.getInputStream();
102         try {
103             doc = XMLUtil.parse(new InputSource JavaDoc(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 JavaDoc goldenDoc = null;
111         input = getClass().getResourceAsStream("bar-driver.xml");
112         try {
113             goldenDoc = XMLUtil.parse(new InputSource JavaDoc(input), true, true, null, EntityCatalog.getDefault());
114         } finally {
115             input.close();
116         }
117         
118         assertTrue(DOMCompare.compareDocuments(doc, goldenDoc));
119     }
120     
121     /**
122      * Tests that the instance retrieved from the DO created by JDBCDC.create(JDBCD driver) is the same object as driver.
123      * Note that this test does not ensure that the DataObject-s returned from
124      * {@link JDBCDriverConvertor#create} cannot be GCd. They can be, which will
125      * cause new JDBCDriver instances to be created. See issue 75204.
126      */

127     public void testSameInstanceAfterCreate() throws Exception JavaDoc {
128         JDBCDriver driver1 = JDBCDriver.create("bar_driver", "Bar Driver", "org.bar.BarDriver", new URL JavaDoc[] { new URL JavaDoc("file:///bar1.jar"), new URL JavaDoc("file:///bar2.jar") });
129         DataObject dobj1 = JDBCDriverConvertor.create(driver1);
130         JDBCDriver driver2 = JDBCDriver.create("foo_driver", "Foo Driver", "org.foo.FooDriver", new URL JavaDoc[] { new URL JavaDoc("file:///foo1.jar"), new URL JavaDoc("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 JavaDoc {
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 JavaDoc instances = result.allInstances();
142         assertEquals(1, instances.size());
143     }
144     
145     public void testEncodeURL() throws Exception JavaDoc {
146         assertEquals(new URL JavaDoc("file:///test%20file#fragment"), JDBCDriverConvertor.encodeURL(new URL JavaDoc("file:///test file#fragment")));
147         assertEquals(new URL JavaDoc("file:///test%20file"), JDBCDriverConvertor.encodeURL(new URL JavaDoc("file:///test file")));
148     }
149     
150     public void testImportOldDrivers() throws Exception JavaDoc {
151         final String JavaDoc 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 JavaDoc[] urls = new URL JavaDoc[] { new URL JavaDoc(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 JavaDoc instances = result.allInstances();
171         JDBCDriver drv = (JDBCDriver)instances.iterator().next();
172         assertEquals(JDBCDriverConvertor.encodeURL(new URL JavaDoc(UNENCODED_URL)), drv.getURLs()[0]);
173         // assert the imported driver has a display name set
174
assertEquals(drv.getName(), drv.getDisplayName());
175     }
176     
177     private static FileObject createDriverFile10(String JavaDoc fileName, FileObject folder) throws Exception JavaDoc {
178         URL JavaDoc[] urls = new URL JavaDoc[] {
179             new URL JavaDoc("file:///foo1.jar"),
180             new URL JavaDoc("file:///foo2.jar"),
181         };
182         return createDriverFile10(fileName, folder, urls);
183     }
184     
185     private static FileObject createDriverFile10(String JavaDoc fileName, FileObject folder, URL JavaDoc[] urls) throws Exception JavaDoc {
186         return createDriverFile(10, fileName, folder, urls);
187     }
188     
189     private static FileObject createDriverFile11(String JavaDoc fileName, FileObject folder) throws Exception JavaDoc {
190         URL JavaDoc[] urls = new URL JavaDoc[] {
191             new URL JavaDoc("file:///foo1.jar"),
192             new URL JavaDoc("file:///foo2.jar"),
193         };
194         return createDriverFile(11, fileName, folder, urls);
195     }
196     
197     private static FileObject createDriverFile(int version, String JavaDoc fileName, FileObject folder, URL JavaDoc[] urls) throws Exception JavaDoc {
198         String JavaDoc publicIdVer = version == 10 ? "1.0" : "1.1";
199         String JavaDoc systemIdVer = version == 10 ? "1_0" : "1_1";
200         
201         FileObject fo = folder.createData(fileName);
202         FileLock lock = fo.lock();
203         try {
204             OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(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 JavaDoc {
230         
231         public boolean error = false;
232         
233         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
234         }
235
236         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
237             error = true;
238         }
239
240         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
241             error = true;
242         }
243     }
244 }
245
Popular Tags