KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > lib > TestEJBProxyFactory


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package hivemind.test.lib;
16
17 import hivemind.test.lib.impl.FakeContext;
18 import hivemind.test.lib.impl.NameLookupHack;
19 import hivemind.test.lib.impl.SimpleHomeImpl;
20
21 import java.rmi.RemoteException JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.naming.Context JavaDoc;
25
26 import org.apache.hivemind.ApplicationRuntimeException;
27 import org.apache.hivemind.Registry;
28 import org.apache.hivemind.xml.XmlTestCase;
29
30 /**
31  * Tests for {@link org.apache.hivemind.lib.impl.EJBProxyFactory}.
32  *
33  * @author Howard M. Lewis Ship
34  */

35
36 public class TestEJBProxyFactory extends XmlTestCase
37 {
38     protected void tearDown() throws Exception JavaDoc
39     {
40         super.tearDown();
41
42         NameLookupHack._context = null;
43         NameLookupHack._properties = null;
44     }
45
46     public void testEJBProxy() throws Exception JavaDoc
47     {
48         Registry r = buildFrameworkRegistry("EJBProxy.xml");
49
50         SimpleHomeImpl home = new SimpleHomeImpl();
51         FakeContext context = new FakeContext();
52         context.bind("hivemind.test.lib.Simple", home);
53         NameLookupHack._context = context;
54
55         SimpleRemote object = (SimpleRemote) r.getService(
56                 "hivemind.test.lib.SimpleRemote",
57                 SimpleRemote.class);
58
59         assertEquals(7, object.add(4, 3));
60         // Exercise several code paths where objects are ready or cached.
61
assertEquals(201, object.add(1, 200));
62
63         // Tacked on here, a few tests that the NameLookup service builds
64
// the initial context properties correctly.
65

66         Map JavaDoc p = NameLookupHack._properties;
67
68         assertEquals("fred", p.get(Context.INITIAL_CONTEXT_FACTORY));
69         assertEquals("barney", p.get(Context.URL_PKG_PREFIXES));
70         assertEquals("wilma", p.get(Context.PROVIDER_URL));
71     }
72
73     public void testEJBProxyNameFailure() throws Exception JavaDoc
74     {
75         Registry r = buildFrameworkRegistry("EJBProxy.xml");
76
77         FakeContext context = new FakeContext();
78         context.setForceError(true);
79
80         NameLookupHack._context = context;
81
82         SimpleRemote object = (SimpleRemote) r.getService(
83                 "hivemind.test.lib.SimpleRemote",
84                 SimpleRemote.class);
85
86         try
87         {
88
89             object.add(4, 3);
90             unreachable();
91         }
92         catch (ApplicationRuntimeException ex)
93         {
94             assertExceptionSubstring(
95                     ex,
96                     "Unable to lookup 'hivemind.test.lib.Simple' in JNDI context");
97
98             Throwable JavaDoc t = findNestedException(ex);
99
100             assertExceptionSubstring(t, "Forced error: hivemind.test.lib.Simple");
101         }
102     }
103
104     public void testEJBProxyRemoteFailure() throws Exception JavaDoc
105     {
106         Registry r = buildFrameworkRegistry("EJBProxy.xml");
107
108         SimpleHomeImpl home = new SimpleHomeImpl();
109         home.setForceError(true);
110
111         FakeContext context = new FakeContext();
112         context.bind("hivemind.test.lib.Simple", home);
113         NameLookupHack._context = context;
114
115         NameLookupHack._context = context;
116
117         SimpleRemote object = (SimpleRemote) r.getService(
118                 "hivemind.test.lib.SimpleRemote",
119                 SimpleRemote.class);
120
121         try
122         {
123
124             object.add(4, 3);
125             unreachable();
126         }
127         catch (RemoteException JavaDoc ex)
128         {
129             assertExceptionSubstring(ex, "Forced error.");
130         }
131     }
132
133 }
Popular Tags