/**
   * Tests XmlAnyObjectMapping configuration via eclipselink-oxm.xml. Here an unmarshal operation is
   * performed. In this case, the 'any' is marked write-only, and should not be read in.
   *
   * <p>Positive test.
   */
  public void testAnyObjectWriteOnlyUnmarshal() {
    // load instance doc
    String src = PATH + "write-only-employee.xml";
    InputStream iDocStream = loader.getResourceAsStream(src);
    if (iDocStream == null) {
      fail("Couldn't load instance doc [" + src + "]");
    }
    JAXBContext jCtx = null;
    try {
      jCtx =
          createContext(
              new Class[] {Employee.class}, CONTEXT_PATH, PATH + "write-only-employee-oxm.xml");
    } catch (JAXBException e1) {
      fail("JAXBContext creation failed: " + e1.getMessage());
    }

    // unmarshal
    Employee ctrlEmp = getControlObject();
    ctrlEmp.stuff = null;
    Employee eObj = null;
    Unmarshaller unmarshaller = jCtx.createUnmarshaller();
    try {
      eObj = (Employee) unmarshaller.unmarshal(iDocStream);
      assertNotNull("Unmarshalled object is null.", eObj);
      assertTrue("Unmarshal failed:  Employee objects are not equal", ctrlEmp.equals(eObj));
    } catch (JAXBException e) {
      e.printStackTrace();
      fail("Unmarshal operation failed.");
    }
  }
  /**
   * Tests XmlAnyObjectMapping configuration via eclipselink-oxm.xml. Here a marshal operation is
   * performed. In this case, the 'any' is marked write-only.
   *
   * <p>Positive test.
   */
  public void testAnyObjectWriteOnlyMarshal() {
    // setup control document
    String src = PATH + "write-only-employee.xml";
    Document testDoc = parser.newDocument();
    Document ctrlDoc = parser.newDocument();
    try {
      ctrlDoc = getControlDocument(src);
    } catch (Exception e) {
      e.printStackTrace();
      fail("An unexpected exception occurred loading control document [" + src + "].");
    }
    JAXBContext jCtx = null;
    try {
      jCtx =
          createContext(
              new Class[] {Employee.class}, CONTEXT_PATH, PATH + "write-only-employee-oxm.xml");
    } catch (JAXBException e1) {
      fail("JAXBContext creation failed: " + e1.getMessage());
    }

    // test marshal
    Employee ctrlEmp = getControlObject();
    Marshaller marshaller = jCtx.createMarshaller();
    try {
      marshaller.marshal(ctrlEmp, testDoc);
      // marshaller.marshal(ctrlEmp, System.out);
      assertTrue("Document comparison failed unxepectedly: ", compareDocuments(ctrlDoc, testDoc));
    } catch (JAXBException e) {
      e.printStackTrace();
      fail("Marshal operation failed.");
    }
  }
Ejemplo n.º 3
0
 public JAXBValueStore(JAXBHelperContext aJAXBHelperContext, Object anEntity) {
   this.jaxbHelperContext = aJAXBHelperContext;
   this.listWrappers = new WeakHashMap<Property, JAXBListWrapper>();
   JAXBContext jaxbContext = (JAXBContext) jaxbHelperContext.getJAXBContext();
   this.descriptor =
       (XMLDescriptor) jaxbContext.getXMLContext().getSession(anEntity).getDescriptor(anEntity);
   this.entity = anEntity;
 }
Ejemplo n.º 4
0
 JAXBContextWrapper(javax.xml.bind.JAXBContext cxt, Map<TypeInfo, TypeMappingInfo> map) {
   jaxbContext = (org.eclipse.persistence.jaxb.JAXBContext) cxt;
   infoMap = map;
   mpool =
       new ObjectPool<JAXBMarshaller>() {
         protected JAXBMarshaller newInstance() {
           try {
             return (JAXBMarshaller) jaxbContext.createMarshaller();
           } catch (JAXBException e) {
             e.printStackTrace();
             throw new DatabindingException(e);
           }
         }
       };
   upool =
       new ObjectPool<JAXBUnmarshaller>() {
         protected JAXBUnmarshaller newInstance() {
           try {
             return (JAXBUnmarshaller) jaxbContext.createUnmarshaller();
           } catch (JAXBException e) {
             e.printStackTrace();
             throw new DatabindingException(e);
           }
         }
       };
   wrapperAccessors = new HashMap<Class<?>, JAXBWrapperAccessor>();
   hasSwaRef = jaxbContext.hasSwaRef();
 }
  /** 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());
    }
  }
 private void unmarshal(JAXBContext jCtx, String inputType, Object foo, String instanceDoc) {
   // setup control document
   Document ctrlDoc = parser.newDocument();
   try {
     ctrlDoc = getControlDocument(instanceDoc);
   } catch (Exception e) {
     e.printStackTrace();
     fail("An unexpected exception occurred loading control document [" + instanceDoc + "].");
   }
   // unmarshal
   Object obj = null;
   try {
     obj = jCtx.createUnmarshaller().unmarshal(ctrlDoc);
   } catch (JAXBException e) {
     e.printStackTrace();
     fail("An unexpected exception occurred during unmarshal [" + inputType + "]");
   }
   assertNotNull("Unmarshal [" + inputType + "] failed - returned object is null", obj);
   assertEquals("Unmarshal [" + inputType + "] failed - objects are not equal", obj, foo);
 }
 private void marshal(JAXBContext jCtx, String inputType, Object foo, String instanceDoc) {
   // setup control document
   Document testDoc = parser.newDocument();
   Document ctrlDoc = parser.newDocument();
   try {
     ctrlDoc = getControlDocument(instanceDoc);
   } catch (Exception e) {
     e.printStackTrace();
     fail("An unexpected exception occurred loading control document [" + instanceDoc + "].");
   }
   // marshal
   try {
     jCtx.createMarshaller().marshal(foo, testDoc);
     // jCtx.createMarshaller().marshal(foo, System.out);
     // System.out.println("\n");
   } catch (JAXBException e) {
     e.printStackTrace();
     fail("An unexpected exception occurred during marshal [" + inputType + "]");
   }
   assertTrue(
       "Marshal [" + inputType + "] failed - documents are not equal: ",
       compareDocuments(ctrlDoc, testDoc));
 }
 @Override
 public void setUp() throws Exception {
   JAXBContext ctx = (JAXBContext) JAXBContextFactory.createContext(new Class[] {Foo.class}, null);
   this.project = ctx.getXMLContext().getSession(0).getProject();
 }
Ejemplo n.º 9
0
 public QName getTypeName(TypeInfo tr) {
   if (typeNames == null) typeNames = jaxbContext.getTypeMappingInfoToSchemaType();
   TypeMappingInfo tmi = infoMap.get(tr);
   return typeNames.get(tmi);
 }
Ejemplo n.º 10
0
 public void generateSchema(SchemaOutputResolver outputResolver) throws IOException {
   jaxbContext.generateSchema(outputResolver);
 }
Ejemplo n.º 11
0
 public Unmarshaller createUnmarshaller() throws JAXBException {
   return jaxbContext.createUnmarshaller();
 }