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.util.ArrayList;
042: import java.util.Arrays;
043: import java.util.Collection;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.LinkedList;
047: import java.util.Map;
048: import javax.xml.namespace.QName;
049:
050: /**
051: *
052: * @author Marek Potociar (marek.potociar@sun.com)
053: */
054: public class PolicyAssertionTest extends AbstractPolicyApiClassTestBase {
055:
056: private static final QName attribName = new QName("http://foo.com",
057: "attribute");
058: private static final String attribValue = "avalue";
059: private static final QName assertionName = new QName(
060: "http://foo.com", "assertion");
061: private static final AssertionData data = AssertionData
062: .createAssertionData(assertionName);
063:
064: private Map<QName, String> attributes;
065:
066: private PolicyAssertion a1;
067: private PolicyAssertion a2;
068: private PolicyAssertion a3;
069: private PolicyAssertion a4;
070: private PolicyAssertion a5;
071: private PolicyAssertion assertionWithAttributes;
072:
073: public PolicyAssertionTest(String testName) {
074: super (testName);
075: }
076:
077: @Override
078: protected void setUp() throws Exception {
079: a1 = new PolicyAssertion() {
080: };
081: a2 = new PolicyAssertion(null, null) {
082: };
083: a3 = new PolicyAssertion(data, null) {
084: };
085: a4 = new PolicyAssertion(data,
086: new ArrayList<PolicyAssertion>(0)) {
087: };
088: a5 = new PolicyAssertion(data, Arrays
089: .asList(new PolicyAssertion() {
090: })) {
091: };
092: attributes = new HashMap<QName, String>();
093: attributes.put(attribName, attribValue);
094: assertionWithAttributes = new PolicyAssertion(
095: AssertionData.createAssertionData(assertionName,
096: "test", attributes), null) {
097: };
098: }
099:
100: @Override
101: protected void tearDown() throws Exception {
102: }
103:
104: protected PolicyAssertion[][] getEqualInstanceRows()
105: throws Exception {
106: Collection<PolicyAssertion[]> rows = new LinkedList<PolicyAssertion[]>();
107:
108: for (String name : PolicyResourceLoader.SINGLE_ALTERNATIVE_POLICY) {
109: Iterator<AssertionSet> setsA = PolicyResourceLoader
110: .loadPolicy(name).iterator();
111: Iterator<AssertionSet> setsB = PolicyResourceLoader
112: .loadPolicy(name).iterator();
113:
114: if (setsA.hasNext()) {
115: AssertionSet setA = setsA.next();
116: AssertionSet setB = setsB.next();
117:
118: Iterator<PolicyAssertion> assertionsA = setA.iterator();
119: Iterator<PolicyAssertion> assertionsB = setB.iterator();
120:
121: while (assertionsA.hasNext()) {
122: rows.add(new PolicyAssertion[] {
123: assertionsA.next(), assertionsB.next() });
124: }
125: }
126: }
127:
128: return rows.toArray(new PolicyAssertion[rows.size()][]);
129: }
130:
131: public void testGetAttributesValueReturnsProperValue()
132: throws Exception {
133: QName headerParameterName = new QName(
134: "http://schemas.xmlsoap.org/ws/2005/07/securitypolicy",
135: "Header");
136: QName nameAttributeName = new QName("Name");
137: QName namespaceAttributeName = new QName("Namespace");
138:
139: Policy policy = PolicyResourceLoader
140: .loadPolicy("bug_reproduction/securityPolicy1.xml");
141: AssertionSet alternative = policy.iterator().next();
142: PolicyAssertion signedParts = alternative
143: .get(
144: new QName(
145: "http://schemas.xmlsoap.org/ws/2005/07/securitypolicy",
146: "SignedParts")).iterator().next();
147: Iterator<PolicyAssertion> iterator = signedParts
148: .getParametersIterator();
149: while (iterator.hasNext()) {
150: PolicyAssertion assertion = iterator.next();
151: if (assertion.getName().equals(headerParameterName)) {
152: // System.out.println(assertion.toString());
153: String nameValue = assertion
154: .getAttributeValue(nameAttributeName);
155: String namespaceValue = assertion
156: .getAttributeValue(namespaceAttributeName);
157: // System.out.println();
158: // System.out.println("Name value: '" + nameValue + "'");
159: // System.out.println("Namespace value: '" + namespaceValue + "'");
160: // System.out.println("==========================================");
161: assertNotNull(
162: "'Name' attribute of 'Header' parameter is expected to be not null.",
163: nameValue);
164: assertNotNull(
165: "'Namespace' attribute of 'Header' parameter is expected to be not null.",
166: namespaceValue);
167: }
168: }
169: }
170:
171: public void testGetName() {
172: assertNull(a1.getName());
173: assertNull(a2.getName());
174: assertEquals(assertionName, a3.getName());
175: assertEquals("Name equality check", assertionName, a4.getName());
176: assertEquals("Name equality check", assertionName, a5.getName());
177: }
178:
179: public void testHasParameters() {
180: assertFalse(a1.hasParameters());
181: assertFalse(a2.hasParameters());
182: assertFalse(a3.hasParameters());
183: assertFalse(
184: "Empty parameter list should result in no parameters",
185: a4.hasParameters());
186: assertTrue(
187: "Non-empty parameter list should result in existing parameters",
188: a5.hasParameters());
189: }
190:
191: public void testGetValue() {
192: assertNull(a1.getValue());
193: assertNull(a2.getValue());
194: assertNull(a3.getValue());
195: assertNull(a4.getValue());
196: assertNull(a5.getValue());
197:
198: String expValue = "test";
199: PolicyAssertion assertionWithValue = new PolicyAssertion(
200: AssertionData.createAssertionData(assertionName,
201: expValue, null), null) {
202: };
203: assertEquals(assertionWithValue.getValue(), expValue);
204: }
205:
206: public void testGetAttributeSet() {
207: assertTrue(a1.getAttributesSet().isEmpty());
208: assertTrue(a2.getAttributesSet().isEmpty());
209: assertTrue(a3.getAttributesSet().isEmpty());
210: assertTrue(a4.getAttributesSet().isEmpty());
211: assertTrue(a5.getAttributesSet().isEmpty());
212:
213: assertFalse(assertionWithAttributes.getAttributesSet()
214: .isEmpty());
215: }
216:
217: public void testGetAttributes() {
218: assertTrue(a1.getAttributes().isEmpty());
219: assertTrue(a2.getAttributes().isEmpty());
220: assertTrue(a3.getAttributes().isEmpty());
221: assertTrue(a4.getAttributes().isEmpty());
222: assertTrue(a5.getAttributes().isEmpty());
223:
224: assertFalse(assertionWithAttributes.getAttributes().isEmpty());
225: }
226:
227: public void testGetAttributeValue() {
228: assertNull(a1.getAttributeValue(attribName));
229: assertNull(a2.getAttributeValue(attribName));
230: assertNull(a3.getAttributeValue(attribName));
231: assertNull(a4.getAttributeValue(attribName));
232: assertNull(a5.getAttributeValue(attribName));
233:
234: assertEquals(attribValue, assertionWithAttributes
235: .getAttributeValue(attribName));
236: }
237:
238: public void testIsOptional() {
239: assertFalse(a1.isOptional());
240: assertFalse(a2.isOptional());
241: assertFalse(a3.isOptional());
242: assertFalse(a4.isOptional());
243: assertFalse(a5.isOptional());
244:
245: attributes.put(PolicyConstants.OPTIONAL, "true");
246: PolicyAssertion assertion = new PolicyAssertion(
247: AssertionData.createAssertionData(assertionName,
248: "test", attributes), null) {
249: };
250: assertTrue(assertion.isOptional());
251: }
252:
253: public void testIsIgnorable() {
254: assertFalse(a1.isIgnorable());
255: assertFalse(a2.isIgnorable());
256: assertFalse(a3.isIgnorable());
257: assertFalse(a4.isIgnorable());
258: assertFalse(a5.isIgnorable());
259:
260: attributes.put(PolicyConstants.IGNORABLE, "true");
261: PolicyAssertion assertion = new PolicyAssertion(
262: AssertionData.createAssertionData(assertionName,
263: "test", attributes), null) {
264: };
265: assertTrue(assertion.isIgnorable());
266: }
267:
268: public void testIsPrivate() {
269: assertFalse(a1.isPrivate());
270: assertFalse(a2.isPrivate());
271: assertFalse(a3.isPrivate());
272: assertFalse(a4.isPrivate());
273: assertFalse(a5.isPrivate());
274:
275: attributes.put(PolicyConstants.VISIBILITY_ATTRIBUTE,
276: PolicyConstants.VISIBILITY_VALUE_PRIVATE);
277: PolicyAssertion assertion = new PolicyAssertion(
278: AssertionData.createAssertionData(assertionName,
279: "test", attributes), null) {
280: };
281: assertTrue(assertion.isPrivate());
282: }
283: }
|