1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.session.mgt;
20  
21  import org.apache.shiro.session.ExpiredSessionException;
22  import org.apache.shiro.util.ThreadContext;
23  import org.junit.After;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import java.io.Serializable;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.fail;
31  
32  /**
33   * Unit test for the {@link DelegatingSession} class.
34   */
35  public class DelegatingSessionTest {
36  
37      DelegatingSession session = null;
38      DefaultSessionManager sm = null;
39  
40      @Before
41      public void setup() {
42          ThreadContext.remove();
43          sm = new DefaultSessionManager();
44          this.session = new DelegatingSession(sm, new DefaultSessionKey(sm.start(null).getId()));
45      }
46  
47      @After
48      public void tearDown() {
49          sm.destroy();
50          ThreadContext.remove();
51      }
52  
53      public void sleep(long millis) {
54          try {
55              Thread.sleep(millis);
56          } catch (InterruptedException e) {
57              throw new IllegalStateException(e);
58          }
59      }
60  
61      @Test
62      public void testTimeout() {
63          Serializable origId = session.getId();
64          assertEquals(session.getTimeout(), AbstractSessionManager.DEFAULT_GLOBAL_SESSION_TIMEOUT);
65          session.touch();
66          session.setTimeout(100);
67          assertEquals(100, session.getTimeout());
68          sleep(150);
69          try {
70              session.getTimeout();
71              fail("Session should have expired.");
72          } catch (ExpiredSessionException expected) {
73          }
74      }
75  
76  }