View Javadoc
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  
20  package org.apache.wss4j.common.cache;
21  
22  import java.time.Duration;
23  import java.time.Instant;
24  import java.time.temporal.ChronoUnit;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertNotNull;
30  import static org.junit.jupiter.api.Assertions.assertTrue;
31  
32  /**
33   * Some unit tests for the EHCacheExpiry implementation
34   */
35  public class EHCacheExpiryTest {
36  
37      @Test
38      public void testNoExpirySpecified() {
39          EHCacheExpiry cacheExpiry = new EHCacheExpiry();
40  
41          Duration expiryForCreation =
42                  cacheExpiry.getExpiryForCreation("xyz",
43                          new EHCacheValue("xyz", null));
44          assertNotNull(expiryForCreation);
45  
46          assertEquals(EHCacheExpiry.DEFAULT_TTL, expiryForCreation.getSeconds());
47      }
48  
49      @Test
50      public void testExpirySpecified() {
51          EHCacheExpiry cacheExpiry = new EHCacheExpiry();
52  
53          Duration expiryForCreation =
54                  cacheExpiry.getExpiryForCreation("xyz",
55                          new EHCacheValue("xyz", Instant.now().plusSeconds(30L)));
56          assertNotNull(expiryForCreation);
57  
58          // Some loose boundary checking to allow for slow tests
59          assertTrue(expiryForCreation.getSeconds() <= 30L);
60          assertTrue(expiryForCreation.getSeconds() > 30L - 5L);
61      }
62  
63      @Test
64      public void testExpirySpecified2() {
65          EHCacheExpiry cacheExpiry = new EHCacheExpiry();
66  
67          Duration expiryForCreation =
68                  cacheExpiry.getExpiryForCreation("xyz",
69                          new EHCacheValue("xyz", Instant.now().plus(6L, ChronoUnit.HOURS)));
70          assertNotNull(expiryForCreation);
71  
72          // Some loose boundary checking to allow for slow tests
73          assertTrue(expiryForCreation.getSeconds() <= 6 * 60 * 60L);
74          assertTrue(expiryForCreation.getSeconds() > 6 * 60 * 60L - 5L);
75      }
76  
77      @Test
78      public void testNegativeExpirySpecified() {
79          EHCacheExpiry cacheExpiry = new EHCacheExpiry();
80  
81          Duration expiryForCreation =
82                  cacheExpiry.getExpiryForCreation("xyz",
83                          new EHCacheValue("xyz", Instant.now().minusSeconds(30L)));
84          assertNotNull(expiryForCreation);
85  
86          assertEquals(EHCacheExpiry.DEFAULT_TTL, expiryForCreation.getSeconds());
87      }
88  
89      @Test
90      public void testHugeExpirySpecified() {
91          EHCacheExpiry cacheExpiry = new EHCacheExpiry();
92  
93          Duration expiryForCreation =
94                  cacheExpiry.getExpiryForCreation("xyz",
95                          new EHCacheValue("xyz", Instant.now().plus(14, ChronoUnit.HOURS)));
96          assertNotNull(expiryForCreation);
97  
98          assertEquals(EHCacheExpiry.DEFAULT_TTL, expiryForCreation.getSeconds());
99      }
100 }