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.PolicyModelUnmarshaller;
040: import java.util.HashSet;
041: import javax.xml.namespace.QName;
042: import junit.framework.Test;
043: import junit.framework.TestCase;
044: import junit.framework.TestSuite;
045:
046: import static com.sun.xml.ws.policy.testutils.PolicyResourceLoader.loadPolicy;
047:
048: /**
049: *
050: * @author japod
051: */
052: public class EffectiveAlternativeSelectorTest extends TestCase {
053:
054: private static final PolicyModelUnmarshaller xmlUnmarshaller = PolicyModelUnmarshaller
055: .getXmlUnmarshaller();
056:
057: public EffectiveAlternativeSelectorTest(String testName) {
058: super (testName);
059: }
060:
061: public static Test suite() {
062: TestSuite suite = new TestSuite(
063: EffectiveAlternativeSelectorTest.class);
064:
065: return suite;
066: }
067:
068: /**
069: * Test of doSelection method, of class com.sun.xml.ws.policy.EffectiveAlternativeSelector.
070: */
071: public void testDoPositiveSelection() throws Exception {
072: HashSet<PolicyMapMutator> mutators = new HashSet<PolicyMapMutator>();
073: EffectivePolicyModifier myModifier = EffectivePolicyModifier
074: .createEffectivePolicyModifier();
075: mutators.add(myModifier);
076: PolicyMapExtender myExtender = PolicyMapExtender
077: .createPolicyMapExtender();
078: mutators.add(myExtender);
079: PolicyMap policyMap = PolicyMap.createPolicyMap(mutators);
080:
081: //Policy pol1 = PolicyModelTranslator.getTranslator()
082: // .translate(unmarshalModel("single_alternative_policy/policy3.xml"));
083: Policy pol2 = loadPolicy("complex_policy/nested_assertions_with_alternatives.xml");
084:
085: PolicyMapKey aKey = PolicyMap.createWsdlEndpointScopeKey(
086: new QName("service"), new QName("port"));
087:
088: //myExtender.putEndpointSubject(aKey,new PolicySubject("one",pol1));
089: myExtender.putEndpointSubject(aKey, new PolicySubject("two",
090: pol2));
091:
092: //System.out.println(myExtender.getMap());
093:
094: if (2 > myExtender.getMap().getEndpointEffectivePolicy(aKey)
095: .getNumberOfAssertionSets()) {
096: fail("Insufficient number of alternatives found. At least 2 of them needed.");
097: }
098: ;
099:
100: EffectiveAlternativeSelector.doSelection(myModifier);
101:
102: if (1 != myExtender.getMap().getEndpointEffectivePolicy(aKey)
103: .getNumberOfAssertionSets()) {
104: fail("Too many alternatives has left.");
105: }
106: }
107:
108: public void testDoNegativeSelection() throws Exception {
109: HashSet<PolicyMapMutator> mutators = new HashSet<PolicyMapMutator>();
110: EffectivePolicyModifier myModifier = EffectivePolicyModifier
111: .createEffectivePolicyModifier();
112: mutators.add(myModifier);
113: PolicyMapExtender myExtender = PolicyMapExtender
114: .createPolicyMapExtender();
115: mutators.add(myExtender);
116: PolicyMap policyMap = PolicyMap.createPolicyMap(mutators);
117:
118: //Policy pol1 = PolicyModelTranslator.getTranslator()
119: // .translate(unmarshalModel("single_alternative_policy/policy3.xml"));
120: Policy pol2 = loadPolicy("complex_policy/nested_assertions_with_alternatives.xml");
121:
122: PolicyMapKey aKey = PolicyMap.createWsdlEndpointScopeKey(
123: new QName("service"), new QName("port"));
124:
125: //myExtender.putEndpointSubject(aKey,new PolicySubject("one",pol1));
126: myExtender.putEndpointSubject(aKey, new PolicySubject("two",
127: pol2));
128:
129: //System.out.println(myExtender.getMap());
130:
131: if (2 > myExtender.getMap().getEndpointEffectivePolicy(aKey)
132: .getNumberOfAssertionSets()) {
133: fail("Insufficient number of alternatives found. At least 2 of them needed.");
134: }
135: ;
136:
137: EffectiveAlternativeSelector.doSelection(myModifier);
138:
139: if (1 != myExtender.getMap().getEndpointEffectivePolicy(aKey)
140: .getNumberOfAssertionSets()) {
141: fail("Too many alternatives has left.");
142: }
143:
144: EffectiveAlternativeSelector.doSelection(myModifier);
145:
146: if (1 != myExtender.getMap().getEndpointEffectivePolicy(aKey)
147: .getNumberOfAssertionSets()) {
148: fail("Too many alternatives has left.");
149: }
150: }
151:
152: public void testDoEmptySelection() throws Exception {
153: HashSet<PolicyMapMutator> mutators = new HashSet<PolicyMapMutator>();
154: EffectivePolicyModifier modifier = EffectivePolicyModifier
155: .createEffectivePolicyModifier();
156: mutators.add(modifier);
157: PolicyMapExtender extender = PolicyMapExtender
158: .createPolicyMapExtender();
159: mutators.add(extender);
160: PolicyMap policyMap = PolicyMap.createPolicyMap(mutators);
161:
162: Policy emptyPolicy = Policy.createEmptyPolicy();
163: PolicyMapKey key = PolicyMap.createWsdlEndpointScopeKey(
164: new QName("service"), new QName("port"));
165: extender.putEndpointSubject(key, new PolicySubject("two",
166: emptyPolicy));
167:
168: EffectiveAlternativeSelector.doSelection(modifier);
169:
170: assertTrue(extender.getMap().getEndpointEffectivePolicy(key)
171: .isEmpty());
172: }
173:
174: public void testDoNullSelection() throws Exception {
175: HashSet<PolicyMapMutator> mutators = new HashSet<PolicyMapMutator>();
176: EffectivePolicyModifier modifier = EffectivePolicyModifier
177: .createEffectivePolicyModifier();
178: mutators.add(modifier);
179: PolicyMapExtender extender = PolicyMapExtender
180: .createPolicyMapExtender();
181: mutators.add(extender);
182: PolicyMap policyMap = PolicyMap.createPolicyMap(mutators);
183:
184: Policy nullPolicy = Policy.createNullPolicy();
185: PolicyMapKey key = PolicyMap.createWsdlEndpointScopeKey(
186: new QName("service"), new QName("port"));
187: extender.putEndpointSubject(key, new PolicySubject("two",
188: nullPolicy));
189:
190: EffectiveAlternativeSelector.doSelection(modifier);
191:
192: assertTrue(extender.getMap().getEndpointEffectivePolicy(key)
193: .isNull());
194: }
195:
196: }
|