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.List;
021: import java.util.logging.Logger;
022:
023: import javax.xml.ws.Endpoint;
024:
025: import org.apache.cxf.Bus;
026: import org.apache.cxf.BusFactory;
027: import org.apache.cxf.bus.spring.SpringBusFactory;
028: import org.apache.cxf.endpoint.Client;
029: import org.apache.cxf.frontend.ClientProxy;
030: import org.apache.cxf.greeter_control.BasicGreeterService;
031: import org.apache.cxf.greeter_control.Greeter;
032: import org.apache.cxf.greeter_control.PingMeFault;
033: import org.apache.cxf.interceptor.Interceptor;
034: import org.apache.cxf.service.model.ServiceInfo;
035: import org.apache.cxf.systest.ws.util.ConnectionHelper;
036: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
037: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
038: import org.apache.cxf.ws.policy.ServerPolicyInInterceptor;
039: import org.apache.cxf.ws.policy.ServerPolicyOutInterceptor;
040: import org.apache.neethi.Policy;
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 inline policy in addr-inline-policy.xml.
047: */
048: public class AddressingInlinePolicyTest extends
049: AbstractBusClientServerTestBase {
050:
051: private static final Logger LOG = Logger
052: .getLogger(AddressingInlinePolicyTest.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-inline-policy.xml");
060:
061: GreeterImpl implementor = new GreeterImpl();
062: String address = "http://localhost:9020/SoapContext/GreeterPort";
063: Endpoint.publish(address, implementor);
064: LOG.info("Published greeter endpoint.");
065: testInterceptors(bus);
066: }
067:
068: public static void main(String[] args) {
069: try {
070: Server s = new Server();
071: s.start();
072: } catch (Exception ex) {
073: ex.printStackTrace();
074: System.exit(-1);
075: } finally {
076: System.out.println("done!");
077: }
078: }
079: }
080:
081: @BeforeClass
082: public static void startServers() throws Exception {
083: assertTrue("server did not launch correctly",
084: launchServer(Server.class));
085: }
086:
087: @Test
088: public void testUsingAddressing() throws Exception {
089: SpringBusFactory bf = new SpringBusFactory();
090: bus = bf
091: .createBus("org/apache/cxf/systest/ws/policy/addr-inline-policy.xml");
092: BusFactory.setDefaultBus(bus);
093:
094: BasicGreeterService gs = new BasicGreeterService();
095: final Greeter greeter = gs.getGreeterPort();
096: LOG.fine("Created greeter client.");
097:
098: ConnectionHelper.setKeepAliveConnection(greeter, true);
099:
100: Client client = ClientProxy.getClient(greeter);
101: List<ServiceInfo> sis = client.getEndpoint().getService()
102: .getServiceInfos();
103:
104: ServiceInfo si = sis.get(0);
105: Policy p = si.getExtensor(Policy.class);
106: assertNotNull(p);
107:
108: testInterceptors(bus);
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:
135: private static void testInterceptors(Bus b) {
136: boolean hasServerIn = false;
137: boolean hasServerOut = false;
138: List<Interceptor> inInterceptors = b.getInInterceptors();
139: for (Interceptor i : inInterceptors) {
140: if (i instanceof ServerPolicyInInterceptor) {
141: hasServerIn = true;
142: }
143: }
144: assertTrue(hasServerIn);
145:
146: for (Interceptor i : b.getOutInterceptors()) {
147: if (i instanceof ServerPolicyOutInterceptor) {
148: hasServerOut = true;
149: }
150: }
151: assertTrue(hasServerOut);
152: }
153: }
|