01: package org.bouncycastle.asn1.test;
02:
03: import org.bouncycastle.asn1.DERUTCTime;
04: import org.bouncycastle.util.test.SimpleTest;
05:
06: import java.text.SimpleDateFormat;
07: import java.util.SimpleTimeZone;
08:
09: /**
10: * X.690 test example
11: */
12: public class UTCTimeTest extends SimpleTest {
13: String[] input = { "020122122220Z", "020122122220-1000",
14: "020122122220+1000", "020122122220+00", "0201221222Z",
15: "0201221222-1000", "0201221222+1000", "0201221222+00",
16: "550122122220Z", "5501221222Z" };
17:
18: String[] output = { "20020122122220GMT+00:00",
19: "20020122122220GMT-10:00", "20020122122220GMT+10:00",
20: "20020122122220GMT+00:00", "20020122122200GMT+00:00",
21: "20020122122200GMT-10:00", "20020122122200GMT+10:00",
22: "20020122122200GMT+00:00", "19550122122220GMT+00:00",
23: "19550122122200GMT+00:00" };
24:
25: String[] zOutput1 = { "20020122122220Z", "20020122222220Z",
26: "20020122022220Z", "20020122122220Z", "20020122122200Z",
27: "20020122222200Z", "20020122022200Z", "20020122122200Z",
28: "19550122122220Z", "19550122122200Z" };
29:
30: String[] zOutput2 = { "20020122122220Z", "20020122222220Z",
31: "20020122022220Z", "20020122122220Z", "20020122122200Z",
32: "20020122222200Z", "20020122022200Z", "20020122122200Z",
33: "19550122122220Z", "19550122122200Z" };
34:
35: public String getName() {
36: return "UTCTime";
37: }
38:
39: public void performTest() throws Exception {
40: SimpleDateFormat yyyyF = new SimpleDateFormat(
41: "yyyyMMddHHmmss'Z'");
42: SimpleDateFormat yyF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
43:
44: yyyyF.setTimeZone(new SimpleTimeZone(0, "Z"));
45: yyF.setTimeZone(new SimpleTimeZone(0, "Z"));
46:
47: for (int i = 0; i != input.length; i++) {
48: DERUTCTime t = new DERUTCTime(input[i]);
49:
50: if (!t.getAdjustedTime().equals(output[i])) {
51: fail("failed conversion test " + i);
52: }
53:
54: if (!yyyyF.format(t.getAdjustedDate()).equals(zOutput1[i])) {
55: fail("failed date conversion test " + i);
56: }
57:
58: if (!yyF.format(t.getDate()).equals(zOutput2[i])) {
59: fail("failed date shortened conversion test " + i);
60: }
61: }
62: }
63:
64: public static void main(String[] args) {
65: runTest(new UTCTimeTest());
66: }
67: }
|