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.jaxws;
038:
039: import com.sun.xml.ws.api.model.wsdl.WSDLModel;
040: import com.sun.xml.ws.api.server.Container;
041: import com.sun.xml.ws.policy.Policy;
042: import com.sun.xml.ws.policy.PolicyConstants;
043: import com.sun.xml.ws.policy.PolicyException;
044: import com.sun.xml.ws.policy.PolicyMap;
045: import com.sun.xml.ws.policy.PolicyMapKey;
046: import com.sun.xml.ws.policy.privateutil.PolicyUtils;
047: import com.sun.xml.ws.policy.testutils.PolicyResourceLoader;
048: import java.io.File;
049: import java.io.FileInputStream;
050: import java.io.FileOutputStream;
051: import java.io.IOException;
052: import java.net.URL;
053: import java.nio.channels.FileChannel;
054: import javax.xml.namespace.QName;
055: import javax.xml.ws.WebServiceException;
056: import junit.framework.TestCase;
057:
058: /**
059: *
060: */
061: public class PolicyConfigParserTest extends TestCase {
062: private static final String TEST_FILE_PATH = "test/unit/data/policy/config/wsit.xml";
063: private static final String CONFIG_FILE_PATH = "test/unit/data/META-INF";
064: private static final String CLASSPATH_CONFIG_FILE_PATH = "test/unit/data";
065: private static final String CONFIG_FILE_NAME = "wsit-test.xml";
066: private static final String CLIENT_CONFIG_FILE_NAME = "wsit-client.xml";
067:
068: public PolicyConfigParserTest(String testName) {
069: super (testName);
070: }
071:
072: protected void setUp() throws Exception {
073: }
074:
075: protected void tearDown() throws Exception {
076: }
077:
078: public void testParseContainerNullWithoutConfig() throws Exception {
079: PolicyMap result = PolicyConfigParser
080: .parse((String) null, null);
081: assertNull(result);
082: }
083:
084: public void testParseContainerWithoutContextWithoutConfig()
085: throws Exception {
086: Container container = new MockContainer(null);
087: PolicyMap result = PolicyConfigParser.parse((String) null,
088: container);
089: assertNull(result);
090: }
091:
092: public void testParseContainerNullWithConfig() throws Exception {
093: PolicyMap map = prepareTestFileAndLoadPolicyMap(TEST_FILE_PATH,
094: CONFIG_FILE_PATH, CONFIG_FILE_NAME, "test", null);
095: testLoadedMap(map);
096: }
097:
098: public void testParseContainerWithoutContext() throws Exception {
099: Container container = new MockContainer(null);
100: PolicyMap map = prepareTestFileAndLoadPolicyMap(TEST_FILE_PATH,
101: CONFIG_FILE_PATH, CONFIG_FILE_NAME, "test", container);
102: testLoadedMap(map);
103: }
104:
105: public void testParseContainerWithContext() throws Exception {
106: // TODO Need MockServletContext
107: }
108:
109: public void testWsitXmlNotLoadedContainerNullWithConfig()
110: throws Exception {
111: PolicyMap map = prepareTestFileAndLoadPolicyMap(TEST_FILE_PATH,
112: CONFIG_FILE_PATH, "wsit.xml", "test", null);
113: assertNull(map);
114: }
115:
116: public void testWsitXmlNotLoadedContainerWithoutContext()
117: throws Exception {
118: Container container = new MockContainer(null);
119: PolicyMap map = prepareTestFileAndLoadPolicyMap(TEST_FILE_PATH,
120: CONFIG_FILE_PATH, "wsit.xml", "test", container);
121: assertNull(map);
122: }
123:
124: public void testWsitXmlNotLoadedContainerWithContext()
125: throws Exception {
126: // TODO Need MockServletContext
127: }
128:
129: public void testParseClientWithoutContextWithoutConfig()
130: throws Exception {
131: PolicyMap result = PolicyConfigParser.parse(
132: PolicyConstants.CLIENT_CONFIGURATION_IDENTIFIER, null);
133: assertNull(result);
134: }
135:
136: public void testParseClientMetainfContainerNullWithConfig()
137: throws Exception {
138: PolicyMap map = prepareTestFileAndLoadPolicyMap(TEST_FILE_PATH,
139: CONFIG_FILE_PATH, CLIENT_CONFIG_FILE_NAME,
140: PolicyConstants.CLIENT_CONFIGURATION_IDENTIFIER, null);
141: testLoadedMap(map);
142: }
143:
144: public void testParseClientMetainfWithoutContext() throws Exception {
145: Container container = new MockContainer(null);
146: PolicyMap map = prepareTestFileAndLoadPolicyMap(TEST_FILE_PATH,
147: CONFIG_FILE_PATH, CLIENT_CONFIG_FILE_NAME,
148: PolicyConstants.CLIENT_CONFIGURATION_IDENTIFIER,
149: container);
150: testLoadedMap(map);
151: }
152:
153: public void testParseClientClasspathContainerNullWithConfig()
154: throws Exception {
155: PolicyMap map = prepareTestFileAndLoadPolicyMap(TEST_FILE_PATH,
156: CLASSPATH_CONFIG_FILE_PATH, CLIENT_CONFIG_FILE_NAME,
157: PolicyConstants.CLIENT_CONFIGURATION_IDENTIFIER, null);
158: testLoadedMap(map);
159: }
160:
161: public void testParseClientClasspathWithoutContext()
162: throws Exception {
163: Container container = new MockContainer(null);
164: PolicyMap map = prepareTestFileAndLoadPolicyMap(TEST_FILE_PATH,
165: CLASSPATH_CONFIG_FILE_PATH, CLIENT_CONFIG_FILE_NAME,
166: PolicyConstants.CLIENT_CONFIGURATION_IDENTIFIER,
167: container);
168: testLoadedMap(map);
169: }
170:
171: /**
172: * Test of parse method, of class com.sun.xml.ws.policy.jaxws.PolicyConfigParser.
173: */
174: public void testParseURLNull() throws Exception {
175: PolicyMap result = null;
176:
177: try {
178: result = PolicyConfigParser.parse((URL) null, false);
179: fail("Expected IllegalArgumentException");
180: } catch (IllegalArgumentException e) {
181: }
182: assertNull(result);
183: }
184:
185: public void testParseBufferMex() throws Exception {
186: PolicyMap map = parseConfigFile("mex/mex.xml");
187: PolicyMapKey key = map.createWsdlEndpointScopeKey(new QName(
188: "http://schemas.xmlsoap.org/ws/2004/09/mex",
189: "MetadataExchangeService"), new QName(
190: "http://schemas.xmlsoap.org/ws/2004/09/mex",
191: "MetadataExchangePort"));
192: Policy policy = map.getEndpointEffectivePolicy(key);
193: assertNotNull(policy);
194: assertEquals("MEXPolicy", policy.getId());
195: }
196:
197: public void testParseBufferSimple() throws Exception {
198: PolicyMap map = parseConfigFile("config/simple.wsdl");
199: PolicyMapKey key = map.createWsdlEndpointScopeKey(new QName(
200: "http://example.org/", "AddNumbersService"), new QName(
201: "http://example.org/", "AddNumbersPort"));
202: Policy policy = map.getEndpointEffectivePolicy(key);
203: assertNotNull(policy);
204: assertEquals("MutualCertificate10Sign_IPingService_policy",
205: policy.getId());
206: }
207:
208: public void testParseBufferSingleImport() throws Exception {
209: WSDLModel result = null;
210:
211: PolicyMap map = parseConfigFile("config/single-import.wsdl");
212: assertNotNull(map);
213:
214: PolicyMapKey key1 = map.createWsdlEndpointScopeKey(new QName(
215: "http://example.org/", "AddNumbersService"), new QName(
216: "http://example.org/", "AddNumbersPort"));
217: Policy policy1 = map.getEndpointEffectivePolicy(key1);
218: assertNotNull(policy1);
219: assertEquals("MutualCertificate10Sign_IPingService_policy",
220: policy1.getId());
221:
222: PolicyMapKey key2 = map.createWsdlEndpointScopeKey(new QName(
223: "http://example.net/", "AddNumbersService"), new QName(
224: "http://example.net/", "AddNumbersPort"));
225: Policy policy2 = map.getEndpointEffectivePolicy(key2);
226: assertNotNull(policy2);
227: assertEquals("MutualCertificate10Sign_IPingService_policy",
228: policy2.getId());
229: }
230:
231: public void testParseBufferMultiImport() throws Exception {
232: PolicyMap map = parseConfigFile("config/import.wsdl");
233:
234: assertNotNull(map);
235:
236: PolicyMapKey key1 = map.createWsdlEndpointScopeKey(new QName(
237: "http://example.org/", "AddNumbersService"), new QName(
238: "http://example.org/", "AddNumbersPort"));
239: Policy policy1 = map.getEndpointEffectivePolicy(key1);
240: assertNotNull(policy1);
241: assertEquals("MutualCertificate10Sign_IPingService_policy",
242: policy1.getId());
243:
244: PolicyMapKey key2 = map.createWsdlEndpointScopeKey(new QName(
245: "http://example.net/", "AddNumbersService"), new QName(
246: "http://example.net/", "AddNumbersPort"));
247: Policy policy2 = map.getEndpointEffectivePolicy(key2);
248: assertNotNull(policy2);
249: assertEquals("MutualCertificate10Sign_IPingService_policy",
250: policy2.getId());
251:
252: PolicyMapKey key3 = map.createWsdlEndpointScopeKey(new QName(
253: "http://example.com/", "AddNumbersService"), new QName(
254: "http://example.com/", "AddNumbersPort"));
255: Policy policy3 = map.getEndpointEffectivePolicy(key3);
256: assertNotNull(policy3);
257: assertEquals("MutualCertificate10Sign_IPingService_policy",
258: policy3.getId());
259:
260: PolicyMapKey key4 = map.createWsdlEndpointScopeKey(new QName(
261: "http://example.com/import3/", "AddNumbersService"),
262: new QName("http://example.com/import3/",
263: "AddNumbersPort"));
264: Policy policy4 = map.getEndpointEffectivePolicy(key4);
265: assertNotNull(policy4);
266: assertEquals("MutualCertificate10Sign_IPingService_policy",
267: policy4.getId());
268: }
269:
270: public void testParseBufferCyclicImport() throws Exception {
271: PolicyMap map = parseConfigFile("config/cyclic.wsdl");
272: PolicyMapKey key = map.createWsdlEndpointScopeKey(new QName(
273: "http://example.org/", "AddNumbersService"), new QName(
274: "http://example.org/", "AddNumbersPort"));
275: Policy policy = map.getEndpointEffectivePolicy(key);
276: assertNotNull(policy);
277: assertEquals("MutualCertificate10Sign_IPingService_policy",
278: policy.getId());
279: }
280:
281: public void testParseBufferExternalReference() throws Exception {
282: PolicyMap map = null;
283: try {
284: map = parseConfigFile("config/service.wsdl");
285: assert false; // should throw "failed to find policy" exception
286: } catch (WebServiceException wse) {
287: }
288: }
289:
290: public void testParseBufferExternalReferenceName() throws Exception {
291: PolicyMap map = parseConfigFile("config/service-name.wsdl");
292: PolicyMapKey key = map.createWsdlEndpointScopeKey(new QName(
293: "http://example.org/AddNumbers/service",
294: "AddNumbersService"), new QName(
295: "http://example.org/AddNumbers/service",
296: "AddNumbersPort"));
297: Policy policy = map.getEndpointEffectivePolicy(key);
298: assertNotNull(policy);
299: assertEquals(
300: "http://example.org/AddNumbers/porttype#AddNumbersServicePolicy",
301: policy.getName());
302: }
303:
304: private PolicyMap parseConfigFile(String configFile)
305: throws Exception {
306: URL url = PolicyUtils.ConfigFile
307: .loadFromClasspath(PolicyResourceLoader.POLICY_UNIT_TEST_RESOURCE_ROOT
308: + configFile);
309: return PolicyConfigParser.parse(url, false);
310: }
311:
312: /**
313: * Copy a file
314: */
315: private static final void copyFile(String sourceName,
316: String destPath, String destName) throws IOException {
317: FileChannel source = null;
318: FileChannel dest = null;
319: try {
320: File destDir = new File(destPath);
321: destDir.mkdir();
322:
323: // Create channel on the source
324: source = new FileInputStream(sourceName).getChannel();
325:
326: // Create channel on the destination
327: dest = new FileOutputStream(destPath + File.separatorChar
328: + destName).getChannel();
329:
330: // Copy file contents from source to destination
331: dest.transferFrom(source, 0, source.size());
332:
333: } finally {
334: // Close the channels
335: if (source != null) {
336: try {
337: source.close();
338: } catch (IOException e) {
339: }
340: }
341: if (dest != null) {
342: dest.close();
343: }
344: }
345: }
346:
347: private PolicyMap prepareTestFileAndLoadPolicyMap(
348: String sourceName, String destPath, String destName,
349: String cfgFileId, Container container)
350: throws PolicyException, IOException {
351: PolicyMap result;
352: try {
353: copyFile(sourceName, destPath, destName);
354: result = PolicyConfigParser.parse(cfgFileId, container);
355: return result;
356: } finally {
357: File wsitxml = new File(destPath + File.separatorChar
358: + destName);
359: wsitxml.delete();
360: }
361: }
362:
363: private void testLoadedMap(PolicyMap map) throws PolicyException {
364: PolicyMapKey key = PolicyMap.createWsdlEndpointScopeKey(
365: new QName("http://example.org/", "AddNumbersService"),
366: new QName("http://example.org/", "AddNumbersPort"));
367: Policy policy = map.getEndpointEffectivePolicy(key);
368: assertNotNull(policy);
369: assertEquals("MutualCertificate10Sign_IPingService_policy",
370: policy.getId());
371: }
372:
373: class MockContainer extends Container {
374: private final Object spi;
375:
376: public <T> MockContainer(T spi) {
377: this .spi = spi;
378: }
379:
380: public <T> T getSPI(Class<T> spiType) {
381: if (spiType.isInstance(this .spi)) {
382: return (T) this.spi;
383: } else {
384: return null;
385: }
386: }
387:
388: }
389: }
|