/** * 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."); } }
/** Create the control Employee. */ private Employee getControlObject() { Employee ctrlEmp = new Employee(); Element elt = null; try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); elt = doc.createElementNS(OTHER_NS, OTHER); elt.appendChild(doc.createTextNode(STUFF)); } catch (ParserConfigurationException e) { e.printStackTrace(); } ctrlEmp.stuff = elt; return ctrlEmp; }
protected Object getControlObject() { Employee emp = new Employee(); emp.a = 1; emp.b = "3"; emp.stuff = new ArrayList(); emp.stuff.add("blah."); Element elem = parser.newDocument().createElementNS("extra", "stuff"); elem.setTextContent("This is my stuff."); elem.setAttributeNS(XMLConstants.XMLNS_URL, XMLConstants.XMLNS, "extra"); emp.stuff.add(elem); emp.stuff.add("lame."); return emp; }