Search in sources :

Example 11 with TesterContext

use of org.apache.tomcat.unittest.TesterContext in project tomcat70 by apache.

the class TestPersistentManager method testMinIdleSwap.

@Test
public void testMinIdleSwap() throws Exception {
    PersistentManager manager = new PersistentManager();
    manager.setStore(new TesterStore());
    Host host = new TesterHost();
    Context context = new TesterContext();
    context.setParent(host);
    manager.setContainer(context);
    manager.setMaxActiveSessions(2);
    manager.setMinIdleSwap(0);
    manager.start();
    // Create the maximum number of sessions
    manager.createSession(null);
    manager.createSession(null);
    // Given the minIdleSwap settings, this should swap one out to get below
    // the limit
    manager.processPersistenceChecks();
    Assert.assertEquals(1, manager.getActiveSessions());
    Assert.assertEquals(2, manager.getActiveSessionsFull());
    manager.createSession(null);
    Assert.assertEquals(2, manager.getActiveSessions());
    Assert.assertEquals(3, manager.getActiveSessionsFull());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) TesterHost(org.apache.tomcat.unittest.TesterHost) TesterHost(org.apache.tomcat.unittest.TesterHost) Host(org.apache.catalina.Host) TesterContext(org.apache.tomcat.unittest.TesterContext) Test(org.junit.Test)

Example 12 with TesterContext

use of org.apache.tomcat.unittest.TesterContext in project tomcat by apache.

the class TestResponse method doTestSendRedirect.

private void doTestSendRedirect(String input, String expectedLocation) throws Exception {
    // Set-up.
    // Note: Not sufficient for testing relative -> absolute
    Connector connector = new Connector();
    org.apache.coyote.Response cResponse = new org.apache.coyote.Response();
    Response response = new Response();
    response.setCoyoteResponse(cResponse);
    Request request = new Request(connector);
    org.apache.coyote.Request cRequest = new org.apache.coyote.Request();
    request.setCoyoteRequest(cRequest);
    Context context = new TesterContext();
    request.getMappingData().context = context;
    response.setRequest(request);
    // Do test
    response.sendRedirect(input);
    String location = response.getHeader("Location");
    Assert.assertEquals(expectedLocation, location);
}
Also used : HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) TesterRequest(org.apache.tomcat.unittest.TesterRequest) TesterContext(org.apache.tomcat.unittest.TesterContext)

Example 13 with TesterContext

use of org.apache.tomcat.unittest.TesterContext in project tomcat by apache.

the class TestAsyncContextImpl method testAsyncListenerSupplyRequestResponse.

