1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.osjava.sj.loader;
33
34 import java.io.File;
35 import java.io.IOException;
36
37 import java.util.Calendar;
38 import java.util.Date;
39 import java.util.Hashtable;
40 import java.util.List;
41 import java.util.Properties;
42
43 import javax.naming.Context;
44 import javax.naming.InitialContext;
45 import javax.naming.NamingException;
46
47 import java.sql.Connection;
48 import java.sql.SQLException;
49 import javax.sql.DataSource;
50
51 import junit.framework.TestCase;
52
53 public class JndiLoaderTest extends TestCase {
54
55 private Context ctxt;
56 private JndiLoader loader;
57
58 public JndiLoaderTest(String name) {
59 super(name);
60 }
61
62 public void setUp() {
63
64
65
66
67 Hashtable contextEnv = new Hashtable();
68
69
70 contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory");
71 contextEnv.put("jndi.syntax.direction", "left_to_right");
72 contextEnv.put("jndi.syntax.separator", "/");
73 /***/
74
75
76
77
78
79
80
81
82 contextEnv.put(JndiLoader.SIMPLE_DELIMITER, "/");
83
84 loader = new JndiLoader(contextEnv);
85
86 try {
87 ctxt = new InitialContext(contextEnv);
88 } catch(NamingException ne) {
89 ne.printStackTrace();
90 }
91 }
92
93 public void tearDown() {
94 this.ctxt = null;
95 }
96
97 public void testProperties() {
98 try {
99 Properties props = new Properties();
100 props.put("foo", "13");
101 props.put("bar/foo", "42");
102 props.put("bar/test/foo", "101");
103 loader.load( props, ctxt );
104 assertEquals( "13", ctxt.lookup("foo") );
105 assertEquals( "42", ctxt.lookup("bar/foo") );
106 assertEquals( "101", ctxt.lookup("bar/test/foo") );
107 } catch(NamingException ne) {
108 ne.printStackTrace();
109 fail("NamingException: "+ne.getMessage());
110 }
111 }
112
113 public void testDirectory() {
114 try {
115 File file = new File("src/test/config/");
116 loader.loadDirectory( file, ctxt );
117 assertEquals( "13", ctxt.lookup("test/value") );
118 } catch(IOException ioe) {
119 ioe.printStackTrace();
120 fail("IOException: "+ioe.getMessage());
121 } catch(NamingException ne) {
122 ne.printStackTrace();
123 fail("NamingException: "+ne.getMessage());
124 }
125 }
126
127 public void testDefaultFile() {
128 try {
129 File file = new File("src/test/config/");
130 loader.loadDirectory( file, ctxt );
131 List list = (List) ctxt.lookup("name");
132 assertEquals( "Henri", list.get(0) );
133 assertEquals( "Fred", list.get(1) );
134 assertEquals( "Foo", ctxt.lookup("com.genjava") );
135 } catch(IOException ioe) {
136 ioe.printStackTrace();
137 fail("IOException: "+ioe.getMessage());
138 } catch(NamingException ne) {
139 ne.printStackTrace();
140 fail("NamingException: "+ne.getMessage());
141 }
142 }
143
144 public void testSubContext() {
145 String dsString = "bing::::foofoo::::Boo";
146 try {
147 File file = new File("src/test/config/");
148 loader.loadDirectory( file, ctxt );
149 Context subctxt = (Context) ctxt.lookup("java");
150 assertEquals( dsString, subctxt.lookup("TestDS").toString() );
151 DataSource ds = (DataSource) ctxt.lookup("java/TestDS");
152 assertEquals( dsString, ds.toString() );
153 } catch(IOException ioe) {
154 ioe.printStackTrace();
155 fail("IOException: "+ioe.getMessage());
156 } catch(NamingException ne) {
157 ne.printStackTrace();
158 fail("NamingException: "+ne.getMessage());
159 }
160 }
161
162 public void testTopLevelDataSource() {
163 String dsString = "org.gjt.mm.mysql.Driver::::jdbc:mysql://127.0.0.1/tmp::::sa";
164 try {
165 File file = new File("src/test/config/");
166 loader.loadDirectory( file, ctxt );
167 DataSource ds = (DataSource) ctxt.lookup("TopLevelDS");
168 assertEquals( dsString, ds.toString() );
169 } catch(IOException ioe) {
170 ioe.printStackTrace();
171 fail("IOException: "+ioe.getMessage());
172 } catch(NamingException ne) {
173 ne.printStackTrace();
174 fail("NamingException: "+ne.getMessage());
175 }
176 }
177
178 public void testBoolean() {
179 try {
180 Properties props = new Properties();
181 props.put("foo", "true");
182 props.put("foo/type", "java.lang.Boolean");
183 loader.load( props, ctxt );
184 assertEquals( new Boolean(true), ctxt.lookup("foo") );
185 } catch(NamingException ne) {
186 ne.printStackTrace();
187 fail("NamingException: "+ne.getMessage());
188 }
189 }
190
191 public void testDate() {
192 try {
193 Properties props = new Properties();
194 props.put("birthday", "2004-10-22");
195 props.put("birthday/type", "java.util.Date");
196 props.put("birthday/format", "yyyy-MM-dd");
197
198 loader.load( props, ctxt );
199
200 Date d = (Date) ctxt.lookup("birthday");
201 Calendar c = Calendar.getInstance();
202 c.setTime(d);
203
204 assertEquals( 2004, c.get(Calendar.YEAR) );
205 assertEquals( 10 - 1, c.get(Calendar.MONTH) );
206 assertEquals( 22, c.get(Calendar.DATE) );
207 } catch(NamingException ne) {
208 ne.printStackTrace();
209 fail("NamingException: "+ne.getMessage());
210 }
211 }
212
213 public void testConverterPlugin() {
214 try {
215 Properties props = new Properties();
216 props.put("math", "Pi");
217
218 props.put("math/type", "magic number");
219 props.put("math/converter", "org.osjava.sj.loader.convert.PiConverter");
220
221 loader.load( props, ctxt );
222
223 assertEquals( new Double(Math.PI), ctxt.lookup("math") );
224 } catch(NamingException ne) {
225 ne.printStackTrace();
226 fail("NamingException: "+ne.getMessage());
227 }
228 }
229
230 public void testBeanConverter() {
231 try {
232 Properties props = new Properties();
233 props.put("bean/type", "org.osjava.sj.loader.TestBean");
234 props.put("bean/converter", "org.osjava.sj.loader.convert.BeanConverter");
235 props.put("bean/text", "Example");
236
237 loader.load( props, ctxt );
238
239 TestBean testBean = new TestBean();
240 testBean.setText("Example");
241
242 assertEquals( testBean, ctxt.lookup("bean") );
243 } catch(NamingException ne) {
244 ne.printStackTrace();
245 fail("NamingException: "+ne.getMessage());
246 }
247 }
248
249 public void testDbcp() throws IOException, NamingException {
250 File file = new File("src/test/config/");
251 loader.loadDirectory( file, ctxt );
252 DataSource ds = (DataSource) ctxt.lookup("pooltest/TestDS");
253 DataSource ds1 = (DataSource) ctxt.lookup("pooltest/OneDS");
254 DataSource ds2 = (DataSource) ctxt.lookup("pooltest/TwoDS");
255 DataSource ds3 = (DataSource) ctxt.lookup("pooltest/ThreeDS");
256
257 try {
258 Connection conn = ds.getConnection();
259 fail("No database is hooked up, so this should have failed");
260 } catch (SQLException sqle) {
261
262 }
263 }
264
265 }