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.policy;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.List;
024:
025: import javax.xml.namespace.QName;
026:
027: import org.apache.cxf.helpers.CastUtils;
028: import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion;
029: import org.apache.neethi.All;
030: import org.apache.neethi.Assertion;
031: import org.apache.neethi.ExactlyOne;
032: import org.apache.neethi.Policy;
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 AssertionInfoMapTest 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 testAlternativeSupported() {
053: Assertion a1 = control.createMock(Assertion.class);
054: QName aqn = new QName("http://x.y.z", "a");
055: EasyMock.expect(a1.getName()).andReturn(aqn).anyTimes();
056: Assertion a2 = control.createMock(Assertion.class);
057: EasyMock.expect(a2.getName()).andReturn(aqn).anyTimes();
058: Assertion b = control.createMock(Assertion.class);
059: QName bqn = new QName("http://x.y.z", "b");
060: EasyMock.expect(b.getName()).andReturn(bqn).anyTimes();
061: Assertion c = control.createMock(Assertion.class);
062: QName cqn = new QName("http://x.y.z", "c");
063: EasyMock.expect(c.getName()).andReturn(cqn).anyTimes();
064: AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(
065: Collections.EMPTY_LIST, Assertion.class));
066: AssertionInfo ai1 = new AssertionInfo(a1);
067: AssertionInfo ai2 = new AssertionInfo(a2);
068: Collection<AssertionInfo> ais = new ArrayList<AssertionInfo>();
069: AssertionInfo bi = new AssertionInfo(b);
070: AssertionInfo ci = new AssertionInfo(c);
071: ais.add(ai1);
072: ais.add(ai2);
073: aim.put(aqn, ais);
074: aim.put(bqn, Collections.singleton(bi));
075: aim.put(cqn, Collections.singleton(ci));
076: ai2.setAsserted(true);
077: bi.setAsserted(true);
078: ci.setAsserted(true);
079: EasyMock.expect(a1.equal(a1)).andReturn(true).anyTimes();
080: EasyMock.expect(a2.equal(a2)).andReturn(true).anyTimes();
081: EasyMock.expect(b.equal(b)).andReturn(true).anyTimes();
082: EasyMock.expect(c.equal(c)).andReturn(true).anyTimes();
083:
084: List<Assertion> alt1 = new ArrayList<Assertion>();
085: alt1.add(a1);
086: alt1.add(b);
087:
088: List<Assertion> alt2 = new ArrayList<Assertion>();
089: alt2.add(a2);
090: alt2.add(c);
091:
092: control.replay();
093: assertTrue(!aim.supportsAlternative(alt1));
094: assertTrue(aim.supportsAlternative(alt2));
095: control.verify();
096: }
097:
098: @Test
099: public void testCheckEffectivePolicy() {
100: Policy p = new Policy();
101: QName aqn = new QName("http://x.y.z", "a");
102: Assertion a = new PrimitiveAssertion(aqn);
103: QName bqn = new QName("http://x.y.z", "b");
104: Assertion b = new PrimitiveAssertion(bqn);
105: QName cqn = new QName("http://x.y.z", "c");
106: Assertion c = new PrimitiveAssertion(cqn);
107: All alt1 = new All();
108: alt1.addAssertion(a);
109: alt1.addAssertion(b);
110: All alt2 = new All();
111: alt2.addAssertion(c);
112: ExactlyOne ea = new ExactlyOne();
113: ea.addPolicyComponent(alt1);
114: ea.addPolicyComponent(alt2);
115: p.addPolicyComponent(ea);
116: AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(
117: Collections.EMPTY_LIST, Assertion.class));
118: AssertionInfo ai = new AssertionInfo(a);
119: AssertionInfo bi = new AssertionInfo(b);
120: AssertionInfo ci = new AssertionInfo(c);
121: aim.put(aqn, Collections.singleton(ai));
122: aim.put(bqn, Collections.singleton(bi));
123: aim.put(cqn, Collections.singleton(ci));
124:
125: try {
126: aim.checkEffectivePolicy(p);
127: fail("Expected PolicyException not thrown.");
128: } catch (PolicyException ex) {
129: // expected
130: }
131:
132: ai.setAsserted(true);
133: ci.setAsserted(true);
134:
135: aim.checkEffectivePolicy(p);
136: }
137:
138: @Test
139: public void testCheck() throws PolicyException {
140: QName aqn = new QName("http://x.y.z", "a");
141: Assertion a = new PrimitiveAssertion(aqn);
142: Collection<Assertion> assertions = new ArrayList<Assertion>();
143: assertions.add(a);
144: AssertionInfoMap aim = new AssertionInfoMap(assertions);
145: try {
146: aim.check();
147: fail("Expected PolicyException not thrown.");
148: } catch (PolicyException ex) {
149: assertEquals("NOT_ASSERTED_EXC", ex.getCode());
150: }
151: aim.get(aqn).iterator().next().setAsserted(true);
152: aim.check();
153: }
154: }
|