KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > types > TestYearMonth


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.YearMonth;
21
22 /**
23  * Test validation of types.YearMonth
24  */

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

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

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

89     public void testNormal() throws Exception JavaDoc {
90         runPassTest(2002, 8, null);
91     }
92     public void testNormalString() throws Exception JavaDoc {
93         runPassTest("9999-12");
94     }
95     public void testNormalString2() throws Exception JavaDoc {
96         // check for leading zeros in toString().
97
runPassTest("0001-01Z");
98     }
99     public void testNegativeYear() throws Exception JavaDoc {
100         runPassTest(-1955, 4, null);
101     }
102     public void testNegativeYearString() throws Exception JavaDoc {
103         runPassTest("-1955-04+05:00");
104     }
105     public void testNegativeYearString2() throws Exception JavaDoc {
106         // negative year with leading zeros
107
runPassTest("-0055-04+05:00");
108     }
109     public void testBigYear() throws Exception JavaDoc {
110         // Big year should be allowed (per Schema, not ISO).
111
runPassTest(12000, 4, null);
112     }
113     public void testBigYearString() throws Exception JavaDoc {
114         runPassTest("-27000-04+05:00");
115     }
116
117     /**
118      * Test that a bad year fails
119      * Schema says the year can have any number of digits
120      */

121     public void testBadYear() throws Exception JavaDoc {
122         runFailTest(0, 7, null);
123     }
124     public void testBadYearString() throws Exception JavaDoc {
125         runFailTest("0000-7");
126     }
127
128     /**
129      * Test that a bad month fails
130      */

131     public void testBadMonth() throws Exception JavaDoc {
132         runFailTest(1999, 13, null);
133     }
134     public void testBadMonthString() throws Exception JavaDoc {
135         runFailTest("1999-13");
136     }
137
138     /**
139      * Test that a bad timezone fails
140      */

141     public void testBadTimezone() throws Exception JavaDoc {
142         runFailTest(1966, 7, "badzone");
143     }
144     public void testBadTimezoneString() throws Exception JavaDoc {
145         runFailTest("1966-07+EDT");
146     }
147
148     /**
149     * Test that a year at MaxInclusive succeeds
150     */

151     public void testMaxYear() throws Exception JavaDoc {
152        runPassTest(9999, 1, null);
153     }
154
155 }
156
Popular Tags