Debian Codegraph @

test/types/TestDuration.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 package test.types;
17
18 import junit.framework.TestCase;
19 import org.apache.axis.types.Duration;
20 import java.util.Calendar;
21 import java.util.GregorianCalendar;
22
23 /**
24 * <p>Title: </p>
25 * <p>Description: </p>
26 * @author Dominik Kacprzak <[email protected]>
27 * @version $Id: TestDuration.java 259917 2004-11-27 21:08:12Z dims $
28 *
29 * @todo try to find original author, add more docs
30 */
31 public class TestDuration extends TestCase {
32
33
34 public TestDuration(String name) {
35 super(name);
36 }
37
38 public void testValidDurations() throws Exception {
39 // invoke the web service as if it was a local java object
40 String[] durationStrings = new String[11];
41 durationStrings[0] = "P2Y3M8DT8H1M3.3S";
42 durationStrings[1] = "P2Y3M8DT8H1M3S";
43 durationStrings[2] = "PT8H1M3.3S";
44 durationStrings[3] = "P2Y3M8D";
45 durationStrings[4] = "P2YT8H";
46 durationStrings[5] = "P8DT3.3S";
47 durationStrings[6] = "P3MT1M";
48 durationStrings[7] = "PT0.3S";
49 durationStrings[8] = "P1M";
50 durationStrings[9] = "-P1M";
51 durationStrings[10] = "-P2Y3M8DT8H1M3.3S";
52
53 for (int i = 0; i < durationStrings.length; i++) {
54 String durationString = durationStrings[i];
55 Duration duration = new Duration(durationString);
56
57 assertTrue("Duration string \"" + durationString +
58 "\" not equal to returned: " + duration.toString(),
59 durationString.equals(duration.toString()));
60 }
61 }
62
63 public void testInvalidDurations() throws Exception {
64 // make sure that using invalid duration strings results in an
65 // exception
66 String[] invalidDurationStrings = {"P", "T", "P-2Y3M8D", "P8H1M3S",
67 "P8Y1MT", "PT8Y1M3D", "PYMDTHMS"};
68 // each of the above strings should result in an exception
69 for (int i = 0; i < invalidDurationStrings.length; i++) {
70 String durationString = invalidDurationStrings[i];
71 try {
72 Duration duration = new Duration(durationString);
73 throw new junit.framework.AssertionFailedError(
74 "org.apache.axis.types.Duration constructor accepted invalid string: " +
75 durationString);
76 } catch (IllegalArgumentException e) {
77 // this is good
78 }
79 }
80
81 /* need to test setTime(String) and setDate(String) for handling
82 * invalid time and date strings. A handling of properly formatted
83 * time and date strings is tested as part of testValidDurations()
84 * test case.
85 * NOTE: the code below can be removed if/when
86 * parseDate(String) and parseTime(String) methods are changed
87 * to private ones.
88 */
89 String[] invalidTimeStrings = {"", "-8H1M3.3S", "8Y1M3D", "HMS"};
90 Duration duration = new Duration();
91 for (int i = 0; i < invalidTimeStrings.length; i++) {
92 String durationString = invalidTimeStrings[i];
93 try {
94 duration.parseTime(durationString);
95 throw new junit.framework.AssertionFailedError(
96 "parseTime(String) method accepted invalid string: " +
97 durationString);
98 } catch (IllegalArgumentException e) {
99 // this is good
100 }
101 }
102
103 String[] invalidDateStrings = {"", "-2Y3M8D", "8H1M3S", "-8Y1M"};
104 for (int i = 0; i < invalidDateStrings.length; i++) {
105 String durationString = invalidDateStrings[i];
106 try {
107 duration.parseDate(durationString);
108 throw new junit.framework.AssertionFailedError(
109 "parseDate(String) method accepted invalid string: " +
110 durationString);
111 } catch (IllegalArgumentException e) {
112 // this is good
113 }
114 }
115
116 }
117
118 /**
119 * Test if duration object is properly created from a Calendar.
120 * @throws Exception
121 */
122 public void testDurationFromCalendar() throws Exception {
123 // negative date
124 Calendar calendar = new GregorianCalendar(1, 3, 20);
125 Duration duration = new Duration(true, calendar);
126 assertTrue("Negative flag does not match", duration.isNegative());
127 assertTrue("Years do not match", duration.getYears() == 1);
128 assertTrue("Months do not match", duration.getMonths() == 3);
129 assertTrue("Days do not match", duration.getDays() == 20);
130 assertEquals("String representation does not match", duration.toString(),
131 "-P1Y3M20D");
132 // positive date and time
133 calendar.clear();
134 calendar.set(1, 2, 20, 10, 3, 11);
135 duration = new Duration(false, calendar);
136 assertTrue("Negative flag does not match", !duration.isNegative());
137 assertTrue("Years do not match", duration.getYears() == 1);
138 assertTrue("Months do not match", duration.getMonths() == 2);
139 assertTrue("Days do not match", duration.getDays() == 20);
140 assertTrue("Hours do not match", duration.getHours() == 10);
141 assertTrue("Minutes do not match", duration.getMinutes() == 3);
142 assertTrue("Seconds do not match", duration.getSeconds() == 11);
143 assertEquals("String representation does not match", duration.toString(),
144 "P1Y2M20DT10H3M11S");
145 }
146
147 public void testCalendarFromDuration() throws Exception {
148 // Check if a calendar object used to create a duration object and
149 // a calendar object created from the same duration are equal
150 // that's good test for handling of miliseconds
151 Calendar calendar = new GregorianCalendar(1, 2, 20, 10, 3, 11);
152 calendar.set(Calendar.MILLISECOND, 15);
153 Duration duration = new Duration(true, calendar);
154 Calendar durationCalendar = duration.getAsCalendar();
155 assertTrue("Negative flag does not match", duration.isNegative());
156 assertEquals("Years do not match",
157 calendar.get(Calendar.YEAR),
158 durationCalendar.get(Calendar.YEAR));
159 assertEquals("Months do not match",
160 calendar.get(Calendar.MONTH),
161 durationCalendar.get(Calendar.MONTH));
162 assertEquals("Days do not match",
163 calendar.get(Calendar.DATE),
164 durationCalendar.get(Calendar.DATE));
165 assertEquals("Hours do not match",
166 calendar.get(Calendar.HOUR),
167 durationCalendar.get(Calendar.HOUR));
168 assertEquals("Minutes do not match",
169 calendar.get(Calendar.MINUTE),
170 durationCalendar.get(Calendar.MINUTE));
171 assertEquals("Seconds do not match",
172 calendar.get(Calendar.SECOND),
173 durationCalendar.get(Calendar.SECOND));
174 assertEquals("Miliseconds do not match",
175 calendar.get(Calendar.MILLISECOND),
176 durationCalendar.get(Calendar.MILLISECOND));
177
178 // test for overflows - Calendar class does automatic conversion
179 // of dates
180 duration.setMonths(20);
181 durationCalendar = duration.getAsCalendar();
182 assertEquals("Years do not match",
183 duration.getYears() + 1,
184 durationCalendar.get(Calendar.YEAR));
185 assertEquals("Months do not match",
186 duration.getMonths() - 12,
187 durationCalendar.get(Calendar.MONTH));
188
189 // make sure that Duration enforces milliseconds precision
190 duration.setSeconds(10.1234);
191 assertTrue("Milliseconds precision is not enforced",
192 duration.getSeconds() == 10.12);
193 }
194
195 public void testHash() {
196 // test if hash is taking milliseconds in account
197 Duration d1 = new Duration(false, 10, 1, 2, 1, 20, 2.51);
198 Duration d2 = new Duration(false, 10, 1, 2, 1, 20, 2.51);
199 Duration d3 = new Duration(false, 10, 1, 2, 1, 20, 2);
200 Duration d4 = new Duration(false, 10, 1, 2, 1, 20, 2.51233);
201 assertEquals("Hash code values do not match", d1.hashCode(),
202 d2.hashCode());
203 assertFalse("Hash code values match", d1.hashCode() == d3.hashCode());
204 // test precistion
205 assertEquals("Hash code values do not match", d1.hashCode(),
206 d4.hashCode());
207 }
208
209 public void testEquals() {
210 // test if equals is taking milliseconds in account
211 Duration d1 = new Duration(false, 10, 1, 2, 1, 20, 2.51);
212 Duration d2 = new Duration(false, 10, 1, 2, 1, 20, 2.51);
213 Duration d3 = new Duration(true, 10, 1, 2, 1, 20, 2.51);
214 Duration d4 = new Duration(false, 8, 25, 2, 0, 80, 2.51);
215 Duration d5 = new Duration(false, 8, 25, 2, 0, 80, 2.51);
216 // the durations are equal: testing precision
217 assertTrue("Comparison failed", d1.equals(d2));
218 // the durations are equal, except of the sign
219 assertFalse("Comparison failed", d1.equals(d3));
220 // the durations are equal, but represented differently
221 assertTrue("Comparison failed", d1.equals(d4));
222 // test precision
223 assertTrue("Comparison failed", d1.equals(d5));
224 }
225 }