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;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  import javax.naming.Context;
38  import javax.naming.InitialContext;
39  import javax.naming.NamingException;
40  
41  import javax.sql.DataSource;
42  
43  
44  import junit.framework.TestCase;
45  
46  public class SimpleContextTest extends TestCase {
47  
48      private InitialContext ctxt;
49  
50      public SimpleContextTest(String name) {
51          super(name);
52      }
53  
54      public void setUp() {
55          try {
56              ctxt = new InitialContext();
57          } catch(NamingException ne) {
58              ne.printStackTrace();
59          }
60      }
61  
62      public void tearDown() {
63          this.ctxt = null;
64      }
65  
66      public void testValueLookup() throws NamingException {
67          assertEquals( "13", this.ctxt.lookup("test.value") );
68      }
69  
70      public void testListLookup() throws NamingException {
71          ArrayList list = new ArrayList();
72          list.add( "24" );
73          list.add( "25" );
74          list.add( "99" );
75          assertEquals( list, this.ctxt.lookup("thing.type.bob.age") );
76      }
77  
78      public void testList2Lookup() throws NamingException {
79          ArrayList list2 = new ArrayList();
80          list2.add( "Henri" );
81          list2.add( "Fred" );
82          assertEquals( list2, this.ctxt.lookup("name") );
83          assertEquals( "yandell.org", this.ctxt.lookup("url") );
84          assertEquals( "Foo", this.ctxt.lookup("com.genjava") );
85      }
86  
87      public void testXmlLookup() throws NamingException {
88  //            System.err.println("XML: "+this.ctxt.lookup("xmltest") );
89  // TODO: Should this return something? XML?
90  //            System.err.println("XML: "+this.ctxt.lookup("xmltest.config") );
91              assertEquals( "13", this.ctxt.lookup("xmltest.config.value") );
92              assertEquals( "Bang", this.ctxt.lookup("xmltest.config.four.five") );
93              assertEquals( "three", this.ctxt.lookup("xmltest.config.one.two") );
94              List list = new ArrayList();
95              list.add("one");
96              list.add("two");
97              assertEquals( list, this.ctxt.lookup("xmltest.config.multi.item") );
98      }
99  
100     public void testIniLookup() throws NamingException {
101         assertEquals( "blockless", this.ctxt.lookup("testini.first") );
102         assertEquals( "13", this.ctxt.lookup("testini.block1.value") );
103         assertEquals( "pears", this.ctxt.lookup("testini.block2.apple") );
104         assertEquals( "stairs", this.ctxt.lookup("testini.block2.orange") );
105     }
106 
107     public void testColonReplaceLookup() throws NamingException {
108         assertEquals( "42", this.ctxt.lookup("java:.magic") );
109 
110         Context subCtxt = (Context) this.ctxt.lookup("java:");
111         assertEquals( "42", subCtxt.lookup("magic") );
112     }
113 
114     public void testDoubleDSLookup() throws NamingException {
115         String dsString = "org.gjt.mm.mysql.Driver::::jdbc:mysql://127.0.0.1/tmp::::sa";
116         DataSource fooDS = (DataSource) this.ctxt.lookup("nested-datasource.com.foo.FooDS");
117         DataSource barDS = (DataSource) this.ctxt.lookup("nested-datasource.com.foo.BarDS");
118         assertEquals( dsString, fooDS.toString() );
119         assertEquals( dsString, barDS.toString() );
120     }
121 
122 }