@EvoSuiteInclude @Constraints(atMostOnce = true, after = "initServlet") public static AsyncContext getAsyncContext() throws IllegalStateException{ checkInit(); if(getRequest().isAsyncStarted()){ return getRequest().getAsyncContext(); } else { return getRequest().startAsync(); } }
@Override public ServletResponse getResponse() { return EvoServletState.getResponse(); }
@EvoSuiteAssertionOnly @Override public boolean isAsyncSupported() { Servlet sut = EvoServletState.getServlet(); WebServlet annotation = sut.getClass().getAnnotation(WebServlet.class); if(annotation == null){ return false; //TODO: unsure if really correct. need more investigation } return annotation.asyncSupported(); }
EvoServletState.initServlet(servlet); EvoHttpServletRequest req = EvoServletState.getRequest(); req.asPOST(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(val[0]); Assert.assertTrue(!val[1]); EvoServletState.reset(); EvoServletState.initServlet(servlet); req = EvoServletState.getRequest(); req.asGET(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(!val[0]); Assert.assertTrue(val[1]); EvoServletState.reset(); EvoServletState.initServlet(servlet); req = EvoServletState.getRequest(); req.asPUT(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(!val[0]); Assert.assertTrue(!val[1]);
@Test public void testAsyn() throws ServletException, IOException { HttpServlet servlet = new AnnotatedServlet_for_testAsyn(); EvoServletState.initServlet(servlet); boolean supported = EvoServletState.getRequest().isAsyncSupported(); Assert.assertTrue(supported); servlet.service(EvoServletState.getRequest(), EvoServletState.getResponse()); String body = EvoServletState.getResponse().getBody(); Assert.assertEquals("foo", body); }
@Test public void testNoAsyn() throws ServletException { HttpServlet servlet = new HttpServlet() { @Override public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }; EvoServletState.initServlet(servlet); boolean supported = EvoServletState.getRequest().isAsyncSupported(); Assert.assertFalse(supported); }
@Override public ServletRequest getRequest() { return EvoServletState.getRequest(); }
@EvoSuiteInclude @Constraints(atMostOnce = true, after = "initServlet") public static EvoHttpServletRequest getRequest() throws IllegalStateException{ checkInit(); if(req == null){ req = new EvoHttpServletRequest(); } return req; }
@EvoSuiteInclude @Constraints(atMostOnce = true, noNullInputs = true, noDirectInsertion = true) public static <T extends Servlet> T initServlet( @BoundInputVariable(initializer = true, atMostOnce = true) T servlet) throws IllegalStateException, IllegalArgumentException, ServletException { if(servlet == null){ throw new IllegalArgumentException("Null servlet"); } if(EvoServletState.servlet != null){ throw new IllegalStateException("Should only be one servlet per test"); } EvoServletState.servlet = servlet; servlet.init(getConfiguration()); TestDataJavaEE.getInstance().setWasAServletInitialized(true); return servlet; }
@Before public void init(){ TestDataJavaEE.getInstance().reset(); EvoServletState.reset(); }
EvoServletState.initServlet(servlet); EvoHttpServletRequest req = EvoServletState.getRequest(); req.asPOST(); req.asTextXml(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(EvoServletState.getResponse().getBody().contains("42")); EvoServletState.reset(); EvoServletState.initServlet(servlet); req = EvoServletState.getRequest(); req.asPOST(); req.asMultipartFormData(); Assert.assertFalse(EvoServletState.getResponse().isCommitted()); servlet.service(req, EvoServletState.getResponse()); Assert.assertFalse(EvoServletState.getResponse().getBody().contains("42")); Assert.assertTrue(EvoServletState.getResponse().isCommitted());
@Test public void testParts() throws ServletException, IOException{ HttpServlet servlet = new HttpServlet() { @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Collection<Part> parts = req.getParts(); String s = ""; for(Part p : parts){ Scanner in = new Scanner(p.getInputStream()); s += in.nextLine(); in.close(); } PrintWriter out = resp.getWriter(); out.print(s); out.close(); } }; String msg0 = "foo"; String msg1 = "bar"; EvoServletState.initServlet(servlet); EvoHttpServletRequest req = EvoServletState.getRequest(); req.asPOST(); req.asMultipartFormData(); req.addPart(new EvoPart("first", msg0)); req.addPart(new EvoPart("second", msg1)); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(TestDataJavaEE.getInstance().getViewOfParts().size() == 0); Assert.assertTrue(EvoServletState.getResponse().getBody().equals(msg0+msg1)); }
@EvoSuiteInclude @Constraints(atMostOnce = true, after = "initServlet") public static EvoServletConfig getConfiguration() throws IllegalStateException{ checkInit(); if(config == null){ config = new EvoServletConfig(); } return config; }
@Override public AsyncContext startAsync() throws IllegalStateException { if(!isAsyncSupported() || EvoServletState.getResponse().isCommitted()){ /* TODO Need also to handle: - this request is within the scope of a filter - this method is called again without any asynchronous dispatch (resulting from one of the AsyncContext.dispatch methods), is called outside the scope of any such dispatch, or is called again within the scope of the same dispatch */ throw new IllegalStateException("Cannot start async"); } if(asyncContext==null){ asyncContext = new EvoAsyncContext(); } else { //should re-init, as per JavaDoc } return asyncContext; }
@EvoSuiteInclude @Constraints(atMostOnce = true, after = "initServlet") public static EvoHttpServletResponse getResponse() throws IllegalStateException{ checkInit(); if(resp == null){ resp = new EvoHttpServletResponse(); } return resp; }