/*
* Copyright 2003, 2004 The Apache Software Foundation
*
* Licensed 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.ws.jaxme.junit;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.ws.jaxme.JMMarshaller;
import org.apache.ws.jaxme.JMUnmarshaller;
import org.apache.ws.jaxme.JMXmlSerializer;
import org.apache.ws.jaxme.XMLWriter;
import org.apache.ws.jaxme.impl.JAXBContextImpl;
import org.apache.ws.jaxme.impl.JMMarshallerImpl;
import org.apache.ws.jaxme.impl.XMLWriterImpl;
import org.apache.ws.jaxme.test.misc.types.AllSimpleTypes;
import org.apache.ws.jaxme.test.misc.types.AllTypesElement;
import org.apache.ws.jaxme.test.misc.types.Author;
import org.apache.ws.jaxme.test.misc.types.ObjectFactory;
import org.apache.ws.jaxme.test.misc.types.impl.AllSimpleTypesHandler;
import org.apache.ws.jaxme.test.misc.types.impl.AllSimpleTypesImpl;
import org.apache.ws.jaxme.test.misc.types.impl.AllSimpleTypesSerializer;
import org.apache.ws.jaxme.test.misc.types.impl.AllTypesElementImpl;
import org.apache.ws.jaxme.util.Duration;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
* @version $Id: MarshallerTest.java,v 1.9 2004/02/16 23:39:54 jochen Exp $
*/
public class MarshallerTest extends BaseTestCase {
private JAXBContextImpl factory;
public MarshallerTest(String arg) { super(arg); }
public void setUp() throws JAXBException {
factory = (JAXBContextImpl) JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.types");
}
public JAXBContextImpl getFactory() {
return factory;
}
public Calendar getDateTime() {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.set(Calendar.DAY_OF_MONTH, 22);
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.YEAR, 2002);
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 43);
cal.set(Calendar.SECOND, 37);
cal.set(Calendar.MILLISECOND, 0);
return cal;
}
public Calendar getTime() {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 43);
cal.set(Calendar.SECOND, 37);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.YEAR, 0);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 0);
return cal;
}
public Calendar getDate() {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.set(Calendar.DAY_OF_MONTH, 22);
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.YEAR, 2002);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
}
public Duration getDuration() {
Duration d = new Duration(false, 1, 2, 3, 4, 5, 6, 7);
return d;
}
public byte[] getHexBytes() {
return new byte[]{1, 17, 35, 78, 115, -99, -69, -1};
}
public AllSimpleTypes getAllSimpleTypesElement() {
AllSimpleTypes element = new AllSimpleTypesImpl();
element.setStringElem("This is a string with german Umlauts: \u00e4\u00f6\u00fc\u00df\u00c4\u00d6\u00dc");
element.setDoubleElem(23979782937923.2398798);
element.setFloatElem(-34768.3486787f);
element.setIntElem(-2139879);
element.setLongElem(1290380128309182303l);
element.setShortElem((short) 23878);
element.setDateElem(getDate());
element.setDateTimeElem(getDateTime());
element.setTimeElem(getTime());
element.setDurationElem(getDuration());
element.setHexBinaryElem(getHexBytes());
element.setBase64BinaryElem(getHexBytes());
element.setNmTokenElem("a-name-token");
List nmTokens = element.getNmTokensElem();
nmTokens.add("a-name-token");
nmTokens.add("another:name-token");
element.setDecimalElem(new BigDecimal("6.023e23"));
element.setIntegerElem(new BigDecimal("-6023e20").toBigInteger());
element.setNonNegativeIntegerElem(new BigInteger("101"));
element.setPositiveIntegerElem(new BigDecimal("+6023e20").toBigInteger());
element.setNonPositiveIntegerElem(new BigInteger("-21714"));
element.setNegativeIntegerElem(new BigInteger("-21714"));
element.setQNameElem(new QName("http://ws.apache.org/jaxme/test/misc/types", "ias"));
return element;
}
public AllTypesElement getAllTypesElement() throws JAXBException {
AllTypesElement element = (AllTypesElement) getFactory().getElement(AllTypesElement.class);
element.setAllSimpleTypesElement(getAllSimpleTypesElement());
List list = element.getListTypeElement();
list.add(new BigInteger("7"));
list.add(new BigInteger("-3"));
list.add(new BigInteger("0"));
element.setUnionTypeElement(getDateTime());
return element;
}
public String getAllSimpleTypesElementString() {
AllTypesElementImpl elem = new AllTypesElementImpl();
String uri = elem.getQName().getNamespaceURI();
return
"<ex:AllSimpleTypesElement xmlns:ex=\"" + uri + "\">" +
"<ex:StringElem>This is a string with german Umlauts: äöüßÄÖÜ</ex:StringElem>" +
"<ex:IntElem>-2139879</ex:IntElem>" +
"<ex:LongElem>1290380128309182303</ex:LongElem>" +
"<ex:ShortElem>23878</ex:ShortElem>" +
"<ex:DoubleElem>2.397978293792324E13</ex:DoubleElem>" +
"<ex:FloatElem>-34768.348</ex:FloatElem>" +
"<ex:DateTimeElem>2002-11-22T16:43:37Z</ex:DateTimeElem>" +
"<ex:DateElem>2002-11-22Z</ex:DateElem>" +
"<ex:TimeElem>16:43:37Z</ex:TimeElem>" +
"<ex:DurationElem>P1Y2M3DT4H5M6.7S</ex:DurationElem>" +
"<ex:HexBinaryElem>0111234E739DBBFF</ex:HexBinaryElem>" +
"<ex:Base64BinaryElem>AREjTnOdu/8=</ex:Base64BinaryElem>" +
"<ex:NmTokenElem>a-name-token</ex:NmTokenElem>" +
"<ex:NmTokensElem>a-name-token another:name-token</ex:NmTokensElem>" +
"<ex:DecimalElem>602300000000000000000000</ex:DecimalElem>" +
"<ex:IntegerElem>-602300000000000000000000</ex:IntegerElem>" +
"<ex:NonNegativeIntegerElem>101</ex:NonNegativeIntegerElem>" +
"<ex:PositiveIntegerElem>602300000000000000000000</ex:PositiveIntegerElem>" +
"<ex:NonPositiveIntegerElem>-21714</ex:NonPositiveIntegerElem>" +
"<ex:NegativeIntegerElem>-21714</ex:NegativeIntegerElem>" +
"<ex:QNameElem>ex:ias</ex:QNameElem>" +
"</ex:AllSimpleTypesElement>";
}
public String getAllTypesElementString() {
AllTypesElementImpl elem = new AllTypesElementImpl();
String uri = elem.getQName().getNamespaceURI();
return
"<ex:AllTypesElement xmlns:ex=\"" + uri + "\">\n" +
" <ex:AllSimpleTypesElement>\n" +
" <ex:StringElem>This is a string with german Umlauts: äöüßÄÖÜ</ex:StringElem>\n" +
" <ex:IntElem>-2139879</ex:IntElem>\n" +
" <ex:LongElem>1290380128309182303</ex:LongElem>\n" +
" <ex:ShortElem>23878</ex:ShortElem>\n" +
" <ex:DoubleElem>2.397978293792324E13</ex:DoubleElem>\n" +
" <ex:FloatElem>-34768.348</ex:FloatElem>\n" +
" <ex:DateTimeElem>2002-11-22T16:43:37Z</ex:DateTimeElem>\n" +
" <ex:DateElem>2002-11-22Z</ex:DateElem>\n" +
" <ex:TimeElem>16:43:37Z</ex:TimeElem>\n" +
" <ex:DurationElem>P1Y2M3DT4H5M6.7S</ex:DurationElem>\n" +
" <ex:HexBinaryElem>0111234E739DBBFF</ex:HexBinaryElem>\n" +
" <ex:Base64BinaryElem>AREjTnOdu/8=</ex:Base64BinaryElem>\n" +
" <ex:NmTokenElem>a-name-token</ex:NmTokenElem>\n" +
" <ex:NmTokensElem>a-name-token another:name-token</ex:NmTokensElem>\n" +
" <ex:DecimalElem>602300000000000000000000</ex:DecimalElem>\n" +
" <ex:IntegerElem>-602300000000000000000000</ex:IntegerElem>\n" +
" <ex:NonNegativeIntegerElem>101</ex:NonNegativeIntegerElem>\n" +
" <ex:PositiveIntegerElem>602300000000000000000000</ex:PositiveIntegerElem>\n" +
" <ex:NonPositiveIntegerElem>-21714</ex:NonPositiveIntegerElem>\n" +
" <ex:NegativeIntegerElem>-21714</ex:NegativeIntegerElem>\n" +
" <ex:QNameElem>ex:ias</ex:QNameElem>\n" +
" </ex:AllSimpleTypesElement>\n" +
" <ex:ListTypeElement>7 -3 0</ex:ListTypeElement>\n" +
" <ex:UnionTypeElement>2002-11-22T16:43:37Z</ex:UnionTypeElement>\n" +
"</ex:AllTypesElement>";
}
public XMLReader getXMLReader() throws ParserConfigurationException, SAXException {
// Verify whether we can create a SAX Parser; it's better to detect this here
// than within JAXB
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);
SAXParser sp = spf.newSAXParser();
return sp.getXMLReader();
}
public void verifyAllSimpleTypesElement(AllSimpleTypes pElement) {
assertStringEquals("This is a string with german Umlauts: \u00e4\u00f6\u00fc\u00df\u00c4\u00d6\u00dc",
pElement.getStringElem());
assertEquals(new Double("2.397978293792324E13").doubleValue(),
pElement.getDoubleElem(), 0);
assertEquals(new Float(-34768.348).floatValue(), pElement.getFloatElem(), 0);
assertEquals(new Integer(-2139879).intValue(), pElement.getIntElem());
assertEquals(new Long(1290380128309182303L).longValue(), pElement.getLongElem());
assertEquals(new Short((short) 23878).shortValue(), pElement.getShortElem());
assertEquals(getDateTime(), pElement.getDateTimeElem());
assertEquals(getDate().getTimeInMillis(), pElement.getDateElem().getTimeInMillis());
assertEquals(getTime().getTimeInMillis(), pElement.getTimeElem().getTimeInMillis());
assertEquals(getDuration(), pElement.getDurationElem());
assertEquals(getHexBytes(), pElement.getHexBinaryElem());
assertEquals(getHexBytes(), pElement.getBase64BinaryElem());
assertEquals(new BigInteger("101"), pElement.getNonNegativeIntegerElem());
}
public void verifyAllTypesElement(AllTypesElement pElement) {
verifyAllSimpleTypesElement(pElement.getAllSimpleTypesElement());
List list = pElement.getListTypeElement();
assertEquals(3, list.size());
assertEquals(new BigInteger("7"), list.get(0));
assertEquals(new BigInteger("-3"), list.get(1));
assertEquals(new BigInteger("0"), list.get(2));
Object object = pElement.getUnionTypeElement();
String dateTime1 = DateFormat.getDateTimeInstance().format(getDateTime().getTime());
String dateTime2 = DateFormat.getDateTimeInstance().format(((Calendar) object).getTime());
assertStringEquals(dateTime1, dateTime2);
}
public void testUnmarshalSimpleElements() throws Exception {
XMLReader xr = getXMLReader();
AllSimpleTypesHandler unmarshaller = new AllSimpleTypesHandler();
JMUnmarshaller jmUnmarshaller = (JMUnmarshaller) getFactory().createUnmarshaller();
unmarshaller.init(jmUnmarshaller);
xr.setContentHandler(unmarshaller);
String s = getAllSimpleTypesElementString();
StringReader sr = new StringReader(s);
xr.parse(new InputSource(sr));
AllSimpleTypes result = (AllSimpleTypes) unmarshaller.getResult();
verifyAllSimpleTypesElement(result);
}
public void testUnmarshalComplexElements() throws Exception {
JAXBContext myFactory = getFactory();
Unmarshaller unmarshaller = myFactory.createUnmarshaller();
StringReader sr = new StringReader(getAllTypesElementString());
AllTypesElement result = (AllTypesElement) unmarshaller.unmarshal(new InputSource(sr));
verifyAllTypesElement(result);
}
public void testMarshalSimpleElements() throws Exception {
AllSimpleTypesSerializer marshaller = new AllSimpleTypesSerializer();
marshaller.init(getFactory());
XMLWriter xw = new XMLWriterImpl();
StringWriter sw = new StringWriter();
xw.setWriter(sw);
JMXmlSerializer.Data data = marshaller.getData((JMMarshaller) factory.createMarshaller(), xw);
AllTypesElementImpl element = new AllTypesElementImpl();
marshaller.marshal(data, new QName(element.getQName().getNamespaceURI(), "AllSimpleTypesElement"), getAllSimpleTypesElement());
assertStringEquals(getAllSimpleTypesElementString(), sw.toString());
}
public void testMarshalComplexElements() throws Exception {
JAXBContext myFactory = getFactory();
JMMarshallerImpl marshaller = (JMMarshallerImpl) myFactory.createMarshaller();
StringWriter sw = new StringWriter();
Class c = marshaller.getXMLWriterClass();
XMLWriter w = (XMLWriter) c.newInstance();
w.init(marshaller);
String rawInput = getAllTypesElementString();
StringBuffer input = new StringBuffer();
for (int i = 0; i < rawInput.length(); i++) {
char ch = rawInput.charAt(i);
boolean done = false;
if (ch == '&') {
if (i+1 < rawInput.length() && rawInput.charAt(i+1) == '#') {
int j = 2;
StringBuffer digits = new StringBuffer();
while (i+j < rawInput.length() && Character.isDigit(rawInput.charAt(i+j))) {
digits.append(rawInput.charAt(i+j));
++j;
}
if (digits.length() > 0 &&
i+j < rawInput.length() && rawInput.charAt(i+j) == ';') {
char chr = (char) Integer.parseInt(digits.toString());
if (w.canEncode(chr)) {
done = true;
i += j;
input.append(chr);
}
}
}
}
if (!done) {
input.append(ch);
}
}
marshaller.marshal(getAllTypesElement(), sw);
assertStringEquals(input.toString(), sw.toString());
}
protected String getMarshalledAuthor() throws JAXBException {
Author author = new ObjectFactory().createAuthor();
author.setRating(2);
author.setValue("This is a test.");
StringWriter sw = new StringWriter();
getFactory().createMarshaller().marshal(author, sw);
return sw.toString();
}
public void testMarshalSimpleContent() throws Exception {
String expect = "<ex:Author rating=\"2\" xmlns:ex=\"http://ws.apache.org/jaxme/test/misc/types\">This is a test.</ex:Author>";
String got = getMarshalledAuthor();
assertStringEquals(expect, got);
}
public void testUnmarshalSimpleContent() throws Exception {
Author author = (Author) getFactory().createUnmarshaller().unmarshal(new InputSource(new StringReader(getMarshalledAuthor())));
assertEquals(2, author.getRating());
assertEquals("This is a test.", author.getValue());
}
/**
* Tests that toString returns an appropriate xsd:duration value
*/
public void testExplicitToString() {
String dur1 = "P0Y0M0DT0H2M60S";
Duration duration1 = Duration.valueOf(dur1);
String actualReturn = duration1.toString();
assertEquals("return value not as expected", dur1, actualReturn);
}
/**
* Tests that toString returns an appropriate xsd:duration value
*/
public void testImplicitToString() {
String dur2 = "PT2M60S";
Duration duration2 = Duration.valueOf(dur2);
String actualReturn = duration2.toString();
String expect = "P0Y0M0DT0H2M60S";
assertEquals("return value not as expected ", expect, actualReturn);
}
/**
* Test that getMillis returns the total time of duration in
milliseconds
*/
public void testMillis() {
String dur2 = "PT2M60S";
Duration duration2 = Duration.valueOf(dur2);
assertEquals(2, duration2.getMinutes());
assertEquals(60, duration2.getSeconds());
}
}