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.jaxb;
019:
020: import java.io.OutputStream;
021: import java.util.Arrays;
022: import java.util.HashSet;
023: import java.util.List;
024: import java.util.Set;
025: import java.util.logging.Logger;
026:
027: import javax.wsdl.Definition;
028: import javax.wsdl.Service;
029: import javax.wsdl.factory.WSDLFactory;
030: import javax.wsdl.xml.WSDLReader;
031: import javax.xml.bind.JAXBContext;
032: import javax.xml.stream.XMLEventReader;
033: import javax.xml.stream.XMLEventWriter;
034: import javax.xml.stream.XMLStreamReader;
035: import javax.xml.stream.XMLStreamWriter;
036:
037: import org.w3c.dom.Node;
038:
039: import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
040:
041: import org.apache.cxf.Bus;
042: import org.apache.cxf.binding.BindingFactoryManager;
043: import org.apache.cxf.databinding.DataReader;
044: import org.apache.cxf.databinding.DataWriter;
045: import org.apache.cxf.helpers.CastUtils;
046: import org.apache.cxf.jaxb.io.DataReaderImpl;
047: import org.apache.cxf.jaxb.io.DataWriterImpl;
048: import org.apache.cxf.jaxb_misc.ObjectFactory;
049: import org.apache.cxf.jaxb_misc.TestJAXBClass;
050: import org.apache.cxf.transport.DestinationFactoryManager;
051: import org.apache.cxf.wsdl11.WSDLServiceBuilder;
052: import org.apache.hello_world.types.GreetMe;
053: import org.apache.hello_world_soap_http.types.GreetMeOneWay;
054: import org.easymock.classextension.EasyMock;
055: import org.easymock.classextension.IMocksControl;
056: import org.junit.After;
057: import org.junit.Assert;
058: import org.junit.Before;
059: import org.junit.Test;
060:
061: public class JAXBDataBindingTest extends Assert {
062:
063: private static final Logger LOG = Logger
064: .getLogger(JAXBDataBindingTest.class.getName());
065: private static final String WSDL_PATH = "/wsdl/hello_world.wsdl";
066: private Definition def;
067: private Service service;
068:
069: private IMocksControl control;
070: private Bus bus;
071: private BindingFactoryManager bindingFactoryManager;
072: private JAXBDataBinding jaxbDataBinding;
073: private DestinationFactoryManager destinationFactoryManager;
074:
075: @Before
076: public void setUp() throws Exception {
077: jaxbDataBinding = new JAXBDataBinding();
078: String wsdlUrl = getClass().getResource(WSDL_PATH).toString();
079: LOG.info("the path of wsdl file is " + wsdlUrl);
080: WSDLFactory wsdlFactory = WSDLFactory.newInstance();
081: WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
082: wsdlReader.setFeature("javax.wsdl.verbose", false);
083: def = wsdlReader.readWSDL(wsdlUrl);
084:
085: control = EasyMock.createNiceControl();
086: bus = control.createMock(Bus.class);
087: bindingFactoryManager = control
088: .createMock(BindingFactoryManager.class);
089: destinationFactoryManager = control
090: .createMock(DestinationFactoryManager.class);
091:
092: EasyMock.expect(bus.getExtension(BindingFactoryManager.class))
093: .andReturn(bindingFactoryManager);
094: EasyMock.expect(
095: bus.getExtension(DestinationFactoryManager.class))
096: .andStubReturn(destinationFactoryManager);
097:
098: control.replay();
099:
100: WSDLServiceBuilder wsdlServiceBuilder = new WSDLServiceBuilder(
101: bus);
102: for (Service serv : CastUtils.cast(def.getServices().values(),
103: Service.class)) {
104: if (serv != null) {
105: service = serv;
106: break;
107: }
108: }
109:
110: wsdlServiceBuilder.buildServices(def, service);
111: }
112:
113: @After
114: public void tearDown() throws Exception {
115:
116: }
117:
118: @Test
119: public void testCreateJAXBContext() throws Exception {
120: try {
121: Set<Class<?>> classes = new HashSet<Class<?>>();
122: classes.add(Class.forName("java.rmi.RemoteException"));
123: assertNotNull(new JAXBDataBinding()
124: .createJAXBContext(classes));
125: } catch (Exception e) {
126: assertFalse(true);
127: }
128:
129: }
130:
131: @Test
132: public void testCreateReader() {
133: DataReader reader = jaxbDataBinding
134: .createReader(XMLStreamReader.class);
135: assertTrue(reader instanceof DataReaderImpl);
136:
137: reader = jaxbDataBinding.createReader(XMLEventReader.class);
138: assertTrue(reader instanceof DataReaderImpl);
139:
140: reader = jaxbDataBinding.createReader(Node.class);
141: assertTrue(reader instanceof DataReaderImpl);
142:
143: reader = jaxbDataBinding.createReader(null);
144: assertNull(reader);
145: }
146:
147: @Test
148: public void testSupportedFormats() {
149: List<Class<?>> cls = Arrays.asList(jaxbDataBinding
150: .getSupportedWriterFormats());
151: assertNotNull(cls);
152: assertEquals(4, cls.size());
153: assertTrue(cls.contains(XMLStreamWriter.class));
154: assertTrue(cls.contains(XMLEventWriter.class));
155: assertTrue(cls.contains(Node.class));
156: assertTrue(cls.contains(OutputStream.class));
157:
158: cls = Arrays
159: .asList(jaxbDataBinding.getSupportedReaderFormats());
160: assertNotNull(cls);
161: assertEquals(3, cls.size());
162: assertTrue(cls.contains(XMLStreamReader.class));
163: assertTrue(cls.contains(XMLEventReader.class));
164: assertTrue(cls.contains(Node.class));
165: }
166:
167: @Test
168: public void testCreateWriter() {
169: DataWriter writer = jaxbDataBinding
170: .createWriter(XMLStreamWriter.class);
171: assertTrue(writer instanceof DataWriterImpl);
172:
173: writer = jaxbDataBinding.createWriter(XMLEventWriter.class);
174: assertTrue(writer instanceof DataWriterImpl);
175:
176: writer = jaxbDataBinding.createWriter(Node.class);
177: assertTrue(writer instanceof DataWriterImpl);
178:
179: writer = jaxbDataBinding.createWriter(null);
180: assertNull(writer);
181: }
182:
183: @Test
184: public void testExtraClass() {
185: Class[] extraClass = new Class[] { GreetMe.class,
186: GreetMeOneWay.class };
187: jaxbDataBinding.setExtraClass(extraClass);
188: assertEquals(jaxbDataBinding.getExtraClass().length, 2);
189: assertEquals(jaxbDataBinding.getExtraClass()[0], GreetMe.class);
190: assertEquals(jaxbDataBinding.getExtraClass()[1],
191: GreetMeOneWay.class);
192: }
193:
194: @Test
195: public void testJaxbIndex() throws Exception {
196: JAXBDataBinding db = new JAXBDataBinding();
197: Set<Class<?>> classes = new HashSet<Class<?>>();
198: classes.add(ObjectFactory.class);
199: JAXBContext ctx = db.createJAXBContext(classes);
200: if (ctx instanceof JAXBContextImpl) {
201: JAXBContextImpl rictx = (JAXBContextImpl) ctx;
202: assertNotNull(rictx.getBeanInfo(TestJAXBClass.class));
203: }
204: }
205:
206: }
|