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.PolicyException;
040: import com.sun.xml.ws.policy.testutils.PolicyResourceLoader;
041: import java.io.StringReader;
042: import java.io.StringWriter;
043: import javax.xml.stream.XMLOutputFactory;
044: import junit.framework.*;
045: import javax.xml.stream.XMLStreamWriter;
046:
047: public class XmlPolicyModelMarshallerTest extends TestCase {
048: private PolicyModelMarshaller marshaller = PolicyModelMarshaller
049: .getXmlMarshaller(false);
050: private PolicyModelUnmarshaller unmarshaller = PolicyModelUnmarshaller
051: .getXmlUnmarshaller();
052:
053: public XmlPolicyModelMarshallerTest(String testName) {
054: super (testName);
055: }
056:
057: protected void setUp() throws Exception {
058: }
059:
060: protected void tearDown() throws Exception {
061: }
062:
063: /**
064: * Test of marshal method, of class com.sun.xml.ws.policy.sourcemodel.XmlPolicyModelMarshaller.
065: */
066: public void testMarshal() throws Exception {
067: PolicySourceModel model = null;
068: Object storage = null;
069:
070: try {
071: marshaller.marshal(model, storage);
072: fail("Expected NullPointerException");
073: } catch (NullPointerException e) {
074: }
075:
076: StringWriter writer = new StringWriter();
077: XMLOutputFactory factory = XMLOutputFactory.newInstance();
078: XMLStreamWriter streamWriter = factory
079: .createXMLStreamWriter(writer);
080: storage = streamWriter;
081:
082: try {
083: marshaller.marshal(model, storage);
084: fail("Expected NullPointerException");
085: } catch (NullPointerException e) {
086: }
087:
088: model = PolicyResourceLoader
089: .unmarshallModel("complex_policy/nested_assertions_with_alternatives.xml");
090: storage = null;
091:
092: try {
093: marshaller.marshal(model, storage);
094: fail("Expected NullPointerException");
095: } catch (NullPointerException e) {
096: }
097:
098: storage = new Object();
099:
100: try {
101: marshaller.marshal(model, storage);
102: fail("Expected PolicyException");
103: } catch (PolicyException e) {
104: }
105:
106: storage = streamWriter;
107: marshaller.marshal(model, storage);
108: String policy = writer.toString();
109: // Verifying that produced policy String is not empty
110: assertTrue(policy.length() > 10);
111: }
112:
113: public void testMarshallingAssertionsWithVisibilityAttribute()
114: throws Exception {
115: String[] modelFileNames = new String[] { "policy_0_visible",
116: "policy_1_visible", "policy_2_visible" };
117:
118: XMLOutputFactory factory = XMLOutputFactory.newInstance();
119: for (String modelFileName : modelFileNames) {
120: PolicySourceModel model = PolicyResourceLoader
121: .unmarshallModel("visibility/" + modelFileName
122: + ".xml");
123: StringWriter writer = new StringWriter();
124: XMLStreamWriter streamWriter = factory
125: .createXMLStreamWriter(writer);
126: marshaller.marshal(model, streamWriter);
127:
128: StringReader reader = new StringReader(writer.toString());
129: PolicySourceModel resultModel = unmarshaller
130: .unmarshalModel(reader);
131: PolicySourceModel expectedModel = PolicyResourceLoader
132: .unmarshallModel("visibility/" + modelFileName
133: + "_expected.xml");
134: assertEquals(modelFileName, expectedModel, resultModel);
135: System.out.println(writer.toString());
136: }
137: }
138:
139: }
|