1   package org.apache.maven.plugin.version;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  
24  import java.util.Calendar;
25  import java.util.Date;
26  
27  public class IntervalUtilsTest
28      extends TestCase
29  {
30      
31      private static final long ONE_MINUTE = 60 * 1000;
32      private static final long ONE_HOUR = 60 * ONE_MINUTE;
33      private static final long ONE_DAY = 24 * ONE_HOUR;
34      private static final long ONE_WEEK = 7 * ONE_DAY;
35  
36      public void testOneWeek()
37      {
38          assertEquals( ONE_WEEK, IntervalUtils.parseInterval( "1w" ) );
39      }
40  
41      public void testTwoWeeks()
42      {
43          assertEquals( ( 2 * ONE_WEEK ), IntervalUtils.parseInterval( "2w" ) );
44      }
45  
46      public void testOneDay()
47      {
48          assertEquals( ONE_DAY, IntervalUtils.parseInterval( "1d" ) );
49      }
50  
51      public void testOneHour()
52      {
53          assertEquals( ONE_HOUR, IntervalUtils.parseInterval( "1h" ) );
54      }
55  
56      public void testOneMinute()
57      {
58          assertEquals( ONE_MINUTE, IntervalUtils.parseInterval( "1m" ) );
59      }
60  
61      public void testTwoDaysThreeHoursAndOneMinute()
62      {
63          assertEquals( 2 * ONE_DAY + 3 * ONE_HOUR + ONE_MINUTE, IntervalUtils.parseInterval( "2d 3h 1m" ) );
64      }
65  
66      public void testTwoDaysThreeHoursAndOneMinuteCondensed()
67      {
68          assertEquals( 2 * ONE_DAY + 3 * ONE_HOUR + ONE_MINUTE, IntervalUtils.parseInterval( "2d3h1m" ) );
69      }
70  
71      public void testTwoDaysThreeHoursAndOneMinuteCommaSeparated()
72      {
73          assertEquals( 2 * ONE_DAY + 3 * ONE_HOUR + ONE_MINUTE, IntervalUtils.parseInterval( "2d,3h,1m" ) );
74      }
75  
76      public void testTwoDaysThreeHoursAndOneMinuteRearranged()
77      {
78          assertEquals( 2 * ONE_DAY + 3 * ONE_HOUR + ONE_MINUTE, IntervalUtils.parseInterval( "1m 2d 3h" ) );
79      }
80  
81      public void testThreeDaysPoorlySpecified()
82      {
83          assertEquals( 3 * ONE_DAY, IntervalUtils.parseInterval( "1d 2d" ) );
84      }
85      
86      public void testNeverInterval()
87      {
88          assertFalse( IntervalUtils.isExpired( "never", null ) );
89      }
90  
91      public void testAlwaysInterval()
92      {
93          assertTrue( IntervalUtils.isExpired( "always", null ) );
94      }
95  
96      public void testOneMinuteIntervalShouldBeExpired()
97      {
98          Calendar cal = Calendar.getInstance();
99          cal.add( Calendar.MINUTE, -2 );
100         
101         Date lastChecked = cal.getTime();
102         
103         assertTrue( IntervalUtils.isExpired( "interval:1m", lastChecked ) );
104     }
105 
106 }