1   /*
2    * Copyright (c) 2003-2005, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of Simple-JNDI nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
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          /* The default is 'flat', which isn't hierarchial and not what I want. */
65          /* Separator is required for non-flat */
66  
67          Hashtable contextEnv = new Hashtable();
68  
69          /* For GenericContext */
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          /* For Directory-Naming
76          contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
77          contextEnv.put(Context.URL_PKG_PREFIXES, "org.apache.naming");
78          contextEnv.put("jndi.syntax.direction", "left_to_right");
79          contextEnv.put("jndi.syntax.separator", "/");
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             // type is needed here as otherwise it does not know to allow subelements
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             // expected
262         }
263     }
264 
265 }