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.util.logging.Logger;
021:
022: import javax.xml.ws.Endpoint;
023: import javax.xml.ws.WebServiceException;
024:
025: import org.apache.cxf.Bus;
026: import org.apache.cxf.binding.soap.SoapFault;
027: import org.apache.cxf.bus.spring.SpringBusFactory;
028: import org.apache.cxf.greeter_control.BasicGreeterService;
029: import org.apache.cxf.greeter_control.Greeter;
030: import org.apache.cxf.greeter_control.PingMeFault;
031: import org.apache.cxf.interceptor.LoggingInInterceptor;
032: import org.apache.cxf.interceptor.LoggingOutInterceptor;
033: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
034: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
035: import org.junit.BeforeClass;
036: import org.junit.Test;
037:
038: /**
039: * Tests the use of the WS-Policy Framework to determine the behaviour of the HTTP client
040: * by policies including the HTTPClientPolicy assertion attached to different subjects
041: * of the contract (endpoint, operation, binding, messager).
042: * The server in this test is not policy aware.
043: * Neither client nor server do have addressing interceptors installed: there are no addressing
044: * assertions that would trigger the installation of the interceptors on the client side. The use
045: * of the DecoupledEndpoint attribute in the HTTPClientPolicy assertions is merely for illustrating
046: * the use of multiple compatible or incompatible assertions.
047: */
048: public class HTTPServerPolicyTest extends
049: AbstractBusClientServerTestBase {
050:
051: private static final Logger LOG = Logger
052: .getLogger(HTTPServerPolicyTest.class.getName());
053:
054: public static class Server extends AbstractBusTestServerBase {
055:
056: protected void run() {
057: SpringBusFactory bf = new SpringBusFactory();
058: Bus bus = bf
059: .createBus("org/apache/cxf/systest/ws/policy/http-server.xml");
060:
061: GreeterImpl implementor = new GreeterImpl();
062: implementor.setThrowAlways(true);
063: Endpoint.publish(
064: "http://localhost:9020/SoapContext/GreeterPort",
065: implementor);
066:
067: LOG.info("Published greeter endpoint.");
068:
069: LoggingInInterceptor in = new LoggingInInterceptor();
070: LoggingOutInterceptor out = new LoggingOutInterceptor();
071:
072: bus.getInInterceptors().add(in);
073: bus.getOutInterceptors().add(out);
074: bus.getOutFaultInterceptors().add(out);
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 testUsingHTTPServerPolicies() throws Exception {
098:
099: // use a plain client
100:
101: SpringBusFactory bf = new SpringBusFactory();
102: Bus bus = bf.createBus();
103:
104: BasicGreeterService gs = new BasicGreeterService();
105: final Greeter greeter = gs.getGreeterPort();
106: LoggingInInterceptor in = new LoggingInInterceptor();
107: LoggingOutInterceptor out = new LoggingOutInterceptor();
108:
109: bus.getInInterceptors().add(in);
110: bus.getOutInterceptors().add(out);
111:
112: LOG.fine("Created greeter client.");
113:
114: // sayHi - this operation has message policies that are incompatible with
115: // the endpoint policies
116:
117: try {
118: greeter.sayHi();
119: fail("Did not receive expected Exception.");
120: } catch (WebServiceException wse) {
121: SoapFault sf = (SoapFault) wse.getCause();
122: assertEquals("Server", sf.getFaultCode().getLocalPart());
123: assertEquals(
124: "None of the policy alternatives can be satisfied.",
125: sf.getMessage());
126: // assertEquals("INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS", ex.getCode());
127: }
128:
129: // greetMe - no operation or message specific policies
130:
131: assertEquals("CXF", greeter.greetMe("cxf"));
132:
133: // pingMe - policy attached to binding operation fault should have no effect
134:
135: try {
136: greeter.pingMe();
137: fail("Expected PingMeFault not thrown.");
138: } catch (PingMeFault ex) {
139: assertEquals(2, (int) ex.getFaultInfo().getMajor());
140: assertEquals(1, (int) ex.getFaultInfo().getMinor());
141: }
142:
143: }
144: }
|