001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.wsdl;
019:
020: import java.io.BufferedReader;
021: import java.io.ByteArrayOutputStream;
022: import java.io.PrintWriter;
023: import java.io.StringReader;
024: import java.util.List;
025:
026: import javax.wsdl.Definition;
027: import javax.wsdl.Port;
028: import javax.wsdl.Service;
029: import javax.wsdl.extensions.ExtensionRegistry;
030: import javax.wsdl.factory.WSDLFactory;
031: import javax.wsdl.xml.WSDLReader;
032: import javax.xml.namespace.QName;
033:
034: import org.apache.cxf.abc.test.AnotherPolicyType;
035: import org.apache.cxf.abc.test.NewServiceType;
036: import org.apache.cxf.abc.test.TestPolicyType;
037: import org.junit.Assert;
038: import org.junit.Before;
039: import org.junit.Test;
040:
041: public class JAXBExtensionHelperTest extends Assert {
042:
043: private WSDLFactory wsdlFactory;
044:
045: private WSDLReader wsdlReader;
046:
047: private Definition wsdlDefinition;
048:
049: private ExtensionRegistry registry;
050:
051: @Before
052: public void setUp() throws Exception {
053:
054: wsdlFactory = WSDLFactory.newInstance();
055: wsdlReader = wsdlFactory.newWSDLReader();
056: wsdlReader.setFeature("javax.wsdl.verbose", false);
057: registry = wsdlReader.getExtensionRegistry();
058: if (registry == null) {
059: registry = wsdlFactory.newPopulatedExtensionRegistry();
060: }
061: }
062:
063: @Test
064: public void testAddTestExtension() throws Exception {
065:
066: JAXBExtensionHelper.addExtensions(registry, "javax.wsdl.Port",
067: "org.apache.cxf.abc.test.TestPolicyType", Thread
068: .currentThread().getContextClassLoader());
069:
070: JAXBExtensionHelper.addExtensions(registry, "javax.wsdl.Port",
071: "org.apache.cxf.abc.test.AnotherPolicyType", Thread
072: .currentThread().getContextClassLoader());
073:
074: JAXBExtensionHelper.addExtensions(registry,
075: "javax.wsdl.Definition",
076: "org.apache.cxf.abc.test.NewServiceType", Thread
077: .currentThread().getContextClassLoader());
078:
079: String file = this .getClass()
080: .getResource("/wsdl/test_ext.wsdl").toURI().toString();
081:
082: wsdlReader.setExtensionRegistry(registry);
083:
084: wsdlDefinition = wsdlReader.readWSDL(file);
085: Service s = wsdlDefinition.getService(new QName(
086: "http://cxf.apache.org/test/hello_world",
087: "HelloWorldService"));
088: Port p = s.getPort("HelloWorldPort");
089: List extPortList = p.getExtensibilityElements();
090:
091: TestPolicyType tp = null;
092: AnotherPolicyType ap = null;
093: for (Object ext : extPortList) {
094: if (ext instanceof TestPolicyType) {
095: tp = (TestPolicyType) ext;
096: }
097: if (ext instanceof AnotherPolicyType) {
098: ap = (AnotherPolicyType) ext;
099: }
100: }
101: assertNotNull(
102: "Could not find extension element TestPolicyType", tp);
103: assertNotNull(
104: "Could not find extension element AnotherPolicyType",
105: ap);
106:
107: assertEquals("Unexpected value for TestPolicyType intAttr", 30,
108: tp.getIntAttr());
109: assertEquals("Unexpected value for TestPolicyType stringAttr",
110: "hello", tp.getStringAttr());
111: assertTrue("Unexpected value for AnotherPolicyType floatAttr",
112: Math.abs(0.1F - ap.getFloatAttr()) < 0.5E-5);
113: }
114:
115: @Test
116: public void testPrettyPrintXMLStreamWriter() throws Exception {
117: JAXBExtensionHelper.addExtensions(registry,
118: "javax.wsdl.Definition",
119: "org.apache.cxf.abc.test.NewServiceType", Thread
120: .currentThread().getContextClassLoader());
121:
122: String file = this .getClass()
123: .getResource("/wsdl/test_ext.wsdl").toURI().toString();
124:
125: wsdlReader.setExtensionRegistry(registry);
126:
127: wsdlDefinition = wsdlReader.readWSDL(file);
128:
129: List extList = wsdlDefinition.getExtensibilityElements();
130: NewServiceType newService = null;
131: for (Object ext : extList) {
132: if (ext instanceof NewServiceType) {
133: newService = (NewServiceType) ext;
134: break;
135: }
136: }
137:
138: assertNotNull(
139: "Could not find extension element NewServiceType",
140: newService);
141:
142: ByteArrayOutputStream stream = new ByteArrayOutputStream();
143:
144: JAXBExtensionHelper helper = new JAXBExtensionHelper(
145: NewServiceType.class);
146: helper.marshall(javax.wsdl.Definition.class,
147: new QName("http://cxf.apache.org/test/hello_world",
148: "newService"), newService, new PrintWriter(
149: stream), wsdlDefinition, registry);
150: BufferedReader reader = new BufferedReader(new StringReader(
151: new String(stream.toByteArray())));
152: String actual = reader.readLine();
153: int spaces = 0;
154: while (actual != null) {
155: if (!actual.endsWith("/>")) {
156: if (!actual.contains("</")) {
157: spaces += 2;
158: } else {
159: spaces -= 2;
160: }
161: }
162: checkSpaces(actual, spaces);
163: actual = reader.readLine();
164: }
165: }
166:
167: private void checkSpaces(String actual, int spaces) {
168: String space = "";
169: for (int i = 0; i < spaces; i++) {
170: space += " ";
171: }
172: assertTrue(
173: "Indentation level not proper when marshalling a extension element;"
174: + actual, actual.startsWith(space));
175: }
176:
177: }
|