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.builder.primitive;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.InputStream;
022: import java.util.Collections;
023:
024: import javax.xml.namespace.QName;
025:
026: import org.w3c.dom.Element;
027:
028: import org.apache.cxf.helpers.DOMUtils;
029: import org.apache.cxf.ws.policy.AssertionBuilderRegistry;
030: import org.apache.cxf.ws.policy.PolicyBuilder;
031: import org.apache.cxf.ws.policy.PolicyException;
032: import org.apache.neethi.Assertion;
033: import org.apache.neethi.Policy;
034: import org.easymock.classextension.EasyMock;
035: import org.easymock.classextension.IMocksControl;
036: import org.junit.Assert;
037: import org.junit.Before;
038: import org.junit.Test;
039:
040: /**
041: *
042: */
043: public class NestedPrimitiveAssertionBuilderTest extends Assert {
044:
045: private static final String TEST_NAMESPACE = "http://www.w3.org/2007/01/addressing/metadata";
046: private static final QName TEST_NAME1 = new QName(TEST_NAMESPACE,
047: "Addressing");
048: private static final QName TEST_NAME2 = new QName(TEST_NAMESPACE,
049: "AnonymousResponses");
050: private static final QName TEST_NAME3 = new QName(TEST_NAMESPACE,
051: "NonAnonymousResponses");
052:
053: private NestedPrimitiveAssertionBuilder npab;
054: private IMocksControl control;
055: private PolicyBuilder builder;
056: private AssertionBuilderRegistry reg;
057:
058: @Before
059: public void setUp() {
060: control = EasyMock.createNiceControl();
061: npab = new NestedPrimitiveAssertionBuilder();
062: npab.setKnownElements(Collections.singletonList(TEST_NAME1));
063: builder = control.createMock(PolicyBuilder.class);
064: npab.setPolicyBuilder(builder);
065: reg = control.createMock(AssertionBuilderRegistry.class);
066: npab.setAssertionBuilderRegistry(reg);
067: }
068:
069: @Test
070: public void testBuildFail() throws Exception {
071: String data = "<wsam:Addressing wsp:Optional=\"true\""
072: + " xmlns:wsp=\"http://www.w3.org/2006/07/ws-policy\""
073: + " xmlns:wsam=\"http://www.w3.org/2007/01/addressing/metadata\" />";
074:
075: try {
076: npab.build(getElement(data));
077: fail("Expected PolicyException not thrown.");
078: } catch (PolicyException ex) {
079: // expected
080: }
081: }
082:
083: @Test
084: public void testBuild() throws Exception {
085: String data = "<wsam:Addressing wsp:Optional=\"true\""
086: + " xmlns:wsp=\"http://www.w3.org/2006/07/ws-policy\""
087: + " xmlns:wsam=\"http://www.w3.org/2007/01/addressing/metadata\">"
088: + "<wsp:Policy/></wsam:Addressing>";
089:
090: Policy nested = control.createMock(Policy.class);
091: EasyMock.expect(builder.getPolicy(EasyMock.isA(Element.class)))
092: .andReturn(nested);
093: control.replay();
094: NestedPrimitiveAssertion npc = (NestedPrimitiveAssertion) npab
095: .build(getElement(data));
096: assertEquals(TEST_NAME1, npc.getName());
097: assertSame(nested, npc.getNested());
098: assertTrue(npc.isOptional());
099: control.verify();
100: }
101:
102: @Test
103: public void testBuildCompatibleNoRegistry() {
104: npab.setAssertionBuilderRegistry(null);
105: Policy[] policies = NestedPrimitiveAssertionTest
106: .buildTestPolicies();
107: Assertion a = (Assertion) policies[4].getFirstPolicyComponent();
108: assertNull(
109: "Should not have been able to build compatible policy.",
110: npab.buildCompatible(a, a));
111: }
112:
113: @Test
114: public void testCompatibleWithSelf() {
115: Policy[] policies = NestedPrimitiveAssertionTest
116: .buildTestPolicies();
117: EasyMock.expect(reg.get(TEST_NAME1)).andReturn(npab).anyTimes();
118: PrimitiveAssertionBuilder ab1 = new PrimitiveAssertionBuilder();
119: ab1.setKnownElements(Collections.singleton(TEST_NAME2));
120: PrimitiveAssertionBuilder ab2 = new PrimitiveAssertionBuilder();
121: ab2.setKnownElements(Collections.singleton(TEST_NAME3));
122: EasyMock.expect(reg.get(TEST_NAME2)).andReturn(ab1).anyTimes();
123: EasyMock.expect(reg.get(TEST_NAME3)).andReturn(ab2).anyTimes();
124:
125: control.replay();
126: Assertion a = (Assertion) policies[2].getFirstPolicyComponent();
127: Assertion compatible = npab.buildCompatible(a, a);
128: assertNotNull(
129: "assertion in policy 2 should be compatible with itself.",
130: compatible);
131: control.verify();
132: }
133:
134: @Test
135: public void testBuildCompatible() {
136: Policy[] policies = NestedPrimitiveAssertionTest
137: .buildTestPolicies();
138: EasyMock.expect(reg.get(TEST_NAME1)).andReturn(npab).anyTimes();
139: PrimitiveAssertionBuilder ab1 = new PrimitiveAssertionBuilder();
140: ab1.setKnownElements(Collections.singleton(TEST_NAME2));
141: PrimitiveAssertionBuilder ab2 = new PrimitiveAssertionBuilder();
142: ab2.setKnownElements(Collections.singleton(TEST_NAME3));
143: EasyMock.expect(reg.get(TEST_NAME2)).andReturn(ab1).anyTimes();
144: EasyMock.expect(reg.get(TEST_NAME3)).andReturn(ab2).anyTimes();
145:
146: control.replay();
147: for (int i = 0; i < policies.length; i++) {
148: Assertion a = (Assertion) policies[i]
149: .getFirstPolicyComponent();
150: Assertion compatible = npab.buildCompatible(a, a);
151: assertNotNull("assertion in policy " + i
152: + " should be compatible with itself.", compatible);
153: }
154:
155: for (int i = 1; i < 5; i++) {
156: Assertion a = (Assertion) policies[0]
157: .getFirstPolicyComponent();
158: Assertion b = (Assertion) policies[i]
159: .getFirstPolicyComponent();
160: Assertion compatible = npab.buildCompatible(a, b);
161: assertNotNull(
162: "assertion in policy 0 should be compatible with assertion in policy "
163: + i + ".", compatible);
164: }
165:
166: for (int i = 2; i < 5; i++) {
167: Assertion a = (Assertion) policies[1]
168: .getFirstPolicyComponent();
169: Assertion b = (Assertion) policies[i]
170: .getFirstPolicyComponent();
171: Assertion compatible = npab.buildCompatible(a, b);
172: assertNotNull(
173: "assertion in policy "
174: + 1
175: + " should be compatible with assertion in policy i.",
176: compatible);
177: }
178: control.verify();
179:
180: }
181:
182: Element getElement(String data) throws Exception {
183: InputStream is = new ByteArrayInputStream(data.getBytes());
184: return DOMUtils.readXml(is).getDocumentElement();
185: }
186: }
|