// Create an object using ObjectFactory class generated by XJC
// during
// mapping generation
ObjectFactory factory = new ObjectFactory();
Catalog catalog = factory.createCatalog();
Catalog.Book book = factory.createCatalogBook();
book.setAuthor("Dominique");
book.setDescription("Spring par la pratique");
book.setGenre("IT");
book.setId("bk155");
book.setPrice(22.50f);
book.setTitle("Spring");
book.setAvailable(Boolean.TRUE);
book.setPublishDate(DatatypeFactory.newInstance().newXMLGregorianCalendar((GregorianCalendar) GregorianCalendar.getInstance()));
catalog.getBook().add(book);
// Marshall Object to XML file
marshaller.marshal(catalog, sampleFile);
// Save "catalog" object hash code in order to check that object
// reference are different before and after unmarshalling
int hcBefore = catalog.hashCode();
/* Step 2 : Unmarshall XML to Object */
// Create an Unmarshaller
Unmarshaller unmarshaller = context.createUnmarshaller();
// Unmarshall XML to Object
catalog = (Catalog) unmarshaller.unmarshal(sampleFile);
/* Step 3 : Check behavior of the "StockAdapter" adapter */
Assert.assertFalse((hcBefore == catalog.hashCode()));
Assert.assertNotNull(catalog);
Assert.assertEquals(catalog.getBook().size(), 1);
Assert.assertNotNull(catalog.getBook().get(0));
Assert.assertTrue(catalog.getBook().get(0).isAvailable());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}