View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.model;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertNotNull;
15  import static org.junit.jupiter.api.Assertions.assertNull;
16  import static org.junit.jupiter.api.Assertions.assertTrue;
17  
18  import com.codebox.bean.JavaBeanTester;
19  
20  import java.util.Date;
21  
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * The Class ApplicationSessionTest.
26   */
27  class ApplicationSessionTest {
28  
29    /**
30     * Javabean tester.
31     */
32    @Test
33    void javabeanTester() {
34      JavaBeanTester.builder(ApplicationSession.class).loadData().test();
35    }
36  
37    @Test
38    void testGetAgeWithCreationTime() {
39      ApplicationSession session = new ApplicationSession();
40      Date past = new Date(System.currentTimeMillis() - 5000);
41      session.setCreationTime(past);
42      assertTrue(session.getAge() >= 5000);
43    }
44  
45    @Test
46    void testGetAgeWithNullCreationTime() {
47      ApplicationSession session = new ApplicationSession();
48      // creationTime is null by default
49      assertEquals(0, session.getAge());
50    }
51  
52    @Test
53    void testGetIdleTimeWithLastAccessTime() {
54      ApplicationSession session = new ApplicationSession();
55      Date past = new Date(System.currentTimeMillis() - 3000);
56      session.setLastAccessTime(past);
57      assertTrue(session.getIdleTime() >= 3000);
58    }
59  
60    @Test
61    void testGetIdleTimeWithNullLastAccessTime() {
62      ApplicationSession session = new ApplicationSession();
63      // No last access time - should fall back to getAge()
64      assertEquals(0, session.getIdleTime());
65    }
66  
67    @Test
68    void testGetExpiryTimePositiveMaxIdleTime() {
69      ApplicationSession session = new ApplicationSession();
70      session.setMaxIdleTime(60000); // 60 seconds
71      Date expiry = session.getExpiryTime();
72      assertNotNull(expiry);
73      assertTrue(expiry.getTime() > System.currentTimeMillis());
74    }
75  
76    @Test
77    void testGetExpiryTimeZeroMaxIdleTime() {
78      ApplicationSession session = new ApplicationSession();
79      session.setMaxIdleTime(0);
80      assertNull(session.getExpiryTime());
81    }
82  
83    @Test
84    void testGetExpiryTimeNegativeMaxIdleTime() {
85      ApplicationSession session = new ApplicationSession();
86      session.setMaxIdleTime(-1);
87      assertNull(session.getExpiryTime());
88    }
89  
90    @Test
91    void testGetCreationTimeReturnsDefensiveCopy() {
92      ApplicationSession session = new ApplicationSession();
93      Date original = new Date(1000L);
94      session.setCreationTime(original);
95      Date retrieved = session.getCreationTime();
96      assertNotNull(retrieved);
97      assertEquals(1000L, retrieved.getTime());
98    }
99  
100   @Test
101   void testGetCreationTimeWhenNull() {
102     ApplicationSession session = new ApplicationSession();
103     assertNull(session.getCreationTime());
104   }
105 
106   @Test
107   void testGetLastAccessTimeWhenNull() {
108     ApplicationSession session = new ApplicationSession();
109     assertNull(session.getLastAccessTime());
110   }
111 }