001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.jaxws.service;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import javax.jws.WebService;
024:
025: import org.apache.cxf.feature.AbstractFeature;
026: import org.apache.cxf.feature.Features;
027: import org.apache.cxf.frontend.ServerFactoryBean;
028: import org.apache.cxf.interceptor.InFaultInterceptors;
029: import org.apache.cxf.interceptor.InInterceptors;
030: import org.apache.cxf.interceptor.Interceptor;
031: import org.apache.cxf.interceptor.OutFaultInterceptors;
032: import org.apache.cxf.jaxws.AbstractJaxWsTest;
033: import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
034: import org.apache.cxf.jaxws.service.AnnotationFeature.AnnotationFeatureInterceptor;
035: import org.junit.Before;
036: import org.junit.Test;
037:
038: public class AnnotationInterceptorTest extends AbstractJaxWsTest {
039:
040: private ServerFactoryBean fb;
041:
042: private JaxWsServerFactoryBean jfb;
043:
044: @Before
045: public void setUp() {
046: fb = new ServerFactoryBean();
047: fb.setAddress("http://localhost");
048: fb.setBus(getBus());
049:
050: jfb = new JaxWsServerFactoryBean();
051: jfb.setAddress("http://localhost");
052: jfb.setBus(getBus());
053: }
054:
055: @Test
056: public void testSimpleFrontend() throws Exception {
057: fb.setServiceClass(HelloService.class);
058: HelloService hello = new HelloServiceImpl();
059: fb.setServiceBean(hello);
060: fb.create();
061:
062: List<Interceptor> interceptors = fb.getServer().getEndpoint()
063: .getInInterceptors();
064: assertTrue(hasTestInterceptor(interceptors));
065: assertFalse(hasTest2Interceptor(interceptors));
066:
067: List<Interceptor> outFaultInterceptors = fb.getServer()
068: .getEndpoint().getOutFaultInterceptors();
069: assertTrue(hasTestInterceptor(outFaultInterceptors));
070: assertTrue(hasTest2Interceptor(outFaultInterceptors));
071: }
072:
073: @Test
074: public void testSimpleFrontendWithFeature() throws Exception {
075: fb.setServiceClass(HelloService.class);
076: HelloService hello = new HelloServiceImpl();
077: fb.setServiceBean(hello);
078: fb.create();
079:
080: List<AbstractFeature> features = fb.getFeatures();
081: assertTrue(hasAnnotationFeature(features));
082: }
083:
084: @Test
085: public void testSimpleFrontendWithNoAnnotation() throws Exception {
086: fb.setServiceClass(HelloService.class);
087: HelloService hello = new HelloServiceImplNoAnnotation();
088: fb.setServiceBean(hello);
089: fb.create();
090:
091: List<Interceptor> interceptors = fb.getServer().getEndpoint()
092: .getInInterceptors();
093: assertFalse(hasTestInterceptor(interceptors));
094:
095: List<AbstractFeature> features = fb.getFeatures();
096: assertFalse(hasAnnotationFeature(features));
097: }
098:
099: @Test
100: public void testJaxwsFrontendWithNoAnnotation() throws Exception {
101: jfb.setServiceClass(SayHi.class);
102: jfb.setServiceBean(new SayHiNoInterceptor());
103:
104: jfb.create();
105: List<Interceptor> interceptors = jfb.getServer().getEndpoint()
106: .getInInterceptors();
107: assertFalse(hasTestInterceptor(interceptors));
108:
109: List<AbstractFeature> features = fb.getFeatures();
110: assertFalse(hasAnnotationFeature(features));
111: }
112:
113: @Test
114: public void testJaxwsFrontendWithAnnotationInImpl()
115: throws Exception {
116: jfb.setServiceClass(SayHi.class);
117: SayHi implementor = new SayHiImplementation();
118: jfb.setServiceBean(implementor);
119:
120: jfb.create();
121: List<Interceptor> interceptors = jfb.getServer().getEndpoint()
122: .getInInterceptors();
123: assertTrue(hasTestInterceptor(interceptors));
124:
125: List<Interceptor> inFaultInterceptors = jfb.getServer()
126: .getEndpoint().getInFaultInterceptors();
127: assertFalse(hasTestInterceptor(inFaultInterceptors));
128: assertTrue(hasTest2Interceptor(inFaultInterceptors));
129:
130: List<AbstractFeature> features = jfb.getFeatures();
131: assertTrue(hasAnnotationFeature(features));
132: }
133:
134: @Test
135: public void testJaxwsFrontendWithFeatureAnnotation()
136: throws Exception {
137: jfb.setServiceClass(SayHi.class);
138: SayHi implementor = new SayHiImplementation();
139: jfb.setServiceBean(implementor);
140:
141: jfb.create();
142: List<Interceptor> interceptors = jfb.getServer().getEndpoint()
143: .getInInterceptors();
144: assertTrue(hasAnnotationFeatureInterceptor(interceptors));
145:
146: List<Interceptor> outInterceptors = jfb.getServer()
147: .getEndpoint().getOutInterceptors();
148: assertTrue(hasAnnotationFeatureInterceptor(outInterceptors));
149: }
150:
151: @Test
152: public void testJaxWsFrontendWithAnnotationInSEI() throws Exception {
153: jfb.setServiceClass(SayHiInterface.class);
154: jfb.setServiceBean(new SayHiInterfaceImpl());
155: jfb.create();
156:
157: List<Interceptor> interceptors = jfb.getServer().getEndpoint()
158: .getInInterceptors();
159: assertTrue(hasTestInterceptor(interceptors));
160:
161: List<AbstractFeature> features = jfb.getFeatures();
162: assertTrue(hasAnnotationFeature(features));
163: }
164:
165: @Test
166: public void testJaxWsFrontendWithAnnotationInSEIAndImpl()
167: throws Exception {
168: jfb.setServiceClass(SayHiInterface.class);
169: jfb.setServiceBean(new SayHiInterfaceImpl2());
170: jfb.create();
171:
172: List<Interceptor> interceptors = jfb.getServer().getEndpoint()
173: .getInInterceptors();
174: assertFalse(hasTestInterceptor(interceptors));
175: assertTrue(hasTest2Interceptor(interceptors));
176: }
177:
178: private boolean hasTestInterceptor(List<Interceptor> interceptors) {
179: boolean flag = false;
180: for (Interceptor it : interceptors) {
181: if (it instanceof TestInterceptor) {
182: flag = true;
183: }
184: }
185: return flag;
186: }
187:
188: private boolean hasTest2Interceptor(List<Interceptor> interceptors) {
189: boolean flag = false;
190: for (Interceptor it : interceptors) {
191: if (it instanceof Test2Interceptor) {
192: flag = true;
193: }
194: }
195: return flag;
196: }
197:
198: private boolean hasAnnotationFeature(List<AbstractFeature> features) {
199: boolean flag = false;
200: for (AbstractFeature af : features) {
201: if (af instanceof AnnotationFeature) {
202: flag = true;
203: }
204: }
205: return flag;
206: }
207:
208: private boolean hasAnnotationFeatureInterceptor(
209: List<Interceptor> interceptors) {
210: boolean flag = false;
211: for (Interceptor it : interceptors) {
212: if (it instanceof AnnotationFeatureInterceptor) {
213: flag = true;
214: }
215: }
216: return flag;
217: }
218:
219: @InInterceptors(interceptors="org.apache.cxf.jaxws.service.TestInterceptor")
220: @OutFaultInterceptors(interceptors={"org.apache.cxf.jaxws.service.TestInterceptor","org.apache.cxf.jaxws.service.Test2Interceptor"})
221: @Features(features="org.apache.cxf.jaxws.service.AnnotationFeature")
222: public class HelloServiceImpl implements HelloService {
223: public String sayHi() {
224: return "HI";
225: }
226: }
227:
228: public class HelloServiceImplNoAnnotation implements HelloService {
229: public String sayHi() {
230: return "HI";
231: }
232: }
233:
234: @WebService(serviceName="SayHiService",portName="HelloPort",targetNamespace="http://mynamespace.com/",endpointInterface="org.apache.cxf.jaxws.service.SayHi")
235: @InInterceptors(interceptors={"org.apache.cxf.jaxws.service.TestInterceptor"})
236: @InFaultInterceptors(interceptors={"org.apache.cxf.jaxws.service.Test2Interceptor"})
237: @Features(features="org.apache.cxf.jaxws.service.AnnotationFeature")
238: public class SayHiImplementation implements SayHi {
239: public long sayHi(long arg) {
240: return arg;
241: }
242:
243: public void greetMe() {
244:
245: }
246:
247: public String[] getStringArray(String[] strs) {
248: String[] strings = new String[2];
249: strings[0] = "Hello" + strs[0];
250: strings[1] = "Bonjour" + strs[1];
251: return strings;
252: }
253:
254: public List<String> getStringList(List<String> list) {
255: List<String> ret = new ArrayList<String>();
256: ret.add("Hello" + list.get(0));
257: ret.add("Bonjour" + list.get(1));
258: return ret;
259: }
260: }
261:
262: @WebService(serviceName="SayHiService",portName="HelloPort",targetNamespace="http://mynamespace.com/",endpointInterface="org.apache.cxf.jaxws.service.SayHi")
263: public class SayHiNoInterceptor implements SayHi {
264: public long sayHi(long arg) {
265: return arg;
266: }
267:
268: public void greetMe() {
269:
270: }
271:
272: public String[] getStringArray(String[] strs) {
273: String[] strings = new String[2];
274: strings[0] = "Hello" + strs[0];
275: strings[1] = "Bonjour" + strs[1];
276: return strings;
277: }
278:
279: public List<String> getStringList(List<String> list) {
280: List<String> ret = new ArrayList<String>();
281: ret.add("Hello" + list.get(0));
282: ret.add("Bonjour" + list.get(1));
283: return ret;
284: }
285: }
286:
287: @WebService(endpointInterface="org.apache.cxf.jaxws.service.SayHiInterface")
288: public class SayHiInterfaceImpl implements SayHiInterface {
289: public String sayHi(String s) {
290: return "HI";
291: }
292: }
293:
294: @WebService(endpointInterface="org.apache.cxf.jaxws.service.SayHiInterface")
295: @InInterceptors(interceptors="org.apache.cxf.jaxws.service.Test2Interceptor")
296: public class SayHiInterfaceImpl2 implements SayHiInterface {
297: public String sayHi(String s) {
298: return "HI";
299: }
300: }
301:
302: }
|