DurationTest.java revision 06b3293d5af3454a39681cfd659271551354b8a0
1/* //device/content/providers/pim/DurationTest.java
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18package com.android.calendarcommon2;
19
20import junit.framework.TestCase;
21
22import android.test.suitebuilder.annotation.SmallTest;
23
24public class DurationTest extends TestCase {
25
26    private void verifyDuration(String str,
27            int sign, int weeks, int days, int hours,
28            int minutes, int seconds) throws DateException {
29
30        Duration duration = new Duration();
31        duration.parse(str);
32
33        assertEquals("Duration sign is not equal for " + str, sign, duration.sign);
34        assertEquals("Duration weeks is not equal for " + str, weeks, duration.weeks);
35        assertEquals("Duration days is not equal for " + str, days, duration.days);
36        assertEquals("Duration hours is not equal for " + str, hours, duration.hours);
37        assertEquals("Duration minutes is not equal for " + str, minutes, duration.minutes);
38        assertEquals("Duration seconds is not equal for " + str, seconds, duration.seconds);
39    }
40
41    @SmallTest
42    public void testParse() throws Exception {
43        verifyDuration("P7W", 1, 7, 0, 0, 0, 0);
44        verifyDuration("PT7W", 1, 7, 0, 0, 0, 0);
45        verifyDuration("-PT7W", -1, 7, 0, 0, 0, 0);
46        verifyDuration("P15DT5H0M20S", 1, 0, 15, 5, 0, 20);
47        verifyDuration("-P15DT5H0M20S", -1, 0, 15, 5, 0, 20);
48        verifyDuration("PT1H2M3S", 1, 0, 0, 1, 2, 3);
49
50        verifyDuration("", 1, 0, 0, 0, 0, 0);
51        verifyDuration("P", 1, 0, 0, 0, 0, 0);
52        verifyDuration("P0W", 1, 0, 0, 0, 0, 0);
53        verifyDuration("P0D", 1, 0, 0, 0, 0, 0);
54        verifyDuration("PT0H0M0S", 1, 0, 0, 0, 0, 0);
55        verifyDuration("P0DT0H0M0S", 1, 0, 0, 0, 0, 0);
56    }
57
58    @SmallTest
59    public void testParseInvalidStrings() throws Exception {
60        try {
61            verifyDuration(" -P15DT5H0M20S", 0, 0, 0, 0, 0, 0);
62            fail("test didn't throw an exception but we expected it to");
63        } catch (DateException e) {
64            // expected
65        }
66
67        try {
68            verifyDuration(" not even close", 0, 0, 0, 0, 0, 0);
69            fail("test didn't throw an exception but we expected it to");
70        } catch (DateException e) {
71            // expected
72        }
73    }
74}
75
76
77
78