KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestDeser2001


1 package test.encoding;
2
3 import org.apache.axis.Constants;
4 import org.apache.axis.types.Day;
5 import org.apache.axis.types.Duration;
6 import org.apache.axis.types.HexBinary;
7 import org.apache.axis.types.Month;
8 import org.apache.axis.types.MonthDay;
9 import org.apache.axis.types.NCName;
10 import org.apache.axis.types.NMToken;
11 import org.apache.axis.types.Name;
12 import org.apache.axis.types.NegativeInteger;
13 import org.apache.axis.types.NonNegativeInteger;
14 import org.apache.axis.types.NonPositiveInteger;
15 import org.apache.axis.types.NormalizedString;
16 import org.apache.axis.types.PositiveInteger;
17 import org.apache.axis.types.Time;
18 import org.apache.axis.types.Token;
19 import org.apache.axis.types.URI;
20 import org.apache.axis.types.UnsignedByte;
21 import org.apache.axis.types.UnsignedInt;
22 import org.apache.axis.types.UnsignedLong;
23 import org.apache.axis.types.UnsignedShort;
24 import org.apache.axis.types.Year;
25 import org.apache.axis.types.YearMonth;
26
27 import javax.xml.namespace.QName JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.TimeZone JavaDoc;
32
33 /**
34  * Test deserialization of SOAP responses
35  */

