001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.cxf;
017:
018: import java.io.InputStream;
019: import java.util.List;
020:
021: import javax.xml.bind.JAXBContext;
022: import javax.xml.bind.JAXBElement;
023: import javax.xml.bind.JAXBException;
024: import javax.xml.bind.Unmarshaller;
025: import javax.xml.namespace.QName;
026: import javax.xml.transform.stream.StreamSource;
027:
028: import javax.xml.ws.handler.Handler;
029:
030: import org.apache.cxf.jaxws.handler.PortInfoImpl;
031: import org.apache.cxf.jaxws.javaee.HandlerChainsType;
032:
033: import org.apache.geronimo.testsupport.TestSupport;
034:
035: public class CXFHandlerResolverTest extends TestSupport {
036:
037: public void testBasic() throws Exception {
038: InputStream in = getClass()
039: .getResourceAsStream("/handlers.xml");
040: assertTrue(in != null);
041: HandlerChainsType handlerChains = toHandlerChains(in);
042: assertEquals(3, handlerChains.getHandlerChain().size());
043:
044: CXFHandlerResolver resolver = new CXFHandlerResolver(getClass()
045: .getClassLoader(), getClass(), handlerChains, null);
046:
047: List<Handler> handlers = null;
048:
049: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
050: null, null));
051: assertEquals(3, handlers.size());
052: }
053:
054: public void testServiceMatching() throws Exception {
055: InputStream in = getClass().getResourceAsStream(
056: "/handlers_service.xml");
057: assertTrue(in != null);
058: HandlerChainsType handlerChains = toHandlerChains(in);
059: assertEquals(3, handlerChains.getHandlerChain().size());
060:
061: CXFHandlerResolver resolver = new CXFHandlerResolver(getClass()
062: .getClassLoader(), getClass(), handlerChains, null);
063:
064: List<Handler> handlers = null;
065:
066: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
067: null, null));
068: assertEquals(0, handlers.size());
069:
070: QName serviceName1 = new QName("http://foo", "Bar");
071: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
072: null, serviceName1));
073: assertEquals(1, handlers.size());
074:
075: QName serviceName2 = new QName("http://foo", "Foo");
076: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
077: null, serviceName2));
078: assertEquals(2, handlers.size());
079:
080: QName serviceName3 = new QName("http://foo", "FooBar");
081: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
082: null, serviceName3));
083: assertEquals(1, handlers.size());
084:
085: QName serviceName4 = new QName("http://foo", "BarFoo");
086: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
087: null, serviceName4));
088: assertEquals(0, handlers.size());
089: }
090:
091: public void testBindingMatching() throws Exception {
092: InputStream in = getClass().getResourceAsStream(
093: "/handlers_bindings.xml");
094: assertTrue(in != null);
095: HandlerChainsType handlerChains = toHandlerChains(in);
096: assertEquals(4, handlerChains.getHandlerChain().size());
097:
098: CXFHandlerResolver resolver = new CXFHandlerResolver(getClass()
099: .getClassLoader(), getClass(), handlerChains, null);
100:
101: List<Handler> handlers = null;
102:
103: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
104: null, null));
105: assertEquals(0, handlers.size());
106:
107: handlers = resolver.getHandlerChain(new PortInfoImpl(
108: "http://foobar", null, null));
109: assertEquals(0, handlers.size());
110:
111: handlers = resolver.getHandlerChain(new PortInfoImpl(
112: "##SOAP11_HTTP", null, null));
113: assertEquals(2, handlers.size());
114:
115: handlers = resolver
116: .getHandlerChain(new PortInfoImpl(
117: "http://schemas.xmlsoap.org/wsdl/soap/http",
118: null, null));
119: assertEquals(2, handlers.size());
120:
121: handlers = resolver.getHandlerChain(new PortInfoImpl(
122: "##SOAP11_HTTP_MTOM", null, null));
123: assertEquals(1, handlers.size());
124:
125: handlers = resolver.getHandlerChain(new PortInfoImpl(
126: "##XML_HTTP", null, null));
127: assertEquals(2, handlers.size());
128: }
129:
130: public void testPortMatching() throws Exception {
131: InputStream in = getClass().getResourceAsStream(
132: "/handlers_port.xml");
133: assertTrue(in != null);
134: HandlerChainsType handlerChains = toHandlerChains(in);
135: assertEquals(3, handlerChains.getHandlerChain().size());
136:
137: CXFHandlerResolver resolver = new CXFHandlerResolver(getClass()
138: .getClassLoader(), getClass(), handlerChains, null);
139:
140: List<Handler> handlers = null;
141:
142: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
143: null, null));
144: assertEquals(0, handlers.size());
145:
146: QName portName1 = new QName("http://foo", "Bar");
147: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
148: portName1, null));
149: assertEquals(1, handlers.size());
150:
151: QName portName2 = new QName("http://foo", "Foo");
152: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
153: portName2, null));
154: assertEquals(2, handlers.size());
155:
156: QName portName3 = new QName("http://foo", "FooBar");
157: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
158: portName3, null));
159: assertEquals(1, handlers.size());
160:
161: QName portName4 = new QName("http://foo", "BarFoo");
162: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
163: portName4, null));
164: assertEquals(0, handlers.size());
165: }
166:
167: public void testMixedMatching() throws Exception {
168: InputStream in = getClass().getResourceAsStream(
169: "/handlers_mixed.xml");
170: assertTrue(in != null);
171: HandlerChainsType handlerChains = toHandlerChains(in);
172: assertEquals(3, handlerChains.getHandlerChain().size());
173:
174: CXFHandlerResolver resolver = new CXFHandlerResolver(getClass()
175: .getClassLoader(), getClass(), handlerChains, null);
176:
177: List<Handler> handlers = null;
178:
179: handlers = resolver.getHandlerChain(new PortInfoImpl(null,
180: null, null));
181: assertEquals(0, handlers.size());
182:
183: QName serviceName1 = new QName("http:/foo", "Bar");
184: QName portName1 = new QName("http://foo", "FooBar");
185: String binding1 = "##XML_HTTP";
186: handlers = resolver.getHandlerChain(new PortInfoImpl(binding1,
187: portName1, serviceName1));
188: assertEquals(3, handlers.size());
189:
190: String binding2 = "##SOAP11_HTTP";
191: handlers = resolver.getHandlerChain(new PortInfoImpl(binding2,
192: portName1, serviceName1));
193: assertEquals(2, handlers.size());
194:
195: QName serviceName2 = new QName("http://foo", "Baaz");
196: QName portName2 = new QName("http://foo", "Baaz");
197: handlers = resolver.getHandlerChain(new PortInfoImpl(binding1,
198: portName2, serviceName2));
199: assertEquals(1, handlers.size());
200: }
201:
202: private static HandlerChainsType toHandlerChains(InputStream input)
203: throws JAXBException {
204: JAXBContext ctx = JAXBContext
205: .newInstance(HandlerChainsType.class);
206: Unmarshaller unmarshaller = ctx.createUnmarshaller();
207: StreamSource in = new StreamSource(input);
208: JAXBElement handlerElement = unmarshaller.unmarshal(in,
209: HandlerChainsType.class);
210:
211: return (HandlerChainsType) handlerElement.getValue();
212: }
213:
214: }
|