Friday, January 22, 2010

Unit Testing Restful Web Services using Restlet and Jaxb

One way to post data to a Restful web services is to Serialize your object using Jaxb (Java Architecture for XML Binding) when you are using Java.  Since you are dealing with Web Services you client code could also be in .Net, javascript, PHP, Rails, etc.  But, in this post we are assuming your client is Java based.  If you Object class is in a separate project from your Test project, one thing that will cause you to pull your hair out is you will get : JAXBException stating errors creating marshaller or unmarshaller, unless you place a jaxb.index file with your Object class in the same package. For example, if the Object that you are Serializing is Sample.java with the package com.acme.businessobjects, then you have to place a jaxb.index file with the package com.acme.businessobjects. Within the jaxb.index file just place one line with just the class name: Sample, and that's it.
Here is link to javadoc that discuss this: http://java.sun.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html
The best way to understand Jaxb is to create a sample object called Sample, and create a test case
To demonstrate this issue create 2 projects , one that contains the Sample.java class, and one the contains you Test case. Note: Initially do not place a jaxb.index file with your Sample.java, this way you will see the JaxbException. After you see that you get the JaxbException, then place an jaxb.index file with your Sample.java, and rebuild your project. The second time around, once you have an jaxb.index file, your JAXBException will eliminated.
The test case uses a schema file named: sample.xsd, this is created using Sun's schemagen tool that comes with Java. From the command line: schemagen path/Sample.java. This creates a file named schema1.xsd, rename this file and copy to your test project folder. For detailed info see: Sun JAXB Tutorial
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Sample {
@XmlElement(name = "value")
private String val;

public Sample() {
}

public Sample(String val) {
this.val = val;
}

public String getVal() {
return val;
}

public void setVal(String val) {
this.val = val;
}

@Override
public String toString() {
return "Sample [val=" + val + "]";
}
}
This test case below tests both Marshalling and Unmarshalling using Jaxb.
public void testGenerateMarshallerToFile() throws JAXBException, FileNotFoundException, SAXException
{
JAXBContext context1 = JAXBContext.newInstance(Sample.class);

JAXBContext context2 = JAXBContext.newInstance("com.acme.businessobjects");

Marshaller m = context1.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Sample sample = new Sample();
sample.setVal("Hello");
final File f = new File("src/test/java/com/endurotracker/gwt/test/rest/api/sample.xml");
m.marshal(sample, new FileOutputStream(f));

Unmarshaller um = context2.createUnmarshaller();

um.setSchema(getSchema("Sample.xsd"));

Object bce = um.unmarshal(f);

m.marshal(bce, System.out);



}
To test your Restlet web services with Jaxb, you will need to create a SampleResource class. Luckily, for you there is such a test in the org.restlet.test package, so you will only need to download the latest restlet source code. restlet.org The name of the test class you are interested in is: JaxbIntegrationConverterTestCase

28 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. This comment has been removed by a blog administrator.

    ReplyDelete
  13. This comment has been removed by a blog administrator.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. This comment has been removed by a blog administrator.

    ReplyDelete
  16. This comment has been removed by a blog administrator.

    ReplyDelete
  17. This comment has been removed by a blog administrator.

    ReplyDelete
  18. This comment has been removed by a blog administrator.

    ReplyDelete
  19. This comment has been removed by a blog administrator.

    ReplyDelete
  20. This comment has been removed by a blog administrator.

    ReplyDelete
  21. This comment has been removed by a blog administrator.

    ReplyDelete
  22. This comment has been removed by a blog administrator.

    ReplyDelete
  23. This comment has been removed by a blog administrator.

    ReplyDelete
  24. This comment has been removed by a blog administrator.

    ReplyDelete
  25. This comment has been removed by a blog administrator.

    ReplyDelete
  26. This comment has been removed by a blog administrator.

    ReplyDelete
  27. This comment has been removed by a blog administrator.

    ReplyDelete
  28. This comment has been removed by a blog administrator.

    ReplyDelete