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.tools;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertThrows;
15  
16  import com.codebox.bean.JavaBeanTester;
17  
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * Tests for {@link TimeExpression}.
22   */
23  class TimeExpressionTest {
24  
25    @Test
26    void testPrivateConstructor() {
27      JavaBeanTester.builder(TimeExpression.class).testPrivateConstructor();
28    }
29  
30    // --- inSeconds tests ---
31  
32    @Test
33    void testInSecondsSeconds() {
34      assertEquals(30, TimeExpression.inSeconds("30s"));
35      assertEquals(1, TimeExpression.inSeconds("1s"));
36      assertEquals(0, TimeExpression.inSeconds("0s"));
37    }
38  
39    @Test
40    void testInSecondsMinutes() {
41      assertEquals(60, TimeExpression.inSeconds("1m"));
42      assertEquals(300, TimeExpression.inSeconds("5m"));
43    }
44  
45    @Test
46    void testInSecondsHours() {
47      assertEquals(3600, TimeExpression.inSeconds("1h"));
48      assertEquals(7200, TimeExpression.inSeconds("2h"));
49    }
50  
51    @Test
52    void testInSecondsNullReturnsZero() {
53      assertEquals(0, TimeExpression.inSeconds(null));
54    }
55  
56    @Test
57    void testInSecondsEmptyReturnsZero() {
58      assertEquals(0, TimeExpression.inSeconds(""));
59    }
60  
61    @Test
62    void testInSecondsInvalidFormatThrows() {
63      assertThrows(IllegalArgumentException.class, () -> TimeExpression.inSeconds("100"));
64      assertThrows(IllegalArgumentException.class, () -> TimeExpression.inSeconds("100d"));
65      assertThrows(IllegalArgumentException.class, () -> TimeExpression.inSeconds("abc"));
66    }
67  
68    // --- dataPoints tests ---
69  
70    @Test
71    void testDataPointsFromExpressions() {
72      long result = TimeExpression.dataPoints("10s", "1m");
73      assertEquals(6, result);
74    }
75  
76    @Test
77    void testDataPointsZeroPeriod() {
78      assertEquals(0, TimeExpression.dataPoints(0L, 100L));
79    }
80  
81    @Test
82    void testDataPointsNegativePeriod() {
83      assertEquals(0, TimeExpression.dataPoints(-1L, 100L));
84    }
85  
86    @Test
87    void testDataPointsExact() {
88      assertEquals(10, TimeExpression.dataPoints(10L, 100L));
89      assertEquals(6, TimeExpression.dataPoints(600L, 3600L));
90    }
91  
92    // --- cronExpression tests ---
93  
94    @Test
95    void testCronExpressionEverySecond() {
96      String cron = TimeExpression.cronExpression(1L, 0L);
97      assertEquals("* * * * * ?", cron);
98    }
99  
100   @Test
101   void testCronExpressionEvery30Seconds() {
102     String cron = TimeExpression.cronExpression(30L, 0L);
103     assertEquals("0/30 * * * * ?", cron);
104   }
105 
106   @Test
107   void testCronExpressionEvery30SecondsWithPhase() {
108     String cron = TimeExpression.cronExpression(30L, 5L);
109     assertEquals("5/30 * * * * ?", cron);
110   }
111 
112   @Test
113   void testCronExpressionEveryMinute() {
114     // period=1 minute, phase=0: cronSubexpression(1, 0) returns "*"
115     String cron = TimeExpression.cronExpression(60L, 0L);
116     assertEquals("0 * * * * ?", cron);
117   }
118 
119   @Test
120   void testCronExpressionEvery5Minutes() {
121     String cron = TimeExpression.cronExpression(300L, 0L);
122     assertEquals("0 0/5 * * * ?", cron);
123   }
124 
125   @Test
126   void testCronExpressionEvery5MinutesWithPhase() {
127     String cron = TimeExpression.cronExpression(300L, 60L);
128     assertEquals("0 1/5 * * * ?", cron);
129   }
130 
131   @Test
132   void testCronExpressionEveryHour() {
133     // period=1 hour, phase=0: cronSubexpression(1, 0) returns "*"
134     String cron = TimeExpression.cronExpression(3600L, 0L);
135     assertEquals("0 0 * * * ?", cron);
136   }
137 
138   @Test
139   void testCronExpressionEvery2Hours() {
140     String cron = TimeExpression.cronExpression(7200L, 0L);
141     assertEquals("0 0 0/2 * * ?", cron);
142   }
143 
144   @Test
145   void testCronExpressionPhaseWrapsAround() {
146     // phase >= period: phase = phase - period
147     String cron = TimeExpression.cronExpression(30L, 35L);
148     // phase = 35 - 30 = 5
149     assertEquals("5/30 * * * * ?", cron);
150   }
151 
152   @Test
153   void testCronExpressionTooLargeThrows() {
154     assertThrows(IllegalArgumentException.class, () -> TimeExpression.cronExpression(86400L, 0L));
155   }
156 
157   @Test
158   void testCronExpressionFromExpressions() {
159     String cron = TimeExpression.cronExpression("30s", "0s");
160     assertEquals("0/30 * * * * ?", cron);
161   }
162 
163   @Test
164   void testCronExpressionSpecificSeconds() {
165     // period=60s (1 min), phase=30s
166     // minutesPeriod=1, minutesPhase=0; secondsPeriod=0, secondsPhase=30
167     // secondsCron = "30", minutesCron = cronSubexpression(1,0) = "*"
168     String cron = TimeExpression.cronExpression(60L, 30L);
169     assertEquals("30 * * * * ?", cron);
170   }
171 }