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;
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
89
90
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 }