Want more features on Pastebin? Sign Up, it's FREE!
Guest

Arquillian - URL injection

By: a guest on Feb 14th, 2011  |  syntax: Java  |  size: 1.41 KB  |  views: 303  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }
clone this paste RAW Paste Data