KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > types > TestYear


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

25 public class TestYear extends TestCase {
26
27     public TestYear(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, String JavaDoc tz) throws Exception JavaDoc {
35         Year oYear = null;
36         try {
37             oYear = new Year(year, 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) +
44                    ",tz=" + tz + "]. did not restrict bad value.", oYear);
45     }
46
47     private void runFailTest(String JavaDoc source) throws Exception JavaDoc {
48         Year oYear = null;
49         try {
50             oYear = new Year(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.", oYear);
57     }
58
59     /**
60      * Run a successful test. values should be valid.
61      */

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

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

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

131     public void testBadTimezone() throws Exception JavaDoc {
132         runFailTest(1966, "badzone");
133     }
134     public void testBadTimezoneString() throws Exception JavaDoc {
135         runFailTest("1966+EDT");
136     }
137
138     /**
139     * Test that a year at MaxInclusive succeeds
140     */

141     public void testMaxYear() throws Exception JavaDoc {
142        runPassTest(9999, null);
143     }
144
145 }
146
Popular Tags