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.systest.ws.policy;
019:
020: import java.net.SocketTimeoutException;
021: import java.net.URL;
022: import java.util.logging.Logger;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.ws.Endpoint;
026: import javax.xml.ws.WebServiceException;
027:
028: import org.apache.cxf.Bus;
029: import org.apache.cxf.BusFactory;
030: import org.apache.cxf.bus.spring.SpringBusFactory;
031: import org.apache.cxf.greeter_control.BasicGreeterService;
032: import org.apache.cxf.greeter_control.Greeter;
033: import org.apache.cxf.greeter_control.PingMeFault;
034: import org.apache.cxf.interceptor.LoggingInInterceptor;
035: import org.apache.cxf.interceptor.LoggingOutInterceptor;
036: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
037: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
038: import org.apache.cxf.ws.policy.PolicyException;
039: import org.junit.BeforeClass;
040: import org.junit.Test;
041:
042: /**
043: * Tests the use of the WS-Policy Framework to determine the behaviour of the HTTP client
044: * by policies including the HTTPClientPolicy assertion attached to different subjects
045: * of the contract (endpoint, operation, binding, messager).
046: * The server in this test is not policy aware.
047: * Neither client nor server do have addressing interceptors installed: there are no addressing
048: * assertions that would trigger the installation of the interceptors on the client side. The use
049: * of the DecoupledEndpoint attribute in the HTTPClientPolicy assertions is merely for illustrating
050: * the use of multiple compatible or incompatible assertions.
051: */
052: public class HTTPClientPolicyTest extends
053: AbstractBusClientServerTestBase {
054:
055: private static final Logger LOG = Logger
056: .getLogger(HTTPClientPolicyTest.class.getName());
057:
058: public static class Server extends AbstractBusTestServerBase {
059:
060: protected void run() {
061: SpringBusFactory bf = new SpringBusFactory();
062: Bus bus = bf.createBus();
063: BusFactory.setDefaultBus(bus);
064: LoggingInInterceptor in = new LoggingInInterceptor();
065: LoggingOutInterceptor out = new LoggingOutInterceptor();
066: bus.getInInterceptors().add(in);
067: bus.getOutInterceptors().add(out);
068: bus.getOutFaultInterceptors().add(out);
069:
070: HttpGreeterImpl implementor = new HttpGreeterImpl();
071: implementor.setThrowAlways(true);
072: String address = "http://localhost:9020/SoapContext/GreeterPort";
073: Endpoint.publish(address, implementor);
074: LOG.info("Published greeter endpoint.");
075: }
076:
077: public static void main(String[] args) {
078: try {
079: Server s = new Server();
080: s.start();
081: } catch (Exception ex) {
082: ex.printStackTrace();
083: System.exit(-1);
084: } finally {
085: System.out.println("done!");
086: }
087: }
088: }
089:
090: @BeforeClass
091: public static void startServers() throws Exception {
092: assertTrue("server did not launch correctly",
093: launchServer(Server.class));
094: }
095:
096: @Test
097: public void testUsingHTTPClientPolicies() throws Exception {
098: SpringBusFactory bf = new SpringBusFactory();
099: bus = bf.createBus("org/apache/cxf/systest/ws/policy/http.xml");
100: BusFactory.setDefaultBus(bus);
101: LoggingInInterceptor in = new LoggingInInterceptor();
102: bus.getInInterceptors().add(in);
103: bus.getInFaultInterceptors().add(in);
104: LoggingOutInterceptor out = new LoggingOutInterceptor();
105: bus.getOutInterceptors().add(out);
106: bus.getOutFaultInterceptors().add(out);
107:
108: // use a client wsdl with policies attached to endpoint, operation and message subjects
109:
110: URL url = HTTPClientPolicyTest.class
111: .getResource("http_client_greeter.wsdl");
112:
113: BasicGreeterService gs = new BasicGreeterService(url,
114: new QName("http://cxf.apache.org/greeter_control",
115: "BasicGreeterService"));
116: final Greeter greeter = gs.getGreeterPort();
117: LOG.fine("Created greeter client.");
118:
119: // sayHi - this operation has message policies that are incompatible with
120: // the endpoint policies
121:
122: try {
123: greeter.sayHi();
124: fail("Did not receive expected PolicyException.");
125: } catch (WebServiceException wex) {
126: PolicyException ex = (PolicyException) wex.getCause();
127: assertEquals("INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS", ex
128: .getCode());
129: }
130:
131: // greetMeOneWay - no message or operation policies
132:
133: greeter.greetMeOneWay("CXF");
134:
135: // greetMe - operation policy specifies receive timeout and should cause every
136: // other invocation to fail
137:
138: assertEquals("CXF", greeter.greetMe("cxf"));
139:
140: try {
141: greeter.greetMe("cxf");
142: } catch (Exception ex) {
143: assertTrue(ex.getCause() instanceof SocketTimeoutException);
144: }
145:
146: // pingMe - policy attached to binding operation fault should have no effect
147:
148: try {
149: greeter.pingMe();
150: fail("Expected PingMeFault not thrown.");
151: } catch (PingMeFault ex) {
152: assertEquals(2, (int) ex.getFaultInfo().getMajor());
153: assertEquals(1, (int) ex.getFaultInfo().getMinor());
154: }
155:
156: }
157: }
|