1 17 package org.apache.ws.jaxme.junit; 18 19 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 22 import javax.xml.bind.JAXBContext; 23 import javax.xml.bind.JAXBException; 24 import javax.xml.bind.Marshaller; 25 import javax.xml.bind.Unmarshaller; 26 27 import org.apache.ws.jaxme.JMElement; 28 import org.apache.ws.jaxme.JMUnmarshaller; 29 import org.apache.ws.jaxme.impl.JAXBContextImpl; 30 import org.apache.ws.jaxme.impl.JMMarshallerImpl; 31 import org.apache.ws.jaxme.impl.JMSAXDriver; 32 import org.apache.ws.jaxme.test.misc.enumeration.AllSimpleTypes; 33 import org.apache.ws.jaxme.test.misc.enumeration.AllTypesElement; 34 import org.apache.ws.jaxme.test.misc.enumeration.MyDoubleTypeClass; 35 import org.apache.ws.jaxme.test.misc.enumeration.MyFloatTypeClass; 36 import org.apache.ws.jaxme.test.misc.enumeration.MyIntTypeClass; 37 import org.apache.ws.jaxme.test.misc.enumeration.MyLongTypeClass; 38 import org.apache.ws.jaxme.test.misc.enumeration.MyShortTypeClass; 39 import org.apache.ws.jaxme.test.misc.enumeration.MyStringTypeClass; 40 import org.apache.ws.jaxme.test.misc.enumeration.impl.AllSimpleTypesImpl; 41 import org.apache.ws.jaxme.test.misc.enumeration.impl.AllTypesElementImpl; 42 import org.apache.ws.jaxme.test.misc.enumeration.impl.AllTypesElementTypeDriver; 43 import org.xml.sax.InputSource ; 44 45 46 48 public class EnumerationTest extends BaseTestCase { 49 private JAXBContextImpl factory; 50 51 53 public EnumerationTest(String arg) { super(arg); } 54 55 public void setUp() throws JAXBException { 56 factory = (JAXBContextImpl) JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.enumeration"); 57 } 58 59 private JAXBContextImpl getFactory() { 60 return factory; 61 } 62 63 private AllSimpleTypes getAllSimpleTypesElement() { 64 AllSimpleTypes element = new AllSimpleTypesImpl(); 65 element.setStringElem(MyStringTypeClass.FOO); 66 element.setIntElem(MyIntTypeClass.INT3); 67 element.setLongElem(MyLongTypeClass.LONG_NEGATIVE); 68 element.setShortElem(MyShortTypeClass.SHORT_POSITIVE); 69 element.setDoubleElem(MyDoubleTypeClass.DOUBLE_POSITIVE); 70 element.setFloatElem(MyFloatTypeClass.FLOAT_NEGATIVE); 71 return element; 72 } 73 74 private AllTypesElement getAllTypesElement() throws JAXBException { 75 AllTypesElement element = (AllTypesElement) getFactory().getManager(AllTypesElement.class).getElementJ(); 76 element.setAllSimpleTypesElement(getAllSimpleTypesElement()); 77 return element; 78 } 79 80 private String getAllTypesElementString() { 81 AllTypesElementImpl elem = new AllTypesElementImpl(); 82 String uri = elem.getQName().getNamespaceURI(); 83 return 84 "<ex:AllTypesElement xmlns:ex=\"" + uri + "\">\n" + 85 " <ex:AllSimpleTypesElement>\n" + 86 " <ex:StringElem>FOO</ex:StringElem>\n" + 87 " <ex:IntElem>3</ex:IntElem>\n" + 88 " <ex:LongElem>-23987982739273989</ex:LongElem>\n" + 89 " <ex:ShortElem>3468</ex:ShortElem>\n" + 90 " <ex:DoubleElem>3249239847982.234</ex:DoubleElem>\n" + 91 " <ex:FloatElem>-24234.234</ex:FloatElem>\n" + 92 " </ex:AllSimpleTypesElement>\n" + 93 "</ex:AllTypesElement>"; 94 } 95 96 private void verifyAllSimpleTypesElement(AllSimpleTypes pElement) { 97 assertEquals(MyStringTypeClass.FOO, pElement.getStringElem()); 98 assertEquals("FOO", pElement.getStringElem().getValue()); 99 assertEquals("FOO", pElement.getStringElem().toString()); 100 assertEquals(MyIntTypeClass.INT3, pElement.getIntElem()); 101 assertEquals(3, pElement.getIntElem().getValue()); 102 assertEquals("INT3", pElement.getIntElem().getName()); 103 assertEquals("3", pElement.getIntElem().toString()); 104 assertEquals(MyLongTypeClass.LONG_NEGATIVE, pElement.getLongElem()); 105 assertEquals(-23987982739273989L, pElement.getLongElem().getValue()); 106 assertEquals("LONG_NEGATIVE", pElement.getLongElem().getName()); 107 assertEquals("-23987982739273989", pElement.getLongElem().toString()); 108 assertEquals(MyShortTypeClass.SHORT_POSITIVE, pElement.getShortElem()); 109 assertEquals(3468, pElement.getShortElem().getValue()); 110 assertEquals("SHORT_POSITIVE", pElement.getShortElem().getName()); 111 assertEquals("3468", pElement.getShortElem().toString()); 112 assertEquals(MyDoubleTypeClass.DOUBLE_POSITIVE, pElement.getDoubleElem()); 113 assertTrue(3249239847982.234 == pElement.getDoubleElem().getValue()); 114 assertEquals("DOUBLE_POSITIVE", pElement.getDoubleElem().getName()); 115 assertEquals("3249239847982.234", pElement.getDoubleElem().toString()); 116 assertEquals(MyFloatTypeClass.FLOAT_NEGATIVE, pElement.getFloatElem()); 117 assertTrue(Float.parseFloat("-24234.234") == pElement.getFloatElem().getValue()); 118 assertEquals("FLOAT_NEGATIVE", pElement.getFloatElem().getName()); 119 assertEquals("-24234.234", pElement.getFloatElem().toString()); 120 } 121 122 private void verifyAllTypesElement(AllTypesElement pElement) { 123 verifyAllSimpleTypesElement(pElement.getAllSimpleTypesElement()); 124 } 125 126 128 public void testUnmarshalSimpleElements() throws Exception { 129 JMUnmarshaller jmUnmarshaller = (JMUnmarshaller) getFactory().createUnmarshaller(); 130 StringReader sr = new StringReader (getAllTypesElementString()); 131 AllTypesElement result = (AllTypesElement) jmUnmarshaller.unmarshal(new InputSource (sr)); 132 verifyAllSimpleTypesElement(result.getAllSimpleTypesElement()); 133 } 134 135 137 public void testUnmarshalComplexElements() throws Exception { 138 JAXBContext myFactory = getFactory(); 139 Unmarshaller unmarshaller = myFactory.createUnmarshaller(); 140 StringReader sr = new StringReader (getAllTypesElementString()); 141 AllTypesElement result = (AllTypesElement) unmarshaller.unmarshal(new InputSource (sr)); 142 verifyAllTypesElement(result); 143 } 144 145 147 public void testMarshalSimpleElements() throws Exception { 148 StringWriter sw = new StringWriter (); 149 AllTypesElement element = getAllTypesElement(); 150 Marshaller m = getFactory().createMarshaller(); 151 m.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE); 152 m.marshal(element, sw); 153 String expected = getAllTypesElementString(); 154 String got = sw.toString(); 155 assertEquals(expected, got); 156 } 157 158 160 public void testMarshalComplexElements() throws Exception { 161 JAXBContext myFactory = getFactory(); 162 Marshaller marshaller = myFactory.createMarshaller(); 163 marshaller.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE); 164 StringWriter sw = new StringWriter (); 165 JMElement jmElement = (JMElement) getAllTypesElement(); 166 assertNotNull(jmElement.getQName()); 167 marshaller.marshal(jmElement, sw); 168 assertEquals(getAllTypesElementString(), sw.toString()); 169 } 170 171 173 public void testPrefixes() throws Exception { 174 AllTypesElementImpl el = new AllTypesElementImpl(); 175 JMSAXDriver driver = new AllTypesElementTypeDriver(); 176 assertEquals("ex", driver.getPreferredPrefix(el.getQName().getNamespaceURI())); 177 assertNull(driver.getPreferredPrefix("dummyURI")); 178 } 179 } 180 | Popular Tags |