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 23package org.apache.harmony.security.tests.java.security; 24 25import java.security.Timestamp; 26import java.security.cert.CertPath; 27import java.util.Date; 28 29import org.apache.harmony.security.tests.support.cert.MyCertPath; 30 31import junit.framework.TestCase; 32 33/** 34 * Tests for <code>Timestamp</code> class fields and methods 35 * 36 */ 37 38public class TimestampTest extends TestCase { 39 private Date now = new Date(); 40 41 private static final byte[] encoding = { 1, 2, 3 }; 42 43 private CertPath cpath = new MyCertPath(encoding); 44 45 public void testTimestamp() { 46 try { 47 new Timestamp(null, cpath); 48 fail("null was accepted"); 49 } catch (NullPointerException ex) { /* ok */ 50 } 51 52 try { 53 new Timestamp(now, null); 54 fail("null was accepted"); 55 return; 56 } catch (NullPointerException ex) { /* ok */ 57 } 58 59 Timestamp timestamp = new Timestamp(now, cpath); 60 assertEquals("not expected value", now, timestamp.getTimestamp()); 61 assertEquals("not expected cert path", cpath, timestamp.getSignerCertPath()); 62 } 63 64 /* 65 * Class under test for boolean equals(Object) 66 */ 67 public void testEqualsObject() { 68 Timestamp one = new Timestamp(now, cpath); 69 Timestamp two = new Timestamp(now, cpath); 70 71 assertTrue(one.equals(one)); 72 assertTrue(one.equals(two)); 73 assertTrue(two.equals(one)); 74 assertFalse(one.equals(null)); 75 assertFalse(one.equals(new Object())); 76 77 Timestamp two1 = new Timestamp(new Date(9999), cpath); 78 assertFalse(one.equals(two1)); 79 assertTrue(two1.equals(two1)); 80 } 81 82 public void testGetSignerCertPath() { 83 assertSame(new Timestamp(now, cpath).getSignerCertPath(), cpath); 84 } 85 86 public void testGetTimestamp() { 87 Timestamp t = new Timestamp(now, cpath); 88 assertEquals(now, t.getTimestamp()); 89 assertNotSame(now, t.getTimestamp()); 90 } 91 92 /* 93 * Class under test for String toString() 94 */ 95 public void testToString() { 96 try { 97 String tt = new Timestamp(now, cpath).toString(); 98 } catch (Exception e) { 99 fail("Unexpected exception"); 100 } 101 } 102 103 /* 104 * Class under test for String hashCode() 105 */ 106 public void testHashCode() { 107 Timestamp one = new Timestamp(now, cpath); 108 Timestamp two = new Timestamp(now, cpath); 109 Timestamp three = new Timestamp(now, new MyCertPath(new byte[] { 10, 110 20, 30 })); 111 Timestamp four = null; 112 113 assertTrue(one.hashCode() == two.hashCode()); 114 assertTrue(one.hashCode() != three.hashCode()); 115 assertTrue(two.hashCode() != three.hashCode()); 116 117 try { 118 four.hashCode(); 119 fail("NullPointerException expected"); 120 } catch (NullPointerException e) { 121 // expected 122 } 123 124 } 125} 126