@Before
  public void setUp() throws Exception {
    preferredValidatorFactory = Validation.buildDefaultValidatorFactory();

    JAXBContext ctx = JAXBContextFactory.createContext(EMPLOYEE, null);
    marshallerValidOn = (JAXBMarshaller) ctx.createMarshaller();
    marshallerValidOn.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshallerValidOff = (JAXBMarshaller) ctx.createMarshaller();
    /* tests setting the property through marshaller */
    marshallerValidOff.setProperty(
        MarshallerProperties.BEAN_VALIDATION_MODE, BeanValidationMode.NONE);
    marshallerValidOff.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    JAXBContext ctxValidationOff =
        JAXBContextFactory.createContext(
            EMPLOYEE,
            new HashMap<String, Object>() {
              {
                put(JAXBContextProperties.BEAN_VALIDATION_MODE, BeanValidationMode.NONE);
                put(JAXBContextProperties.BEAN_VALIDATION_FACTORY, preferredValidatorFactory);
              }
            });
    unmarshallerValidOn = (JAXBUnmarshaller) ctxValidationOff.createUnmarshaller();
    /* tests setting the property through unmarshaller */
    unmarshallerValidOn.setProperty(
        UnmarshallerProperties.BEAN_VALIDATION_MODE, BeanValidationMode.CALLBACK);
    unmarshallerValidOff = (JAXBUnmarshaller) ctxValidationOff.createUnmarshaller();
  }
 protected JAXBContext createJaxbContextFromJSONBindings() throws JAXBException {
   if (classes != null) {
     return JAXBContextFactory.createContext(classes, getPropertiesFromJSON(), classLoader);
   } else if (types != null) {
     return JAXBContextFactory.createContext(types, getPropertiesFromJSON(), classLoader);
   } else if (contextPath != null) {
     return JAXBContextFactory.createContext(contextPath, classLoader, getPropertiesFromJSON());
   }
   return null;
 }
 public void testCreateContextUnrelatedSessionsXmlInvalidPath() throws Exception {
   try {
     JAXBContext context =
         JAXBContextFactory.createContext(
             "org.eclipse.persistence.testing.jaxb.jaxbcontext.fake",
             new ClassLoader() {
               public URL getResource(String resourceName) {
                 if (resourceName.equals("sessions.xml")) {
                   return getParent()
                       .getResource(
                           "org/eclipse/persistence/testing/jaxb/jaxbcontext/sessions.xml");
                 }
                 return this.getParent().getResource(resourceName);
               }
             });
   } catch (JAXBException ex) {
     assertTrue(
         ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException())
                 .getErrorCode()
             == org.eclipse.persistence.exceptions.JAXBException
                 .NO_OBJECT_FACTORY_OR_JAXB_INDEX_IN_PATH);
     assertTrue(
         ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException())
                 .getInternalException()
             instanceof SessionLoaderException);
   }
 }
 public void testBindingFormatList() throws Exception {
   List<Object> inputFiles = new ArrayList<Object>();
   inputFiles.add(new File(FILE_OXM_XML));
   inputFiles.add(new InputSource(new FileInputStream(INPUT_SRC_OXM_XML)));
   inputFiles.add(new FileInputStream(INPUT_STRM_OXM_XML));
   inputFiles.add(new InputStreamReader(new FileInputStream(READER_OXM_XML)));
   inputFiles.add(new StreamSource(ClassLoader.getSystemResourceAsStream(SOURCE_OXM_XML)));
   Map<String, Object> properties = new HashMap<String, Object>();
   properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, inputFiles);
   Class[] listClasses =
       new Class[] {
         org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.bindingformat
             .file.Foo.class,
         org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.bindingformat
             .inputsource.Foo.class,
         org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.bindingformat
             .inputstream.Foo.class,
         org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.bindingformat
             .reader.Foo.class,
         org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.bindingformat
             .source.Foo.class
       };
   JAXBContext jCtx =
       (JAXBContext) JAXBContextFactory.createContext(listClasses, properties, loader);
   doTestFile(jCtx);
   doTestInputSrc(jCtx);
   doTestInputStrm(jCtx);
   doTestReader(jCtx);
   doTestSource(jCtx);
 }
  public void testOptimizedUnmarshal() throws Exception {
    if (null != XML_INPUT_FACTORY) {
      InputStream instream = ClassLoader.getSystemResourceAsStream(XML_RESOURCE);
      XMLStreamReader xmlStreamReader = XML_INPUT_FACTORY.createXMLStreamReader(instream);

      XMLReader xmlReader = new LargeInlineBinaryDataXMLStreamReaderReader();
      InputSource inputSource = new XMLStreamReaderInputSource(xmlStreamReader);
      SAXSource saxSource = new SAXSource(xmlReader, inputSource);

      JAXBContext jc =
          JAXBContextFactory.createContext(new Class[] {LargeInlineBinaryRoot.class}, null);
      Unmarshaller unmarshaller = jc.createUnmarshaller();
      LargeInlineBinaryRoot root = (LargeInlineBinaryRoot) unmarshaller.unmarshal(saxSource);

      assertSame(LargeInlineBinaryDataXMLStreamReaderReader.DATA_HANDLER, root.getDataHandler());
      assertSame(LargeInlineBinaryDataXMLStreamReaderReader.IMAGE, root.getImage());
      assertSame(LargeInlineBinaryDataXMLStreamReaderReader.SOURCE, root.getSource());
      assertSame(
          LargeInlineBinaryDataXMLStreamReaderReader.MIME_MULTIPART, root.getMimeMultipart());
      assertSame(
          LargeInlineBinaryDataXMLStreamReaderReader.DATA_HANDLER,
          root.getDataHandlerList().get(0));
      assertSame(LargeInlineBinaryDataXMLStreamReaderReader.IMAGE, root.getImageList().get(0));
      assertSame(LargeInlineBinaryDataXMLStreamReaderReader.SOURCE, root.getSourceList().get(0));
      assertSame(
          LargeInlineBinaryDataXMLStreamReaderReader.MIME_MULTIPART,
          root.getMimeMultipartList().get(0));
    }
  }
