ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/session/TestStandardSessionAccessor.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/session/TestStandardSessionAccessor.java
Status
scanned
Type
file
Name
TestStandardSessionAccessor.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
4656 (4.5 KB)
MD5
3be3dff455ed76bcfbd66c2fa709e8bd
SHA1
453491d84bdd7afa32b83f88cc021ac3273006ed
SHA256
c4cff48955c8c35b796ec9fe332abeb492f424adfd44a0048fffbe3eae4be60f
SHA512

      
    
SHA1_git
7dd3175cfc00de40cdd12a8c9e44dd6edfa9e609
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestStandardSessionAccessor.java | 4.5 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.catalina.session; import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; import jakarta.servlet.http.HttpSession.Accessor; import org.junit.Assert; import org.junit.Test; import org.apache.catalina.Context; import org.apache.catalina.Session; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; public class TestStandardSessionAccessor extends TomcatBaseTest { @Test public void testLastAccess() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = getProgrammaticRootContext(); CountDownLatch latch = new CountDownLatch(1); Tomcat.addServlet(ctx, "accessor", new AccessorServlet(latch)); ctx.addServletMappingDecoded("/accessor", "accessor"); tomcat.start(); int rc = getUrl("http://localhost:" + getPort() + "/accessor", null, null); Assert.assertEquals(HttpServletResponse.SC_OK, rc); // There should be a single session in the Manager at this point Session[] sessions = ctx.getManager().findSessions(); Assert.assertNotNull(sessions); Assert.assertEquals(1, sessions.length); Session session = sessions[0]; // Check the current last accessed time long lastAccessedA = session.getLastAccessedTime(); // Wait 1 second Thread.sleep(1000); // Release the latch latch.countDown(); // The last accessed time should have increased - allow up to 10 seconds for that to happen int count = 0; long lastAccessedB = session.getLastAccessedTime(); while (lastAccessedB == lastAccessedA && count < 200) { Thread.sleep(50); lastAccessedB = session.getLastAccessedTime(); count++; } Assert.assertTrue("Session last access time not updated", lastAccessedB > lastAccessedA); } public static class AccessorServlet extends HttpServlet { private static final long serialVersionUID = 1L; private final CountDownLatch latch; public AccessorServlet(CountDownLatch latch) { this.latch = latch; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession httpSession = req.getSession(); Accessor accessor = httpSession.getAccessor(); Thread t = new Thread(new AccessorRunnable(accessor, latch)); t.start(); } } public static class AccessorRunnable implements Runnable { private final Accessor accessor; private final CountDownLatch latch; public AccessorRunnable(Accessor accessor, CountDownLatch latch) { this.accessor = accessor; this.latch = latch; } @Override public void run() { try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } accessor.access(new SessionUpdater()); } } public static class SessionUpdater implements Consumer<HttpSession> { @Override public void accept(HttpSession httpSession) { // NO-OP - process of accessing the session will update the last accessed time. } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
25.54
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9