KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.avalon.excalibur.component.DefaultRoleManager;
9 import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
10 import org.apache.avalon.framework.configuration.Configuration;
11 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
12 import org.apache.avalon.framework.context.DefaultContext;
13 import org.apache.log.Hierarchy;
14 import org.apache.log.LogTarget;
15 import org.apache.log.Logger;
16 import org.apache.log.Priority;
17 import org.logicalcobwebs.proxool.AbstractProxoolTest;
18 import org.logicalcobwebs.proxool.ProxoolException;
19 import org.logicalcobwebs.proxool.ProxoolFacade;
20 import org.logicalcobwebs.proxool.TestHelper;
21
22 import java.io.File JavaDoc;
23
24 /**
25  * Tests that the AvalonConfgiuration works.
26  *
27  * @version $Revision: 1.10 $, $Date: 2003/03/04 10:58:45 $
28  * @author Christian Nedregaard (christian_nedregaard@email.com)
29  * @author $Author: billhorsman $ (current maintainer)
30  * @since Proxool 0.6
31  */

32 public class AvalonConfiguratorTest extends AbstractProxoolTest {
33     private ExcaliburComponentManager componentManager;
34
35     /**
36      * @see junit.framework.TestCase#TestCase
37      */

38     public AvalonConfiguratorTest(String JavaDoc name) {
39         super(name);
40     }
41
42     /**
43      * Test that the configuration succeds and that all expected properties
44      * has been received by Proxool. The configuration file does not use namspaces.
45      * @throws org.apache.avalon.framework.component.ComponentException if the configuration fails.
46      * @throws java.sql.SQLException if ProxoolFacade operation fails.
47      */

48     public void testNoNamspaces() throws Exception JavaDoc {
49         initializeAvalon("src/java-test/org/logicalcobwebs/proxool/configuration/role.conf",
50                 "src/java-test/org/logicalcobwebs/proxool/configuration/component.conf");
51         this.componentManager.lookup(AvalonConfigurator.ROLE);
52         try {
53             TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("avalon-test"));
54         } catch (ProxoolException e) {
55             throw e;
56         }
57         try {
58             TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("avalon-test-2"));
59         } catch (ProxoolException e) {
60             throw e;
61         }
62         this.componentManager.dispose();
63     }
64
65     /**
66      * Test that the configuration succeds and that all expected properties
67      * has been received by Proxool. The configuration file uses namspaces.
68      * @throws Exception if the test is interrupted.
69      */

70     public void testWithNamespaces() throws Exception JavaDoc {
71         initializeAvalon("src/java-test/org/logicalcobwebs/proxool/configuration/role.conf",
72                 "src/java-test/org/logicalcobwebs/proxool/configuration/component-ns.conf");
73         this.componentManager.lookup(AvalonConfigurator.ROLE);
74         try {
75             TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("avalon-test-ns"));
76         } catch (ProxoolException e) {
77             throw e;
78         }
79         try {
80             TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("avalon-test-ns-2"));
81         } catch (ProxoolException e) {
82             throw e;
83         }
84         ProxoolFacade.removeConnectionPool("avalon-test-ns");
85         ProxoolFacade.removeConnectionPool("avalon-test-ns-2");
86         this.componentManager.dispose();
87     }
88
89     /**
90      * Test that a configurator that does not have close-on-dispose="false"
91      * closes the pools it has configured when it is disposed.
92      * @throws Exception if the test is interrupted.
93      */

94     public void testDisposeOnClose() throws Exception JavaDoc {
95         initializeAvalon("src/java-test/org/logicalcobwebs/proxool/configuration/role.conf",
96                 "src/java-test/org/logicalcobwebs/proxool/configuration/component.conf");
97         this.componentManager.lookup(AvalonConfigurator.ROLE);
98         this.componentManager.dispose();
99         try {
100             ProxoolFacade.getConnectionPoolDefinition("avalon-test");
101             fail("ProxoolFacade found pool 'avalon-test' but we expected the configurator to have removed it.");
102         } catch (ProxoolException e) {
103             // This is what we want.
104
}
105         try {
106             ProxoolFacade.getConnectionPoolDefinition("avalon-test-2");
107             fail("ProxoolFacade found pool 'avalon-test-2' but we expected the configurator to have removed it.");
108         } catch (ProxoolException e) {
109             // This is what we want.
110
}
111     }
112
113     /**
114      * Test that a configurator that does have close-on-dispose="false"
115      * does not close the pools it has configured when it is disposed.
116      * @throws Exception if the test is interrupted.
117      */

