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: * SignedElementsTest.java
038: * JUnit based test
039: *
040: * Created on August 24, 2006, 12:27 AM
041: */
042:
043: package com.sun.xml.ws.security.impl.policy;
044:
045: import com.sun.xml.ws.policy.Policy;
046: import com.sun.xml.ws.policy.PolicyException;
047: import com.sun.xml.ws.policy.sourcemodel.PolicyModelTranslator;
048: import com.sun.xml.ws.policy.sourcemodel.PolicyModelUnmarshaller;
049: import com.sun.xml.ws.policy.sourcemodel.PolicySourceModel;
050: import java.io.IOException;
051: import java.io.InputStreamReader;
052: import java.io.Reader;
053:
054: import junit.framework.*;
055: import com.sun.xml.ws.policy.AssertionSet;
056: import com.sun.xml.ws.policy.PolicyAssertion;
057: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
058: import com.sun.xml.ws.policy.Policy;
059: import com.sun.xml.ws.security.policy.SecurityAssertionValidator;
060: import java.util.ArrayList;
061: import java.util.Collection;
062: import java.util.Collections;
063: import java.util.Iterator;
064: import java.util.List;
065: import java.util.Set;
066: import java.util.logging.Level;
067: import javax.xml.namespace.QName;
068: import static com.sun.xml.ws.security.impl.policy.Constants.*;
069:
070: /**
071: *
072: * @author Mayank.Mishra@SUN.com
073: */
074: public class SignedElementsTest extends TestCase {
075:
076: public SignedElementsTest(String testName) {
077: super (testName);
078: }
079:
080: protected void setUp() throws Exception {
081: }
082:
083: protected void tearDown() throws Exception {
084: }
085:
086: public static Test suite() {
087: TestSuite suite = new TestSuite(SignedElementsTest.class);
088:
089: return suite;
090: }
091:
092: private PolicySourceModel unmarshalPolicyResource(String resource)
093: throws PolicyException, IOException {
094: Reader reader = getResourceReader(resource);
095: PolicySourceModel model = PolicyModelUnmarshaller
096: .getXmlUnmarshaller().unmarshalModel(reader);
097: reader.close();
098: return model;
099: }
100:
101: private Reader getResourceReader(String resourceName) {
102: return new InputStreamReader(Thread.currentThread()
103: .getContextClassLoader().getResourceAsStream(
104: resourceName));
105: }
106:
107: public Policy unmarshalPolicy(String xmlFile) throws Exception {
108: PolicySourceModel model = unmarshalPolicyResource(xmlFile);
109: Policy mbp = PolicyModelTranslator.getTranslator().translate(
110: model);
111: return mbp;
112:
113: }
114:
115: public boolean hasXPathTarget(String xpathExpr, Iterator itr) {
116: while (itr.hasNext()) {
117: if (xpathExpr.equals(itr.next())) {
118: return true;
119: }
120: }
121: return false;
122: }
123:
124: public void testSignElements1() throws Exception {
125: String fileName = "security/SignElements1.xml";
126: Policy policy = unmarshalPolicy(fileName);
127: assertNotNull(policy);
128: Iterator<AssertionSet> itr = policy.iterator();
129: if (itr.hasNext()) {
130: AssertionSet as = itr.next();
131: for (PolicyAssertion assertion : as) {
132: assertEquals("Invalid assertion", "SignedElements",
133: assertion.getName().getLocalPart());
134: SignedElements se = (SignedElements) assertion;
135: assertTrue(hasXPathTarget("//soapEnv:Body", se
136: .getTargets()));
137: assertTrue(hasXPathTarget("//addr:To", se.getTargets()));
138: assertTrue(hasXPathTarget("//addr:From", se
139: .getTargets()));
140: assertTrue(hasXPathTarget("//addr:RealtesTo", se
141: .getTargets()));
142:
143: // assertFalse("Headers should not be present",se.getHeaders().hasNext());
144: }
145: } else {
146: throw new Exception(
147: "No Assertions found!. Unmarshalling of "
148: + fileName + "failed!");
149: }
150: }
151:
152: public void testSignElements2() throws Exception {
153: String fileName = "security/SignElements2.xml";
154: Policy policy = unmarshalPolicy(fileName);
155: assertNotNull(policy);
156: Iterator<AssertionSet> itr = policy.iterator();
157: if (itr.hasNext()) {
158: AssertionSet as = itr.next();
159: for (PolicyAssertion assertion : as) {
160: assertEquals("Invalid assertion", "SignedElements",
161: assertion.getName().getLocalPart());
162: SignedElements se = (SignedElements) assertion;
163: assertFalse(se.getTargets().hasNext());
164:
165: // assertFalse("Headers should not be present",se.getHeaders().hasNext());
166: }
167: } else {
168: throw new Exception(
169: "No Assertions found!. Unmarshalling of "
170: + fileName + "failed!");
171: }
172: }
173:
174: }
|