aslak

Arquillian - URL injection

Feb 14th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. @RunWith(Arquillian.class)
  2. public class LocalRunServletTestCase
  3. {
  4.    @Deployment(testable = false)
  5.    public static WebArchive createDeployment()
  6.    {
  7.       return ShrinkWrap.create(WebArchive.class, "test.war")
  8.                .addClass(TestServlet.class);
  9.    }
  10.    
  11.    @Test
  12.    public void shouldBeAbleToInjectBaseHTTPContext(@ArquillianResource URL httpContext) throws Exception
  13.    {
  14.       String body = readAllAndClose(new URL(httpContext, "/test/TestServlet").openStream());
  15.      
  16.       Assert.assertEquals(
  17.             "Verify that the servlet was deployed and returns expected result",
  18.             "hello",
  19.             body);
  20.    }
  21.    
  22.    @Test
  23.    public void shouldBeAbleToInjectBaseServletContext(@ArquillianResource(TestServlet.class) URL testServlet) throws Exception
  24.    {
  25.       String body = readAllAndClose(testServlet.openStream());
  26.      
  27.       Assert.assertEquals(
  28.             "Verify that the servlet was deployed and returns expected result",
  29.             "hello",
  30.             body);
  31.    }
  32.  
  33.    private String readAllAndClose(InputStream is) throws Exception
  34.    {
  35.       ByteArrayOutputStream out = new ByteArrayOutputStream();
  36.       try
  37.       {
  38.          int read;
  39.          while( (read = is.read()) != -1)
  40.          {
  41.             out.write(read);
  42.          }
  43.       }
  44.       finally
  45.       {
  46.          try { is.close(); } catch (Exception e) { }
  47.       }
  48.       return out.toString();
  49.    }
  50. }
Add Comment
Please, Sign In to add comment