KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > config > XmlConfiguratorTest


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.naming.config;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.Statement JavaDoc;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.sql.DataSource JavaDoc;
27
28 import junit.framework.TestCase;
29
30 /**
31  * Test case for the XML configuration methods, testing environment entries
32  * and database connection resource factories.
33  *
34  * @author <a HREF="brett@apache.org">Brett Porter</a>
35  * @version $Id: XmlConfiguratorTest.java,v 1.2 2003/12/01 02:02:45 brett Exp $
36  */

37 public class XmlConfiguratorTest extends TestCase
38 {
39     public XmlConfiguratorTest(String JavaDoc name) {
40         super(name);
41     }
42
43     /*
44      * @see TestCase#setUp()
45      */

46     protected void setUp() throws Exception JavaDoc {
47         super.setUp();
48     }
49
50     /*
51      * @see TestCase#tearDown()
52      */

53     protected void tearDown() throws Exception JavaDoc {
54         super.tearDown();
55         XmlConfigurator.destroyInitialContext();
56     }
57     
58     /**
59      * Default root context name -- what XmlConfigurator assumes if root context
60      * element in xml config file does not have a name attribute, as in
61      * test-jndi.xml
62      */

63     protected static String JavaDoc DEFAULT_ROOT="java:comp/env";
64     
65     /**
66      * Alternate root context name specified in test-jndi2.xml. Must match
67      * name attribute of top-level context element in test-jndi2.xml
68      */

69     protected static String JavaDoc ALT_ROOT="alt/root/context";
70
71     /**
72      * Test for correctly configured environment entries.
73      * @throws Exception as tests do
74      */

75     public void testEnvironment() throws Exception JavaDoc {
76         XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml"));
77         checkEnvironment(DEFAULT_ROOT);
78         XmlConfigurator.destroyInitialContext();
79         XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi2.xml"));
80         checkEnvironment(ALT_ROOT);
81     }
82     
83     protected void checkEnvironment(String JavaDoc root) throws Exception JavaDoc {
84         Context JavaDoc ctx = new InitialContext JavaDoc();
85         Context JavaDoc env = (Context JavaDoc) ctx.lookup(root);
86         String JavaDoc host = (String JavaDoc) env.lookup("config/host");
87         Integer JavaDoc port = (Integer JavaDoc) env.lookup("config/port");
88
89         assertEquals("Check host", "www.apache.org", host);
90         assertEquals("Check port", new Integer JavaDoc(80), port);
91
92         Boolean JavaDoc trueBool = (Boolean JavaDoc) env.lookup("config/mytruebool");
93         Boolean JavaDoc falseBool = (Boolean JavaDoc) env.lookup("config/myfalsebool");
94
95         assertTrue("Check true boolean value", trueBool.booleanValue());
96         assertTrue("Check false boolean value", !falseBool.booleanValue());
97         
98         trueBool = (Boolean JavaDoc) ctx.lookup(root + "/config/mytruebool");
99         assertTrue("Check true boolean value -- root lookup", trueBool.booleanValue());
100         
101     }
102   
103     /**
104      * Test config as a subcontext of a different root.
105      * @throws Exception if it fails
106      */

107     public void testDuplicateSubcontextName() throws Exception JavaDoc{
108         XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml"));
109         checkDuplicateSubcontextName(DEFAULT_ROOT);
110     }
111     
112     protected void checkDuplicateSubcontextName(String JavaDoc root) throws Exception JavaDoc {
113         Context JavaDoc ctx = new InitialContext JavaDoc();
114         Context JavaDoc env = (Context JavaDoc) ctx.lookup(root);
115         String JavaDoc user = (String JavaDoc) env.lookup("jdbc/config/pool/user");
116         assertEquals("Check user", "dbuser", user);
117         user = (String JavaDoc) ctx.lookup(root + "/jdbc/config/pool/user");
118         assertEquals("Check user -- root lookup", "dbuser", user);
119     }
120
121     /**
122      * Test for correctly configured and operational database connection
123      * resource factories.
124      * @throws Exception as tests do
125      */

126     public void testJdbc() throws Exception JavaDoc {
127         XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml"));
128         checkJdbc(DEFAULT_ROOT);
129         XmlConfigurator.destroyInitialContext();
130         XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi2.xml"));
131         checkJdbc(ALT_ROOT);
132     }
133     
134     protected void checkJdbc(String JavaDoc root) throws Exception JavaDoc {
135         Context JavaDoc ctx = new InitialContext JavaDoc();
136         Context JavaDoc env = (Context JavaDoc) ctx.lookup(root);
137         DataSource JavaDoc ds = (DataSource JavaDoc) env.lookup("jdbc/pool");
138         Connection JavaDoc con = null;
139         Statement JavaDoc stat = null;
140         ResultSet JavaDoc rs = null;
141         try {
142             con = ds.getConnection();
143             stat = con.createStatement();
144             stat.executeUpdate("DROP TABLE DUAL IF EXISTS");
145             stat.executeUpdate("CREATE TABLE DUAL(value char(50))");
146             stat.executeUpdate("INSERT INTO DUAL VALUES(1)");
147             rs = stat.executeQuery("SELECT * FROM DUAL");
148             while (rs.next()) {
149                assertEquals("Check you get back what you put into the DB", 1, rs.getInt(1));
150             }
151         }
152         finally {
153             if (rs != null) {
154                 rs.close();
155             }
156             if (stat != null) {
157                 stat.close();
158             }
159             if (con != null) {
160                 con.close();
161             }
162         }
163     }
164 }
165
166
Popular Tags