1   package org.apache.jcs.auxiliary.disk.jdbc.mysql.util;
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 java.util.Date;
23  
24  import junit.framework.TestCase;
25  
26  /***
27   * Unit tests for the schedule parser.
28   * <p>
29   * @author Aaron Smuts
30   */
31  public class ScheduleParserUtilUnitTest
32      extends TestCase
33  {
34  
35      /***
36       * Verify that we get an exception and not a null pointer for null input.
37       */
38      public void testGetDatesWithNullInput()
39      {
40          try
41          {
42              ScheduleParser.createDatesForSchedule( null );
43  
44              fail( "Should have thrown an exception" );
45          }
46          catch ( ScheduleFormatException e )
47          {
48              // expected
49          }
50      }
51  
52      /***
53       * Verify that we get an exception and not a null pointer for null input.
54       */
55      public void testGetDateWithNullInput()
56      {
57          try
58          {
59              ScheduleParser.getDateForSchedule( null );
60  
61              fail( "Should have thrown an exception" );
62          }
63          catch ( ScheduleFormatException e )
64          {
65              // expected
66          }
67      }
68  
69      /***
70       * Verify that we get one date for one date.
71       * @throws ScheduleFormatException
72       */
73      public void testGetsDatesSingle()
74          throws ScheduleFormatException
75      {
76          String schedule = "12:34:56";
77          Date[] dates = ScheduleParser.createDatesForSchedule( schedule );
78  
79          assertEquals( "Wrong number of dates returned.", 1, dates.length );
80      }
81      /***
82       * Verify that we get one date for one date.
83       * @throws ScheduleFormatException
84       */
85      public void testGetsDatesMultiple()
86          throws ScheduleFormatException
87      {
88          String schedule = "12:34:56,03:51:00,12:34:12";
89          Date[] dates = ScheduleParser.createDatesForSchedule( schedule );
90          //System.out.println( dates );
91          assertEquals( "Wrong number of dates returned.", 3, dates.length );
92      }
93  
94      /***
95       * Verify that we get an exception for a single bad date in a list.
96       */
97      public void testGetDatesMalformedNoColon()
98      {
99          try
100         {
101             String schedule = "12:34:56,03:51:00,123234";
102             ScheduleParser.createDatesForSchedule( schedule );
103 
104             fail( "Should have thrown an exception for a malformed date" );
105         }
106         catch ( ScheduleFormatException e )
107         {
108             // expected
109         }
110     }
111     /***
112      * Verify that we get an exception for a schedule that has a non numeric item.
113      */
114     public void testGetDatesMalformedNan()
115     {
116         try
117         {
118             String schedule = "12:34:56,03:51:00,aa:12:12";
119             ScheduleParser.createDatesForSchedule( schedule );
120 
121             fail( "Should have thrown an exception for a malformed date" );
122         }
123         catch ( ScheduleFormatException e )
124         {
125             // expected
126         }
127     }
128 }