36 public class TestDeser2001 extends TestDeser {
37
38     public TestDeser2001(String JavaDoc name) {
39         super(name, Constants.URI_2001_SCHEMA_XSI,
40                     Constants.URI_2001_SCHEMA_XSD);
41     }
42
43     /**
44      * Test deserialization of Date responses
45      */

46     public void testMinDate() throws Exception JavaDoc {
47         Calendar JavaDoc date = Calendar.getInstance();
48         date.set(1999, 04, 31, 0, 0, 0);
49         date.set(Calendar.MILLISECOND,0);
50         deserialize("<result xsi:type=\"xsd:date\">" +
51                        "1999-05-31" +
52                      "</result>",
53                      date.getTime());
54     }
55
56     /**
57      * Test deserialization of dateTime (Calendar) responses
58      */

59     public void testMinDateTime() throws Exception JavaDoc {
60         Calendar JavaDoc date = Calendar.getInstance();
61         date.set(1999,04,31, 12, 01, 30);
62         date.setTimeZone(TimeZone.getTimeZone("GMT"));
63         date.set(Calendar.MILLISECOND,0);
64         deserialize("<result xsi:type=\"xsd:dateTime\">" +
65                        "1999-05-31T12:01:30Z" +
66                      "</result>",
67                      date);
68     }
69
70     public void testDateTimeZ() throws Exception JavaDoc {
71         Calendar JavaDoc date = Calendar.getInstance();
72         date.set(1999,04,31,12,01,30);
73         date.setTimeZone(TimeZone.getTimeZone("GMT"));
74         date.set(Calendar.MILLISECOND,150);
75         deserialize("<result xsi:type=\"xsd:dateTime\">" +
76                        "1999-05-31T12:01:30.150Z" +
77                      "</result>",
78                      date);
79     }
80
81     public void testDateTZ() throws Exception JavaDoc {
82         Calendar JavaDoc date = Calendar.getInstance();
83         date.set(1999, 04, 31, 0, 0, 0);
84         date.set(Calendar.MILLISECOND,0);
85         deserialize("<result xsi:type=\"xsd:date\">" +
86                        "1999-05-31" +
87                      "</result>",
88                      date.getTime());
89     }
90
91     public void testDateTimeTZ() throws Exception JavaDoc {
92         Calendar JavaDoc date = Calendar.getInstance();
93         date.set(1999,04,31,12,01,30);
94         date.set(Calendar.MILLISECOND,150);
95         deserialize("<result xsi:type=\"xsd:dateTime\">" +
96                        "1999-05-31T12:01:30.150" + calcGMTOffset(date) +
97                      "</result>",
98                      date);
99     }
100
101     /**
102      * Test the xsd:Time deserialization
103      */

104     public void testTimeZ() throws Exception JavaDoc {
105         Calendar JavaDoc date = Calendar.getInstance();
106         date.set(Calendar.HOUR_OF_DAY, 12);
107         date.set(Calendar.MINUTE, 01);
108         date.set(Calendar.SECOND, 30);
109         date.set(Calendar.MILLISECOND,150);
110         date.setTimeZone(TimeZone.getTimeZone("GMT"));
111         Time time = new Time(date);
112         deserialize("<result xsi:type=\"xsd:time\">" +
113                        "12:01:30.150Z" +
114                      "</result>",
115                      time);
116     }
117
118 /* Commented out for now (not sure how I feel about checking in tests which
119    break the build without fixes...) --Glen
120    
121     public void testTimePacific() throws Exception {
122         deserializeCalendar(TimeZone.getTimeZone("PST8PDT"));
123     }
124 */

125
126     /**
127      * this isnt a test, it is here to list timezones
128      */

129
130     public void NotestListTimeZones() throws Exception JavaDoc {
131         String JavaDoc ids[] = TimeZone.getAvailableIDs();
132         for (int i = 9; i < ids.length; i++) {
133             System.out.println(ids[i]);
134         }
135     }
136
137     /**
138      * test local time costs
139      * @throws Exception
140      */

141     public void NotestTimeLocal() throws Exception JavaDoc {
142         deserializeCalendar(TimeZone.getDefault());
143     }
144
145 // /**
146
// * test that this works in Wintertime
147
// * @throws Exception
148
// */
149
public void testTimeUKWinter() throws Exception JavaDoc {
150         deserializeCalendar(TimeZone.getTimeZone("GMT+0:00"));
151     }
152
153     public void testTimeUKSummer() throws Exception JavaDoc {
154         deserializeCalendar(TimeZone.getTimeZone("GMT+1:00"));
155     }
156
157     public void testTimeUK() throws Exception JavaDoc {
158         deserializeCalendar(TimeZone.getTimeZone("Europe/London"));
159     }
160
161     /**
162      * this test is here to track down odd behaviour on one system related to these TZ tests
163      *
164      */

165     public void testTimeZoneLogicWorks() {
166
167         TimeZone JavaDoc tz=TimeZone.getTimeZone("GMT");
168         //assertEquals(0,tz.getDSTSavings());
169
assertEquals(0,tz.getRawOffset());
170         Time time=new Time("12:01:30.150+00:00");
171         String JavaDoc timeVal=time.toString();
172         assertEquals("12:01:30.150Z",timeVal);
173     }
174
175     private void deserializeCalendar(TimeZone JavaDoc tz) throws Exception JavaDoc {
176         deserializeCalendar(2004, 1, 1, tz);
177         deserializeCalendar(2004, 7, 1, tz);
178     }
179
180     private void deserializeCalendar(int year, int month,int day,TimeZone JavaDoc tz) throws Exception JavaDoc {
181         Calendar JavaDoc date = Calendar.getInstance();
182         date.set(Calendar.YEAR, year);
183         date.set(Calendar.MONTH, month);
184         date.set(Calendar.DAY_OF_MONTH, day);
185         date.set(Calendar.HOUR_OF_DAY, 12);
186         date.set(Calendar.MINUTE, 01);
187         date.set(Calendar.SECOND, 30);
188         date.set(Calendar.MILLISECOND, 150);
189         date.setTimeZone(tz);
190         Time time = new Time(date);
191         String JavaDoc offset = calcGMTOffset(date);
192         //diagnostics string
193
String JavaDoc comment=" [time="+time.toString()+"; tz="+tz.getDisplayName()
194                 +"; offset="+offset+"]";
195
196         deserialize("<result xsi:type=\"xsd:time\">" +
197                        "12:01:30.150" + offset +
198                      "</result>",
199                      time,
200                     false,
201                     comment);
202     }
203
204
205     private static final int MILLISECONDS_IN_MINUTE = 60000;
206     private static final int MILLISECONDS_IN_HOUR = 60 * MILLISECONDS_IN_MINUTE;
207
208     /**
209      *
210      * calculate the offset from GMT of the current time zone.
211      * If the underlying time zone of the calendar contains historical
212      * summer time information, the offset will be corrected for summertime
213      * if the date of the calendar is a summertime date
214      * @param cal
215      * @return an offset string such as +3:00 or -2:30. GMT is returned as -00:00
216      */

217     private String JavaDoc calcGMTOffset(Calendar JavaDoc cal) {
218         int msecOffset = cal.get(Calendar.ZONE_OFFSET) +
219                 cal.get(Calendar.DST_OFFSET);
220         int hourOffset = Math.abs(msecOffset / MILLISECONDS_IN_HOUR);
221         String JavaDoc offsetString = msecOffset > 0 ? "+" : "-";
222         offsetString += hourOffset >= 10 ? "" + hourOffset : "0" + hourOffset;
223         offsetString += ":";
224         int minOffset = Math.abs(msecOffset % MILLISECONDS_IN_HOUR);
225         if (minOffset == 0) {
226             offsetString += "00";
227         }
228         else {
229             offsetString += minOffset >= 10 ? "" + minOffset : "0" + minOffset;
230         }
231         return offsetString;
232     }
233
234     public void testBase64() throws Exception JavaDoc {
235         deserialize("<result xsi:type=\"xsd:base64Binary\">QmFzZTY0</result>",
236                     "Base64".getBytes());
237     }
238
239     public void testBase64Null() throws Exception JavaDoc {
240         deserialize("<result xsi:type=\"xsd:base64Binary\"></result>",
241                     new byte[0]);
242     }
243
244     public void testHex() throws Exception JavaDoc {
245         deserialize("<result xsi:type=\"xsd:hexBinary\">50A9</result>",
246                     new HexBinary("50A9"),true);
247     }
248
249     public void testHexNull() throws Exception JavaDoc {
250         deserialize("<result xsi:type=\"xsd:hexBinary\"></result>",
251                     new HexBinary(""),true);
252     }
253
254     public void testToken() throws Exception JavaDoc {
255         deserialize("<result xsi:type=\"xsd:token\">abcdefg</result>",
256                     new Token("abcdefg"),true);
257     }
258
259     public void testNormalizedString() throws Exception JavaDoc {
260         deserialize("<result xsi:type=\"xsd:normalizedString\">abcdefg</result>",
261                     new NormalizedString("abcdefg"),true);
262     }
263
264     public void testUnsignedLong() throws Exception JavaDoc {
265         deserialize("<result xsi:type=\"xsd:unsignedLong\">100</result>",
266                     new UnsignedLong(100),true);
267     }
268
269     public void testUnsignedInt() throws Exception JavaDoc {
270         deserialize("<result xsi:type=\"xsd:unsignedInt\">101</result>",
271                     new UnsignedInt(101),true);
272     }
273
274     public void testUnsignedShort() throws Exception JavaDoc {
275         deserialize("<result xsi:type=\"xsd:unsignedShort\">102</result>",
276                     new UnsignedShort(102),true);
277     }
278
279     public void testUnsignedByte() throws Exception JavaDoc {
280         deserialize("<result xsi:type=\"xsd:unsignedByte\">103</result>",
281                     new UnsignedByte(103),true);
282     }
283
284     public void testNonNegativeInteger() throws Exception JavaDoc {
285         deserialize("<result xsi:type=\"xsd:nonNegativeInteger\">12345678901234567890</result>",
286                     new NonNegativeInteger("12345678901234567890"), true);
287     }
288
289     public void testNonPositiveInteger() throws Exception JavaDoc {
290         deserialize("<result xsi:type=\"xsd:nonPositiveInteger\">-12345678901234567890</result>",
291                     new NonPositiveInteger("-12345678901234567890"), true);
292     }
293
294     public void testNegativeInteger() throws Exception JavaDoc {
295         deserialize("<result xsi:type=\"xsd:negativeInteger\">-12345678901234567890</result>",
296                     new NegativeInteger("-12345678901234567890"), true);
297     }
298
299     public void testPositiveInteger() throws Exception JavaDoc {
300         deserialize("<result xsi:type=\"xsd:positiveInteger\">12345678901234567890</result>",
301                     new PositiveInteger("12345678901234567890"), true);
302     }
303
304     public void testName() throws Exception JavaDoc {
305         deserialize("<result xsi:type=\"xsd:Name\">:Braves</result>",
306                     new Name(":Braves"),true);
307     }
308
309     public void testNCName() throws Exception JavaDoc {
310         deserialize("<result xsi:type=\"xsd:NCName\">_Atlanta.Braves</result>",
311                     new NCName("_Atlanta.Braves"),true);
312     }
313
314     public void testNMToken() throws Exception JavaDoc {
315         deserialize("<result xsi:type=\"xsd:NMTOKEN\">_A.B.C.1-2-3</result>",
316                     new NMToken("_A.B.C.1-2-3"),true);
317     }
318
319     public void testQName() throws Exception JavaDoc {
320         deserialize("<result xsi:type=\"xsd:QName\" xmlns:qns=\"namespace\">qns:localPart</result>", new QName JavaDoc("namespace", "localPart"), true);
321     }
322
323     public void testMapWithNils() throws Exception JavaDoc {
324         HashMap JavaDoc m = new HashMap JavaDoc();
325         m.put(null, new Boolean JavaDoc("false"));
326         m.put("hi", null);
327         deserialize("<result xsi:type=\"xmlsoap:Map\" " +
328                     "xmlns:xmlsoap=\"http://xml.apache.org/xml-soap\"> " +
329                       "<item>" +
330                        "<key xsi:nil=\"true\"/>" +
331                        "<value xsi:type=\"xsd:boolean\">false</value>" +
332                       "</item><item>" +
333                        "<key xsi:type=\"xsd:string\">hi</key>" +
334                        "<value xsi:nil=\"true\"/>" +
335                       "</item>" +
336                     "</result>",
337                     m);
338     }
339
340     public void testArrayWithNilInt() throws Exception JavaDoc {
341         ArrayList JavaDoc list = new ArrayList JavaDoc(4);
342         list.add(new Integer JavaDoc(1));
343         list.add(null);
344         list.add(new Integer JavaDoc(3));
345         deserialize("<result xsi:type=\"soapenc:Array\" " +
346                             "soapenc:arrayType=\"xsd:int[3]\"> " +
347                        "<item xsi:type=\"xsd:int\">1</item>" +
348                        "<item xsi:nil=\"true\"/>" +
349                        "<item xsi:type=\"xsd:int\">3</item>" +
350                     "</result>",
351                     list, true);
352     }
353
354     public void testArrayWithNilString() throws Exception JavaDoc {
355         ArrayList JavaDoc list = new ArrayList JavaDoc(4);
356         list.add("abc");
357         list.add(null);
358         list.add("def");
359         deserialize("<result xsi:type=\"soapenc:Array\" " +
360                             "soapenc:arrayType=\"xsd:string[3]\"> " +
361                        "<item xsi:type=\"xsd:string\">abc</item>" +
362                        "<item xsi:nil=\"true\"/>" +
363                        "<item xsi:type=\"xsd:string\">def</item>" +
364                     "</result>",
365                     list, true);
366     }
367
368     public void testNilSOAPBoolean() throws Exception JavaDoc {
369         deserialize("<result xsi:type=\"soapenc:boolean\" xsi:nil=\"true\" />",
370                     null);
371     }
372
373     public void testYearMonth() throws Exception JavaDoc {
374         org.apache.axis.types.YearMonth ym = new YearMonth(2002, 8);
375         deserialize("<result xsi:type=\"xsd:gYearMonth\">2002-08</result>",
376                      ym);
377     }
378     public void testYear() throws Exception JavaDoc {
379         org.apache.axis.types.Year ym = new Year(2002);
380         deserialize("<result xsi:type=\"xsd:gYear\">2002</result>",
381                      ym);
382     }
383     public void testMonth() throws Exception JavaDoc {
384         org.apache.axis.types.Month ym = new Month(8);
385         deserialize("<result xsi:type=\"xsd:gMonth\">--08--</result>",
386                      ym);
387     }
388     public void testDay() throws Exception JavaDoc {
389         org.apache.axis.types.Day ym = new Day(15);
390         deserialize("<result xsi:type=\"xsd:gDay\">---15</result>",
391                      ym);
392     }
393     public void testMonthDay() throws Exception JavaDoc {
394         org.apache.axis.types.MonthDay ym = new MonthDay(8, 5);
395         deserialize("<result xsi:type=\"xsd:gMonthDay\">--08-05</result>",
396                      ym);
397     }
398     public void testDuration() throws Exception JavaDoc {
399         org.apache.axis.types.Duration ym = new Duration(false, 2, 3, 8, 8, 1, 3.3);
400         deserialize("<result xsi:type=\"xsd:duration\">P2Y3M8DT8H1M3.3S</result>",
401                      ym);
402         org.apache.axis.types.Duration ym2 = new Duration(true, 2, 3, 8, 8, 1, 3.3);
403         deserialize("<result xsi:type=\"xsd:duration\">-P2Y3M8DT8H1M3.3S</result>",
404                      ym2);
405     }
406     public void testAnyURI() throws Exception JavaDoc {
407         org.apache.axis.types.URI uri = new URI("urn:this-is-a-test");
408         deserialize("<result xsi:type=\"xsd:anyURI\">urn:this-is-a-test</result>",
409                      uri);
410         uri = new URI("http", "www.macromedia.com", "/testing", "query=1", null);
411         deserialize("<result xsi:type=\"xsd:anyURI\">http://www.macromedia.com/testing?query=1</result>",
412                      uri);
413     }
414
415     public static void main() throws Exception JavaDoc {
416         TestDeser2001 deser=new TestDeser2001("");
417         deser.testTimeUKWinter();
418     }
419 }
420
Popular Tags