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;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023:
024: import javax.xml.ws.WebServiceContext;
025:
026: import org.apache.cxf.Bus;
027: import org.apache.cxf.BusException;
028: import org.apache.cxf.BusFactory;
029: import org.apache.cxf.jaxws.service.Hello;
030: import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
031: import org.apache.cxf.message.Message;
032: import org.apache.cxf.service.invoker.BeanInvoker;
033: import org.apache.cxf.transport.Conduit;
034: import org.apache.cxf.transport.MessageObserver;
035: import org.apache.hello_world_soap_http.GreeterImpl;
036: import org.apache.hello_world_soap_http.HelloImpl;
037: import org.apache.hello_world_soap_http.HelloWrongAnnotation;
038: import org.junit.Test;
039:
040: public class EndpointImplTest extends AbstractJaxWsTest {
041:
042: @Override
043: protected Bus createBus() throws BusException {
044: return BusFactory.getDefaultBus();
045: }
046:
047: @Test
048: public void testEndpoint() throws Exception {
049: GreeterImpl greeter = new GreeterImpl();
050: EndpointImpl endpoint = new EndpointImpl(getBus(), greeter,
051: (String) null);
052:
053: WebServiceContext ctx = greeter.getContext();
054: assertNull(ctx);
055: try {
056: String address = "http://localhost:8080/test";
057: endpoint.publish(address);
058: } catch (IllegalArgumentException ex) {
059: assertTrue(ex.getCause() instanceof BusException);
060: assertEquals("BINDING_INCOMPATIBLE_ADDRESS_EXC",
061: ((BusException) ex.getCause()).getCode());
062: }
063: ctx = greeter.getContext();
064:
065: assertNotNull(ctx);
066:
067: }
068:
069: @Test
070: public void testEndpointServiceConstructor() throws Exception {
071: GreeterImpl greeter = new GreeterImpl();
072: JaxWsServiceFactoryBean serviceFactory = new JaxWsServiceFactoryBean();
073: serviceFactory.setBus(getBus());
074: serviceFactory.setInvoker(new BeanInvoker(greeter));
075: serviceFactory.setServiceClass(GreeterImpl.class);
076:
077: EndpointImpl endpoint = new EndpointImpl(getBus(), greeter,
078: new JaxWsServerFactoryBean(serviceFactory));
079:
080: WebServiceContext ctx = greeter.getContext();
081: assertNull(ctx);
082: try {
083: String address = "http://localhost:8080/test";
084: endpoint.publish(address);
085: } catch (IllegalArgumentException ex) {
086: assertTrue(ex.getCause() instanceof BusException);
087: assertEquals("BINDING_INCOMPATIBLE_ADDRESS_EXC",
088: ((BusException) ex.getCause()).getCode());
089: }
090: ctx = greeter.getContext();
091:
092: assertNotNull(ctx);
093: }
094:
095: @Test
096: public void testWSAnnoWithoutWSDLLocationInSEI() throws Exception {
097: HelloImpl hello = new HelloImpl();
098: JaxWsServiceFactoryBean serviceFactory = new JaxWsServiceFactoryBean();
099: serviceFactory.setBus(getBus());
100: serviceFactory.setInvoker(new BeanInvoker(hello));
101: serviceFactory.setServiceClass(HelloImpl.class);
102:
103: EndpointImpl endpoint = new EndpointImpl(getBus(), hello,
104: new JaxWsServerFactoryBean(serviceFactory));
105:
106: try {
107: String address = "http://localhost:8080/test";
108: endpoint.publish(address);
109: } catch (IllegalArgumentException ex) {
110: assertTrue(ex.getCause() instanceof BusException);
111: assertEquals("BINDING_INCOMPATIBLE_ADDRESS_EXC",
112: ((BusException) ex.getCause()).getCode());
113: }
114: }
115:
116: @Test
117: public void testSOAPBindingOnMethodWithRPC() {
118: HelloWrongAnnotation hello = new HelloWrongAnnotation();
119: JaxWsServiceFactoryBean serviceFactory = new JaxWsServiceFactoryBean();
120: serviceFactory.setBus(getBus());
121: serviceFactory.setInvoker(new BeanInvoker(hello));
122: serviceFactory.setServiceClass(HelloWrongAnnotation.class);
123:
124: try {
125: new EndpointImpl(getBus(), hello,
126: new JaxWsServerFactoryBean(serviceFactory));
127: } catch (Exception e) {
128: String expeced = "Method [sayHi] processing error: SOAPBinding can not on method with RPC style";
129: assertEquals(expeced, e.getMessage());
130: }
131: }
132:
133: @Test
134: public void testPublishEndpointPermission() throws Exception {
135: Hello service = new Hello();
136: EndpointImpl ep = new EndpointImpl(getBus(), service,
137: (String) null);
138:
139: System.setProperty(
140: EndpointImpl.CHECK_PUBLISH_ENDPOINT_PERMISSON_PROPERTY,
141: "true");
142:
143: try {
144: ep.publish("local://localhost:9090/hello");
145: fail("Did not throw exception as expected");
146: } catch (SecurityException e) {
147: // that's expected
148: } finally {
149: System
150: .setProperty(
151: EndpointImpl.CHECK_PUBLISH_ENDPOINT_PERMISSON_PROPERTY,
152: "false");
153: }
154:
155: ep.publish("local://localhost:9090/hello");
156: }
157:
158: static class EchoObserver implements MessageObserver {
159:
160: public void onMessage(Message message) {
161: try {
162: Conduit backChannel = message.getDestination()
163: .getBackChannel(message, null, null);
164:
165: backChannel.prepare(message);
166:
167: OutputStream out = message
168: .getContent(OutputStream.class);
169: assertNotNull(out);
170: InputStream in = message.getContent(InputStream.class);
171: assertNotNull(in);
172:
173: copy(in, out, 2045);
174:
175: out.close();
176: in.close();
177: } catch (Exception e) {
178: e.printStackTrace();
179: }
180: }
181: }
182:
183: private static void copy(final InputStream input,
184: final OutputStream output, final int bufferSize)
185: throws IOException {
186: try {
187: final byte[] buffer = new byte[bufferSize];
188:
189: int n = input.read(buffer);
190: while (-1 != n) {
191: output.write(buffer, 0, n);
192: n = input.read(buffer);
193: }
194: } finally {
195: input.close();
196: output.close();
197: }
198: }
199: }
|