001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.policy;
038:
039: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
040: import com.sun.xml.ws.policy.testutils.PolicyResourceLoader;
041: import java.io.IOException;
042: import junit.framework.*;
043: import java.util.Arrays;
044: import java.util.Collection;
045: import java.util.LinkedList;
046: import javax.xml.namespace.QName;
047:
048: /**
049: *
050: * @author Marek Potociar (marek.potociar at sun.com)
051: */
052: public class PolicyIntersectorTest extends TestCase {
053: PolicyIntersector strictIntersector = PolicyIntersector
054: .createStrictPolicyIntersector();
055: PolicyIntersector laxIntersector = PolicyIntersector
056: .createLaxPolicyIntersector();
057:
058: public PolicyIntersectorTest(String testName) {
059: super (testName);
060: }
061:
062: protected void setUp() throws Exception {
063: }
064:
065: protected void tearDown() throws Exception {
066: }
067:
068: public void testPolicyIntersectorFactoryMethodsDoNotReturnNull()
069: throws Exception {
070: assertNotNull(PolicyIntersector.createStrictPolicyIntersector());
071: assertNotNull(PolicyIntersector.createLaxPolicyIntersector());
072: }
073:
074: public void testStrictIntersectEmptyPolicyCollectionThrowsIAE()
075: throws Exception {
076: try {
077: Policy result = strictIntersector.intersect();
078: fail("IllegalArgumentException should have been thrown");
079: } catch (IllegalArgumentException e) {
080: // ok
081: }
082: }
083:
084: public void testStrictIntersectSinglePolicyInCollectionReturnsTheSameSinglePolicyInstance()
085: throws Exception {
086: Policy expected = Policy.createEmptyPolicy("fake", null);
087: Policy result = strictIntersector.intersect(expected);
088:
089: assertTrue(
090: "Intersecting collection with single policy instance should return the very same policy instance",
091: expected == result);
092: }
093:
094: public void testStrictIntersectNullPolicyInCollectionReturnsNullPolicy()
095: throws Exception {
096: Collection<AssertionSet> alternatives = Arrays
097: .asList(new AssertionSet[] { AssertionSet
098: .createAssertionSet(Arrays
099: .asList(new PolicyAssertion[] { new PolicyAssertion(
100: AssertionData
101: .createAssertionData(new QName(
102: "A")), null,
103: null) {
104: } })) });
105: Collection<Policy> policies = new LinkedList<Policy>();
106: for (int i = 0; i < 10; i++) {
107: policies.add(Policy
108: .createPolicy("fake", null, alternatives));
109: }
110: policies.add(Policy.createNullPolicy());
111:
112: Policy result = strictIntersector.intersect(policies
113: .toArray(new Policy[policies.size()]));
114:
115: assertTrue(
116: "Intersecting collection with null policy instance should return null policy as a result",
117: result.isNull());
118: }
119:
120: public void testStrictIntersectEmptyPolicyInCollectionReturnsNullPolicy()
121: throws Exception {
122: Collection<AssertionSet> alternatives = Arrays
123: .asList(new AssertionSet[] { AssertionSet
124: .createAssertionSet(Arrays
125: .asList(new PolicyAssertion[] { new PolicyAssertion(
126: AssertionData
127: .createAssertionData(new QName(
128: "A")), null,
129: null) {
130: } })) });
131: Collection<Policy> policies = new LinkedList<Policy>();
132: for (int i = 0; i < 10; i++) {
133: policies.add(Policy
134: .createPolicy("fake", null, alternatives));
135: }
136: policies.add(Policy.createEmptyPolicy());
137:
138: Policy result = strictIntersector.intersect(policies
139: .toArray(new Policy[policies.size()]));
140:
141: assertTrue(
142: "Intersecting collection with empty policy instance should return null policy as a result",
143: result.isNull());
144: }
145:
146: public void testStrictIntersectAllPolicyInCollectionEmptyReturnsEmptyPolicy()
147: throws Exception {
148: Collection<Policy> policies = new LinkedList<Policy>();
149: for (int i = 0; i < 10; i++) {
150: policies.add(Policy.createEmptyPolicy("fake" + i, null));
151: }
152:
153: Policy result = strictIntersector.intersect(policies
154: .toArray(new Policy[policies.size()]));
155:
156: assertTrue(
157: "Intersecting collection with all policy instances empty should return empty policy as a result",
158: result.isEmpty());
159: }
160:
161: public void testStrictIntersectSimplePolicies() throws Exception {
162: Policy p1 = PolicyResourceLoader
163: .loadPolicy("intersection/policy1.xml");
164: Policy p2 = PolicyResourceLoader
165: .loadPolicy("intersection/policy2.xml");
166: Policy p3 = PolicyResourceLoader
167: .loadPolicy("intersection/policy3.xml");
168:
169: Policy expectedResult = PolicyResourceLoader
170: .loadPolicy("intersection/policy1-2-3.xml");
171: Policy result = strictIntersector.intersect(p1, p2, p3);
172: assertEquals("Intersection did not provide expected result.",
173: expectedResult, result);
174: }
175:
176: public void testStrictInteroperablePolicies() throws Exception {
177: performInteroperabilityTests("strict", strictIntersector);
178: }
179:
180: public void testLaxInteroperablePolicies() throws Exception {
181: performInteroperabilityTests("lax", laxIntersector);
182: }
183:
184: public void performInteroperabilityTests(String mode,
185: PolicyIntersector intersector) throws PolicyException,
186: IOException {
187: Policy p4 = PolicyResourceLoader
188: .loadPolicy("intersection/policy4.xml");
189: Policy p5 = PolicyResourceLoader
190: .loadPolicy("intersection/policy5.xml");
191: Policy p6 = PolicyResourceLoader
192: .loadPolicy("intersection/policy6.xml");
193: Policy p7 = PolicyResourceLoader
194: .loadPolicy("intersection/policy7.xml");
195:
196: String testId;
197:
198: testId = "4-7-" + mode;
199: performSingleIntersectionTest(intersector, testId,
200: PolicyResourceLoader.loadPolicy("intersection/policy"
201: + testId + ".xml"), p4, p7);
202: testId = "7-4-" + mode;
203: performSingleIntersectionTest(intersector, testId,
204: PolicyResourceLoader.loadPolicy("intersection/policy"
205: + testId + ".xml"), p7, p4);
206:
207: testId = "5-7-" + mode;
208: performSingleIntersectionTest(intersector, testId,
209: PolicyResourceLoader.loadPolicy("intersection/policy"
210: + testId + ".xml"), p5, p7);
211: testId = "7-5-" + mode;
212: performSingleIntersectionTest(intersector, testId,
213: PolicyResourceLoader.loadPolicy("intersection/policy"
214: + testId + ".xml"), p7, p5);
215:
216: testId = "6-7-" + mode;
217: performSingleIntersectionTest(intersector, testId,
218: PolicyResourceLoader.loadPolicy("intersection/policy"
219: + testId + ".xml"), p6, p7);
220: testId = "7-6-" + mode;
221: performSingleIntersectionTest(intersector, testId,
222: PolicyResourceLoader.loadPolicy("intersection/policy"
223: + testId + ".xml"), p7, p6);
224:
225: testId = "7-7-" + mode;
226: performSingleIntersectionTest(intersector, testId,
227: PolicyResourceLoader
228: .loadPolicy("intersection/policy7-7.xml"), p7,
229: p7);
230: }
231:
232: private void performSingleIntersectionTest(
233: PolicyIntersector intersector, String testId,
234: Policy expectedResult, Policy... inputPolicies) {
235: Policy result = intersector.intersect(inputPolicies);
236: assertEquals("Intersection for test [" + testId
237: + "] did not provide expected result.", expectedResult,
238: result);
239: }
240: }
|