File: TestDuration.java

package info (click to toggle)
axis 1.4-16.2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 51,876 kB
  • sloc: java: 129,119; xml: 10,594; jsp: 983; sh: 84; cs: 36; makefile: 19
file content (225 lines) | stat: -rw-r--r-- 10,030 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright 2002-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package test.types;

import junit.framework.TestCase;
import org.apache.axis.types.Duration;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * @author Dominik Kacprzak <dominik@opentoolbox.com>
 * @version $Id: TestDuration.java 259917 2004-11-27 21:08:12Z dims $
 *
 * @todo try to find original author, add more docs
 */
public class TestDuration extends TestCase {


    public TestDuration(String name) {
        super(name);
    }

    public void testValidDurations() throws Exception {
        // invoke the web service as if it was a local java object
        String[] durationStrings = new String[11];
        durationStrings[0] = "P2Y3M8DT8H1M3.3S";
        durationStrings[1] = "P2Y3M8DT8H1M3S";
        durationStrings[2] = "PT8H1M3.3S";
        durationStrings[3] = "P2Y3M8D";
        durationStrings[4] = "P2YT8H";
        durationStrings[5] = "P8DT3.3S";
        durationStrings[6] = "P3MT1M";
        durationStrings[7] = "PT0.3S";
        durationStrings[8] = "P1M";
        durationStrings[9] = "-P1M";
        durationStrings[10] = "-P2Y3M8DT8H1M3.3S";

        for (int i = 0; i < durationStrings.length; i++) {
            String durationString = durationStrings[i];
            Duration duration = new Duration(durationString);

            assertTrue("Duration string \"" + durationString +
                       "\" not equal to returned: " + duration.toString(),
                       durationString.equals(duration.toString()));
        }
    }

    public void testInvalidDurations() throws Exception {
        // make sure that using invalid duration strings results in an
        // exception
        String[] invalidDurationStrings = {"P", "T", "P-2Y3M8D", "P8H1M3S",
                                          "P8Y1MT", "PT8Y1M3D", "PYMDTHMS"};
        // each of the above strings should result in an exception
        for (int i = 0; i < invalidDurationStrings.length; i++) {
            String durationString = invalidDurationStrings[i];
            try {
                Duration duration = new Duration(durationString);
                throw new junit.framework.AssertionFailedError(
                        "org.apache.axis.types.Duration constructor accepted invalid string: " +
                        durationString);
            } catch (IllegalArgumentException e) {
                // this is good
            }
        }

        /* need to test setTime(String) and setDate(String) for handling
         * invalid time and date strings. A handling of properly formatted
         * time and date strings is tested as part of testValidDurations()
         * test case.
         * NOTE: the code below can be removed if/when
         * parseDate(String) and parseTime(String) methods are changed
         * to private ones.
         */
        String[] invalidTimeStrings = {"", "-8H1M3.3S", "8Y1M3D", "HMS"};
        Duration duration = new Duration();
        for (int i = 0; i < invalidTimeStrings.length; i++) {
            String durationString = invalidTimeStrings[i];
            try {
                duration.parseTime(durationString);
                throw new junit.framework.AssertionFailedError(
                        "parseTime(String) method accepted invalid string: " +
                        durationString);
            } catch (IllegalArgumentException e) {
                // this is good
            }
        }

        String[] invalidDateStrings = {"", "-2Y3M8D", "8H1M3S", "-8Y1M"};
        for (int i = 0; i < invalidDateStrings.length; i++) {
            String durationString = invalidDateStrings[i];
            try {
                duration.parseDate(durationString);
                throw new junit.framework.AssertionFailedError(
                        "parseDate(String) method accepted invalid string: " +
                        durationString);
            } catch (IllegalArgumentException e) {
                // this is good
            }
        }

    }

