KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > jndi > JNDIDriverTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.driver.jndi;
17
18 import scriptella.AbstractTestCase;
19 import scriptella.configuration.MockConnectionEl;
20 import scriptella.jdbc.JdbcConnection;
21 import scriptella.jdbc.JdbcException;
22 import scriptella.spi.ConnectionParameters;
23 import scriptella.spi.MockDriverContext;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.naming.spi.InitialContextFactory JavaDoc;
28 import javax.sql.DataSource JavaDoc;
29 import java.lang.reflect.InvocationHandler JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.lang.reflect.Proxy JavaDoc;
32 import java.sql.DriverManager JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Tests JNDI driver class.
41  *
42  * @author Fyodor Kupolov
43  * @version 1.0
44  */

45 public class JNDIDriverTest extends AbstractTestCase {
46
47     /**
48      * Tests the driver by emulating the JNDI environment with a JNDI-bound datasource.
49      */

50     public void testGetConnection() throws NamingException JavaDoc {
51         //Just to initialize HSLQB driver class
52
Logger.getAnonymousLogger().fine("Initializing " + new scriptella.driver.hsqldb.Driver());
53         //Preparing the environment
54
Map JavaDoc<String JavaDoc, String JavaDoc> env = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
55         //Setting up a test JNDI factory
56
env.put(Context.INITIAL_CONTEXT_FACTORY, CtxFactory.class.getName());
57         CtxFactory.jndiName = "datasourceName";
58         CtxFactory.connections = 0;
59         CtxFactory.lookups = 0;
60         ConnectionParameters params = new ConnectionParameters(new MockConnectionEl(env, CtxFactory.jndiName), MockDriverContext.INSTANCE);
61         Driver drv = new Driver();
62         JdbcConnection con1 = drv.connect(params);
63         con1.close();
64         assertNotNull(con1);
65         JdbcConnection con2 = drv.connect(params);
66         con2.close();
67         assertNotNull(con2);
68         assertTrue("con1 and con2 must be different connections", con1 != con2);
69         //lookup and getConnection called 2 times
70
assertEquals("Illegal number of lookups", 2, CtxFactory.lookups);
71         assertEquals("Illegal number of created connections", 2, CtxFactory.connections);
72
73     }
74
75     /**
76      * Tests if validation of connection parameters is performed.
77      * @throws SQLException
78      */

79     public void testValidation() throws SQLException JavaDoc {
80         Driver drv = new Driver();
81         try {
82             drv.getConnection(null , null);
83         } catch (JdbcException e) {
84             //ok
85
}
86
87     }
88
89     /**
90      * Represents {@link InitialContextFactory} for JNDI and an {@link InvocationHandler} for
91      * emulating Context and Datasource simultaneously.
92      */

93     public static class CtxFactory implements InitialContextFactory JavaDoc, InvocationHandler JavaDoc {
94         public static String JavaDoc jndiName;
95         public static int lookups;
96         public static int connections;
97
98         public Context getInitialContext(Hashtable JavaDoc<?, ?> environment) throws NamingException JavaDoc {
99             return (Context) Proxy.newProxyInstance(getClass().getClassLoader(),
100                     new Class JavaDoc<?>[]{Context.class, DataSource JavaDoc.class}, this);
101         }
102
103         /**
104          * This invoker supports 2 methods:
105          * {@link Context#lookup(String)} and {@link javax.sql.DataSource#getConnection()}
106          *
107          * @throws SQLException if db exception occurs
108          */

109         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws SQLException JavaDoc {
110             if ("lookup".equals(method.getName())) {
111                 lookups++;
112                 String JavaDoc name = (String JavaDoc) args[0];
113                 if (!jndiName.equals(name)) {
114                     fail("Expected " + jndiName + " JNDI name but was " + name);
115                 }
116                 return proxy;
117             } else if ("getConnection".equals(method.getName())) {
118                 connections++;
119                 return DriverManager.getConnection("jdbc:hsqldb:mem:jnditest", "sa", "");
120             } else {
121                 throw new UnsupportedOperationException JavaDoc();
122             }
123         }
124     }
125
126 }
127
Popular Tags