Пример #1
0
  public void testXMLToObjectFromInputStream() throws Exception {

    if (isUnmarshalTest()) {
      InputStream instream = ClassLoader.getSystemResourceAsStream(resourceName);
      jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, getXMLUnmarshalMediaType());
      Object testObject = null;
      if (getUnmarshalClass() != null) {
        testObject =
            ((JAXBUnmarshaller) jaxbUnmarshaller)
                .unmarshal(new StreamSource(instream), getUnmarshalClass());
      } else {
        testObject = jaxbUnmarshaller.unmarshal(instream);
      }

      instream.close();
      xmlToObjectTest(testObject);

      if (getProperties() != null) {
        JAXBContext jaxbContextFromJSONBindings = createJaxbContextFromJSONBindings();
        Unmarshaller jaxbUnmarshallerFromJSONBindings =
            jaxbContextFromJSONBindings.createUnmarshaller();
        jaxbUnmarshallerFromJSONBindings.setAttachmentUnmarshaller(
            jaxbUnmarshaller.getAttachmentUnmarshaller());
        jaxbUnmarshallerFromJSONBindings.setProperty(
            UnmarshallerProperties.OBJECT_GRAPH,
            jaxbUnmarshaller.getProperty(UnmarshallerProperties.OBJECT_GRAPH));
        jaxbUnmarshallerFromJSONBindings.setProperty(
            UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER,
            jaxbMarshaller.getProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER));
        Object testObject2 = null;
        log("************test with JSON bindings*********");
        InputStream instream2 = ClassLoader.getSystemResourceAsStream(resourceName);
        if (getUnmarshalClass() != null) {
          testObject2 =
              ((JAXBUnmarshaller) jaxbUnmarshallerFromJSONBindings)
                  .unmarshal(new StreamSource(instream2), getUnmarshalClass());
        } else {
          testObject2 = jaxbUnmarshallerFromJSONBindings.unmarshal(instream2);
        }
        instream2.close();
        xmlToObjectTest(testObject2);
      }
    }
  }
Пример #2
0
 public void testXMLToObjectFromXMLEventReader() throws Exception {
   if (null != XML_INPUT_FACTORY && isUnmarshalTest()) {
     InputStream instream = ClassLoader.getSystemResourceAsStream(resourceName);
     XMLEventReader xmlEventReader = XML_INPUT_FACTORY.createXMLEventReader(instream);
     jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, getXMLUnmarshalMediaType());
     Object testObject = null;
     if (getUnmarshalClass() != null) {
       testObject = jaxbUnmarshaller.unmarshal(xmlEventReader, getUnmarshalClass());
     } else {
       testObject = jaxbUnmarshaller.unmarshal(xmlEventReader);
     }
     instream.close();
     xmlToObjectTest(testObject);
   }
 }
Пример #3
0
 public void testXMLToObjectFromNode() throws Exception {
   if (isUnmarshalTest()) {
     InputStream instream = ClassLoader.getSystemResourceAsStream(resourceName);
     Node node = parser.parse(instream);
     jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, getXMLUnmarshalMediaType());
     Object testObject = null;
     if (getUnmarshalClass() != null) {
       testObject = jaxbUnmarshaller.unmarshal(node, getUnmarshalClass());
     } else {
       testObject = jaxbUnmarshaller.unmarshal(node);
     }
     instream.close();
     xmlToObjectTest(testObject);
   }
 }
Пример #4
0
  public void testXMLToObjectFromURL() throws Exception {
    if (isUnmarshalTest()) {
      java.net.URL url = ClassLoader.getSystemResource(resourceName);
      jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, getXMLUnmarshalMediaType());

      Object testObject = null;
      if (getUnmarshalClass() != null) {
        testObject =
            ((JAXBUnmarshaller) jaxbUnmarshaller)
                .unmarshal(new StreamSource(url.openStream()), getUnmarshalClass());
      } else {
        testObject = jaxbUnmarshaller.unmarshal(url);
      }
      xmlToObjectTest(testObject);
    }
  }
Пример #5
0
  public void testUnmarshallerHandler() throws Exception {
    if (isUnmarshalTest()) {
      SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
      saxParserFactory.setNamespaceAware(true);
      SAXParser saxParser = saxParserFactory.newSAXParser();
      XMLReader xmlReader = saxParser.getXMLReader();
      jaxbUnmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");

      JAXBUnmarshallerHandler jaxbUnmarshallerHandler =
          (JAXBUnmarshallerHandler) jaxbUnmarshaller.getUnmarshallerHandler();
      xmlReader.setContentHandler(jaxbUnmarshallerHandler);
      InputStream inputStream = ClassLoader.getSystemResourceAsStream(resourceName);
      InputSource inputSource = new InputSource(inputStream);
      xmlReader.parse(inputSource);

      xmlToObjectTest(jaxbUnmarshallerHandler.getResult());
    }
  }
Пример #6
0
  public void testXMLToObjectFromXMLStreamReaderEx() throws Exception {
    if (null != XML_INPUT_FACTORY && isUnmarshalTest()) {
      InputStream instream = ClassLoader.getSystemResourceAsStream(resourceName);
      XMLStreamReader xmlStreamReader = XML_INPUT_FACTORY.createXMLStreamReader(instream);

      ExtendedXMLStreamReaderReader xmlStreamReaderReaderEx = new ExtendedXMLStreamReaderReader();
      xmlStreamReaderReaderEx.setErrorHandler(
          ((JAXBUnmarshaller) jaxbUnmarshaller).getXMLUnmarshaller().getErrorHandler());
      XMLStreamReaderInputSource xmlStreamReaderInputSource =
          new XMLStreamReaderInputSource(xmlStreamReader);
      SAXSource saxSource = new SAXSource(xmlStreamReaderReaderEx, xmlStreamReaderInputSource);
      jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, getXMLUnmarshalMediaType());
      Object testObject = null;
      if (getUnmarshalClass() != null) {
        testObject = jaxbUnmarshaller.unmarshal(saxSource, getUnmarshalClass());
      } else {
        testObject = jaxbUnmarshaller.unmarshal(saxSource);
      }

      instream.close();
      xmlToObjectTest(testObject);
    }
  }
Пример #7
0
  public void testRoundTrip() throws Exception {
    if (isUnmarshalTest()) {
      jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, getXMLUnmarshalMediaType());
      InputStream instream = null;
      if (writeControlDocumentLocation != null) {
        instream = ClassLoader.getSystemResourceAsStream(writeControlDocumentLocation);
      } else {
        instream = ClassLoader.getSystemResourceAsStream(resourceName);
      }
      jaxbUnmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
      Object testObject = null;
      if (getUnmarshalClass() != null) {
        testObject = jaxbUnmarshaller.unmarshal(new StreamSource(instream), getUnmarshalClass());
      } else {
        testObject = jaxbUnmarshaller.unmarshal(instream);
      }

      instream.close();
      xmlToObjectTest(testObject);

      objectToXMLStringWriter(testObject);
    }
  }
Пример #8
0
 public void xmlToObjectTest(Object testObject) throws Exception {
   xmlToObjectTest(testObject, getReadControlObject());
 }
 public void xmlToObjectTest(Object testObject) throws Exception {
   super.xmlToObjectTest(testObject);
   Employee empObj = (Employee) testObject;
   assertTrue("Accessor method was not called as expected", empObj.wasSetCalled);
   assertTrue("Set was not called for absent node as expected", empObj.isAStringSet);
 }