1. Create an uploadservlet class
package com.anllin.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import static com.anllin.servlet.XmlUtils.*;
public class UploadServlet extends HttpServlet
{
private static final long serialVersionUID = 7320350794641788612L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String ipAndPort = String.format("http://%s:%s", req.getServerName(), req.getLocalPort());
String serviceProviderXml = constructResponseXMl(ipAndPort);
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/xml");
PrintWriter out = resp.getWriter();
out.append(serviceProviderXml);
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
doGet(req, resp);
}
protected String constructResponseXMl(String ipAndPort)
{
Document doc = ConstructDocument();
Element root = constructElement("ServiceProvider");
doc.addContent(root);
Element title = constructElement("title", "Test");
Element description = constructElement("description", "Upload data in everyday");
Element url = constructElement("url", ipAndPort + "/test/upload");
constructChildTree(root, title, description, url);
Format format = getFormat("utf-8", " ");
String xml = getXMLStringFromDocunment(doc, format);
return xml;
}
}
2. Create a helper class xmlutils
package com.anllin.servlet;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class XmlUtils
{
public static Element parse(String xml)
{
try
{
SAXBuilder builder = new SAXBuilder();
StringReader sr = new StringReader(xml);
Document doc = builder.build(sr);
return doc.getRootElement();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static Document ConstructDocument()
{
return new Document();
}
public static Element constructElement(String name)
{
return new Element(name);
}
public static Element constructElement(String name, String text)
{
return new Element(name).setText(text);
}
public static Element constructChildTree(Element father, Element... childs)
{
List<Element> list = Arrays.asList(childs);
father.addContent(list);
return father;
}
public static Format getFormat(String encoding, String indent)
{
return Format.getPrettyFormat().setEncoding(encoding).setIndent(indent);
}
public static XMLOutputter getXMLOutputter(Format format)
{
return new XMLOutputter(format);
}
public static String getXMLStringFromDocunment(Document doc, Format format)
{
return getXMLOutputter(format).outputString(doc);
}
}
3. Create a mockedtests class
package com.anllin.servlet;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.runner.RunWith;
@RunWith(JMock.class)
public class MockedTests
{
protected Mockery mockery = new JUnit4Mockery();
}
4. Use jmock for testing
package com.anllin.servlet;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jdom.Element;
import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
public class UploadServletTest extends MockedTests
{
private UploadServlet uploadServlet;
private HttpServletRequest req;
private HttpServletResponse resp;
private ByteArrayOutputStream out;
@Before
public void setUp() throws Exception
{
uploadServlet = new UploadServlet();
req = mockery.mock(HttpServletRequest.class);
resp = mockery.mock(HttpServletResponse.class);
out = new ByteArrayOutputStream();
}
@Test
public void testDoGet() throws Exception
{
mockery.checking(new Expectations()
{
{
one(req).getServerName();
will(returnValue("192.168.0.62"));
one(req).getLocalPort();
will(returnValue(8080));
one(resp).setCharacterEncoding("utf-8");
one(resp).setContentType("text/xml");
PrintWriter writer = new PrintWriter(out);
allowing(resp).getWriter();
will(returnValue(writer));
}
});
uploadServlet.doGet(req, resp);
Element root = XmlUtils.parse(out.toString());
assertTrue(root.getChildren().size() > 0);
}
}
The test result is output as follows:
<?xml version="1.0" encoding="utf-8"?>
<ServiceProvider>
<title>Test</title>
<description>Upload data in everyday</description>
<url>http://192.168.0.62:8080/test/upload</url>
</ServiceProvider>