Package org.apache.ws.jaxme.junit

Source Code of org.apache.ws.jaxme.junit.EnumerationTest

/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2003 The Apache Software Foundation.  All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the
*    distribution.
*
* 3. The end-user documentation included with the redistribution,
*    if any, must include the following acknowledgment: 
*       "This product includes software developed by the
*        Apache Software Foundation (http://www.apache.org/)."
*    Alternately, this acknowledgment may appear in the software itself,
*    if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
*    not be used to endorse or promote products derived from this
*    software without prior written permission. For written
*    permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
*    nor may "Apache" appear in their name, without prior written
*    permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.junit;

import java.io.StringReader;
import java.io.StringWriter;
import java.text.DateFormat;
import java.util.Calendar;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
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.JMElement;
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.XMLWriterImpl;
import org.apache.ws.jaxme.test.misc.enumeration.AllSimpleTypes;
import org.apache.ws.jaxme.test.misc.enumeration.AllTypesElement;
import org.apache.ws.jaxme.test.misc.enumeration.MyDoubleTypeClass;
import org.apache.ws.jaxme.test.misc.enumeration.MyFloatTypeClass;
import org.apache.ws.jaxme.test.misc.enumeration.MyIntTypeClass;
import org.apache.ws.jaxme.test.misc.enumeration.MyLongTypeClass;
import org.apache.ws.jaxme.test.misc.enumeration.MyShortTypeClass;
import org.apache.ws.jaxme.test.misc.enumeration.MyStringTypeClass;
import org.apache.ws.jaxme.test.misc.enumeration.impl.AllSimpleTypesHandler;
import org.apache.ws.jaxme.test.misc.enumeration.impl.AllSimpleTypesImpl;
import org.apache.ws.jaxme.test.misc.enumeration.impl.AllSimpleTypesSerializer;
import org.apache.ws.jaxme.test.misc.enumeration.impl.AllTypesElementImpl;
import org.apache.ws.jaxme.test.misc.enumeration.impl.AllTypesElementTypeSerializer;
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: EnumerationTest.java 231650 2003-11-15 16:39:58Z ias $
*/
public class EnumerationTest extends BaseTestCase {
  private JAXBContextImpl factory;

  public EnumerationTest(String arg) { super(arg); }

  public void setUp() throws JAXBException {
    factory = (JAXBContextImpl) JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.enumeration");
  }

  public JAXBContextImpl getFactory() {
    return factory;
  }

  public AllSimpleTypes getAllSimpleTypesElement() {
    AllSimpleTypes element = new AllSimpleTypesImpl();
    element.setStringElem(MyStringTypeClass.FOO);
    element.setIntElem(MyIntTypeClass.INT3);
    element.setLongElem(MyLongTypeClass.LONG_NEGATIVE);
    element.setShortElem(MyShortTypeClass.SHORT_POSITIVE);
    element.setDoubleElem(MyDoubleTypeClass.DOUBLE_POSITIVE);
    element.setFloatElem(MyFloatTypeClass.FLOAT_NEGATIVE);
    return element;
  }

  public AllTypesElement getAllTypesElement() throws JAXBException {
    AllTypesElement element = (AllTypesElement) getFactory().getElement(AllTypesElement.class);
    element.setAllSimpleTypesElement(getAllSimpleTypesElement());
    return element;
  }

  public String getDateTime() {
    Calendar result = Calendar.getInstance();
    result.set(Calendar.YEAR, 2002);
    result.set(Calendar.MONTH, 12-1);
    result.set(Calendar.DAY_OF_MONTH, 17);
    result.set(Calendar.HOUR_OF_DAY, 12);
    result.set(Calendar.MINUTE, 23);
    result.set(Calendar.SECOND, 11);
    return DateFormat.getDateTimeInstance().format(result.getTime());
  }

  public String getDate() {
    Calendar result = Calendar.getInstance();
    result.set(Calendar.YEAR, 2002);
    result.set(Calendar.MONTH, 12-1);
    result.set(Calendar.DAY_OF_MONTH, 16);
    return DateFormat.getDateInstance().format(result.getTime());
  }

  public String getTime() {
    Calendar result = Calendar.getInstance();
    result.set(Calendar.HOUR_OF_DAY, 12);
    result.set(Calendar.MINUTE, 00);
    result.set(Calendar.SECOND, 11);
    return DateFormat.getTimeInstance().format(result.getTime());
  }

  public String getAllSimpleTypesElementString() {
    AllTypesElementImpl elem = new AllTypesElementImpl();
    String uri = elem.getQName().getNamespaceURI();
    return
      "<ex:AllSimpleTypesElement xmlns:ex=\"" + uri + "\">" +
        "<ex:StringElem>FOO</ex:StringElem>" +
        "<ex:IntElem>3</ex:IntElem>" +
        "<ex:LongElem>-23987982739273989</ex:LongElem>" +
        "<ex:ShortElem>3468</ex:ShortElem>" +
        "<ex:DoubleElem>3249239847982.234</ex:DoubleElem>" +
        "<ex:FloatElem>-24234.234</ex:FloatElem>" +
      "</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>FOO</ex:StringElem>\n" +
      "    <ex:IntElem>3</ex:IntElem>\n" +
      "    <ex:LongElem>-23987982739273989</ex:LongElem>\n" +
      "    <ex:ShortElem>3468</ex:ShortElem>\n" +
      "    <ex:DoubleElem>3249239847982.234</ex:DoubleElem>\n" +
      "    <ex:FloatElem>-24234.234</ex:FloatElem>\n" +
      "  </ex:AllSimpleTypesElement>\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) {
    assertEquals(MyStringTypeClass.FOO, pElement.getStringElem());
    assertStringEquals("FOO", pElement.getStringElem().getValue());
    assertStringEquals("FOO", pElement.getStringElem().toString());
    assertEquals(MyIntTypeClass.INT3, pElement.getIntElem());
    assertEquals(3, pElement.getIntElem().getValue());
    assertStringEquals("INT3", pElement.getIntElem().getName());
    assertStringEquals("3", pElement.getIntElem().toString());
    assertEquals(MyLongTypeClass.LONG_NEGATIVE, pElement.getLongElem());
    assertEquals(-23987982739273989L, pElement.getLongElem().getValue());
    assertStringEquals("LONG_NEGATIVE", pElement.getLongElem().getName());
    assertStringEquals("-23987982739273989", pElement.getLongElem().toString());
    assertEquals(MyShortTypeClass.SHORT_POSITIVE, pElement.getShortElem());
    assertEquals(3468, pElement.getShortElem().getValue());
    assertStringEquals("SHORT_POSITIVE", pElement.getShortElem().getName());
    assertStringEquals("3468", pElement.getShortElem().toString());
    assertEquals(MyDoubleTypeClass.DOUBLE_POSITIVE, pElement.getDoubleElem());
    assertTrue(3249239847982.234 == pElement.getDoubleElem().getValue());
    assertStringEquals("DOUBLE_POSITIVE", pElement.getDoubleElem().getName());
    assertStringEquals("3249239847982.234", pElement.getDoubleElem().toString());
    assertEquals(MyFloatTypeClass.FLOAT_NEGATIVE, pElement.getFloatElem());
    assertTrue(Float.parseFloat("-24234.234") == pElement.getFloatElem().getValue());
    assertStringEquals("FLOAT_NEGATIVE", pElement.getFloatElem().getName());
    assertStringEquals("-24234.234", pElement.getFloatElem().toString());
  }

  public void verifyAllTypesElement(AllTypesElement pElement) {
    verifyAllSimpleTypesElement(pElement.getAllSimpleTypesElement());
  }

  public void testUnmarshalSimpleElements() throws Exception {
    XMLReader xr = getXMLReader();
    AllSimpleTypesHandler unmarshaller = new AllSimpleTypesHandler();
    JMUnmarshaller jmUnmarshaller = (JMUnmarshaller) getFactory().createUnmarshaller();
    unmarshaller.init(jmUnmarshaller);
    xr.setContentHandler(unmarshaller);
    StringReader sr = new StringReader(getAllSimpleTypesElementString());
    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());
    String expected = getAllSimpleTypesElementString();
    String got = sw.toString();
    assertStringEquals(expected, got);
  }

  public void testMarshalComplexElements() throws Exception {
    JAXBContext myFactory = getFactory();
    Marshaller marshaller = myFactory.createMarshaller();
    StringWriter sw = new StringWriter();
    JMElement jmElement = (JMElement) getAllTypesElement();
    assertNotNull(jmElement.getQName());
    marshaller.marshal(jmElement, sw);
    assertStringEquals(getAllTypesElementString(), sw.toString());
  }

  public void testPrefixes() throws Exception {
    AllTypesElementImpl el = new AllTypesElementImpl();
    JMXmlSerializer ser = new AllTypesElementTypeSerializer();
    assertStringEquals("ex", ser.getPreferredPrefix(el.getQName().getNamespaceURI()));
    assertNull(ser.getPreferredPrefix("dummyURI"));
  }
}
TOP

Related Classes of org.apache.ws.jaxme.junit.EnumerationTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.