From 5b67e082ce57df77acc72cd36569a05c9a2fa347 Mon Sep 17 00:00:00 2001 From: slaws Date: Mon, 6 Jul 2009 07:23:44 +0000 Subject: Branch for 1.5.1 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@791397 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/com/example/stock/StockQuoteOffer.java | 45 +++ .../databinding/jaxb/BeanXMLStreamReaderImpl.java | 324 +++++++++++++++++++++ .../sca/databinding/jaxb/JAXB2XMLStreamReader.java | 84 ++++++ .../databinding/jaxb/JAXBContextCacheTestCase.java | 123 ++++++++ .../databinding/jaxb/JAXBDataBindingTestCase.java | 130 +++++++++ .../databinding/jaxb/JAXBReflectionTestCase.java | 92 ++++++ .../tuscany/sca/databinding/jaxb/JAXBTestCase.java | 175 +++++++++++ .../jaxb/JAXBWrapperHandlerTestCase.java | 90 ++++++ .../tuscany/sca/databinding/jaxb/MyBean.java | 162 +++++++++++ .../tuscany/sca/databinding/jaxb/MyInterface.java | 29 ++ .../sca/databinding/jaxb/MyInterfaceImpl.java | 67 +++++ .../tuscany/sca/databinding/jaxb/MyJaxbBean.java | 64 ++++ .../tuscany/sca/databinding/jaxb/MySubBean.java | 35 +++ .../tuscany/sca/databinding/jaxb/POJOTestCase.java | 268 +++++++++++++++++ .../databinding-jaxb/src/test/resources/ipo.xsd | 136 +++++++++ 15 files changed, 1824 insertions(+) create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/com/example/stock/StockQuoteOffer.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCacheTestCase.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBindingTestCase.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBReflectionTestCase.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTestCase.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandlerTestCase.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyBean.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyInterface.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyInterfaceImpl.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyJaxbBean.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MySubBean.java create mode 100644 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/POJOTestCase.java create mode 100755 branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/resources/ipo.xsd (limited to 'branches/sca-java-1.5.1/modules/databinding-jaxb/src/test') diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/com/example/stock/StockQuoteOffer.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/com/example/stock/StockQuoteOffer.java new file mode 100644 index 0000000000..5c5c892f33 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/com/example/stock/StockQuoteOffer.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.example.stock; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + +/** + *

Java class for anonymous complex type. + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = {"input"}) +@XmlRootElement(name = "stockQuoteOffer") +public class StockQuoteOffer { + + protected String input; + + public String getInput() { + return this.input; + } + + public void setInput(String input) { + this.input = input; + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java new file mode 100644 index 0000000000..602bc66569 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java @@ -0,0 +1,324 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +import java.lang.reflect.Array; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; +import org.apache.tuscany.sca.databinding.xml.SimpleXmlNodeImpl; +import org.apache.tuscany.sca.databinding.xml.XmlNode; +import org.apache.tuscany.sca.databinding.xml.XmlTreeStreamReaderImpl; +import org.apache.tuscany.sca.interfacedef.util.TypeInfo; +import org.jvnet.jaxb.reflection.model.annotation.RuntimeInlineAnnotationReader; +import org.jvnet.jaxb.reflection.model.core.Ref; +import org.jvnet.jaxb.reflection.model.impl.RuntimeModelBuilder; +import org.jvnet.jaxb.reflection.model.runtime.RuntimeClassInfo; +import org.jvnet.jaxb.reflection.model.runtime.RuntimePropertyInfo; +import org.jvnet.jaxb.reflection.model.runtime.RuntimeTypeInfoSet; +import org.jvnet.jaxb.reflection.runtime.IllegalAnnotationsException; +import org.jvnet.jaxb.reflection.runtime.JAXBContextImpl; + + + +/** + * @version $Rev$ $Date$ + */ +public class BeanXMLStreamReaderImpl extends XmlTreeStreamReaderImpl { + private static final Comparator COMPARATOR = new Comparator() { + public int compare(Accessor o1, Accessor o2) { + return o1.getName().compareTo(o2.getName()); + } + }; + + private static final String XSI_PREFIX = "xsi"; + private static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"; + + private static XmlNode getXSIType(QName realType) { + QName xsiType = new QName(XSI_NS, "type", XSI_PREFIX); + String prefix = realType.getPrefix(); + String typeName = realType.getLocalPart(); + if (prefix != null && !prefix.equals("")) { + typeName = prefix + ":" + realType.getLocalPart(); + } + return new SimpleXmlNodeImpl(xsiType, XmlNode.Type.ATTRIBUTE); + } + + /** + * Represent a Map.Entry XML node + * @version $Rev$ $Date$ + */ + public static class MapEntryXmlNodeImpl extends SimpleXmlNodeImpl implements XmlNode { + private Map.Entry entry; + + public MapEntryXmlNodeImpl(Entry entry) { + super(new QName("", "entry"), entry); + this.entry = entry; + } + + @Override + public Iterator children() { + List nodes = new ArrayList(); + XmlNode key = new BeanXmlNodeImpl(new QName("", "key"), entry.getKey()); + XmlNode value = new BeanXmlNodeImpl(new QName("", "value"), entry.getValue()); + nodes.add(key); + nodes.add(value); + return nodes.iterator(); + } + } + + public static class BeanXmlNodeImpl extends SimpleXmlNodeImpl implements XmlNode { + private static final Object[] NULL = null; + private static final SimpleTypeMapperImpl MAPPER = new SimpleTypeMapperImpl(); + + public BeanXmlNodeImpl(Object bean) { + super(getName(bean == null ? null : bean.getClass()), bean); + } + + public BeanXmlNodeImpl(QName name, Object bean) { + super(name, bean); + } + + private static boolean isSimpleType(Class javaType) { + return SimpleTypeMapperImpl.getXMLType(javaType) != null; + } + + private static String getStringValue(Object o) { + if (o == null) { + return null; + } + TypeInfo info = SimpleTypeMapperImpl.getXMLType(o.getClass()); + if (info != null) { + return MAPPER.toXMLLiteral(info.getQName(), o, null); + } else { + return String.valueOf(o); + } + } + + @Override + public Iterator children() { + if (name == null) { + return null; + } + if (value == null) { + return super.children(); + } + if (isSimpleType(value.getClass())) { + XmlNode textNode = new BeanXmlNodeImpl(null, value); + return Arrays.asList(textNode).iterator(); + } + if (Map.class.isAssignableFrom(value.getClass())) { + List entries = new ArrayList(); + Map map = (Map)value; + if (map != null) { + for (Object e : map.entrySet()) { + Map.Entry entry = (Map.Entry)e; + entries.add(new MapEntryXmlNodeImpl(entry)); + } + } + return entries.iterator(); + } + try { + Map accessorMap = getAccessors(value); + List accessorList = new ArrayList(accessorMap.values()); + Collections.sort(accessorList, COMPARATOR); + + List props = new ArrayList(); + for (Accessor accessor : accessorList) { + Class pType = accessor.getType(); + + QName pName = new QName(name.getNamespaceURI(), accessor.getName()); + Object pValue = accessor.getValue(); + if (pType.isArray()) { + if (pValue != null) { + int i1 = Array.getLength(pValue); + for (int j = 0; j < i1; j++) { + Object o = Array.get(pValue, j); + props.add(new BeanXmlNodeImpl(pName, o)); + } + } else { + // TODO: How to handle null? + } + } else if (Collection.class.isAssignableFrom(pType)) { + Collection objList = (Collection)pValue; + if (objList != null && objList.size() > 0) { + for (Iterator j = objList.iterator(); j.hasNext();) { + Object o = j.next(); + props.add(new BeanXmlNodeImpl(pName, o)); + } + + } else { + // How to handle null + } + } else { + props.add(new BeanXmlNodeImpl(pName, pValue)); + } + } + return props.iterator(); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } + + @Override + public QName getName() { + return name; + } + + @Override + public String getValue() { + return getStringValue(value); + } + + private static String getPackageName(Class cls) { + String name = cls.getName(); + int index = name.lastIndexOf('.'); + return index == -1 ? "" : name.substring(0, index); + } + + public static QName getName(Class cls) { + if (cls == null) { + return null; + } + + String packageName = getPackageName(cls); + + if ("".equals(packageName)) { + return new QName("", cls.getSimpleName()); + } + StringBuffer ns = new StringBuffer("http://"); + String[] names = packageName.split("\\."); + for (int i = names.length - 1; i >= 0; i--) { + ns.append(names[i]); + if (i != 0) { + ns.append('.'); + } + } + ns.append('/'); + return new QName(ns.toString(), cls.getSimpleName()); + } + } + + public BeanXMLStreamReaderImpl(QName name, Object bean) { + super(getXmlNode(name, bean)); + } + + private static BeanXmlNodeImpl getXmlNode(QName name, Object bean) { + BeanXmlNodeImpl root = null; + if (name != null) { + root = new BeanXmlNodeImpl(name, bean); + } else { + root = new BeanXmlNodeImpl(bean); + } + return root; + } + + public static interface Accessor { + String getName(); + + Class getType(); + + Object getValue() throws Exception; + + void setValue(Object value) throws Exception; + } + + private static RuntimeTypeInfoSet create(Class... classes) throws Exception { + IllegalAnnotationsException.Builder errorListener = new IllegalAnnotationsException.Builder(); + RuntimeInlineAnnotationReader reader = new RuntimeInlineAnnotationReader(); + JAXBContextImpl context = + new JAXBContextImpl(classes, null, Collections. emptyMap(), null, false, reader, false, false); + RuntimeModelBuilder builder = + new RuntimeModelBuilder(context, reader, Collections. emptyMap(), null); + builder.setErrorHandler(errorListener); + for (Class c : classes) + builder.getTypeInfo(new Ref(c)); + + RuntimeTypeInfoSet r = builder.link(); + errorListener.check(); + return r; + } + + private static class JAXBAccessor implements Accessor { + private Object target; + private RuntimePropertyInfo prop; + + public JAXBAccessor(Object target, RuntimePropertyInfo field) { + super(); + this.target = target; + this.prop = field; + } + + public String getName() { + return prop.getName(); + } + + public Object getValue() throws Exception { + return prop.getAccessor().get(target); + } + + public void setValue(Object value) throws Exception { + prop.getAccessor().set(target, value); + } + + public Class getType() { + Type type = prop.getRawType(); + if (type instanceof Class) { + return (Class)type; + } else if (type instanceof ParameterizedType) { + ParameterizedType pType = (ParameterizedType)type; + type = pType.getRawType(); + if (type instanceof Class) { + return (Class)type; + } + } + return Object.class; + } + + } + + private static Map getAccessors(Object target) throws Exception { + if (target == null) { + return Collections.emptyMap(); + } + Map map = new HashMap(); + Class type = target.getClass(); + RuntimeTypeInfoSet set = create(type); + RuntimeClassInfo clsInfo = (RuntimeClassInfo)set.getTypeInfo(type); + for (RuntimePropertyInfo f : clsInfo.getProperties()) { + map.put(f.getName(), new JAXBAccessor(target, f)); + } + return map; + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java new file mode 100644 index 0000000000..e2151e23a1 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.databinding.jaxb; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.databinding.impl.BaseTransformer; + +/** + * + * @version $Rev$ $Date$ + */ +public class JAXB2XMLStreamReader extends BaseTransformer implements + PullTransformer { + + public JAXB2XMLStreamReader() { + super(); + } + + public XMLStreamReader transform(Object source, TransformationContext context) { + if (source == null) { + return null; + } + try { + QName name = null; + Object bean = null; + if (source instanceof JAXBElement) { + bean = ((JAXBElement)source).getValue(); + name = ((JAXBElement)source).getName(); + } else { + JAXBContext jaxbContext = JAXBContextHelper.createJAXBContext(context, true); + Object element = JAXBContextHelper.createJAXBElement(jaxbContext, context.getSourceDataType(), source); + name = jaxbContext.createJAXBIntrospector().getElementName(element); + bean = source; + } + BeanXMLStreamReaderImpl reader = new BeanXMLStreamReaderImpl(name, bean); + return reader; + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return Object.class; + } + + @Override + protected Class getTargetType() { + return XMLStreamReader.class; + } + + @Override + public int getWeight() { + return 10; + } + + @Override + public String getTargetDataBinding() { + return JAXBDataBinding.NAME; + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCacheTestCase.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCacheTestCase.java new file mode 100644 index 0000000000..761414e92b --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCacheTestCase.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.Assert; + +import org.apache.tuscany.sca.databinding.jaxb.JAXBContextCache; +import org.apache.tuscany.sca.databinding.util.LRUCache; +import org.junit.Test; + +import com.example.ipo.jaxb.Address; +import com.example.ipo.jaxb.PurchaseOrderType; + +/** + * @version $Rev$ $Date$ + */ +public class JAXBContextCacheTestCase { + @Test + public void testCache() throws JAXBException { + JAXBContextCache cache = new JAXBContextCache(); + JAXBContext context1 = cache.getJAXBContext(String.class); + JAXBContext context2 = cache.getJAXBContext(int.class); + JAXBContext context3 = cache.getJAXBContext(String[].class); + JAXBContext context4 = cache.getJAXBContext(Source.class); + Assert.assertSame(context1, context2); + Assert.assertNotSame(context2, context3); + Assert.assertSame(context1, context4); + + QName name = new QName("http://example.com/ns1", "e1"); + JAXBElement element = new JAXBElement(name, String.class, "123"); + StringWriter sw = new StringWriter(); + context4.createMarshaller().marshal(element, sw); + StreamSource source = new StreamSource(new StringReader(sw.toString()), null); + context4.createUnmarshaller().unmarshal(source, String.class); + + JAXBContext context5 = cache.getJAXBContext(Address.class); + JAXBContext context6 = cache.getJAXBContext(PurchaseOrderType.class); + Assert.assertSame(context5, context6); + } + + @Test + public void testLRUCache() { + LRUCache cache = new LRUCache(3); + cache.put("1", "A"); + Assert.assertEquals(1, cache.size()); + cache.put("2", "B"); + Assert.assertEquals(2, cache.size()); + cache.put("3", "C"); + Assert.assertEquals(3, cache.size()); + cache.put("4", "D"); + Assert.assertEquals(3, cache.size()); + String data = cache.get("1"); + Assert.assertNull(data); + data = cache.get("2"); + Assert.assertEquals("B", data); + cache.put("5", "E"); + data = cache.get("2"); + Assert.assertEquals("B", data); + } + + @Test + public void testPerf() throws JAXBException { + JAXBContextCache cache = new JAXBContextCache(); + + // Test JAXBContext for simple java classes + long start = System.currentTimeMillis(); + for (int i = 0; i < 100; i++) { + JAXBContext context = JAXBContext.newInstance(String.class); + } + long end = System.currentTimeMillis(); + long d1 = end - start; + start = System.currentTimeMillis(); + for (int i = 0; i < 100; i++) { + JAXBContext context = cache.getJAXBContext(String.class); + } + end = System.currentTimeMillis(); + long d2 = end - start; + System.out.println(d1 + "ms vs. " + d2 + "ms"); + + // Test JAXBContext for generated JAXB classes + start = System.currentTimeMillis(); + for (int i = 0; i < 20; i++) { + JAXBContext context = JAXBContext.newInstance(PurchaseOrderType.class); + } + end = System.currentTimeMillis(); + d1 = end - start; + start = System.currentTimeMillis(); + for (int i = 0; i < 20; i++) { + JAXBContext context = cache.getJAXBContext(PurchaseOrderType.class); + } + end = System.currentTimeMillis(); + d2 = end - start; + System.out.println(d1 + "ms vs. " + d2 + "ms"); + + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBindingTestCase.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBindingTestCase.java new file mode 100644 index 0000000000..5452175c54 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBindingTestCase.java @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +import javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.databinding.jaxb.JAXBDataBinding; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.util.XMLType; + +import com.example.ipo.jaxb.ObjectFactory; +import com.example.ipo.jaxb.PurchaseOrderType; +import com.example.ipo.jaxb.USAddress; +import com.example.ipo.jaxb.USState; + +/** + * + * @version $Rev$ $Date$ + */ +public class JAXBDataBindingTestCase extends TestCase { + private JAXBDataBinding binding; + + /** + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + binding = new JAXBDataBinding(); + } + + /** + * Test method for + * {@link org.apache.tuscany.sca.databinding.jaxb.JAXBDataBinding#introspect(java.lang.Class, Operation)}. + */ + public final void testIntrospect() { + DataType dataType = new DataTypeImpl(JAXBElement.class, null); + Operation op = null; + boolean yes = binding.introspect(dataType, op); + assertTrue(yes); + assertTrue(dataType.getDataBinding().equals(binding.getName())); + assertTrue(dataType.getPhysical() == JAXBElement.class && dataType.getLogical() == XMLType.UNKNOWN); + dataType = new DataTypeImpl(MockJAXBElement.class, null); + yes = binding.introspect(dataType, op); + assertTrue(yes); + assertEquals(MockJAXBElement.class, dataType.getPhysical()); + assertEquals(new QName("http://www.example.com/IPO", "PurchaseOrderType"), ((XMLType)dataType.getLogical()) + .getTypeName()); + dataType = new DataTypeImpl(USAddress.class, null); + yes = binding.introspect(dataType, op); + assertTrue(yes); + assertEquals(USAddress.class, dataType.getPhysical()); + assertEquals(new QName("http://www.example.com/IPO", "USAddress"), ((XMLType)dataType.getLogical()) + .getTypeName()); + dataType = new DataTypeImpl(USState.class, null); + yes = binding.introspect(dataType, op); + assertTrue(yes); + assertTrue(dataType.getDataBinding().equals(binding.getName())); + assertEquals(USState.class, dataType.getPhysical()); + assertEquals(new QName("http://www.example.com/IPO", "USState"), ((XMLType)dataType.getLogical()).getTypeName()); + + } + + private static class MockJAXBElement extends JAXBElement { + + private static final long serialVersionUID = -2767569071002707973L; + + /** + * @param elementName + * @param type + * @param value + */ + public MockJAXBElement(QName elementName, Class type, PurchaseOrderType value) { + super(elementName, type, value); + } + + } + + @SuppressWarnings("unchecked") + public void testCopy() { + ObjectFactory factory = new ObjectFactory(); + PurchaseOrderType poType = factory.createPurchaseOrderType(); + JAXBElement po = factory.createPurchaseOrder(poType); + JAXBElement copy = (JAXBElement)binding.copy(po, null, null); + assertEquals(new QName("http://www.example.com/IPO", "purchaseOrder"), copy.getName()); + } + + @SuppressWarnings("unchecked") + public void testCopyNonElement() { + ObjectFactory factory = new ObjectFactory(); + PurchaseOrderType poType = factory.createPurchaseOrderType(); + poType.setComment("Comment"); + PurchaseOrderType copy = (PurchaseOrderType)binding.copy(poType, null, null); + assertTrue(copy instanceof PurchaseOrderType); + assertEquals("Comment", (copy).getComment()); + } + + @SuppressWarnings("unchecked") + public void testCopyNonRoot() { + ObjectFactory factory = new ObjectFactory(); + USAddress address = factory.createUSAddress(); + address.setCity("San Jose"); + USAddress copy = (USAddress)binding.copy(address, null, null); + assertTrue(copy instanceof USAddress); + assertEquals("San Jose", (copy).getCity()); + + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBReflectionTestCase.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBReflectionTestCase.java new file mode 100644 index 0000000000..500422c9c7 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBReflectionTestCase.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.Map; + +import javax.xml.bind.JAXBContext; +import javax.xml.namespace.QName; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.jvnet.jaxb.reflection.model.annotation.RuntimeInlineAnnotationReader; +import org.jvnet.jaxb.reflection.model.core.Ref; +import org.jvnet.jaxb.reflection.model.impl.RuntimeModelBuilder; +import org.jvnet.jaxb.reflection.model.runtime.RuntimeClassInfo; +import org.jvnet.jaxb.reflection.model.runtime.RuntimePropertyInfo; +import org.jvnet.jaxb.reflection.model.runtime.RuntimeTypeInfoSet; +import org.jvnet.jaxb.reflection.runtime.IllegalAnnotationsException; +import org.jvnet.jaxb.reflection.runtime.JAXBContextImpl; + +import com.example.ipo.jaxb.ObjectFactory; +import com.example.ipo.jaxb.PurchaseOrderType; +import com.example.ipo.jaxb.USAddress; +import com.example.ipo.jaxb.USState; + +/** + * @version $Rev$ $Date$ + */ +public class JAXBReflectionTestCase extends TestCase { + + public void testGenerateSchema() throws Exception { + JAXBContext context = JAXBContext.newInstance("com.example.ipo.jaxb"); + Map schemas = JAXBTypeHelper.generateSchema(context); + System.out.println(schemas); + } + + /** + * This is a workaround for the NPE bug in jaxb-reflection + * @param classes + * @return + * @throws Exception + */ + @SuppressWarnings("unchecked") + private static RuntimeTypeInfoSet create(Class... classes) throws Exception { + IllegalAnnotationsException.Builder errorListener = new IllegalAnnotationsException.Builder(); + RuntimeInlineAnnotationReader reader = new RuntimeInlineAnnotationReader(); + JAXBContextImpl context = + new JAXBContextImpl(classes, null, Collections. emptyMap(), null, false, reader, false, false); + RuntimeModelBuilder builder = + new RuntimeModelBuilder(context, reader, Collections. emptyMap(), null); + builder.setErrorHandler(errorListener); + for (Class c : classes) + builder.getTypeInfo(new Ref(c)); + + RuntimeTypeInfoSet r = builder.link(); + errorListener.check(); + return r; + } + + public void testReflection() throws Exception { + org.jvnet.jaxb.reflection.model.runtime.RuntimeTypeInfoSet model = create(PurchaseOrderType.class); + RuntimeClassInfo type = (RuntimeClassInfo)model.getTypeInfo(PurchaseOrderType.class); + Assert.assertEquals(new QName("http://www.example.com/IPO", "PurchaseOrderType"), type.getTypeName()); + type = (RuntimeClassInfo)model.getTypeInfo(USAddress.class); + Assert.assertEquals(new QName("http://www.example.com/IPO", "USAddress"), type.getTypeName()); + RuntimePropertyInfo prop = type.getProperty("state"); + Assert.assertNotNull(prop); + USAddress address = new ObjectFactory().createUSAddress(); + prop.getAccessor().set(address, USState.CA); + Assert.assertEquals(USState.CA, address.getState()); + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTestCase.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTestCase.java new file mode 100644 index 0000000000..53779a1af6 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTestCase.java @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.databinding.jaxb; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; + +import java.io.StringReader; + +import javax.xml.namespace.QName; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.jaxb.JAXB2Node; +import org.apache.tuscany.sca.databinding.jaxb.Node2JAXB; +import org.apache.tuscany.sca.databinding.jaxb.Reader2JAXB; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.util.XMLType; +import org.w3c.dom.Node; + +import com.example.ipo.jaxb.ObjectFactory; +import com.example.ipo.jaxb.PurchaseOrderType; + +/** + * + * @version $Rev$ $Date$ + */ +public class JAXBTestCase extends TestCase { + private static final String IPO_XML = + "" + "" + + " " + + " Helen Zoe" + + " 47 Eden Street" + + " Cambridge" + + " CB1 1JR" + + " " + + " " + + " Robert Smith" + + " 8 Oak Avenue" + + " Old Town" + + " PA" + + " 95819" + + " " + + " " + + " " + + " Lapis necklace" + + " 1" + + " 99.95" + + " Want this for the holidays" + + " 1999-12-05" + + " " + + " " + + ""; + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + public void testTransform() throws Exception { + Reader2JAXB t0 = new Reader2JAXB(); + + DataType targetDataType = new DataTypeImpl(PurchaseOrderType.class, null); + + TransformationContext tContext = createMock(TransformationContext.class); + expect(tContext.getTargetDataType()).andReturn(targetDataType).anyTimes(); + expect(tContext.getTargetOperation()).andReturn(null).anyTimes(); + replay(tContext); + + Object object1 = t0.transform(new StringReader(IPO_XML), tContext); + + DataType sourceDataType = new DataTypeImpl(PurchaseOrderType.class, null); + + TransformationContext tContext1 = createMock(TransformationContext.class); + expect(tContext1.getSourceDataType()).andReturn(sourceDataType).anyTimes(); + expect(tContext1.getTargetDataType()).andReturn(null).anyTimes(); + expect(tContext1.getSourceOperation()).andReturn(null).anyTimes(); + expect(tContext1.getTargetOperation()).andReturn(null).anyTimes(); + replay(tContext1); + + JAXB2Node t1 = new JAXB2Node(); + Node node = t1.transform(object1, tContext1); + + Assert.assertNotNull(node); + + Node2JAXB t2 = new Node2JAXB(); + Object object2 = t2.transform(node, tContext); + Assert.assertNotNull(object2); + + } + + public void testTransform2() throws Exception { + Reader2JAXB t0 = new Reader2JAXB(); + + QName root = new QName("http://www.example.com/IPO", "purchaseOrder"); + DataType targetDataType = new DataTypeImpl(PurchaseOrderType.class, new XMLType(root, null)); + // targetDataType.setMetadata(JAXBContextHelper.JAXB_CONTEXT_PATH, contextPath); + + TransformationContext tContext = createMock(TransformationContext.class); + expect(tContext.getTargetDataType()).andReturn(targetDataType).anyTimes(); + expect(tContext.getTargetOperation()).andReturn(null).anyTimes(); + replay(tContext); + + Object object1 = t0.transform(new StringReader(IPO_XML), tContext); + + DataType sourceDataType = new DataTypeImpl(PurchaseOrderType.class, new XMLType(root, null)); + // sourceDataType.setMetadata(JAXBContextHelper.JAXB_CONTEXT_PATH, contextPath); + + TransformationContext tContext1 = createMock(TransformationContext.class); + expect(tContext1.getSourceDataType()).andReturn(sourceDataType).anyTimes(); + expect(tContext1.getTargetDataType()).andReturn(null).anyTimes(); + expect(tContext1.getSourceOperation()).andReturn(null).anyTimes(); + expect(tContext1.getTargetOperation()).andReturn(null).anyTimes(); + replay(tContext1); + + JAXB2Node t1 = new JAXB2Node(); + Node node = t1.transform(object1, tContext1); + + Assert.assertNotNull(node); + + Node2JAXB t2 = new Node2JAXB(); + Object object2 = t2.transform(node, tContext); + Assert.assertNotNull(object2); + + } + + public void testTransform3() throws Exception { + + DataType sourceDataType = new DataTypeImpl(PurchaseOrderType.class, null); + + TransformationContext tContext1 = createMock(TransformationContext.class); + expect(tContext1.getSourceDataType()).andReturn(sourceDataType).anyTimes(); + expect(tContext1.getTargetDataType()).andReturn(null).anyTimes(); + expect(tContext1.getSourceOperation()).andReturn(null).anyTimes(); + expect(tContext1.getTargetOperation()).andReturn(null).anyTimes(); + replay(tContext1); + + JAXB2Node t1 = new JAXB2Node(); + PurchaseOrderType po = new ObjectFactory().createPurchaseOrderType(); + Node node = t1.transform(po, tContext1); + + Assert.assertNotNull(node); + + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandlerTestCase.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandlerTestCase.java new file mode 100644 index 0000000000..501f5b21b7 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandlerTestCase.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.sca.databinding.jaxb.JAXBDataBinding; +import org.apache.tuscany.sca.databinding.jaxb.JAXBWrapperHandler; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.impl.OperationImpl; +import org.apache.tuscany.sca.interfacedef.util.ElementInfo; +import org.apache.tuscany.sca.interfacedef.util.WrapperInfo; +import org.apache.tuscany.sca.interfacedef.util.XMLType; + +import com.example.stock.StockQuoteOffer; + +/** + * Test case for JAXBExceptionHandler + * + * @version $Rev$ $Date$ + */ +public class JAXBWrapperHandlerTestCase extends TestCase { + private static final QName ELEMENT = new QName("http://www.example.com/stock", "stockQuoteOffer"); + private static final QName INPUT = new QName("", "input"); + private JAXBWrapperHandler handler; + + /** + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + this.handler = new JAXBWrapperHandler(); + } + + public void testCreate() { + ElementInfo element = new ElementInfo(ELEMENT, null); + Operation op = new OperationImpl(); + WrapperInfo wrapperInfo = new WrapperInfo(JAXBDataBinding.NAME, element, null); + wrapperInfo.setWrapperType(new DataTypeImpl(JAXBDataBinding.NAME, StockQuoteOffer.class, + XMLType.UNKNOWN)); + op.setInputWrapper(wrapperInfo); + Object offer = handler.create(op, true); + Assert.assertTrue(offer instanceof StockQuoteOffer); + } + + public void testSetChild() { + StockQuoteOffer wrapper = new StockQuoteOffer(); + handler.setChild(wrapper, 0, new ElementInfo(INPUT, null), "IBM"); + Assert.assertEquals("IBM", wrapper.getInput()); + } + + public void testGetChildren() { + StockQuoteOffer wrapper = new StockQuoteOffer(); + wrapper.setInput("IBM"); + List elements = new ArrayList(); + elements.add(new ElementInfo(INPUT, null)); + WrapperInfo wrapperInfo = new WrapperInfo(JAXBDataBinding.NAME, null, elements); + Operation op = new OperationImpl(); + op.setInputWrapper(wrapperInfo); + List children = handler.getChildren(wrapper, op, true); + assertNotNull(children); + assertEquals(1, children.size()); + assertEquals("IBM", children.get(0)); + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyBean.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyBean.java new file mode 100644 index 0000000000..9bdfb108fe --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyBean.java @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.databinding.jaxb; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * + * @version $Rev$ $Date$ + */ +public class MyBean { + private int age; + private String name; + private float[] rates = new float[] {1.0f, 2.0f}; + private List notes = new ArrayList(); + private Map map = new HashMap(); + private Object service; + private Object otherService; + private boolean good; + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getNotes() { + return notes; + } + + public void setNotes(List notes) { + this.notes = notes; + } + + public float[] getRates() { + return rates; + } + + public void setRates(float[] rates) { + this.rates = rates; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } + + public Object getService() { + return service; + } + + public void setService(Object service) { + this.service = service; + } + + public Object getOtherService() { + return otherService; + } + + public void setOtherService(Object otherService) { + this.otherService = otherService; + } + + public boolean isGood() { + return good; + } + + public void setGood(boolean good) { + this.good = good; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + age; + result = prime * result + (good ? 1231 : 1237); + result = prime * result + ((map == null) ? 0 : map.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((notes == null) ? 0 : notes.hashCode()); + result = prime * result + ((otherService == null) ? 0 : otherService.hashCode()); + result = prime * result + Arrays.hashCode(rates); + result = prime * result + ((service == null) ? 0 : service.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final MyBean other = (MyBean)obj; + if (age != other.age) + return false; + if (good != other.good) + return false; + if (map == null) { + if (other.map != null) + return false; + } else if (!map.equals(other.map)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (notes == null) { + if (other.notes != null) + return false; + } else if (!notes.equals(other.notes)) + return false; + if (otherService == null) { + if (other.otherService != null) + return false; + } else if (!otherService.equals(other.otherService)) + return false; + if (!Arrays.equals(rates, other.rates)) + return false; + if (service == null) { + if (other.service != null) + return false; + } else if (!service.equals(other.service)) + return false; + return true; + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyInterface.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyInterface.java new file mode 100644 index 0000000000..b8e9ee7f7d --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyInterface.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +/** + * @version $Rev$ $Date$ + */ +public interface MyInterface { + void setId(String id); + + String getId(); +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyInterfaceImpl.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyInterfaceImpl.java new file mode 100644 index 0000000000..5c511e6ccb --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyInterfaceImpl.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +/** + * @version $Rev$ $Date$ + */ +public class MyInterfaceImpl implements MyInterface { + private String id; + + /** + * @see org.apache.tuscany.databinding.jaxb.MyInterface#getId() + */ + public String getId() { + return id; + } + + /** + * @see org.apache.tuscany.databinding.jaxb.MyInterface#setId(java.lang.String) + */ + public void setId(String id) { + this.id = id; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final MyInterfaceImpl other = (MyInterfaceImpl)obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyJaxbBean.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyJaxbBean.java new file mode 100644 index 0000000000..3c3992524a --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyJaxbBean.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +import javax.xml.bind.annotation.XmlAnyElement; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import org.w3c.dom.Element; + +/** + * + */ +@XmlRootElement(name = "myBean", namespace = "http://ns1") +public class MyJaxbBean { + public MyBean myBean; + + @XmlJavaTypeAdapter(AnyTypeXmlAdapter.class) + public MyInterface myInterface; + + @XmlElement(type = MyInterfaceImpl.class) + public MyInterface myInterface1; + + @XmlJavaTypeAdapter(MyInterfaceAdapter.class) + public MyInterface myInterface2; + + public Object myObject; + + @XmlAnyElement + public Element anyElement; + + public static class MyInterfaceAdapter extends XmlAdapter { + + @Override + public MyInterfaceImpl marshal(MyInterface v) throws Exception { + return (MyInterfaceImpl) v; + } + + @Override + public MyInterface unmarshal(MyInterfaceImpl v) throws Exception { + return (MyInterface) v; + } + + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MySubBean.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MySubBean.java new file mode 100644 index 0000000000..62dfa6f73c --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MySubBean.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb; + +/** + * @version $Rev$ $Date$ + */ +public class MySubBean extends MyBean { + private String addtional; + + public String getAddtional() { + return addtional; + } + + public void setAddtional(String addtional) { + this.addtional = addtional; + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/POJOTestCase.java b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/POJOTestCase.java new file mode 100644 index 0000000000..fdfa87ff43 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/POJOTestCase.java @@ -0,0 +1,268 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.databinding.jaxb; + +import java.io.StringReader; +import java.io.StringWriter; +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.impl.TransformationContextImpl; + +import com.sun.xml.bind.v2.model.annotation.RuntimeInlineAnnotationReader; +import com.sun.xml.bind.v2.model.core.Ref; +import com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder; +import com.sun.xml.bind.v2.model.runtime.RuntimeClassInfo; +import com.sun.xml.bind.v2.model.runtime.RuntimePropertyInfo; +import com.sun.xml.bind.v2.model.runtime.RuntimeTypeInfoSet; +import com.sun.xml.bind.v2.runtime.IllegalAnnotationsException; +import com.sun.xml.bind.v2.runtime.JAXBContextImpl; + +/** + * + * @version $Rev$ $Date$ + */ +public class POJOTestCase extends TestCase { + public void testAdapter() throws Exception { + JAXBContext context = JAXBContext.newInstance(MyJaxbBean.class, MyInterfaceImpl.class); + StringWriter writer = new StringWriter(); + MyJaxbBean bean = new MyJaxbBean(); + bean.myBean = new MySubBean(); + bean.myBean.setName("Ray"); + bean.myInterface = new MyInterfaceImpl(); + bean.myInterface.setId("001"); + bean.myObject = new MyBean(); + ((MyBean) bean.myObject).setName("Y"); + context.createMarshaller().marshal(bean, writer); + System.out.println(writer.toString()); + Object result = context.createUnmarshaller().unmarshal(new StringReader(writer.toString())); + assertTrue(result instanceof MyJaxbBean); + Map schemas = JAXBTypeHelper.generateSchema(context); + System.out.println(schemas); + } + + public void testPOJO() throws Exception { + JAXBContext context = JAXBContext.newInstance(MyBean.class, MyInterfaceImpl.class); + StringWriter writer = new StringWriter(); + MyBean bean = new MyBean(); + bean.setName("Test"); + bean.setAge(20); + bean.getNotes().add("1"); + bean.getNotes().add("2"); + bean.getMap().put("1", 1); + MyInterface service = new MyInterfaceImpl(); + service.setId("ID001"); + bean.setService(service); + bean.setOtherService(service); + JAXBElement element = new JAXBElement(new QName("http://ns1", "bean"), Object.class, bean); + context.createMarshaller().marshal(element, writer); + // System.out.println(writer.toString()); + + Object result = context.createUnmarshaller().unmarshal(new StringReader(writer.toString())); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertTrue(e2.getValue() instanceof MyBean); + MyBean newBean = (MyBean)e2.getValue(); + assertEquals(bean, newBean); + } + + public void testPOJOArray() throws Exception { + JAXBContext context = JAXBContext.newInstance(MyBean[].class, MySubBean.class); + StringWriter writer = new StringWriter(); + MySubBean bean = new MySubBean(); + bean.setAddtional("SUB"); + bean.setName("Test"); + bean.setAge(20); + bean.getNotes().add("1"); + bean.getNotes().add("2"); + bean.getMap().put("1", 1); + + JAXBElement element = + new JAXBElement(new QName("http://ns1", "beans"), Object.class, new MyBean[] {bean}); + context.createMarshaller().marshal(element, writer); + System.out.println(writer.toString()); + + Object result = + context.createUnmarshaller().unmarshal(new StreamSource(new StringReader(writer.toString())), + MyBean[].class); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertTrue(e2.getValue() instanceof MyBean[]); + MyBean newBean = ((MyBean[])e2.getValue())[0]; + assertTrue(newBean instanceof MySubBean); + } + + public void testXMLStreamReader() throws Exception { + JAXBContext context = JAXBContext.newInstance(MyBean.class, MyInterfaceImpl.class); + + MyBean bean = new MyBean(); + bean.setName("Test"); + bean.setAge(20); + bean.getNotes().add("1"); + bean.getNotes().add("2"); + bean.getMap().put("1", 1); + MyInterface service = new MyInterfaceImpl(); + service.setId("ID001"); + bean.setService(service); + bean.setOtherService(service); + QName name = new QName("http://ns1", "bean"); + JAXBElement element = new JAXBElement(name, Object.class, bean); + TransformationContext tContext = new TransformationContextImpl(); + XMLStreamReader reader = new JAXB2XMLStreamReader().transform(element, tContext); + +// XMLStreamReader2String t2 = new XMLStreamReader2String(); +// String xml = t2.transform(reader, null); + // System.out.println(xml); + Object result = context.createUnmarshaller().unmarshal(reader, MyBean.class); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertTrue(e2.getValue() instanceof MyBean); + MyBean newBean = (MyBean)e2.getValue(); + // FIXME :To be implemented + // assertEquals(bean, newBean); + } + + public void testString() throws Exception { + JAXBContext context = JAXBContext.newInstance(String.class); + StringWriter writer = new StringWriter(); + JAXBElement element = new JAXBElement(new QName("http://ns1", "bean"), Object.class, "ABC"); + context.createMarshaller().marshal(element, writer); + // System.out.println(writer.toString()); + + Object result = context.createUnmarshaller().unmarshal(new StringReader(writer.toString())); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertEquals("ABC", e2.getValue()); + } + + public void testNull() throws Exception { + JAXBContext context = JAXBContext.newInstance(String.class); + StringWriter writer = new StringWriter(); + JAXBElement element = new JAXBElement(new QName("http://ns1", "bean"), Object.class, null); + element.setNil(true); + context.createMarshaller().marshal(element, writer); + // System.out.println(writer.toString()); + StreamSource source = new StreamSource(new StringReader(writer.toString())); + Object result = context.createUnmarshaller().unmarshal(source, String.class); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertNull(e2.getValue()); + } + + public void testArray() throws Exception { + JAXBContext context = JAXBContext.newInstance(String[].class); + StringWriter writer = new StringWriter(); + JAXBElement element = + new JAXBElement(new QName("http://ns1", "bean"), Object.class, new String[] {"ABC", "123"}); + context.createMarshaller().marshal(element, writer); + // System.out.println(writer.toString()); + + Object result = context.createUnmarshaller().unmarshal(new StringReader(writer.toString())); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertTrue(e2.getValue() instanceof String[]); + } + + public void testByteArray() throws Exception { + JAXBContext context = JAXBContext.newInstance(byte[].class); + StringWriter writer = new StringWriter(); + JAXBElement element = + new JAXBElement(new QName("http://ns1", "bean"), Object.class, "ABC".getBytes()); + context.createMarshaller().marshal(element, writer); + String xml = writer.toString(); + assertTrue(xml.contains("QUJD")); + assertTrue(xml.contains("base64Binary")); + + Object result = context.createUnmarshaller().unmarshal(new StringReader(xml)); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertTrue(e2.getValue() instanceof byte[]); + } + + public void testPrimitive() throws Exception { + JAXBContext context = JAXBContext.newInstance(int.class); + StringWriter writer = new StringWriter(); + JAXBElement element = new JAXBElement(new QName("http://ns1", "bean"), Object.class, 1); + context.createMarshaller().marshal(element, writer); + // System.out.println(writer.toString()); + + Object result = context.createUnmarshaller().unmarshal(new StringReader(writer.toString())); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertEquals(1, e2.getValue()); + } + + /* + public void testException() throws Exception { + JAXBContext context = JAXBContext.newInstance(IllegalArgumentException.class); + StringWriter writer = new StringWriter(); + Exception e = new IllegalArgumentException("123"); + JAXBElement element = new JAXBElement(new QName("http://ns1", "bean"), Object.class, e); + context.createMarshaller().marshal(element, writer); + System.out.println(writer.toString()); + + Object result = context.createUnmarshaller().unmarshal(new StringReader(writer.toString())); + assertTrue(result instanceof JAXBElement); + JAXBElement e2 = (JAXBElement)result; + assertTrue(e2.getValue() instanceof Exception); + } + */ + + private static RuntimeTypeInfoSet create(Class... classes) throws Exception { + IllegalAnnotationsException.Builder errorListener = new IllegalAnnotationsException.Builder(); + RuntimeInlineAnnotationReader reader = new RuntimeInlineAnnotationReader(); + JAXBContextImpl context = + new JAXBContextImpl(classes, null, Collections. emptyMap(), null, false, reader, false, false); + RuntimeModelBuilder builder = + new RuntimeModelBuilder(context, reader, Collections. emptyMap(), null); + builder.setErrorHandler(errorListener); + for (Class c : classes) + builder.getTypeInfo(new Ref(c)); + + RuntimeTypeInfoSet r = builder.link(); + errorListener.check(); + return r; + } + + public void testReflection() throws Exception { + MyBean bean = new MyBean(); + RuntimeTypeInfoSet model = create(MyBean.class); + RuntimeClassInfo clsInfo = (RuntimeClassInfo)model.getTypeInfo(MyBean.class); + for (RuntimePropertyInfo p : clsInfo.getProperties()) { + // System.out.print(p.getName()); + // System.out.println(" " + p.isCollection()); + if (p.getName().equals("notes")) { + Collection c = (Collection)p.getAccessor().get(bean); + c.add("123"); + } + } + + } +} diff --git a/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/resources/ipo.xsd b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/resources/ipo.xsd new file mode 100755 index 0000000000..241ec15d36 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-jaxb/src/test/resources/ipo.xsd @@ -0,0 +1,136 @@ + + + + + + International Purchase order schema for Example.com + Copyright 2000 Example.com. All rights reserved. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3