}
@Test
public void testUpdatePackageFromJAXB() throws Exception {
//create a package for testing
Package p = createTestPackage("testUpdatePackageFromJAXB");
p.setDescription("desc for testUpdatePackageFromJAXB");
JAXBContext context = JAXBContext.newInstance(p.getClass());
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(p, sw);
String xml = sw.toString();
URL url = new URL(generateBaseUrl() + "/packages");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
String userpassword = "test" + ":" + "password";
byte[] authEncBytes = Base64.encodeBase64(userpassword.getBytes());
connection.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", MediaType.APPLICATION_XML);
connection.setRequestProperty("Content-Length", Integer.toString(xml.getBytes().length));
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (xml);
wr.flush ();
wr.close ();
assertEquals (200, connection.getResponseCode());
assertEquals(MediaType.APPLICATION_XML, connection.getContentType());
//System.out.println(IOUtils.toString(connection.getInputStream()));
Package result = unmarshalPackageXML(connection.getInputStream());
assertEquals("testUpdatePackageFromJAXB", result.getTitle());
assertEquals("desc for testUpdatePackageFromJAXB", result.getDescription());
assertEquals(new URL(generateBaseUrl() + "/packages/testUpdatePackageFromJAXB/source").toExternalForm(), result.getSourceLink().toString());
assertEquals(new URL(generateBaseUrl() + "/packages/testUpdatePackageFromJAXB/binary").toExternalForm(), result.getBinaryLink().toString());
PackageMetadata pm = result.getMetadata();
assertFalse(pm.isArchived());
assertNotNull(pm.getCreated());
assertNotNull(pm.getUuid());
assertNotNull(pm.getLastModified());
//Test update package
Package p2 = createTestPackage("testUpdatePackageFromJAXB");
p2.setDescription("update package testUpdatePackageFromJAXB");
p2.setCheckInComment("checkInComment: update package testUpdatePackageFromJAXB");
JAXBContext context2 = JAXBContext.newInstance(p2.getClass());
Marshaller marshaller2 = context2.createMarshaller();
StringWriter sw2 = new StringWriter();
marshaller2.marshal(p2, sw2);
String xml2 = sw2.toString();
URL url2 = new URL(generateBaseUrl() + "/packages/testUpdatePackageFromJAXB");
HttpURLConnection connection2 = (HttpURLConnection)url2.openConnection();
connection2.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection2.setRequestMethod("PUT");
connection2.setRequestProperty("Content-Type", MediaType.APPLICATION_XML);
connection2.setRequestProperty("Content-Length", Integer.toString(xml2.getBytes().length));
connection2.setUseCaches (false);
//connection2.setDoInput(true);
connection2.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection2.getOutputStream());
out.write(xml2);
out.close();
connection2.getInputStream();
//Verify
URL url3 = new URL(generateBaseUrl() + "/packages/testUpdatePackageFromJAXB");
HttpURLConnection connection3 = (HttpURLConnection)url3.openConnection();
connection3.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection3.setRequestMethod("GET");
connection3.setRequestProperty("Accept", MediaType.APPLICATION_XML);
connection3.connect();
assertEquals (200, connection3.getResponseCode());
assertEquals(MediaType.APPLICATION_XML, connection3.getContentType());
//System.out.println("------------------------");
//System.out.println(IOUtils.toString(connection.getInputStream()));
Package p3 = unmarshalPackageXML(connection3.getInputStream());
assertEquals("testUpdatePackageFromJAXB", p3.getTitle());
assertEquals("update package testUpdatePackageFromJAXB", p3.getDescription());
assertEquals("checkInComment: update package testUpdatePackageFromJAXB", p3.getCheckInComment());
//assertEquals("version3", p3.getCheckInComment());
assertEquals(new URL(generateBaseUrl() + "/packages/testUpdatePackageFromJAXB/source").toExternalForm(), p3.getSourceLink().toString());
assertEquals(new URL(generateBaseUrl() + "/packages/testUpdatePackageFromJAXB/binary").toExternalForm(), p3.getBinaryLink().toString());
PackageMetadata pm3 = p3.getMetadata();
assertFalse(pm3.isArchived());
assertNotNull(pm3.getCreated());
assertNotNull(pm3.getUuid());
assertNotNull(pm3.getLastModified());
}