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:
024: import org.apache.cxf.Bus;
025: import org.apache.cxf.BusFactory;
026: import org.apache.cxf.bus.spring.SpringBusFactory;
027: import org.apache.cxf.greeter_control.BasicGreeterService;
028: import org.apache.cxf.greeter_control.Greeter;
029: import org.apache.cxf.greeter_control.PingMeFault;
030: import org.apache.cxf.interceptor.LoggingInInterceptor;
031: import org.apache.cxf.interceptor.LoggingOutInterceptor;
032: import org.apache.cxf.systest.ws.util.ConnectionHelper;
033: import org.apache.cxf.systest.ws.util.InMessageRecorder;
034: import org.apache.cxf.systest.ws.util.MessageFlow;
035: import org.apache.cxf.systest.ws.util.OutMessageRecorder;
036: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
037: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
038: import org.apache.cxf.ws.policy.PolicyEngine;
039: import org.apache.cxf.ws.policy.selector.MinimalAlternativeSelector;
040: import org.apache.cxf.ws.rm.RMUtils;
041: import org.junit.BeforeClass;
042: import org.junit.Test;
043:
044: /**
045: * Tests the use of the WS-Policy Framework to automatically engage WS-Addressing and
046: * WS-RM in response to Policies defined for the endpoint via an external policy attachment.
047: */
048: public class AddressingOptionalPolicyTest extends
049: AbstractBusClientServerTestBase {
050:
051: private static final Logger LOG = Logger
052: .getLogger(AddressingOptionalPolicyTest.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/addr-optional.xml");
060: BusFactory.setDefaultBus(bus);
061: LoggingInInterceptor in = new LoggingInInterceptor();
062: bus.getInInterceptors().add(in);
063: bus.getInFaultInterceptors().add(in);
064: LoggingOutInterceptor out = new LoggingOutInterceptor();
065: bus.getOutInterceptors().add(out);
066: bus.getOutFaultInterceptors().add(out);
067:
068: GreeterImpl implementor = new GreeterImpl();
069: String address = "http://localhost:9020/SoapContext/GreeterPort";
070: Endpoint.publish(address, implementor);
071: LOG.info("Published greeter endpoint.");
072: }
073:
074: public static void main(String[] args) {
075: try {
076: Server s = new Server();
077: s.start();
078: } catch (Exception ex) {
079: ex.printStackTrace();
080: System.exit(-1);
081: } finally {
082: System.out.println("done!");
083: }
084: }
085: }
086:
087: @BeforeClass
088: public static void startServers() throws Exception {
089: assertTrue("server did not launch correctly",
090: launchServer(Server.class));
091: }
092:
093: @Test
094: public void testUsingAddressing() throws Exception {
095: SpringBusFactory bf = new SpringBusFactory();
096: bus = bf
097: .createBus("org/apache/cxf/systest/ws/policy/addr-optional.xml");
098: BusFactory.setDefaultBus(bus);
099: InMessageRecorder in = new InMessageRecorder();
100: bus.getInInterceptors().add(in);
101: OutMessageRecorder out = new OutMessageRecorder();
102: bus.getOutInterceptors().add(out);
103:
104: BasicGreeterService gs = new BasicGreeterService();
105: final Greeter greeter = gs.getGreeterPort();
106: LOG.fine("Created greeter client.");
107:
108: ConnectionHelper.setKeepAliveConnection(greeter, true);
109:
110: // oneway
111:
112: greeter.greetMeOneWay("CXF");
113:
114: // two-way
115:
116: assertEquals("CXF", greeter.greetMe("cxf"));
117:
118: // exception
119:
120: try {
121: greeter.pingMe();
122: } catch (PingMeFault ex) {
123: fail("First invocation should have succeeded.");
124: }
125:
126: try {
127: greeter.pingMe();
128: fail("Expected PingMeFault not thrown.");
129: } catch (PingMeFault ex) {
130: assertEquals(2, (int) ex.getFaultInfo().getMajor());
131: assertEquals(1, (int) ex.getFaultInfo().getMinor());
132: }
133:
134: MessageFlow mf = new MessageFlow(out.getOutboundMessages(), in
135: .getInboundMessages());
136: for (int i = 0; i < 3; i++) {
137: mf.verifyHeader(RMUtils.getAddressingConstants()
138: .getMessageIDQName(), true, i);
139: mf.verifyHeader(RMUtils.getAddressingConstants()
140: .getMessageIDQName(), false, i);
141: }
142: }
143:
144: @Test
145: public void testNotUsingAddressing() throws Exception {
146: SpringBusFactory bf = new SpringBusFactory();
147: bus = bf
148: .createBus("org/apache/cxf/systest/ws/policy/addr-optional.xml");
149: BusFactory.setDefaultBus(bus);
150: InMessageRecorder in = new InMessageRecorder();
151: bus.getInInterceptors().add(in);
152: OutMessageRecorder out = new OutMessageRecorder();
153: bus.getOutInterceptors().add(out);
154:
155: bus.getExtension(PolicyEngine.class).setAlternativeSelector(
156: new MinimalAlternativeSelector());
157:
158: BasicGreeterService gs = new BasicGreeterService();
159: final Greeter greeter = gs.getGreeterPort();
160: LOG.fine("Created greeter client.");
161:
162: ConnectionHelper.setKeepAliveConnection(greeter, true);
163:
164: // oneway
165:
166: greeter.greetMeOneWay("CXF");
167:
168: // two-way
169:
170: assertEquals("CXF", greeter.greetMe("cxf"));
171:
172: // exception
173:
174: try {
175: greeter.pingMe();
176: } catch (PingMeFault ex) {
177: fail("First invocation should have succeeded.");
178: }
179:
180: try {
181: greeter.pingMe();
182: fail("Expected PingMeFault not thrown.");
183: } catch (PingMeFault ex) {
184: assertEquals(2, (int) ex.getFaultInfo().getMajor());
185: assertEquals(1, (int) ex.getFaultInfo().getMinor());
186: }
187:
188: MessageFlow mf = new MessageFlow(out.getOutboundMessages(), in
189: .getInboundMessages());
190: for (int i = 0; i < 3; i++) {
191: mf.verifyNoHeader(RMUtils.getAddressingConstants()
192: .getMessageIDQName(), true, i);
193: mf.verifyNoHeader(RMUtils.getAddressingConstants()
194: .getMessageIDQName(), false, i);
195: }
196: }
197: }
|