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: package com.sun.xml.ws.policy;
037:
038: import com.sun.xml.ws.policy.jaxws.PolicyConfigParser;
039: import java.util.Arrays;
040: import java.util.LinkedList;
041: import javax.xml.namespace.QName;
042: import junit.framework.TestCase;
043: import static com.sun.xml.ws.policy.testutils.PolicyResourceLoader.getPolicyMap;
044: import static com.sun.xml.ws.policy.testutils.PolicyResourceLoader.loadPolicy;
045: import static com.sun.xml.ws.policy.testutils.PolicyResourceLoader.getResourceUrl;
046:
047: /**
048: *
049: * @author Marek Potociar (marek.potociar at sun.com)
050: */
051: public class PolicyMapTest extends TestCase {
052:
053: public PolicyMapTest(String testName) {
054: super (testName);
055: }
056:
057: protected void setUp() throws Exception {
058: }
059:
060: protected void tearDown() throws Exception {
061: }
062:
063: public void testCreatePolicyMapWithNullMutatorCollection()
064: throws Exception {
065: assertNotNull("Policy map instance should not be null",
066: PolicyMap.createPolicyMap(null));
067: }
068:
069: public void testCreatePolicyMapWithEmptyMutatorCollection()
070: throws Exception {
071: assertNotNull(
072: "Policy map instance should not be null",
073: PolicyMap
074: .createPolicyMap(new LinkedList<PolicyMapMutator>()));
075: }
076:
077: public void testCreatePolicyMapWithNonemptyMutatorCollection()
078: throws Exception {
079: assertNotNull(
080: "Policy map instance should not be null",
081: PolicyMap
082: .createPolicyMap(Arrays
083: .asList(new PolicyMapMutator[] { PolicyMapExtender
084: .createPolicyMapExtender() })));
085: }
086:
087: public void testPolicyMapIterator() throws Exception {
088: PolicyMapExtender extender = PolicyMapExtender
089: .createPolicyMapExtender();
090: PolicyMap map = PolicyMap.createPolicyMap(Arrays
091: .asList(new PolicyMapMutator[] { extender }));
092:
093: PolicySubject subject = new PolicySubject("dummy", Policy
094: .createEmptyPolicy());
095:
096: PolicyMapKey key = map
097: .createWsdlServiceScopeKey(new QName("1"));
098: extender.putServiceSubject(key, subject);
099: key = map.createWsdlServiceScopeKey(new QName("2"));
100: extender.putServiceSubject(key, subject);
101:
102: key = map.createWsdlEndpointScopeKey(new QName("3"), new QName(
103: "port"));
104: extender.putEndpointSubject(key, subject);
105: key = map.createWsdlEndpointScopeKey(new QName("4"), new QName(
106: "port"));
107: extender.putEndpointSubject(key, subject);
108: key = map.createWsdlEndpointScopeKey(new QName("5"), new QName(
109: "port"));
110: extender.putEndpointSubject(key, subject);
111:
112: key = map.createWsdlMessageScopeKey(new QName("6"), new QName(
113: "port"), new QName("operation"));
114: extender.putInputMessageSubject(key, subject);
115:
116: key = map.createWsdlMessageScopeKey(new QName("7"), new QName(
117: "port"), new QName("operation"));
118: extender.putOutputMessageSubject(key, subject);
119:
120: key = map.createWsdlMessageScopeKey(new QName("8"), new QName(
121: "port"), new QName("operation"));
122: extender.putFaultMessageSubject(key, subject);
123:
124: int counter = 0;
125: for (Policy policy : map) {
126: counter++;
127: }
128:
129: assertEquals(
130: "Did not iterate over expected number of policies.", 8,
131: counter);
132: }
133:
134: public void testIsEmpty() {
135: PolicyMap map = PolicyMap.createPolicyMap(null);
136: assertTrue(map.isEmpty());
137:
138: map = PolicyMap
139: .createPolicyMap(new LinkedList<PolicyMapMutator>());
140: assertTrue(map.isEmpty());
141:
142: map = PolicyMap.createPolicyMap(Arrays
143: .asList(new PolicyMapMutator[] { PolicyMapExtender
144: .createPolicyMapExtender() }));
145: assertTrue(map.isEmpty());
146:
147: PolicyMapExtender extender = PolicyMapExtender
148: .createPolicyMapExtender();
149: PolicyMapMutator[] mutators = new PolicyMapMutator[] { extender };
150: PolicyMapKey key = map.createWsdlServiceScopeKey(new QName(
151: "service"));
152: map = PolicyMap.createPolicyMap(Arrays.asList(mutators));
153: extender.putServiceSubject(key, null);
154: assertFalse(map.isEmpty());
155:
156: mutators[0].disconnect();
157: key = map.createWsdlEndpointScopeKey(new QName("service"),
158: new QName("port"));
159: map = PolicyMap.createPolicyMap(Arrays
160: .asList(new PolicyMapMutator[] { extender }));
161: extender.putEndpointSubject(key, null);
162: assertFalse(map.isEmpty());
163:
164: mutators[0].disconnect();
165: key = map.createWsdlOperationScopeKey(new QName("service"),
166: new QName("port"), new QName("operation"));
167: map = PolicyMap.createPolicyMap(Arrays
168: .asList(new PolicyMapMutator[] { extender }));
169: extender.putOperationSubject(key, null);
170: assertFalse(map.isEmpty());
171:
172: mutators[0].disconnect();
173: key = map.createWsdlMessageScopeKey(new QName("service"),
174: new QName("port"), new QName("operation"));
175: map = PolicyMap.createPolicyMap(Arrays
176: .asList(new PolicyMapMutator[] { extender }));
177: extender.putInputMessageSubject(key, null);
178: assertFalse(map.isEmpty());
179:
180: mutators[0].disconnect();
181: key = map.createWsdlMessageScopeKey(new QName("service"),
182: new QName("port"), new QName("operation"));
183: map = PolicyMap.createPolicyMap(Arrays
184: .asList(new PolicyMapMutator[] { extender }));
185: extender.putOutputMessageSubject(key, null);
186: assertFalse(map.isEmpty());
187:
188: mutators[0].disconnect();
189: key = map.createWsdlMessageScopeKey(new QName("service"),
190: new QName("port"), new QName("operation"));
191: map = PolicyMap.createPolicyMap(Arrays
192: .asList(new PolicyMapMutator[] { extender }));
193: extender.putFaultMessageSubject(key, null);
194: assertFalse(map.isEmpty());
195: }
196:
197: public void testToString() throws Exception {
198: PolicyMap policyMap = getPolicyMap("bug_reproduction/simple.wsdl");
199: String result = policyMap.toString();
200: assertNotNull(result);
201: }
202:
203: public void testGetOperationEffectivePolicy() throws Exception {
204: PolicyMap policyMap = PolicyConfigParser.parse(
205: getResourceUrl("effective/all.wsdl"), false);
206: Policy expectedPolicy = loadPolicy("effective/resultOperation.xml");
207: PolicyMapKey policyMapKey = policyMap
208: .createWsdlOperationScopeKey(new QName(
209: "http://example.org/", "Service"), new QName(
210: "http://example.org/", "Port"), new QName(
211: "http://example.org/", "Operation"));
212: Policy policy = policyMap
213: .getOperationEffectivePolicy(policyMapKey);
214: assertEquals(expectedPolicy, policy);
215: }
216:
217: public void testGetInputMessageEffectivePolicy() throws Exception {
218: PolicyMap policyMap = PolicyConfigParser.parse(
219: getResourceUrl("effective/all.wsdl"), false);
220: Policy expectedPolicy = loadPolicy("effective/resultInput.xml");
221: PolicyMapKey policyMapKey = policyMap
222: .createWsdlMessageScopeKey(new QName(
223: "http://example.org/", "Service"), new QName(
224: "http://example.org/", "Port"), new QName(
225: "http://example.org/", "Operation"));
226: Policy policy = policyMap
227: .getInputMessageEffectivePolicy(policyMapKey);
228: assertEquals(expectedPolicy, policy);
229: }
230:
231: public void testGetFaultMessageEffectivePolicy() throws Exception {
232: PolicyMap policyMap = PolicyConfigParser.parse(
233: getResourceUrl("effective/all.wsdl"), false);
234: Policy expectedPolicy = loadPolicy("effective/resultFault.xml");
235: PolicyMapKey policyMapKey = policyMap
236: .createWsdlFaultMessageScopeKey(new QName(
237: "http://example.org/", "Service"), new QName(
238: "http://example.org/", "Port"), new QName(
239: "http://example.org/", "Operation"), new QName(
240: "http://example.org/", "Fault"));
241: Policy policy = policyMap
242: .getFaultMessageEffectivePolicy(policyMapKey);
243: assertEquals(expectedPolicy, policy);
244: }
245:
246: public void testGetFaultMessageWithTwoServicesEffectivePolicy()
247: throws Exception {
248: PolicyMap policyMap = PolicyConfigParser.parse(
249: getResourceUrl("effective/twoservices.wsdl"), false);
250: Policy expectedPolicy = loadPolicy("effective/resultFault.xml");
251: PolicyMapKey policyMapKey = policyMap
252: .createWsdlFaultMessageScopeKey(new QName(
253: "http://example.org/", "Service"), new QName(
254: "http://example.org/", "Port"), new QName(
255: "http://example.org/", "Operation"), new QName(
256: "http://example.org/", "Fault"));
257: Policy policy = policyMap
258: .getFaultMessageEffectivePolicy(policyMapKey);
259: assertEquals(expectedPolicy, policy);
260: }
261:
262: }
|