KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > types > TestDay


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.Day;
21
22 import java.text.NumberFormat JavaDoc;
23
24 /**
25  * Test validation of types.Day
26  */

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

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

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

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

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

134     public void testBadDay() throws Exception JavaDoc {
135         runFailTest(32, null);
136     }
137     public void testBadDayString() throws Exception JavaDoc {
138         runFailTest("---32");
139     }
140     public void testBadDayString2() throws Exception JavaDoc {
141         runFailTest("---1");
142     }
143     
144     /**
145      * Test that a bad timezone fails
146      */

147     public void testBadTimezone() throws Exception JavaDoc {
148         runFailTest( 31, "badzone");
149     }
150     public void testBadTimezoneString() throws Exception JavaDoc {
151         runFailTest("---23+EDT");
152     }
153
154
155 }
156
Popular Tags