}
public void testUserContextProvider() throws Exception {
HttpClient httpClient = new HttpClient();
User user = new User();
user.setUserName("joedoe@example.com");
JAXBElement<User> element =
new JAXBElement<User>(new QName("http://jaxb.context.tests", "user"), User.class, user);
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
StringWriter sw = new StringWriter();
Marshaller m = context.createMarshaller();
m.marshal(element, sw);
PostMethod postMethod = new PostMethod(getBaseURI());
try {
postMethod.setRequestEntity(new ByteArrayRequestEntity(sw.toString().getBytes(),
"text/xml"));
httpClient.executeMethod(postMethod);
assertEquals(204, postMethod.getStatusCode());
} finally {
postMethod.releaseConnection();
}
GetMethod getMethod = new GetMethod(getBaseURI() + "/joedoe@example.com");
try {
httpClient.executeMethod(getMethod);
assertEquals(200, getMethod.getStatusCode());
Unmarshaller u = context.createUnmarshaller();
element =
u.unmarshal(new StreamSource(getMethod.getResponseBodyAsStream()), User.class);
assertNotNull(element);
user = element.getValue();
assertNotNull(user);
assertEquals("joedoe@example.com", user.getUserName());
} finally {
getMethod.releaseConnection();
}
}