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