KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > configuration > JAXPConfiguratorTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.configuration;
7
8 import org.logicalcobwebs.proxool.AbstractProxoolTest;
9 import org.logicalcobwebs.proxool.ProxoolConstants;
10 import org.logicalcobwebs.proxool.ProxoolException;
11 import org.logicalcobwebs.proxool.ProxoolFacade;
12 import org.logicalcobwebs.proxool.TestHelper;
13 import org.xml.sax.InputSource JavaDoc;
14
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.sql.DriverManager JavaDoc;
18 import java.sql.SQLException JavaDoc;
19
20 /**
21  * Tests that the JAXPConfgiuration works in various scenarios.
22  * This is also a test of the {@link XMLConfigurator}, as it is delegated to.
23  *
24  * @version $Revision: 1.16 $, $Date: 2003/08/30 14:54:23 $
25  * @author Christian Nedregaard (christian_nedregaard@email.com)
26  * @author $Author: billhorsman $ (current maintainer)
27  * @since Proxool 0.6
28  */

29 public class JAXPConfiguratorTest extends AbstractProxoolTest {
30
31     /**
32      * @see junit.framework.TestCase#TestCase
33      */

34     public JAXPConfiguratorTest(String JavaDoc name) {
35         super(name);
36     }
37
38     /**
39      * Test that the confguration succeds and that all expected properties
40      * has been received by Proxool. This test is done with a
41      * xml without namespaces and validiation.
42      * @throws ProxoolException if the configuration fails.
43      * @throws SQLException if ProxoolFacade operation fails.
44      */

45     public void testNoNamspaces() throws ProxoolException, SQLException JavaDoc {
46         final String JavaDoc xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-no-ns.xml";
47         JAXPConfigurator.configure(xmlFile, false);
48         try {
49             assertNotNull("2nd (deeply nested) pool was not configured.", ProxoolFacade.getConnectionPoolDefinition("xml-test-2"));
50         } catch (ProxoolException e) {
51             fail("2nd (deeply nested) pool was not configured.");
52         }
53         TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test"));
54         TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-2"));
55     }
56
57     /**
58      * Test that the confguration succeds and that all expected properties
59      * has been received by Proxool. This test is done with a
60      * xml with namespaces and without validiation.
61      * @throws ProxoolException if the configuration fails.
62      * @throws SQLException if ProxoolFacade operation fails.
63      */

64     public void testWithNamspaces() throws ProxoolException, SQLException JavaDoc {
65         final String JavaDoc xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-ns.xml";
66         JAXPConfigurator.configure(xmlFile, false);
67         try {
68             assertNotNull("2nd (deeply nested) pool was not configured.", ProxoolFacade.getConnectionPoolDefinition("xml-test-ns-2"));
69         } catch (ProxoolException e) {
70             fail("2nd (deeply nested) pool was not configured.");
71         }
72         TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-ns"));
73         TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-ns-2"));
74     }
75
76     /**
77      * Test that the confguration succeds and that all expected properties
78      * has been received by Proxool, and that validiation succceds. This test is done with a
79      * xml without namespaces.
80      * @throws ProxoolException if the configuration fails.
81      * @throws SQLException if ProxoolFacade operation fails.
82      * @throws FileNotFoundException if the xml file is not found.
83      */

84     public void testValidiation() throws ProxoolException, SQLException JavaDoc, FileNotFoundException JavaDoc {
85         final String JavaDoc xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-valid.xml";
86         final InputSource JavaDoc inputSource = new InputSource JavaDoc(new FileInputStream JavaDoc(xmlFile));
87         inputSource.setSystemId(getWorkingDirectoryUrl());
88         JAXPConfigurator.configure(inputSource, true);
89         try {
90             assertNotNull("2nd (deeply nested) pool was not configured.", ProxoolFacade.getConnectionPoolDefinition("xml-test-validating-2"));
91         } catch (ProxoolException e) {
92             fail("2nd (deeply nested) pool was not configured.");
93         }
94         TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-validating"));
95         TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-validating-2"));
96     }
97
98     /**
99      * Test that we can retrieve a conncetion using the alias after registration
100      * @throws ProxoolException if the configuration fails.
101      * @throws SQLException if ProxoolFacade operation fails.
102      * @throws FileNotFoundException if the xml file is not found.
103      */

104     public void testWithAlias() throws ProxoolException, SQLException JavaDoc, FileNotFoundException JavaDoc {
105         final String JavaDoc xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-valid.xml";
106         final InputSource JavaDoc inputSource = new InputSource JavaDoc(new FileInputStream JavaDoc(xmlFile));
107         inputSource.setSystemId(getWorkingDirectoryUrl());
108         JAXPConfigurator.configure(inputSource, true);
109
110         final String JavaDoc alias = "xml-test-validating";
111         DriverManager.getConnection(ProxoolConstants.PROXOOL + "." + alias).close();
112     }
113
114     /**
115      * Test that the confguration fails when validiation is turned on and the given xml is not valid.
116      * @throws SQLException if ProxoolFacade operation fails.
117      * @throws FileNotFoundException if the xml file is not found.
118      */

119     public void testValidiationFailure() throws SQLException JavaDoc, FileNotFoundException JavaDoc, ProxoolException {
120         final String JavaDoc xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-not-valid.xml";
121         final InputSource JavaDoc inputSource = new InputSource JavaDoc(new FileInputStream JavaDoc(xmlFile));
122         inputSource.setSystemId(getWorkingDirectoryUrl());
123         boolean failure = false;
124         try {
125             JAXPConfigurator.configure(inputSource, true);
126         } catch (ProxoolException e) {
127             failure = true;
128         }
129         assertTrue("Configuration did not fail on unvalid xml document.", failure);
130     }
131
132     private static String JavaDoc getWorkingDirectoryUrl() {
133         String JavaDoc userDir = System.getProperty("user.dir");
134         String JavaDoc toReplace = "\\";
135         String JavaDoc replaceWith = "/";
136         int pos = 0;
137         if (!toReplace.equals(replaceWith)) {
138             while (true) {
139                 pos = userDir.indexOf(toReplace, pos);
140                 if (pos == -1) {
141                     break;
142                 }
143                 userDir = userDir.substring(0, pos) + replaceWith + userDir.substring(pos + toReplace.length());
144                 pos += replaceWith.length();
145             }
146         }
147         if (!userDir.startsWith("/")) {
148             userDir = "/" + userDir;
149         }
150         if (!userDir.endsWith("/")) {
151             userDir = userDir + "/";
152         }
153         return "file://" + userDir;
154     }
155
156 }
157
158 /*
159  Revision history:
160  $Log: JAXPConfiguratorTest.java,v $
161  Revision 1.16 2003/08/30 14:54:23 billhorsman
162  Checkstyle
163
164  Revision 1.15 2003/04/29 11:51:49 billhorsman
165  added new "with-alias" test
166
167  Revision 1.14 2003/03/04 10:58:45 billhorsman
168  checkstyle
169
170  Revision 1.13 2003/03/04 10:24:41 billhorsman
171  removed try blocks around each test
172
173  Revision 1.12 2003/03/03 17:36:33 billhorsman
174  leave shutdown to AbstractProxoolTest
175
176  Revision 1.11 2003/03/03 17:09:18 billhorsman
177  all tests now extend AbstractProxoolTest
178
179  Revision 1.10 2003/03/03 11:12:06 billhorsman
180  fixed licence
181
182  Revision 1.9 2003/03/01 15:27:25 billhorsman
183  checkstyle
184
185  Revision 1.8 2003/02/27 18:01:49 billhorsman
186  completely rethought the test structure. it's now
187  more obvious. no new tests yet though.
188
189  Revision 1.7 2003/02/19 15:14:27 billhorsman
190  fixed copyright (copy and paste error,
191  not copyright change)
192
193  Revision 1.6 2003/01/18 15:13:14 billhorsman
194  Signature changes (new ProxoolException
195  thrown) on the ProxoolFacade API.
196
197  Revision 1.5 2002/12/18 03:13:00 chr32
198  Added tests for xml validation.
199
200  Revision 1.4 2002/12/16 17:06:41 billhorsman
201  new test structure
202
203  Revision 1.3 2002/12/16 02:35:40 chr32
204  Updated to new driver-properties xml format.
205
206  Revision 1.2 2002/12/15 19:41:26 chr32
207  Style fixes.
208
209  Revision 1.1 2002/12/15 19:10:49 chr32
210  Init rev.
211
212 */

213
Popular Tags