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.ws.rm.policy;
019:
020: import java.math.BigInteger;
021: import java.util.ArrayList;
022: import java.util.Collection;
023:
024: import org.apache.cxf.message.Message;
025: import org.apache.cxf.ws.policy.AssertionInfo;
026: import org.apache.cxf.ws.policy.AssertionInfoMap;
027: import org.apache.cxf.ws.policy.builder.jaxb.JaxbAssertion;
028: import org.apache.cxf.ws.rm.RMConstants;
029: import org.apache.cxf.ws.rm.policy.RMAssertion.AcknowledgementInterval;
030: import org.apache.cxf.ws.rm.policy.RMAssertion.BaseRetransmissionInterval;
031: import org.apache.cxf.ws.rm.policy.RMAssertion.ExponentialBackoff;
032: import org.apache.cxf.ws.rm.policy.RMAssertion.InactivityTimeout;
033: import org.easymock.IMocksControl;
034: import org.easymock.classextension.EasyMock;
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 testRMAssertionEquals() {
053: RMAssertion a = new RMAssertion();
054: assertTrue(PolicyUtils.equals(a, a));
055:
056: RMAssertion b = new RMAssertion();
057: assertTrue(PolicyUtils.equals(a, b));
058:
059: InactivityTimeout iat = new RMAssertion.InactivityTimeout();
060: iat.setMilliseconds(BigInteger.TEN);
061: a.setInactivityTimeout(iat);
062: assertTrue(!PolicyUtils.equals(a, b));
063: b.setInactivityTimeout(iat);
064: assertTrue(PolicyUtils.equals(a, b));
065:
066: ExponentialBackoff eb = new RMAssertion.ExponentialBackoff();
067: a.setExponentialBackoff(eb);
068: assertTrue(!PolicyUtils.equals(a, b));
069: b.setExponentialBackoff(eb);
070: assertTrue(PolicyUtils.equals(a, b));
071: }
072:
073: @Test
074: public void testIntersect() {
075: RMAssertion a = new RMAssertion();
076: RMAssertion b = new RMAssertion();
077: assertSame(a, PolicyUtils.intersect(a, b));
078:
079: InactivityTimeout aiat = new RMAssertion.InactivityTimeout();
080: aiat.setMilliseconds(new BigInteger("3600000"));
081: a.setInactivityTimeout(aiat);
082: InactivityTimeout biat = new RMAssertion.InactivityTimeout();
083: biat.setMilliseconds(new BigInteger("7200000"));
084: b.setInactivityTimeout(biat);
085:
086: RMAssertion c = PolicyUtils.intersect(a, b);
087: assertEquals(7200000L, c.getInactivityTimeout()
088: .getMilliseconds().longValue());
089: assertNull(c.getBaseRetransmissionInterval());
090: assertNull(c.getAcknowledgementInterval());
091: assertNull(c.getExponentialBackoff());
092:
093: BaseRetransmissionInterval abri = new RMAssertion.BaseRetransmissionInterval();
094: abri.setMilliseconds(new BigInteger("10000"));
095: a.setBaseRetransmissionInterval(abri);
096: BaseRetransmissionInterval bbri = new RMAssertion.BaseRetransmissionInterval();
097: bbri.setMilliseconds(new BigInteger("20000"));
098: b.setBaseRetransmissionInterval(bbri);
099:
100: c = PolicyUtils.intersect(a, b);
101: assertEquals(7200000L, c.getInactivityTimeout()
102: .getMilliseconds().longValue());
103: assertEquals(10000L, c.getBaseRetransmissionInterval()
104: .getMilliseconds().longValue());
105: assertNull(c.getAcknowledgementInterval());
106: assertNull(c.getExponentialBackoff());
107:
108: AcknowledgementInterval aai = new RMAssertion.AcknowledgementInterval();
109: aai.setMilliseconds(new BigInteger("2000"));
110: a.setAcknowledgementInterval(aai);
111:
112: c = PolicyUtils.intersect(a, b);
113: assertEquals(7200000L, c.getInactivityTimeout()
114: .getMilliseconds().longValue());
115: assertEquals(10000L, c.getBaseRetransmissionInterval()
116: .getMilliseconds().longValue());
117: assertEquals(2000L, c.getAcknowledgementInterval()
118: .getMilliseconds().longValue());
119: assertNull(c.getExponentialBackoff());
120:
121: b.setExponentialBackoff(new RMAssertion.ExponentialBackoff());
122: c = PolicyUtils.intersect(a, b);
123: assertEquals(7200000L, c.getInactivityTimeout()
124: .getMilliseconds().longValue());
125: assertEquals(10000L, c.getBaseRetransmissionInterval()
126: .getMilliseconds().longValue());
127: assertEquals(2000L, c.getAcknowledgementInterval()
128: .getMilliseconds().longValue());
129: assertNotNull(c.getExponentialBackoff());
130: }
131:
132: @Test
133: public void testGetRMAssertion() {
134: RMAssertion a = new RMAssertion();
135: BaseRetransmissionInterval abri = new RMAssertion.BaseRetransmissionInterval();
136: abri.setMilliseconds(new BigInteger("3000"));
137: a.setBaseRetransmissionInterval(abri);
138: a.setExponentialBackoff(new RMAssertion.ExponentialBackoff());
139:
140: Message message = control.createMock(Message.class);
141: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
142: null);
143: control.replay();
144: assertSame(a, PolicyUtils.getRMAssertion(a, message));
145: control.verify();
146:
147: control.reset();
148: AssertionInfoMap aim = control
149: .createMock(AssertionInfoMap.class);
150: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
151: aim);
152: Collection<AssertionInfo> ais = new ArrayList<AssertionInfo>();
153: EasyMock.expect(aim.get(RMConstants.getRMAssertionQName()))
154: .andReturn(ais);
155: control.replay();
156: assertSame(a, PolicyUtils.getRMAssertion(a, message));
157: control.verify();
158:
159: control.reset();
160: RMAssertion b = new RMAssertion();
161: BaseRetransmissionInterval bbri = new RMAssertion.BaseRetransmissionInterval();
162: bbri.setMilliseconds(new BigInteger("2000"));
163: b.setBaseRetransmissionInterval(bbri);
164: JaxbAssertion<RMAssertion> assertion = new JaxbAssertion<RMAssertion>();
165: assertion.setName(RMConstants.getRMAssertionQName());
166: assertion.setData(b);
167: AssertionInfo ai = new AssertionInfo(assertion);
168: ais.add(ai);
169: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
170: aim);
171: EasyMock.expect(aim.get(RMConstants.getRMAssertionQName()))
172: .andReturn(ais);
173: control.replay();
174: RMAssertion c = PolicyUtils.getRMAssertion(a, message);
175: assertNull(c.getAcknowledgementInterval());
176: assertNull(c.getInactivityTimeout());
177: assertEquals(2000L, c.getBaseRetransmissionInterval()
178: .getMilliseconds().longValue());
179: assertNotNull(c.getExponentialBackoff());
180: control.verify();
181: }
182:
183: }
|