@Test
public void testAsyncListenerSupplyRequestResponse() {
    final ServletRequest servletRequest = EasyMock.createMock(ServletRequest.class);
    final ServletResponse servletResponse = EasyMock.createMock(ServletResponse.class);
    final AsyncListener listener = new AsyncListener() {

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        private void checkRequestResponse(AsyncEvent event) {
            Assert.assertEquals(servletRequest, event.getSuppliedRequest());
            Assert.assertEquals(servletResponse, event.getSuppliedResponse());
        }
    };
    final Context context = new TesterContext();
    final Response response = new Response();
    final Request request = new Request(null);
    request.setCoyoteRequest(new org.apache.coyote.Request());
    request.getMappingData().context = context;
    final AsyncContextImpl ac = new AsyncContextImpl(request);
    ac.addListener(listener, servletRequest, servletResponse);
    ac.setStarted(context, request, response, true);
    ac.addListener(listener, servletRequest, servletResponse);
    ac.setErrorState(new Exception(), true);
    ac.fireOnComplete();
}
Also used : AsyncContext(jakarta.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Request(org.apache.catalina.connector.Request) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) AsyncEvent(jakarta.servlet.AsyncEvent) URISyntaxException(java.net.URISyntaxException) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) ServletResponse(jakarta.servlet.ServletResponse) Response(org.apache.catalina.connector.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AsyncListener(jakarta.servlet.AsyncListener) TesterContext(org.apache.tomcat.unittest.TesterContext) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 14 with TesterContext

use of org.apache.tomcat.unittest.TesterContext in project tomcat by apache.

the class TestPersistentManager method testMinIdleSwap.

@Test
public void testMinIdleSwap() throws Exception {
    PersistentManager manager = new PersistentManager();
    manager.setStore(new TesterStore());
    Host host = new TesterHost();
    Context context = new TesterContext();
    context.setParent(host);
    manager.setContext(context);
    manager.setMaxActiveSessions(2);
    manager.setMinIdleSwap(0);
    manager.start();
    // Create the maximum number of sessions
    manager.createSession(null);
    manager.createSession(null);
    // Given the minIdleSwap settings, this should swap one out to get below
    // the limit
    manager.processPersistenceChecks();
    Assert.assertEquals(1, manager.getActiveSessions());
    Assert.assertEquals(2, manager.getActiveSessionsFull());
    manager.createSession(null);
    Assert.assertEquals(2, manager.getActiveSessions());
    Assert.assertEquals(3, manager.getActiveSessionsFull());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) TesterHost(org.apache.tomcat.unittest.TesterHost) TesterHost(org.apache.tomcat.unittest.TesterHost) Host(org.apache.catalina.Host) TesterContext(org.apache.tomcat.unittest.TesterContext) Test(org.junit.Test)

Example 15 with TesterContext

use of org.apache.tomcat.unittest.TesterContext in project tomcat by apache.

the class TestPersistentManager method testBug62175.

@Test
public void testBug62175() throws Exception {
    PersistentManager manager = new PersistentManager();
    AtomicInteger sessionExpireCounter = new AtomicInteger();
    Store mockStore = EasyMock.createNiceMock(Store.class);
    EasyMock.expect(mockStore.load(EasyMock.anyString())).andAnswer(new IAnswer<Session>() {

        @Override
        public Session answer() throws Throwable {
            return timedOutSession(manager, sessionExpireCounter);
        }
    }).anyTimes();
    EasyMock.replay(mockStore);
    manager.setStore(mockStore);
    Host host = new TesterHost();
    RequestCachingSessionListener requestCachingSessionListener = new RequestCachingSessionListener();
    Context context = new TesterContext() {

        @Override
        public Object[] getApplicationLifecycleListeners() {
            return new Object[] { requestCachingSessionListener };
        }

        @Override
        public Manager getManager() {
            return manager;
        }
    };
    context.setParent(host);
    Connector connector = EasyMock.createNiceMock(Connector.class);
    Request req = new Request(connector) {

        @Override
        public Context getContext() {
            return context;
        }
    };
    req.setRequestedSessionId("invalidSession");
    HttpServletRequest request = new RequestFacade(req);
    EasyMock.replay(connector);
    requestCachingSessionListener.request = request;
    manager.setContext(context);
    manager.start();
    Assert.assertNull(request.getSession(false));
    Assert.assertEquals(1, sessionExpireCounter.get());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Connector(org.apache.catalina.connector.Connector) Request(org.apache.catalina.connector.Request) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Store(org.apache.catalina.Store) TesterHost(org.apache.tomcat.unittest.TesterHost) Host(org.apache.catalina.Host) RequestFacade(org.apache.catalina.connector.RequestFacade) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) IAnswer(org.easymock.IAnswer) TesterHost(org.apache.tomcat.unittest.TesterHost) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TesterContext(org.apache.tomcat.unittest.TesterContext) Test(org.junit.Test)

Aggregations

TesterContext (org.apache.tomcat.unittest.TesterContext)18 Context (org.apache.catalina.Context)15 Test (org.junit.Test)8 Request (org.apache.catalina.connector.Request)7 TesterRequest (org.apache.tomcat.unittest.TesterRequest)6 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 Host (org.apache.catalina.Host)4 TesterMapRealm (org.apache.catalina.startup.TesterMapRealm)4 TesterHost (org.apache.tomcat.unittest.TesterHost)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 DirContext (javax.naming.directory.DirContext)3 InitialDirContext (javax.naming.directory.InitialDirContext)3 Response (org.apache.catalina.connector.Response)3 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)3 Field (java.lang.reflect.Field)2 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Store (org.apache.catalina.Store)2