2
* Copyright 2002-2004 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
19
import junit.framework.TestCase;
20
import org.apache.axis.types.MonthDay;
22
import java.text.NumberFormat;
25
* Test validation of types.MonthDay
27
public class TestMonthDay extends TestCase {
29
public TestMonthDay(String name) {
34
* Run a failure test. values should be invalid.
36
private void runFailTest(int month, int day, String tz) throws Exception {
37
MonthDay oMonthDay = null;
39
oMonthDay = new MonthDay(month, day, tz);
41
catch (Exception e) { // catch the validation exception
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);
49
private void runFailTest(String source) throws Exception {
50
MonthDay oMonthDay = null;
52
oMonthDay = new MonthDay(source);
54
catch (Exception e) { // catch the validation exception
56
// object is not instantiated on bad data value
57
assertNull("validation restriction failed [ " + source +
58
"]. did not restrict bad value.", oMonthDay);
62
* Run a successful test. values should be valid.
64
private void runPassTest(int month, int day, String tz) throws Exception {
65
MonthDay oMonthDay = null;
67
oMonthDay = new MonthDay(month, day, tz);
69
catch (Exception e) { // catch the validation exception
70
assertTrue("Validation exception thrown on valid input", false);
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());
77
private void runPassTest(String source) throws Exception {
78
MonthDay oMonthDay = null;
80
oMonthDay = new MonthDay(source);
82
catch (Exception e) { // catch the validation exception
83
assertTrue("Validation exception thrown on valid input", false);
85
assertEquals("MonthDay.toString() not equal", source, oMonthDay.toString());
89
* Test that a normal date succeeeds
91
public void testNormal() throws Exception {
92
// test all twelve months (1/1, 2/2, etc)
93
for (int m=1; m < 13; m++) {
94
runPassTest(m, m, null);
97
public void testNormalString() throws Exception {
98
// test all twelve months
99
// use NumberFormat to ensure leading zeros
100
NumberFormat nf = NumberFormat.getInstance();
101
nf.setMinimumIntegerDigits(2);
102
for (int m=1; m < 13; m++) {
103
String s = "--" + nf.format(m) + "-05";
107
public void testNormalString2() throws Exception {
108
// check for leading zeros in toString().
109
runPassTest("--01-01");
111
public void testNormalTimezone() throws Exception {
112
runPassTest("--01-01Z");
114
public void testNormalPositiveTimezone() throws Exception {
115
runPassTest("--02-11+05:00");
117
public void testNormalNegativeTimezone() throws Exception {
118
runPassTest("--03-11-11:00");
122
* Test that badly formatted strings fail
124
public void testBadString() throws Exception {
125
runFailTest("07-13Z");
126
runFailTest("-07-13");
127
runFailTest("xx07-13");
128
runFailTest("garbage");
132
* Test that a bad month fails
134
public void testBadMonth() throws Exception {
135
runFailTest(13, 20, null);
137
public void testBadMonthString() throws Exception {
138
runFailTest("--13-13");
140
public void testBadMonthString2() throws Exception {
141
runFailTest("--1-01");
145
* Test that a bad day fails
147
public void testBadDay() throws Exception {
148
runFailTest(1, 32, null);
150
public void testBadDayString() throws Exception {
151
runFailTest("--08-32");
153
public void testBadDayString2() throws Exception {
154
runFailTest("--1-01");
156
public void testEndOfMonthDays() throws Exception {
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);
184
* Test that a bad timezone fails
186
public void testBadTimezone() throws Exception {
187
runFailTest(12, 31, "badzone");
189
public void testBadTimezoneString() throws Exception {
190
runFailTest("--07-23+EDT");