KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > types > TestMonthDay


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package test.types;
18
19 import junit.framework.TestCase;
20 import org.apache.axis.types.MonthDay;
21
22 import java.text.NumberFormat JavaDoc;
23
24 /**
25  * Test validation of types.MonthDay
26  */

27 public class TestMonthDay extends TestCase {
28
29     public TestMonthDay(String JavaDoc name) {
30         super(name);
31     }
32
33     /**
34      * Run a failure test. values should be invalid.
35      */

36     private void runFailTest(int month, int day, String JavaDoc tz) throws Exception JavaDoc {
37         MonthDay oMonthDay = null;
38         try {
39             oMonthDay = new MonthDay(month, day, tz);
40         }
41         catch (Exception JavaDoc e) { // catch the validation exception
42
}
43         // object is not instantiated on bad data value
44
assertNull("validation restriction failed [ month=" +
45                 String.valueOf(month) + ",day=" + String.valueOf(day) +
46                    ",tz=" + tz + "]. did not restrict bad value.", oMonthDay);
47     }
48
49     private void runFailTest(String JavaDoc source) throws Exception JavaDoc {
50         MonthDay oMonthDay = null;
51         try {
52             oMonthDay = new MonthDay(source);
53         }
54         catch (Exception JavaDoc e) { // catch the validation exception
55
}
56         // object is not instantiated on bad data value
57
assertNull("validation restriction failed [ " + source +
58                  "]. did not restrict bad value.", oMonthDay);
59     }
60
61     /**
62      * Run a successful test. values should be valid.
63      */

64     private void runPassTest(int month, int day, String JavaDoc tz) throws Exception JavaDoc {
65         MonthDay oMonthDay = null;
66         try {
67             oMonthDay = new MonthDay(month, day, tz);
68         }
69         catch (Exception JavaDoc e) { // catch the validation exception
70
assertTrue("Validation exception thrown on valid input", false);
71         }
72         assertEquals("MonthDay month not equal", month, oMonthDay.getMonth());
73         assertEquals("MonthDay day not equal", day, oMonthDay.getDay());
74         assertEquals("MonthDay timezone not equal", tz, oMonthDay.getTimezone());
75     }
76     
77     private void runPassTest(String JavaDoc source) throws Exception JavaDoc {
78         MonthDay oMonthDay = null;
79         try {
80             oMonthDay = new MonthDay(source);
81         }
82         catch (Exception JavaDoc e) { // catch the validation exception
83
assertTrue("Validation exception thrown on valid input", false);
84         }
85         assertEquals("MonthDay.toString() not equal", source, oMonthDay.toString());
86     }
87
88     /**
89      * Test that a normal date succeeeds
90      */

91     public void testNormal() throws Exception JavaDoc {
92         // test all twelve months (1/1, 2/2, etc)
93
for (int m=1; m < 13; m++) {
94             runPassTest(m, m, null);
95         }
96     }
97     public void testNormalString() throws Exception JavaDoc {
98         // test all twelve months
99
// use NumberFormat to ensure leading zeros
100
NumberFormat JavaDoc nf = NumberFormat.getInstance();
101         nf.setMinimumIntegerDigits(2);
102         for (int m=1; m < 13; m++) {
103             String JavaDoc s = "--" + nf.format(m) + "-05";
104             runPassTest(s);
105         }
106     }
107     public void testNormalString2() throws Exception JavaDoc {
108         // check for leading zeros in toString().
109
runPassTest("--01-01");
110     }
111     public void testNormalTimezone() throws Exception JavaDoc {
112         runPassTest("--01-01Z");
113     }
114     public void testNormalPositiveTimezone() throws Exception JavaDoc {
115         runPassTest("--02-11+05:00");
116     }
117     public void testNormalNegativeTimezone() throws Exception JavaDoc {
118         runPassTest("--03-11-11:00");
119     }
120
121     /**
122      * Test that badly formatted strings fail
123      */

124     public void testBadString() throws Exception JavaDoc {
125         runFailTest("07-13Z");
126         runFailTest("-07-13");
127         runFailTest("xx07-13");
128         runFailTest("garbage");
129     }
130     
131     /**
132      * Test that a bad month fails
133      */

134     public void testBadMonth() throws Exception JavaDoc {
135         runFailTest(13, 20, null);
136     }
137     public void testBadMonthString() throws Exception JavaDoc {
138         runFailTest("--13-13");
139     }
140     public void testBadMonthString2() throws Exception JavaDoc {
141         runFailTest("--1-01");
142     }
143
144     /**
145      * Test that a bad day fails
146      */

147     public void testBadDay() throws Exception JavaDoc {
148         runFailTest(1, 32, null);
149     }
150     public void testBadDayString() throws Exception JavaDoc {
151         runFailTest("--08-32");
152     }
153     public void testBadDayString2() throws Exception JavaDoc {
154         runFailTest("--1-01");
155     }
156     public void testEndOfMonthDays() throws Exception JavaDoc {
157         runFailTest(1, 32, null);
158         runPassTest(1, 31, null);
159         runFailTest(2, 30, null);
160         runPassTest(2, 29, null);
161         runFailTest(3, 32, null);
162         runPassTest(3, 31, null);
163         runFailTest(4, 31, null);
164         runPassTest(4, 30, null);
165         runFailTest(5, 32, null);
166         runPassTest(5, 30, null);
167         runFailTest(6, 31, null);
168         runPassTest(6, 30, null);
169         runFailTest(7, 32, null);
170         runPassTest(7, 31, null);
171         runFailTest(8, 32, null);
172         runPassTest(8, 31, null);
173         runFailTest(9, 31, null);
174         runPassTest(9, 30, null);
175         runFailTest(10, 32, null);
176         runPassTest(10, 31, null);
177         runFailTest(11, 31, null);
178         runPassTest(11, 30, null);
179         runFailTest(12, 32, null);
180         runPassTest(12, 31, null);
181     }
182     
183     /**
184      * Test that a bad timezone fails
185      */

186     public void testBadTimezone() throws Exception JavaDoc {
187         runFailTest(12, 31, "badzone");
188     }
189     public void testBadTimezoneString() throws Exception JavaDoc {
190         runFailTest("--07-23+EDT");
191     }
192
193
194 }
195
Popular Tags