Make JAXB XML marshaller pretty-print

This commit is contained in:
Andrew Donald Kennedy 2012-02-10 12:30:40 +00:00
parent 4fdaa3b6a6
commit 7ca842f44c
2 changed files with 4 additions and 7 deletions

View File

@ -28,7 +28,6 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.xml.XMLParser;
/**
@ -49,6 +48,7 @@ public class JAXBParser implements XMLParser {
try {
JAXBContext context = JAXBContext.newInstance(type);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter writer = new StringWriter();
marshaller.marshal(src, writer);
return writer.toString();
@ -57,7 +57,6 @@ public class JAXBParser implements XMLParser {
}
}
@SuppressWarnings("unchecked")
@Override
public <T> T fromXML(final String xml, final Class<T> type) throws IOException {
try {

View File

@ -18,7 +18,7 @@
*/
package org.jclouds.rest.binders;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.*;
import java.net.URI;
@ -52,8 +52,7 @@ public class BindToXMLPayloadTest {
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
request = binder.bindToRequest(request, obj);
assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER
+ "<test><elem>Hello World</elem></test>");
assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER + "\n<test>\n <elem>Hello World</elem>\n</test>\n");
assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.APPLICATION_XML);
}
@ -72,8 +71,7 @@ public class BindToXMLPayloadTest {
.build();
request = binder.bindToRequest(request, obj);
assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER
+ "<test><elem>Hello World</elem></test>");
assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER + "\n<test>\n <elem>Hello World</elem>\n</test>\n");
assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.APPLICATION_XML);
}