Beispiel #6
0
  private Suggestions transformJsonResponse(final InputStream is, final Path transformation)
      throws IOException, TransformationException {
    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
      jsonTransformer.transform(is, transformation, os);

      try (final InputStream resultIs = new ByteArrayInputStream(os.toByteArray())) {
        final JAXBContext context =
            org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(
                new Class[] {Suggestions.class}, null);

        final Unmarshaller unmarshaller = context.createUnmarshaller();
        unmarshaller.setProperty(
            UnmarshallerProperties.MEDIA_TYPE,
            org.eclipse.persistence.oxm.MediaType.APPLICATION_JSON);
        unmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, null);
        unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
        unmarshaller.setProperty(
            UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
        unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_SEPARATOR, ':');

        final JAXBElement<Suggestions> jaxbElement =
            unmarshaller.unmarshal(new StreamSource(resultIs), Suggestions.class);
        return jaxbElement.getValue();
      } catch (final JAXBException e) {
        throw new TransformationException(e);
      }
    }
  }
 public void testCreateContextWith_TypeMappingInfoArray_Map_NullClassLoader()
     throws JAXBException {
   TypeMappingInfo[] typeMappingInfos = new TypeMappingInfo[1];
   TypeMappingInfo listTMI = new TypeMappingInfo();
   listTMI.setType(List.class);
   listTMI.setXmlTagName(new QName("urn:example", "my-list"));
   typeMappingInfos[0] = listTMI;
   JAXBContextFactory.createContext(typeMappingInfos, null, null);
 }
 public void testCreateContext() throws Exception {
   try {
     JAXBContextFactory.createContext(new Class[] {InvalidChild.class}, null);
   } catch (javax.xml.bind.JAXBException e) {
     JAXBException moxyException = (JAXBException) e.getCause();
     assertEquals(JAXBException.SUBCLASS_CANNOT_HAVE_XMLVALUE, moxyException.getErrorCode());
     return;
   }
   fail();
 }
 public void testBindingFormatFile() throws Exception {
   Map<String, Object> properties = new HashMap<String, Object>();
   properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, new File(FILE_OXM_XML));
   Class[] classes =
       new Class[] {
         org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.bindingformat
             .file.Foo.class
       };
   JAXBContext jCtx = (JAXBContext) JAXBContextFactory.createContext(classes, properties, loader);
   doTestFile(jCtx);
 }
  public void setClasses(Class[] newClasses) throws Exception {

    classLoader = Thread.currentThread().getContextClassLoader();
    jaxbContext = JAXBContextFactory.createContext(newClasses, getProperties(), classLoader);
    classes = newClasses;

    xmlContext = ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext();
    setProject(xmlContext.getSession(0).getProject());
    jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  }
 public void testBindingFormatSource() throws Exception {
   Map<String, Object> properties = new HashMap<String, Object>();
   InputStream is = ClassLoader.getSystemResourceAsStream(SOURCE_OXM_XML);
   properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, new StreamSource(is));
   Class[] classes =
       new Class[] {
         org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.bindingformat
             .source.Foo.class
       };
   JAXBContext jCtx = (JAXBContext) JAXBContextFactory.createContext(classes, properties, loader);
   doTestSource(jCtx);
 }
  @Override
  protected void setUp() throws Exception {
    Class[] classes = {Employee.class};
    JAXBContext jc = JAXBContextFactory.createContext(classes, null);
    unmarshaller = (JAXBUnmarshaller) jc.createUnmarshaller();

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.SCHEMA_URL);
    InputStream stream = ClassLoader.getSystemResourceAsStream(SCHEMA);
    Schema schema = sf.newSchema(new StreamSource(stream));
    stream.close();
    this.schema = schema;
  }
 public void testCreateContextUnrelatedSessionsXml() throws Exception {
   JAXBContext context =
       JAXBContextFactory.createContext(
           "org.eclipse.persistence.testing.jaxb.jaxbcontext",
           new ClassLoader() {
             public URL getResource(String resourceName) {
               if (resourceName.equals("sessions.xml")) {
                 return getParent()
                     .getResource("org/eclipse/persistence/testing/jaxb/jaxbcontext/sessions.xml");
               }
               return this.getParent().getResource(resourceName);
             }
           });
 }
 public void testBindingFormatNoPackageSet() {
   try {
     Map<String, Object> properties = new HashMap<String, Object>();
     properties.put(
         JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY,
         new FileInputStream(new File(NO_PKG_OXM_XML)));
     JAXBContext jaxbContext =
         (JAXBContext) JAXBContextFactory.createContext(new Class[] {}, properties, loader);
   } catch (org.eclipse.persistence.exceptions.JAXBException jaxbe) {
     return;
   } catch (Exception e) {
   }
   fail("The expected exception was not thrown.");
 }
  @Override
  protected void setUp() throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    InputStream schemaStream =
        Thread.currentThread().getContextClassLoader().getResourceAsStream(XSD_RESOURCE);
    Schema s = sf.newSchema(new StreamSource(schemaStream));

    marshaller = JAXBContextFactory.createContext(getClasses(), null).createMarshaller();
    marshaller.setSchema(s);

    child = new Child();
    child.setName("123456789");

    root = setupRootObject();
  }
 public void testCreateConcreteClassWithMultiArgConstructor() throws JAXBException {
   try {
     Class[] classes = new Class[1];
     classes[0] = ConcreteClassWithMultiArgConstructor.class;
     JAXBContextFactory.createContext(classes, null);
   } catch (JAXBException e) {
     org.eclipse.persistence.exceptions.JAXBException je =
         (org.eclipse.persistence.exceptions.JAXBException) e.getLinkedException();
     assertEquals(
         org.eclipse.persistence.exceptions.JAXBException.FACTORY_METHOD_OR_ZERO_ARG_CONST_REQ,
         je.getErrorCode());
     return;
   }
   fail();
 }
 public void testCreateContextNoClassesOrSessions() throws Exception {
   try {
     JAXBContext context =
         JAXBContextFactory.createContext(
             "org.eclipse.persistence.testing.jaxb.jaxbcontext.fake",
             Thread.currentThread().getContextClassLoader());
   } catch (JAXBException ex) {
     assertTrue(
         ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException())
                 .getErrorCode()
             == org.eclipse.persistence.exceptions.JAXBException
                 .NO_OBJECT_FACTORY_OR_JAXB_INDEX_IN_PATH);
     assertTrue(
         ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException())
                 .getInternalException()
             instanceof ValidationException);
   }
 }
  /** Test passing a String[] into the context factory via Class[]. */
  public void testStringArrayInClassesToBeBound() {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    String metadataFile = PATH + "stringarray/a/eclipselink-oxm.xml";
    InputStream iStream = classLoader.getResourceAsStream(metadataFile);
    if (iStream == null) {
      fail("Couldn't load metadata file [" + metadataFile + "]");
    }

    HashMap<String, Source> metadataSourceMap = new HashMap<String, Source>();
    metadataSourceMap.put(
        "org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.stringarray.a",
        new StreamSource(iStream));

    metadataFile = PATH + "stringarray/b/eclipselink-oxm.xml";
    iStream = classLoader.getResourceAsStream(metadataFile);
    if (iStream == null) {
      fail("Couldn't load metadata file [" + metadataFile + "]");
    }
    metadataSourceMap.put(
        "org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory.stringarray.b",
        new StreamSource(iStream));

    Map<String, Map<String, Source>> properties = new HashMap<String, Map<String, Source>>();
    properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadataSourceMap);

    MySchemaOutputResolver outputResolver = new MySchemaOutputResolver();
    JAXBContext jaxbContext;
    try {
      Class<?>[] types = {BeanA.class, BeanB.class, String[].class};

      jaxbContext = (JAXBContext) JAXBContextFactory.createContext(types, properties, loader);
      jaxbContext.generateSchema(outputResolver);
      String controlSchema = PATH + "stringarray/bean_schema.xsd";
      compareSchemas(outputResolver.schemaFiles.get(BEAN_NAMESPACE), new File(controlSchema));
      controlSchema = PATH + "stringarray/string_array_schema.xsd";
      compareSchemas(outputResolver.schemaFiles.get(ARRAY_NAMESPACE), new File(controlSchema));
    } catch (JAXBException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
  public void setContextPath(String contextPath) throws Exception {
    classLoader = Thread.currentThread().getContextClassLoader();
    this.contextPath = contextPath;
    Map props = getProperties();
    if (props != null) {
      Map overrides = (Map) props.get(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY);
      if (overrides != null) {
        Iterator valuesIter = overrides.values().iterator();
        while (valuesIter.hasNext()) {
          Object next = valuesIter.next();
          validateBindingsFileAgainstSchema(next);
        }
      }
    }
    jaxbContext = JAXBContextFactory.createContext(contextPath, classLoader, getProperties());

    xmlContext = ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext();
    setProject(xmlContext.getSession(0).getProject());
    jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  }
  public void testNamespaceCollision() throws Exception {
    String xml =
        "<ns0:employee xmlns:ns1=\"mynamespace3\" xmlns:ns2=\"mynamespace2\" xmlns:ns0=\"mynamespace1\"><ns2:address><street>123 Fake Street</street></ns2:address><firstName>Matt</firstName><id>123</id></ns0:employee>";
    String controlSource = "org/eclipse/persistence/testing/jaxb/binder/nscollision/employee.xml";
    Document controlDocument =
        parser.parse(Thread.currentThread().getContextClassLoader().getResource(controlSource));

    JAXBContext ctx = JAXBContextFactory.createContext(new Class[] {Employee.class}, null);

    Binder binder = ctx.createBinder();

    JAXBElement elem = binder.unmarshal(parser.parse(new StringReader(xml)), Employee.class);
    Employee emp = (Employee) elem.getValue();
    emp.address.city = "Toronto";

    binder.updateXML(emp.address);

    JAXBXMLComparer comparer = new JAXBXMLComparer();
    assertTrue(
        "Marshalled document does not match the control document.",
        comparer.isNodeEqual(controlDocument, ((Node) binder.getXMLNode(emp)).getOwnerDocument()));
  }
 public void testCreateContextWith_String_NullClassLoader_Map() throws JAXBException {
   JAXBContextFactory.createContext(
       "org.eclipse.persistence.testing.jaxb.jaxbcontext", null, null);
 }
 public void testCreateContextWith_TypeArray_Map_NullClassLoader() throws JAXBException {
   Type[] types = new Type[1];
   types[0] = List.class;
   JAXBContextFactory.createContext(types, null, null);
 }
 public void testCreateContextWithStringClass() throws JAXBException {
   Class[] classes = new Class[1];
   classes[0] = String.class;
   JAXBContextFactory.createContext(classes, null);
 }
 public void testCreateContextWith_ClassArray_Map_NullClassLoader_Map() throws JAXBException {
   Class[] classes = new Class[1];
   int[] ints = new int[1];
   classes[0] = ints.getClass();
   JAXBContextFactory.createContext(classes, null, null);
 }
 public void testCreateContextWithObjectFactory() throws Exception {
   JAXBContext context =
       JAXBContextFactory.createContext(
           "org.eclipse.persistence.testing.jaxb.jaxbcontext",
           Thread.currentThread().getContextClassLoader());
 }
 public void testCreateContextXmlAnyAttributeSubTypeMap() throws Exception {
   JAXBContextFactory.createContext(new Class[] {XmlAnyAttributeSubTypeMapModel.class}, null);
 }
 @Override
 protected void setUp() throws Exception {
   JAXBContext jc = JAXBContextFactory.createContext(new Class[] {FlushRoot.class}, null);
   marshaller = jc.createMarshaller();
   unmarshaller = jc.createUnmarshaller();
 }
 public void testCreateAbstractClassWithMultiArgConstructor() throws JAXBException {
   Class[] classes = new Class[1];
   classes[0] = AbstractClassWithMultiArgConstructor.class;
   JAXBContextFactory.createContext(classes, null);
 }
 public void testCreateContextWithGenerics() throws JAXBException {
   Class[] classes = new Class[1];
   classes[0] = ConcreteClassWithGenerics.class;
   JAXBContextFactory.createContext(classes, null);
 }
 public void testCreateContextWithIntArrayClass() throws JAXBException {
   Class[] classes = new Class[1];
   int[] ints = new int[1];
   classes[0] = ints.getClass();
   JAXBContextFactory.createContext(classes, null);
 }