diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:07:53 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:07:53 +0000 |
commit | 89d72264d0af4c65c9bf0b7a6f823f13d69162aa (patch) | |
tree | f6a4ea43b576d56a43adf10ccbed4cd75a9eb827 /branches/sca-java-1.3/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/POJOTestCase.java | |
parent | 6a3ca6736c808c1647854e405ef379b609f276db (diff) |
Moving 1.x branches
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835127 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-java-1.3/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/POJOTestCase.java')
-rw-r--r-- | branches/sca-java-1.3/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/POJOTestCase.java | 250 |
1 files changed, 0 insertions, 250 deletions
diff --git a/branches/sca-java-1.3/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/POJOTestCase.java b/branches/sca-java-1.3/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/POJOTestCase.java deleted file mode 100644 index 2735c650ee..0000000000 --- a/branches/sca-java-1.3/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/POJOTestCase.java +++ /dev/null @@ -1,250 +0,0 @@ -/* - * 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.databinding.jaxb; - -import java.io.StringReader; -import java.io.StringWriter; -import java.lang.reflect.Type; -import java.util.Collection; -import java.util.Collections; - -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 org.apache.tuscany.sca.databinding.jaxb.JAXB2XMLStreamReader; - -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 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<Object> element = new JAXBElement<Object>(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); - 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<Object> element = - new JAXBElement<Object>(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]; - assertFalse(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<Object> element = new JAXBElement<Object>(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<Object> element = new JAXBElement<Object>(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<Object> element = new JAXBElement<Object>(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<Object> element = - new JAXBElement<Object>(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<Object> element = - new JAXBElement<Object>(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<Object> element = new JAXBElement<Object>(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<Object> element = new JAXBElement<Object>(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.<Class, Class> emptyMap(), null, false, reader, false, false); - RuntimeModelBuilder builder = - new RuntimeModelBuilder(context, reader, Collections.<Class, Class> emptyMap(), null); - builder.setErrorHandler(errorListener); - for (Class c : classes) - builder.getTypeInfo(new Ref<Type, Class>(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"); - } - } - - } -} |