KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > types > TestMonth


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

27 public class TestMonth extends TestCase {
28
29     public TestMonth(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, String JavaDoc tz) throws Exception JavaDoc {
37         Month oMonth = null;
38         try {
39             oMonth = new Month(month, 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=" + String.valueOf(month) +
45                    ",tz=" + tz + "]. did not restrict bad value.", oMonth);
46     }
47
48     private void runFailTest(String JavaDoc source) throws Exception JavaDoc {
49         Month oMonth = null;
50         try {
51             oMonth = new Month(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.", oMonth);
58     }
59
60     /**
61      * Run a successful test. values should be valid.
62      */

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

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

118     public void testBadString() throws Exception JavaDoc {
119         runFailTest("11--");
120         runFailTest("-11--");
121         runFailTest("--11-");
122         runFailTest("--11");
123         runFailTest("xx07-13");
124         runFailTest("garbage");
125     }
126
127     /**
128      * Test that a bad month fails
129      */

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

143     public void testBadTimezone() throws Exception JavaDoc {
144         runFailTest(7, "badzone");
145     }
146     public void testBadTimezoneString() throws Exception JavaDoc {
147         runFailTest("--07--+EDT");
148     }
149
150 }
151
Popular Tags