KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > handlers > HandlerChainBuilderTest


1 package org.objectweb.celtix.handlers;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8
9 import javax.xml.ws.WebServiceException;
10 import javax.xml.ws.handler.Handler;
11 import javax.xml.ws.handler.LogicalHandler;
12 import javax.xml.ws.handler.MessageContext;
13
14 import junit.framework.TestCase;
15
16 import org.easymock.EasyMock;
17 import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerChainType;
18 import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerInitParamType;
19 import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerType;
20 import org.objectweb.celtix.bus.jaxws.configuration.types.ObjectFactory;
21
22 public class HandlerChainBuilderTest extends TestCase {
23
24     Handler[] allHandlers = {EasyMock.createMock(LogicalHandler.class), EasyMock.createMock(Handler.class),
25                              EasyMock.createMock(Handler.class), EasyMock.createMock(LogicalHandler.class)};
26     Handler[] logicalHandlers = {allHandlers[0], allHandlers[3]};
27     Handler[] protocolHandlers = {allHandlers[1], allHandlers[2]};
28
29     HandlerChainBuilder builder = new HandlerChainBuilder();
30
31     public void testChainSorting() {
32
33         List JavaDoc<Handler> sortedHandlerChain = builder.sortHandlers(Arrays.asList(allHandlers));
34         assertSame(logicalHandlers[0], sortedHandlerChain.get(0));
35         assertSame(logicalHandlers[1], sortedHandlerChain.get(1));
36         assertSame(protocolHandlers[0], sortedHandlerChain.get(2));
37         assertSame(protocolHandlers[1], sortedHandlerChain.get(3));
38     }
39
40     public void testBuildHandlerChainFromConfiguration() {
41
42         HandlerChainType hc = createHandlerChainType();
43         List JavaDoc<Handler> chain = builder.buildHandlerChainFromConfiguration(hc);
44
45         assertNotNull(chain);
46         assertEquals(4, chain.size());
47         assertEquals(TestLogicalHandler.class, chain.get(0).getClass());
48         assertEquals(TestLogicalHandler.class, chain.get(1).getClass());
49         assertEquals(TestProtocolHandler.class, chain.get(2).getClass());
50         assertEquals(TestProtocolHandler.class, chain.get(3).getClass());
51         
52         TestLogicalHandler tlh = (TestLogicalHandler)chain.get(0);
53         assertTrue(!tlh.initCalled);
54         assertNull(tlh.config);
55     }
56
57     public void testBuilderCallsInit() {
58
59         HandlerChainType hc = createHandlerChainType();
60         hc.getHandler().remove(3);
61         hc.getHandler().remove(2);
62         hc.getHandler().remove(1);
63         HandlerType h = hc.getHandler().get(0);
64         List JavaDoc<HandlerInitParamType> params = h.getInitParam();
65         HandlerInitParamType p = new ObjectFactory().createHandlerInitParamType();
66         p.setParamName("foo");
67         p.setParamValue("1");
68         params.add(p);
69         p = new ObjectFactory().createHandlerInitParamType();
70         p.setParamName("bar");
71         p.setParamValue("2");
72         params.add(p);
73         
74         List JavaDoc<Handler> chain = builder.buildHandlerChainFromConfiguration(hc);
75         assertEquals(1, chain.size());
76         TestLogicalHandler tlh = (TestLogicalHandler)chain.get(0);
77
78         assertTrue(tlh.initCalled);
79         Map JavaDoc cfg = tlh.config;
80         assertNotNull(tlh.config);
81
82         assertEquals(2, cfg.keySet().size());
83         Iterator JavaDoc iter = cfg.keySet().iterator();
84         assertEquals("foo", iter.next());
85         assertEquals("1", cfg.get("foo"));
86         assertEquals("bar", iter.next());
87         assertEquals("2", cfg.get("bar"));
88     }
89
90     public void testBuilderCallsInitWithNoInitParamValues() {
91
92         HandlerChainType hc = createHandlerChainType();
93         hc.getHandler().remove(3);
94         hc.getHandler().remove(2);
95         hc.getHandler().remove(1);
96         HandlerType h = hc.getHandler().get(0);
97         List JavaDoc<HandlerInitParamType> params = h.getInitParam();
98         HandlerInitParamType p = new ObjectFactory().createHandlerInitParamType();
99         p.setParamName("foo");
100         params.add(p);
101         p = new ObjectFactory().createHandlerInitParamType();
102         p.setParamName("bar");
103         params.add(p);
104         
105         List JavaDoc<Handler> chain = builder.buildHandlerChainFromConfiguration(hc);
106         assertEquals(1, chain.size());
107         TestLogicalHandler tlh = (TestLogicalHandler)chain.get(0);
108
109         assertTrue(tlh.initCalled);
110         Map JavaDoc cfg = tlh.config;
111         assertNotNull(tlh.config);
112         assertEquals(2, cfg.keySet().size());
113     }
114
115     public void testBuilderCannotLoadHandlerClass() {
116         HandlerChainType hc = createHandlerChainType();
117         hc.getHandler().remove(3);
118         hc.getHandler().remove(2);
119         hc.getHandler().remove(1);
120         hc.getHandler().get(0).setHandlerClass("no.such.class");
121         
122         try {
123             builder.buildHandlerChainFromConfiguration(hc);
124             fail("did not get expected exception");
125         } catch (WebServiceException ex) {
126             // ex.printStackTrace();
127
assertNotNull(ex.getCause());
128             assertEquals(ClassNotFoundException JavaDoc.class, ex.getCause().getClass());
129             // happy
130
}
131     }
132     
133     private HandlerChainType createHandlerChainType() {
134         HandlerChainType hc = new ObjectFactory().createHandlerChainType();
135         List JavaDoc<HandlerType> handlers = hc.getHandler();
136         HandlerType h = new ObjectFactory().createHandlerType();
137         h.setHandlerName("lh1");
138         h.setHandlerClass(TestLogicalHandler.class.getName());
139         handlers.add(h);
140         h = new ObjectFactory().createHandlerType();
141         h.setHandlerName("ph1");
142         h.setHandlerClass(TestProtocolHandler.class.getName());
143         handlers.add(h);
144         h = new ObjectFactory().createHandlerType();
145         h.setHandlerName("ph2");
146         h.setHandlerClass(TestProtocolHandler.class.getName());
147         handlers.add(h);
148         h = new ObjectFactory().createHandlerType();
149         h.setHandlerName("lh2");
150         h.setHandlerClass(TestLogicalHandler.class.getName());
151         handlers.add(h);
152         return hc;
153     }
154     
155     public static class TestLogicalHandler implements LogicalHandler {
156
157         Map JavaDoc config;
158         boolean initCalled;
159         
160         public void close(MessageContext arg0) {
161         }
162
163         public boolean handleFault(MessageContext arg0) {
164             return false;
165         }
166
167         public boolean handleMessage(MessageContext arg0) {
168             return false;
169         }
170         
171         public final void init(final Map JavaDoc map) {
172             config = map;
173             initCalled = true;
174         }
175     }
176     
177     public static class TestProtocolHandler implements Handler {
178
179         public void close(MessageContext arg0) {
180         }
181
182         public boolean handleFault(MessageContext arg0) {
183             return false;
184         }
185
186         public boolean handleMessage(MessageContext arg0) {
187             return false;
188         }
189     }
190
191 }
192
Free Books   Free Magazines  
Popular Tags