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.sourcemodel;
038:
039: import com.sun.xml.ws.policy.*;
040: import com.sun.xml.ws.policy.testutils.PolicyResourceLoader;
041: import junit.framework.*;
042: import java.util.Collections;
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: public class PolicyModelGeneratorTest extends TestCase {
047: private static final String COMPACT_MODEL_SUFFIX = ".xml";
048: private static final String NORMALIZED_MODEL_SUFFIX = "_normalized.xml";
049: private static final Map<String, Integer> COMPLEX_POLICIES;
050:
051: static {
052: Map<String, Integer> tempMap = new HashMap<String, Integer>();
053: tempMap
054: .put(
055: "complex_policy/nested_assertions_with_alternatives",
056: 3);
057: tempMap.put("complex_policy/single_choice1", 3);
058: tempMap.put("complex_policy/single_choice2", 5);
059: tempMap.put("complex_policy/nested_choice1", 3);
060: tempMap.put("complex_policy/nested_choice2", 3);
061: tempMap.put("complex_policy/assertion_parameters1", 1);
062: tempMap.put("complex_policy/assertion_parameters2", 1);
063: COMPLEX_POLICIES = Collections.unmodifiableMap(tempMap);
064: }
065:
066: private PolicyModelGenerator generator;
067: private PolicyModelTranslator translator;
068:
069: public PolicyModelGeneratorTest(String testName) {
070: super (testName);
071: }
072:
073: protected void setUp() throws Exception {
074: translator = PolicyModelTranslator.getTranslator();
075: generator = PolicyModelGenerator.getGenerator();
076: }
077:
078: protected void tearDown() throws Exception {
079: }
080:
081: public void testGetTranslator() throws Exception {
082: PolicyModelGenerator result = PolicyModelGenerator
083: .getGenerator();
084: assertNotNull(result);
085: }
086:
087: public void testGenerate() throws Exception {
088: for (Map.Entry<String, Integer> entry : COMPLEX_POLICIES
089: .entrySet()) {
090: String compactResourceName = entry.getKey()
091: + COMPACT_MODEL_SUFFIX;
092: String normalizedResouceName = entry.getKey()
093: + NORMALIZED_MODEL_SUFFIX;
094: int expectedNumberOfAlternatives = entry.getValue();
095:
096: PolicySourceModel compactModel = PolicyResourceLoader
097: .unmarshallModel(compactResourceName);
098: PolicySourceModel normalizedModel = PolicyResourceLoader
099: .unmarshallModel(normalizedResouceName);
100: Policy compactModelPolicy = translator
101: .translate(compactModel);
102: Policy normalizedModelPolicy = translator
103: .translate(normalizedModel);
104:
105: PolicySourceModel generatedCompactModel = generator
106: .translate(compactModelPolicy);
107: PolicySourceModel generatedNormalizedModel = generator
108: .translate(normalizedModelPolicy);
109:
110: Policy generatedCompactModelPolicy = translator
111: .translate(generatedCompactModel);
112: Policy generatedNormalizedModelPolicy = translator
113: .translate(generatedNormalizedModel);
114:
115: assertEquals("Generated compact policy should contain '"
116: + expectedNumberOfAlternatives + "' alternatives",
117: expectedNumberOfAlternatives,
118: generatedCompactModelPolicy
119: .getNumberOfAssertionSets());
120: assertEquals(
121: "Generated and translated compact model policy instances should contain equal number of alternatives",
122: compactModelPolicy.getNumberOfAssertionSets(),
123: generatedCompactModelPolicy
124: .getNumberOfAssertionSets());
125: assertEquals(
126: "Generated and translated compact policy expression should form equal Policy instances",
127: compactModelPolicy, generatedCompactModelPolicy);
128:
129: assertEquals("Generated normalized policy should contain '"
130: + expectedNumberOfAlternatives + "' alternatives",
131: expectedNumberOfAlternatives,
132: generatedNormalizedModelPolicy
133: .getNumberOfAssertionSets());
134: assertEquals(
135: "Generated and translated normalized model policy instances should contain equal number of alternatives",
136: normalizedModelPolicy.getNumberOfAssertionSets(),
137: generatedNormalizedModelPolicy
138: .getNumberOfAssertionSets());
139: assertEquals(
140: "Generated and translated normalized policy expression should form equal Policy instances",
141: normalizedModelPolicy,
142: generatedNormalizedModelPolicy);
143:
144: // TODO: somehow compare models, because now the test only checks if the translation does not end in some exception...
145: }
146: }
147: }
|