118     public void testNotDisposeOnClose() throws Exception JavaDoc {
119         initializeAvalon("src/java-test/org/logicalcobwebs/proxool/configuration/role.conf",
120                 "src/java-test/org/logicalcobwebs/proxool/configuration/component-ns.conf");
121         this.componentManager.lookup(AvalonConfigurator.ROLE);
122         this.componentManager.dispose();
123         try {
124             ProxoolFacade.getConnectionPoolDefinition("avalon-test-ns");
125         } catch (ProxoolException e) {
126             fail("ProxoolFacade did not find pool 'avalon-test-ns' but we didn't expect the configurator to have removed it.");
127         }
128         try {
129             ProxoolFacade.getConnectionPoolDefinition("avalon-test-ns-2");
130         } catch (ProxoolException e) {
131             fail("ProxoolFacade did not find pool 'avalon-ns-test-2' but we didn't expect the configurator to have removed it.");
132         }
133         ProxoolFacade.removeConnectionPool("avalon-test-ns");
134         ProxoolFacade.removeConnectionPool("avalon-test-ns-2");
135     }
136
137     private void initializeAvalon(String JavaDoc roleFile, String JavaDoc componentFile) throws Exception JavaDoc {
138         // create a Avalon logger
139
final LogTarget logTarget = new LogKitTargetAdapter();
140         final Logger rootLogger = Hierarchy.getDefaultHierarchy().getLoggerFor("root");
141         rootLogger.unsetLogTargets();
142         rootLogger.setLogTargets(new LogTarget[]{logTarget});
143         rootLogger.setPriority(Priority.WARN);
144         final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(true);
145         // create component configuration
146
final Configuration componentConfiguration = builder.buildFromFile(new File JavaDoc(componentFile));
147         // create role configuration
148
final Configuration roleConfiguration = builder.buildFromFile(new File JavaDoc(roleFile));
149         // create and configure role and component managers.
150
this.componentManager = new ExcaliburComponentManager();
151         this.componentManager.setLogger(rootLogger.getChildLogger(ExcaliburComponentManager.class.getName()));
152         DefaultRoleManager roleManager = new DefaultRoleManager();
153         roleManager.setLogger(rootLogger.getChildLogger(DefaultRoleManager.class.getName()));
154         roleManager.configure(roleConfiguration);
155         componentManager.contextualize(new DefaultContext());
156         componentManager.setRoleManager(roleManager);
157         componentManager.configure(componentConfiguration);
158         componentManager.initialize();
159     }
160
161 }
162
163 /*
164  Revision history:
165  $Log: AvalonConfiguratorTest.java,v $
166  Revision 1.10 2003/03/04 10:58:45 billhorsman
167  checkstyle
168
169  Revision 1.9 2003/03/04 10:24:41 billhorsman
170  removed try blocks around each test
171
172  Revision 1.8 2003/03/03 17:09:12 billhorsman
173  all tests now extend AbstractProxoolTest
174
175  Revision 1.7 2003/03/03 11:12:06 billhorsman
176  fixed licence
177
178  Revision 1.6 2003/03/01 15:27:25 billhorsman
179  checkstyle
180
181  Revision 1.5 2003/02/27 18:01:49 billhorsman
182  completely rethought the test structure. it's now
183  more obvious. no new tests yet though.
184
185  Revision 1.4 2003/02/19 16:52:00 chr32
186  Added tests for close-on-dispose functionality.
187
188  Revision 1.3 2003/02/19 15:14:26 billhorsman
189  fixed copyright (copy and paste error,
190  not copyright change)
191
192  Revision 1.2 2002/12/23 02:48:07 chr32
193  Checkstyle.
194
195  Revision 1.1 2002/12/23 02:40:49 chr32
196  Init rev.
197
198  Revision 1.5 2002/12/18 03:13:00 chr32
199  Added tests for xml validation.
200
201  Revision 1.4 2002/12/16 17:06:41 billhorsman
202  new test structure
203
204  Revision 1.3 2002/12/16 02:35:40 chr32
205  Updated to new driver-properties xml format.
206
207  Revision 1.2 2002/12/15 19:41:26 chr32
208  Style fixes.
209
210  Revision 1.1 2002/12/15 19:10:49 chr32
211  Init rev.
212
213 */

214
Popular Tags