KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > junit > FormatTest


1 /*
2  * Copyright 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 org.apache.ws.jaxme.xs.junit;
18
19 import java.text.ParseException JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.TimeZone JavaDoc;
22
23 import org.apache.ws.jaxme.xs.util.XsDateFormat;
24 import org.apache.ws.jaxme.xs.util.XsDateTimeFormat;
25 import org.apache.ws.jaxme.xs.util.XsTimeFormat;
26
27 import junit.framework.TestCase;
28
29 /** <p>Test case for the various instances of {@link java.text.Format},
30  * which are being used to parse special types like <code>xs:dateTime</code>.</p>
31  *
32  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
33  */

34 public class FormatTest extends TestCase {
35     /** Creates a new test with the given name.
36      */

37     public FormatTest(String JavaDoc pName) {
38         super(pName);
39     }
40
41     private Calendar JavaDoc getCalendar(TimeZone JavaDoc pTimeZone) {
42         Calendar JavaDoc cal = Calendar.getInstance(pTimeZone);
43         cal.set(2004, 01-1, 14, 03, 12, 07);
44         cal.set(Calendar.MILLISECOND, 0);
45         return cal;
46     }
47
48     /** Test for
49      * {@link org.apache.ws.jaxme.xs.util.XsDateTimeFormat#format(Object, StringBuffer, java.text.FieldPosition)}.
50      */

51     public void testFormatDateTime() {
52         Calendar JavaDoc cal = getCalendar(TimeZone.getTimeZone("GMT"));
53         assertEquals(0, cal.get(Calendar.MILLISECOND));
54         XsDateTimeFormat format = new XsDateTimeFormat();
55         String JavaDoc got = format.format(cal);
56         String JavaDoc expect = "2004-01-14T03:12:07Z";
57         assertEquals(expect, got);
58
59         cal = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
60         assertEquals(0, cal.get(Calendar.MILLISECOND));
61         got = format.format(cal);
62         expect = "2004-01-14T03:12:07-03:00";
63         assertEquals(expect, got);
64     }
65
66     /** Test for
67      * {@link org.apache.ws.jaxme.xs.util.XsDateTimeFormat#parseObject(String, java.text.ParsePosition)}.
68      */

69     public void testParseDateTime() throws ParseException JavaDoc {
70         String JavaDoc[] dateTimes = new String JavaDoc[]{
71             "2004-01-14T03:12:07.000Z",
72             "2004-01-14T03:12:07",
73             "2004-01-14T03:12:07-00:00",
74             "2004-01-14T03:12:07+00:00",
75         };
76         XsDateTimeFormat format = new XsDateTimeFormat();
77         Calendar JavaDoc expect = getCalendar(TimeZone.getTimeZone("GMT"));
78         for (int i = 0; i < dateTimes.length; i++) {
79             Calendar JavaDoc got = (Calendar JavaDoc) format.parseObject(dateTimes[0]);
80             assertEquals(expect, got);
81         }
82
83         String JavaDoc dateTime = "2004-01-14T03:12:07.000-03:00";
84         expect = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
85         Calendar JavaDoc got = (Calendar JavaDoc) format.parseObject(dateTime);
86         assertEquals(expect, got);
87     }
88
89     /** Test for
90      * {@link org.apache.ws.jaxme.xs.util.XsDateFormat#format(Object, StringBuffer, java.text.FieldPosition)}.
91      */

92     public void testFormatDate() {
93         Calendar JavaDoc cal = getCalendar(TimeZone.getTimeZone("GMT"));
94         assertEquals(0, cal.get(Calendar.MILLISECOND));
95         XsDateFormat format = new XsDateFormat();
96         String JavaDoc got = format.format(cal);
97         String JavaDoc expect = "2004-01-14Z";
98         assertEquals(expect, got);
99
100         cal = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
101         assertEquals(0, cal.get(Calendar.MILLISECOND));
102         got = format.format(cal);
103         expect = "2004-01-14-03:00";
104         assertEquals(expect, got);
105     }
106
107     protected void assertEqualDate(Calendar JavaDoc pExpect, Calendar JavaDoc pGot) {
108         assertEquals(pExpect.get(Calendar.YEAR), pGot.get(Calendar.YEAR));
109         assertEquals(pExpect.get(Calendar.MONTH), pGot.get(Calendar.MONTH));
110         assertEquals(pExpect.get(Calendar.DAY_OF_MONTH), pGot.get(Calendar.DAY_OF_MONTH));
111         assertEquals(pExpect.getTimeZone(), pGot.getTimeZone());
112     }
113
114     protected void assertEqualTime(Calendar JavaDoc pExpect, Calendar JavaDoc pGot) {
115         assertEquals(pExpect.get(Calendar.HOUR_OF_DAY), pGot.get(Calendar.HOUR_OF_DAY));
116         assertEquals(pExpect.get(Calendar.MINUTE), pGot.get(Calendar.MINUTE));
117         assertEquals(pExpect.get(Calendar.SECOND), pGot.get(Calendar.SECOND));
118         assertEquals(pExpect.get(Calendar.MILLISECOND), pGot.get(Calendar.MILLISECOND));
119         assertEquals(pExpect.getTimeZone(), pGot.getTimeZone());
120     }
121
122     /** Test for
123      * {@link org.apache.ws.jaxme.xs.util.XsDateFormat#parseObject(String, java.text.ParsePosition)}.
124      */

125     public void testParseDate() throws ParseException JavaDoc {
126         String JavaDoc[] dateTimes = new String JavaDoc[]{
127             "2004-01-14Z",
128             "2004-01-14",
129             "2004-01-14+00:00",
130             "2004-01-14-00:00",
131         };
132         XsDateFormat format = new XsDateFormat();
133         Calendar JavaDoc expect = getCalendar(TimeZone.getTimeZone("GMT"));
134         for (int i = 0; i < dateTimes.length; i++) {
135             Calendar JavaDoc got = (Calendar JavaDoc) format.parseObject(dateTimes[0]);
136             assertEqualDate(expect, got);
137         }
138
139         String JavaDoc dateTime = "2004-01-14-03:00";
140         expect = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
141         Calendar JavaDoc got = (Calendar JavaDoc) format.parseObject(dateTime);
142         assertEqualDate(expect, got);
143     }
144
145     /** Test for
146      * {@link org.apache.ws.jaxme.xs.util.XsTimeFormat#format(Object, StringBuffer, java.text.FieldPosition)}.
147      */

148     public void testFormatTime() {
149         Calendar JavaDoc cal = getCalendar(TimeZone.getTimeZone("GMT"));
150         assertEquals(0, cal.get(Calendar.MILLISECOND));
151         XsTimeFormat format = new XsTimeFormat();
152         String JavaDoc got = format.format(cal);
153         String JavaDoc expect = "03:12:07Z";
154         assertEquals(expect, got);
155
156         cal = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
157         assertEquals(0, cal.get(Calendar.MILLISECOND));
158         got = format.format(cal);
159         expect = "03:12:07-03:00";
160         assertEquals(expect, got);
161     }
162
163     /** Test for
164      * {@link org.apache.ws.jaxme.xs.util.XsTimeFormat#parseObject(String, java.text.ParsePosition)}.
165      */

166     public void testParseTime() throws ParseException JavaDoc {
167         String JavaDoc[] dateTimes = new String JavaDoc[]{
168             "03:12:07.000Z",
169             "03:12:07",
170             "03:12:07-00:00",
171             "03:12:07+00:00",
172         };
173         XsTimeFormat format = new XsTimeFormat();
174         Calendar JavaDoc expect = getCalendar(TimeZone.getTimeZone("GMT"));
175         for (int i = 0; i < dateTimes.length; i++) {
176             Calendar JavaDoc got = (Calendar JavaDoc) format.parseObject(dateTimes[0]);
177             assertEqualTime(expect, got);
178         }
179
180         String JavaDoc dateTime = "03:12:07.000-03:00";
181         expect = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
182         Calendar JavaDoc got = (Calendar JavaDoc) format.parseObject(dateTime);
183         assertEqualTime(expect, got);
184     }
185 }
186
Popular Tags