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