OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / security / tests / java / security / TimestampTest.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  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
18 /**
19  * @author Alexander V. Astapchuk
20  * @version $Revision$
21  */
22
23 package org.apache.harmony.security.tests.java.security;
24
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargetNew;
27 import dalvik.annotation.TestTargets;
28 import dalvik.annotation.TestLevel;
29
30 import java.security.Timestamp;
31 import java.security.cert.CertPath;
32 import java.util.Date;
33
34 import org.apache.harmony.security.tests.support.cert.MyCertPath;
35
36 import junit.framework.TestCase;
37 @TestTargetClass(Timestamp.class)
38 /**
39  * Tests for <code>Timestamp</code> class fields and methods
40  *
41  */
42
43 public class TimestampTest extends TestCase {
44     private Date now = new Date();
45
46     private static final byte[] encoding = { 1, 2, 3 };
47
48     private CertPath cpath = new MyCertPath(encoding);
49
50     @TestTargetNew(
51         level = TestLevel.COMPLETE,
52         notes = "",
53         method = "Timestamp",
54         args = {java.util.Date.class, java.security.cert.CertPath.class}
55     )
56     public void testTimestamp() {
57         try {
58             new Timestamp(null, cpath);
59             fail("null was accepted");
60         } catch (NullPointerException ex) { /* ok */
61         }
62
63         try {
64             new Timestamp(now, null);
65             fail("null was accepted");
66             return;
67         } catch (NullPointerException ex) { /* ok */
68         }
69
70         Timestamp timestamp = new Timestamp(now, cpath);
71         assertEquals("not expected value", now, timestamp.getTimestamp());
72         assertEquals("not expected cert path", cpath, timestamp.getSignerCertPath());
73     }
74
75     /*
76      * Class under test for boolean equals(Object)
77      */
78     @TestTargetNew(
79         level = TestLevel.COMPLETE,
80         notes = "",
81         method = "equals",
82         args = {java.lang.Object.class}
83     )
84     public void testEqualsObject() {
85         Timestamp one = new Timestamp(now, cpath);
86         Timestamp two = new Timestamp(now, cpath);
87
88         assertTrue(one.equals(one));
89         assertTrue(one.equals(two));
90         assertTrue(two.equals(one));
91         assertFalse(one.equals(null));
92         assertFalse(one.equals(new Object()));
93
94         Timestamp two1 = new Timestamp(new Date(9999), cpath);
95         assertFalse(one.equals(two1));
96         assertTrue(two1.equals(two1));
97     }
98
99     @TestTargetNew(
100         level = TestLevel.COMPLETE,
101         notes = "",
102         method = "getSignerCertPath",
103         args = {}
104     )
105     public void testGetSignerCertPath() {
106         assertSame(new Timestamp(now, cpath).getSignerCertPath(), cpath);
107     }
108
109     @TestTargetNew(
110         level = TestLevel.COMPLETE,
111         notes = "",
112         method = "getTimestamp",
113         args = {}
114     )
115     public void testGetTimestamp() {
116         Timestamp t = new Timestamp(now, cpath);
117         assertEquals(now, t.getTimestamp());
118         assertNotSame(now, t.getTimestamp());
119     }
120
121     /*
122      * Class under test for String toString()
123      */
124     @TestTargetNew(
125         level = TestLevel.COMPLETE,
126         notes = "",
127         method = "toString",
128         args = {}
129     )
130     public void testToString() {
131         try {
132             String tt = new Timestamp(now, cpath).toString();
133         } catch (Exception e) {
134             fail("Unexpected exception");
135         }
136     }
137
138     /*
139      * Class under test for String hashCode()
140      */
141     @TestTargetNew(
142         level = TestLevel.COMPLETE,
143         notes = "",
144         method = "hashCode",
145         args = {}
146     )
147     public void testHashCode() {
148         Timestamp one = new Timestamp(now, cpath);
149         Timestamp two = new Timestamp(now, cpath);
150         Timestamp three = new Timestamp(now, new MyCertPath(new byte[] { 10,
151                 20, 30 }));
152         Timestamp four = null;
153
154         assertTrue(one.hashCode() == two.hashCode());
155         assertTrue(one.hashCode() != three.hashCode());
156         assertTrue(two.hashCode() != three.hashCode());
157
158         try {
159             four.hashCode();
160             fail("NullPointerException expected");
161         } catch (NullPointerException e) {
162             // expected
163         }
164
165     }
166 }