001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.webservices;
018:
019: import junit.framework.TestCase;
020: import org.apache.openejb.jee.HandlerChains;
021: import org.apache.openejb.config.ReadDescriptors;
022: import org.apache.openejb.config.ConfigurationFactory;
023: import org.apache.openejb.assembler.classic.HandlerChainInfo;
024: import org.apache.openejb.assembler.classic.WsBuilder;
025:
026: import javax.xml.ws.handler.Handler;
027: import javax.xml.ws.handler.PortInfo;
028: import javax.xml.namespace.QName;
029: import javax.naming.InitialContext;
030: import java.util.List;
031: import java.net.URL;
032:
033: public class HandlerResolverImplTest extends TestCase {
034:
035: public void testBasic() throws Exception {
036: HandlerChains handlerChains = readHandlerChains("/handlers.xml");
037: assertEquals(3, handlerChains.getHandlerChain().size());
038:
039: List<HandlerChainInfo> handlerChainInfos = ConfigurationFactory
040: .toHandlerChainInfo(handlerChains);
041: List<HandlerChainData> handlerChainDatas = WsBuilder
042: .toHandlerChainData(handlerChainInfos, getClass()
043: .getClassLoader());
044: HandlerResolverImpl resolver = new HandlerResolverImpl(
045: handlerChainDatas, null, new InitialContext());
046:
047: List<Handler> handlers = null;
048:
049: handlers = resolver.getHandlerChain(new TestPortInfo(null,
050: null, null));
051: assertEquals(3, handlers.size());
052: }
053:
054: public void testServiceMatching() throws Exception {
055: HandlerChains handlerChains = readHandlerChains("/handlers_service.xml");
056: assertEquals(3, handlerChains.getHandlerChain().size());
057:
058: List<HandlerChainInfo> handlerChainInfos = ConfigurationFactory
059: .toHandlerChainInfo(handlerChains);
060: List<HandlerChainData> handlerChainDatas = WsBuilder
061: .toHandlerChainData(handlerChainInfos, getClass()
062: .getClassLoader());
063: HandlerResolverImpl resolver = new HandlerResolverImpl(
064: handlerChainDatas, null, new InitialContext());
065:
066: List<Handler> handlers = null;
067:
068: handlers = resolver.getHandlerChain(new TestPortInfo(null,
069: null, null));
070: assertEquals(0, handlers.size());
071:
072: QName serviceName1 = new QName("http://foo", "Bar");
073: handlers = resolver.getHandlerChain(new TestPortInfo(null,
074: null, serviceName1));
075: assertEquals(1, handlers.size());
076:
077: QName serviceName2 = new QName("http://foo", "Foo");
078: handlers = resolver.getHandlerChain(new TestPortInfo(null,
079: null, serviceName2));
080: assertEquals(2, handlers.size());
081:
082: QName serviceName3 = new QName("http://foo", "FooBar");
083: handlers = resolver.getHandlerChain(new TestPortInfo(null,
084: null, serviceName3));
085: assertEquals(1, handlers.size());
086:
087: QName serviceName4 = new QName("http://foo", "BarFoo");
088: handlers = resolver.getHandlerChain(new TestPortInfo(null,
089: null, serviceName4));
090: assertEquals(0, handlers.size());
091: }
092:
093: public void testBindingMatching() throws Exception {
094: HandlerChains handlerChains = readHandlerChains("/handlers_bindings.xml");
095: assertEquals(3, handlerChains.getHandlerChain().size());
096:
097: List<HandlerChainInfo> handlerChainInfos = ConfigurationFactory
098: .toHandlerChainInfo(handlerChains);
099: List<HandlerChainData> handlerChainDatas = WsBuilder
100: .toHandlerChainData(handlerChainInfos, getClass()
101: .getClassLoader());
102: HandlerResolverImpl resolver = new HandlerResolverImpl(
103: handlerChainDatas, null, new InitialContext());
104:
105: List<Handler> handlers = null;
106:
107: handlers = resolver.getHandlerChain(new TestPortInfo(null,
108: null, null));
109: assertEquals(0, handlers.size());
110:
111: handlers = resolver.getHandlerChain(new TestPortInfo(
112: "##SOAP12_HTTP", null, null));
113: assertEquals(0, handlers.size());
114:
115: handlers = resolver.getHandlerChain(new TestPortInfo(
116: "##SOAP11_HTTP", null, null));
117: assertEquals(2, handlers.size());
118:
119: handlers = resolver.getHandlerChain(new TestPortInfo(
120: "##SOAP11_HTTP_MTOM", null, null));
121: assertEquals(1, handlers.size());
122: }
123:
124: public void testPortMatching() throws Exception {
125: HandlerChains handlerChains = readHandlerChains("/handlers_port.xml");
126: assertEquals(3, handlerChains.getHandlerChain().size());
127:
128: List<HandlerChainInfo> handlerChainInfos = ConfigurationFactory
129: .toHandlerChainInfo(handlerChains);
130: List<HandlerChainData> handlerChainDatas = WsBuilder
131: .toHandlerChainData(handlerChainInfos, getClass()
132: .getClassLoader());
133: HandlerResolverImpl resolver = new HandlerResolverImpl(
134: handlerChainDatas, null, new InitialContext());
135:
136: List<Handler> handlers = null;
137:
138: handlers = resolver.getHandlerChain(new TestPortInfo(null,
139: null, null));
140: assertEquals(0, handlers.size());
141:
142: QName portName1 = new QName("http://foo", "Bar");
143: handlers = resolver.getHandlerChain(new TestPortInfo(null,
144: portName1, null));
145: assertEquals(1, handlers.size());
146:
147: QName portName2 = new QName("http://foo", "Foo");
148: handlers = resolver.getHandlerChain(new TestPortInfo(null,
149: portName2, null));
150: assertEquals(2, handlers.size());
151:
152: QName portName3 = new QName("http://foo", "FooBar");
153: handlers = resolver.getHandlerChain(new TestPortInfo(null,
154: portName3, null));
155: assertEquals(1, handlers.size());
156:
157: QName portName4 = new QName("http://foo", "BarFoo");
158: handlers = resolver.getHandlerChain(new TestPortInfo(null,
159: portName4, null));
160: assertEquals(0, handlers.size());
161: }
162:
163: public void testMixedMatching() throws Exception {
164: HandlerChains handlerChains = readHandlerChains("/handlers_mixed.xml");
165: assertEquals(3, handlerChains.getHandlerChain().size());
166:
167: List<HandlerChainInfo> handlerChainInfos = ConfigurationFactory
168: .toHandlerChainInfo(handlerChains);
169: List<HandlerChainData> handlerChainDatas = WsBuilder
170: .toHandlerChainData(handlerChainInfos, getClass()
171: .getClassLoader());
172: HandlerResolverImpl resolver = new HandlerResolverImpl(
173: handlerChainDatas, null, new InitialContext());
174:
175: List<Handler> handlers = null;
176:
177: handlers = resolver.getHandlerChain(new TestPortInfo(null,
178: null, null));
179: assertEquals(0, handlers.size());
180:
181: QName serviceName1 = new QName("http:/foo", "Bar");
182: QName portName1 = new QName("http://foo", "FooBar");
183: String binding1 = "##XML_HTTP";
184: handlers = resolver.getHandlerChain(new TestPortInfo(binding1,
185: portName1, serviceName1));
186: assertEquals(3, handlers.size());
187:
188: String binding2 = "##SOAP11_HTTP";
189: handlers = resolver.getHandlerChain(new TestPortInfo(binding2,
190: portName1, serviceName1));
191: assertEquals(2, handlers.size());
192:
193: QName serviceName2 = new QName("http://foo", "Baaz");
194: QName portName2 = new QName("http://foo", "Baaz");
195: handlers = resolver.getHandlerChain(new TestPortInfo(binding1,
196: portName2, serviceName2));
197: assertEquals(1, handlers.size());
198: }
199:
200: private HandlerChains readHandlerChains(String filePath)
201: throws Exception {
202: URL url = getClass().getResource(filePath);
203: assertNotNull("Could not find handler chains file " + filePath,
204: url);
205: HandlerChains handlerChains = ReadDescriptors
206: .readHandlerChains(url);
207: return handlerChains;
208: }
209:
210: private static class TestPortInfo implements PortInfo {
211:
212: private String bindingID;
213: private QName portName;
214: private QName serviceName;
215:
216: public TestPortInfo(String bindingID, QName portName,
217: QName serviceName) {
218: this .bindingID = bindingID;
219: this .portName = portName;
220: this .serviceName = serviceName;
221: }
222:
223: public String getBindingID() {
224: return this .bindingID;
225: }
226:
227: public QName getPortName() {
228: return this .portName;
229: }
230:
231: public QName getServiceName() {
232: return this.serviceName;
233: }
234:
235: }
236:
237: }
|