Debian Codegraph @

test/types/TestMonth.java

raw
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;
23
24 /**
25 * Test validation of types.Month
26 */
27 public class TestMonth extends TestCase {
28
29 public TestMonth(String name) {
30 super(name);
31 }
32
33 /**
34 * Run a failure test. values should be invalid.
35 */
36 private void runFailTest(int month, String tz) throws Exception {
37 Month oMonth = null;
38 try {
39 oMonth = new Month(month, tz);
40 }
41 catch (Exception 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 source) throws Exception {
49 Month oMonth = null;
50 try {
51 oMonth = new Month(source);
52 }
53 catch (Exception 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 tz) throws Exception {
64 Month oMonth = null;
65 try {
66 oMonth = new Month(month, tz);
67 }
68 catch (Exception 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 source) throws Exception {
76 Month oMonth = null;
77 try {
78 oMonth = new Month(source);
79 }
80 catch (Exception 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 {
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 {
96 // test all twelve months
97 // use NumberFormat to ensure leading zeros
98 NumberFormat nf = NumberFormat.getInstance();
99 nf.setMinimumIntegerDigits(2);
100 for (int m=1; m < 13; m++) {
101 String s = "--" + nf.format(m) + "--";
102 runPassTest(s);
103 }
104 }
105 public void testNormalTimezone() throws Exception {
106 runPassTest("--01--Z");
107 }
108 public void testNormalPositiveTimezone() throws Exception {
109 runPassTest("--11--+05:00");
110 }
111 public void testNormalNegativeTimezone() throws Exception {
112 runPassTest("--11---11:00");
113 }
114
115 /**
116 * Test that badly formatted strings fail
117 */
118 public void testBadString() throws Exception {
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 {
131 runFailTest(13, null);
132 }
133 public void testBadMonthString() throws Exception {
134 runFailTest("--13--");
135 }
136 public void testBadMonthString2() throws Exception {
137 runFailTest("--1--");
138 }
139
140 /**
141 * Test that a bad timezone fails
142 */
143 public void testBadTimezone() throws Exception {
144 runFailTest(7, "badzone");
145 }
146 public void testBadTimezoneString() throws Exception {
147 runFailTest("--07--+EDT");
148 }
149
150 }