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.transport.http.policy;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023:
024: import org.apache.cxf.helpers.CastUtils;
025: import org.apache.cxf.message.Exchange;
026: import org.apache.cxf.message.Message;
027: import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
028: import org.apache.cxf.transports.http.configuration.HTTPServerPolicy;
029: import org.apache.cxf.ws.policy.AssertionInfo;
030: import org.apache.cxf.ws.policy.AssertionInfoMap;
031: import org.apache.cxf.ws.policy.builder.jaxb.JaxbAssertion;
032: import org.apache.neethi.Assertion;
033: import org.easymock.classextension.EasyMock;
034: import org.easymock.classextension.IMocksControl;
035: import org.junit.Assert;
036: import org.junit.Before;
037: import org.junit.Test;
038:
039: /**
040: *
041: */
042: public class PolicyUtilsTest extends Assert {
043:
044: private IMocksControl control;
045:
046: @Before
047: public void setUp() {
048: control = EasyMock.createNiceControl();
049: }
050:
051: @Test
052: public void testCompatibleClientPolicies() {
053: HTTPClientPolicy p1 = new HTTPClientPolicy();
054: assertTrue("Policy is not compatible with itself.", PolicyUtils
055: .compatible(p1, p1));
056: HTTPClientPolicy p2 = new HTTPClientPolicy();
057: assertTrue("Policies are not compatible.", PolicyUtils
058: .compatible(p1, p2));
059: p1.setBrowserType("browser");
060: assertTrue("Policies are not compatible.", PolicyUtils
061: .compatible(p1, p2));
062: p1.setBrowserType(null);
063: p1.setConnectionTimeout(10000);
064: assertTrue("Policies are not compatible.", PolicyUtils
065: .compatible(p1, p2));
066: p1.setAllowChunking(false);
067: assertTrue("Policies are compatible.", !PolicyUtils.compatible(
068: p1, p2));
069: p2.setAllowChunking(false);
070: assertTrue("Policies are compatible.", PolicyUtils.compatible(
071: p1, p2));
072: }
073:
074: @Test
075: public void testIntersectClientPolicies() {
076: HTTPClientPolicy p1 = new HTTPClientPolicy();
077: HTTPClientPolicy p2 = new HTTPClientPolicy();
078: HTTPClientPolicy p = null;
079:
080: p1.setBrowserType("browser");
081: p = PolicyUtils.intersect(p1, p2);
082: assertEquals("browser", p.getBrowserType());
083: p1.setBrowserType(null);
084: p1.setConnectionTimeout(10000L);
085: p = PolicyUtils.intersect(p1, p2);
086: assertEquals(10000L, p.getConnectionTimeout());
087: p1.setAllowChunking(false);
088: p2.setAllowChunking(false);
089: p = PolicyUtils.intersect(p1, p2);
090: assertTrue(!p.isAllowChunking());
091: }
092:
093: @Test
094: public void testEqualClientPolicies() {
095: HTTPClientPolicy p1 = new HTTPClientPolicy();
096: assertTrue(PolicyUtils.equals(p1, p1));
097: HTTPClientPolicy p2 = new HTTPClientPolicy();
098: assertTrue(PolicyUtils.equals(p1, p2));
099: p1.setDecoupledEndpoint("http://localhost:8080/decoupled");
100: assertTrue(!PolicyUtils.equals(p1, p2));
101: p2.setDecoupledEndpoint("http://localhost:8080/decoupled");
102: assertTrue(PolicyUtils.equals(p1, p2));
103: p1.setReceiveTimeout(10000L);
104: assertTrue(!PolicyUtils.equals(p1, p2));
105: }
106:
107: @Test
108: public void testCompatibleServerPolicies() {
109: HTTPServerPolicy p1 = new HTTPServerPolicy();
110: assertTrue("Policy is not compatible with itself.", PolicyUtils
111: .compatible(p1, p1));
112: HTTPServerPolicy p2 = new HTTPServerPolicy();
113: assertTrue("Policies are not compatible.", PolicyUtils
114: .compatible(p1, p2));
115: p1.setServerType("server");
116: assertTrue("Policies are not compatible.", PolicyUtils
117: .compatible(p1, p2));
118: p1.setServerType(null);
119: p1.setReceiveTimeout(10000);
120: assertTrue("Policies are not compatible.", PolicyUtils
121: .compatible(p1, p2));
122: p1.setSuppressClientSendErrors(false);
123: assertTrue("Policies are compatible.", PolicyUtils.compatible(
124: p1, p2));
125: p1.setSuppressClientSendErrors(true);
126: assertTrue("Policies are compatible.", !PolicyUtils.compatible(
127: p1, p2));
128: p2.setSuppressClientSendErrors(true);
129: assertTrue("Policies are compatible.", PolicyUtils.compatible(
130: p1, p2));
131: }
132:
133: @Test
134: public void testIntersectServerPolicies() {
135: HTTPServerPolicy p1 = new HTTPServerPolicy();
136: HTTPServerPolicy p2 = new HTTPServerPolicy();
137: HTTPServerPolicy p = null;
138:
139: p1.setServerType("server");
140: p = PolicyUtils.intersect(p1, p2);
141: assertEquals("server", p.getServerType());
142: p1.setServerType(null);
143: p1.setReceiveTimeout(10000L);
144: p = PolicyUtils.intersect(p1, p2);
145: assertEquals(10000L, p.getReceiveTimeout());
146: p1.setSuppressClientSendErrors(true);
147: p2.setSuppressClientSendErrors(true);
148: p = PolicyUtils.intersect(p1, p2);
149: assertTrue(p.isSuppressClientSendErrors());
150: }
151:
152: @Test
153: public void testEqualServerPolicies() {
154: HTTPServerPolicy p1 = new HTTPServerPolicy();
155: assertTrue(PolicyUtils.equals(p1, p1));
156: HTTPServerPolicy p2 = new HTTPServerPolicy();
157: assertTrue(PolicyUtils.equals(p1, p2));
158: p1.setContentEncoding("encoding");
159: assertTrue(!PolicyUtils.equals(p1, p2));
160: p2.setContentEncoding("encoding");
161: assertTrue(PolicyUtils.equals(p1, p2));
162: p1.setSuppressClientSendErrors(true);
163: assertTrue(!PolicyUtils.equals(p1, p2));
164: }
165:
166: @Test
167: public void testAssertClientPolicyNoop() {
168: testAssertPolicyNoop(true);
169: }
170:
171: @Test
172: public void testAssertServerPolicyNoop() {
173: testAssertPolicyNoop(false);
174: }
175:
176: void testAssertPolicyNoop(boolean isRequestor) {
177: Message message = control.createMock(Message.class);
178: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
179: null);
180: control.replay();
181: PolicyUtils.assertClientPolicy(message, null);
182: control.verify();
183:
184: control.reset();
185: Collection<Assertion> as = new ArrayList<Assertion>();
186: AssertionInfoMap aim = new AssertionInfoMap(as);
187: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
188: aim);
189: control.replay();
190: if (isRequestor) {
191: PolicyUtils.assertClientPolicy(message, null);
192: } else {
193: PolicyUtils.assertServerPolicy(message, null);
194: }
195: control.verify();
196: }
197:
198: @Test
199: public void testAssertClientPolicyOutbound() {
200: testAssertClientPolicy(true);
201: }
202:
203: @Test
204: public void testAssertClientPolicyInbound() {
205: testAssertClientPolicy(false);
206: }
207:
208: void testAssertClientPolicy(boolean outbound) {
209: Message message = control.createMock(Message.class);
210: HTTPClientPolicy ep = new HTTPClientPolicy();
211: HTTPClientPolicy cmp = new HTTPClientPolicy();
212:
213: cmp.setConnectionTimeout(60000L);
214: HTTPClientPolicy icmp = new HTTPClientPolicy();
215: icmp.setAllowChunking(false);
216:
217: JaxbAssertion<HTTPClientPolicy> ea = new JaxbAssertion<HTTPClientPolicy>(
218: PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, false);
219: ea.setData(ep);
220: JaxbAssertion<HTTPClientPolicy> cma = new JaxbAssertion<HTTPClientPolicy>(
221: PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, false);
222: cma.setData(cmp);
223: JaxbAssertion<HTTPClientPolicy> icma = new JaxbAssertion<HTTPClientPolicy>(
224: PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, false);
225: icma.setData(icmp);
226:
227: AssertionInfo eai = new AssertionInfo(ea);
228: AssertionInfo cmai = new AssertionInfo(cma);
229: AssertionInfo icmai = new AssertionInfo(icma);
230:
231: AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(
232: Collections.EMPTY_LIST, Assertion.class));
233: Collection<AssertionInfo> ais = new ArrayList<AssertionInfo>();
234: ais.add(eai);
235: ais.add(cmai);
236: ais.add(icmai);
237: aim.put(PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, ais);
238: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
239: aim);
240: Exchange ex = control.createMock(Exchange.class);
241: EasyMock.expect(message.getExchange()).andReturn(ex);
242: EasyMock.expect(ex.getOutMessage()).andReturn(
243: outbound ? message : null);
244: if (!outbound) {
245: EasyMock.expect(ex.getOutFaultMessage()).andReturn(null);
246: }
247:
248: control.replay();
249: PolicyUtils.assertClientPolicy(message, ep);
250: assertTrue(eai.isAsserted());
251: assertTrue(cmai.isAsserted());
252: assertTrue(outbound ? !icmai.isAsserted() : icmai.isAsserted());
253: control.verify();
254: }
255:
256: @Test
257: public void testAssertServerPolicyOutbound() {
258: testAssertServerPolicy(true);
259: }
260:
261: @Test
262: public void testAssertServerPolicyInbound() {
263: testAssertServerPolicy(false);
264: }
265:
266: void testAssertServerPolicy(boolean outbound) {
267: Message message = control.createMock(Message.class);
268: HTTPServerPolicy ep = new HTTPServerPolicy();
269: HTTPServerPolicy mp = new HTTPServerPolicy();
270: HTTPServerPolicy cmp = new HTTPServerPolicy();
271: cmp.setReceiveTimeout(60000L);
272: HTTPServerPolicy icmp = new HTTPServerPolicy();
273: icmp.setSuppressClientSendErrors(true);
274:
275: JaxbAssertion<HTTPServerPolicy> ea = new JaxbAssertion<HTTPServerPolicy>(
276: PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
277: ea.setData(ep);
278: JaxbAssertion<HTTPServerPolicy> ma = new JaxbAssertion<HTTPServerPolicy>(
279: PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
280: ma.setData(mp);
281: JaxbAssertion<HTTPServerPolicy> cma = new JaxbAssertion<HTTPServerPolicy>(
282: PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
283: cma.setData(cmp);
284: JaxbAssertion<HTTPServerPolicy> icma = new JaxbAssertion<HTTPServerPolicy>(
285: PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
286: icma.setData(icmp);
287:
288: AssertionInfo eai = new AssertionInfo(ea);
289: AssertionInfo mai = new AssertionInfo(ma);
290: AssertionInfo cmai = new AssertionInfo(cma);
291: AssertionInfo icmai = new AssertionInfo(icma);
292:
293: AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(
294: Collections.EMPTY_LIST, Assertion.class));
295: Collection<AssertionInfo> ais = new ArrayList<AssertionInfo>();
296: ais.add(eai);
297: ais.add(mai);
298: ais.add(cmai);
299: ais.add(icmai);
300: aim.put(PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, ais);
301: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
302: aim);
303: Exchange ex = control.createMock(Exchange.class);
304: EasyMock.expect(message.getExchange()).andReturn(ex);
305: EasyMock.expect(ex.getOutMessage()).andReturn(
306: outbound ? message : null);
307: if (!outbound) {
308: EasyMock.expect(ex.getOutFaultMessage()).andReturn(null);
309: }
310:
311: control.replay();
312: PolicyUtils.assertServerPolicy(message, ep);
313: assertTrue(eai.isAsserted());
314: assertTrue(mai.isAsserted());
315: assertTrue(outbound ? cmai.isAsserted() : !cmai.isAsserted());
316: assertTrue(outbound ? icmai.isAsserted() : !icmai.isAsserted());
317: control.verify();
318: }
319: }
|