    /**
     * Test if duration object is properly created from a Calendar.
     * @throws Exception
     */
    public void testDurationFromCalendar() throws Exception {
        // negative date
        Calendar calendar = new GregorianCalendar(1, 3, 20);
        Duration duration = new Duration(true, calendar);
        assertTrue("Negative flag does not match", duration.isNegative());
        assertTrue("Years do not match", duration.getYears() == 1);
        assertTrue("Months do not match", duration.getMonths() == 3);
        assertTrue("Days do not match", duration.getDays() == 20);
        assertEquals("String representation does not match", duration.toString(),
                     "-P1Y3M20D");
        // positive date and time
        calendar.clear();
        calendar.set(1, 2, 20, 10, 3, 11);
        duration = new Duration(false, calendar);
        assertTrue("Negative flag does not match", !duration.isNegative());
        assertTrue("Years do not match", duration.getYears() == 1);
        assertTrue("Months do not match", duration.getMonths() == 2);
        assertTrue("Days do not match", duration.getDays() == 20);
        assertTrue("Hours do not match", duration.getHours() == 10);
        assertTrue("Minutes do not match", duration.getMinutes() == 3);
        assertTrue("Seconds do not match", duration.getSeconds() == 11);
        assertEquals("String representation does not match", duration.toString(),
                     "P1Y2M20DT10H3M11S");
    }

    public void testCalendarFromDuration() throws Exception {
        // Check if a calendar object used to create a duration object and
        // a calendar object created from the same duration are equal
        // that's good test for handling of miliseconds
        Calendar calendar = new GregorianCalendar(1, 2, 20, 10, 3, 11);
        calendar.set(Calendar.MILLISECOND, 15);
        Duration duration = new Duration(true, calendar);
        Calendar durationCalendar = duration.getAsCalendar();
        assertTrue("Negative flag does not match", duration.isNegative());
        assertEquals("Years do not match",
                     calendar.get(Calendar.YEAR),
                     durationCalendar.get(Calendar.YEAR));
        assertEquals("Months do not match",
                     calendar.get(Calendar.MONTH),
                     durationCalendar.get(Calendar.MONTH));
        assertEquals("Days do not match",
                     calendar.get(Calendar.DATE),
                     durationCalendar.get(Calendar.DATE));
        assertEquals("Hours do not match",
                     calendar.get(Calendar.HOUR),
                     durationCalendar.get(Calendar.HOUR));
        assertEquals("Minutes do not match",
                     calendar.get(Calendar.MINUTE),
                     durationCalendar.get(Calendar.MINUTE));
        assertEquals("Seconds do not match",
                     calendar.get(Calendar.SECOND),
                     durationCalendar.get(Calendar.SECOND));
        assertEquals("Miliseconds do not match",
                     calendar.get(Calendar.MILLISECOND),
                     durationCalendar.get(Calendar.MILLISECOND));

        // test for overflows - Calendar class does automatic conversion
        // of dates
        duration.setMonths(20);
        durationCalendar = duration.getAsCalendar();
        assertEquals("Years do not match",
                     duration.getYears() + 1,
                     durationCalendar.get(Calendar.YEAR));
        assertEquals("Months do not match",
                     duration.getMonths() - 12,
                     durationCalendar.get(Calendar.MONTH));

        // make sure that Duration enforces milliseconds precision
        duration.setSeconds(10.1234);
        assertTrue("Milliseconds precision is not enforced",
                     duration.getSeconds() == 10.12);
    }

    public void testHash() {
        // test if hash is taking milliseconds in account
        Duration d1 = new Duration(false, 10, 1, 2, 1, 20, 2.51);
        Duration d2 = new Duration(false, 10, 1, 2, 1, 20, 2.51);
        Duration d3 = new Duration(false, 10, 1, 2, 1, 20, 2);
        Duration d4 = new Duration(false, 10, 1, 2, 1, 20, 2.51233);
        assertEquals("Hash code values do not match", d1.hashCode(),
                     d2.hashCode());
        assertFalse("Hash code values match", d1.hashCode() == d3.hashCode());
        // test precistion
        assertEquals("Hash code values do not match", d1.hashCode(),
                     d4.hashCode());
    }

    public void testEquals() {
        // test if equals is taking milliseconds in account
        Duration d1 = new Duration(false, 10, 1, 2, 1, 20, 2.51);
        Duration d2 = new Duration(false, 10, 1, 2, 1, 20, 2.51);
        Duration d3 = new Duration(true, 10, 1, 2, 1, 20, 2.51);
        Duration d4 = new Duration(false, 8, 25, 2, 0, 80, 2.51);
        Duration d5 = new Duration(false, 8, 25, 2, 0, 80, 2.51);
        // the durations are equal: testing precision
        assertTrue("Comparison failed", d1.equals(d2));
        // the durations are equal, except of the sign
        assertFalse("Comparison failed", d1.equals(d3));
        // the durations are equal, but represented differently
        assertTrue("Comparison failed", d1.equals(d4));
        // test precision
        assertTrue("Comparison failed", d1.equals(d5));
    }
}