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 18package tests.api.java.io; 19 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import java.io.ObjectStreamClass; 26import java.io.ObjectStreamField; 27import java.io.Serializable; 28 29@TestTargetClass(ObjectStreamClass.class) 30public class ObjectStreamClassTest extends junit.framework.TestCase { 31 32 static class DummyClass implements Serializable { 33 private static final long serialVersionUID = 999999999999999L; 34 35 long bam = 999L; 36 37 int ham = 9999; 38 39 public static long getUID() { 40 return serialVersionUID; 41 } 42 } 43 44 /** 45 * @tests java.io.ObjectStreamClass#forClass() 46 */ 47 @TestTargetNew( 48 level = TestLevel.COMPLETE, 49 notes = "", 50 method = "forClass", 51 args = {} 52 ) 53 public void test_forClass() { 54 // Test for method java.lang.Class java.io.ObjectStreamClass.forClass() 55 // Need to test during serialization to be sure an instance is 56 // returned 57 ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); 58 assertTrue("forClass returned an object: " + osc.forClass(), osc 59 .forClass().equals(DummyClass.class)); 60 } 61 62 /** 63 * @tests java.io.ObjectStreamClass#getField(java.lang.String) 64 */ 65 @TestTargetNew( 66 level = TestLevel.COMPLETE, 67 notes = "", 68 method = "getField", 69 args = {java.lang.String.class} 70 ) 71 public void test_getFieldLjava_lang_String() { 72 // Test for method java.io.ObjectStreamField 73 // java.io.ObjectStreamClass.getField(java.lang.String) 74 ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); 75 assertEquals("getField did not return correct field", 'J', osc.getField("bam") 76 .getTypeCode()); 77 assertNull("getField did not null for non-existent field", osc 78 .getField("wham")); 79 } 80 81 /** 82 * @tests java.io.ObjectStreamClass#getFields() 83 */ 84 @TestTargetNew( 85 level = TestLevel.COMPLETE, 86 notes = "", 87 method = "getFields", 88 args = {} 89 ) 90 public void test_getFields() { 91 // Test for method java.io.ObjectStreamField [] 92 // java.io.ObjectStreamClass.getFields() 93 ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); 94 ObjectStreamField[] osfArray = osc.getFields(); 95 assertTrue( 96 "Array of fields should be of length 2 but is instead of length: " 97 + osfArray.length, osfArray.length == 2); 98 } 99 100 /** 101 * @tests java.io.ObjectStreamClass#getName() 102 */ 103 @TestTargetNew( 104 level = TestLevel.COMPLETE, 105 notes = "", 106 method = "getName", 107 args = {} 108 ) 109 public void test_getName() { 110 // Test for method java.lang.String java.io.ObjectStreamClass.getName() 111 ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); 112 assertTrue("getName returned incorrect name: " + osc.getName(), osc 113 .getName().equals( 114 "tests.api.java.io.ObjectStreamClassTest$DummyClass")); 115 } 116 117 /** 118 * @tests java.io.ObjectStreamClass#getSerialVersionUID() 119 */ 120 @TestTargetNew( 121 level = TestLevel.COMPLETE, 122 notes = "", 123 method = "getSerialVersionUID", 124 args = {} 125 ) 126 public void test_getSerialVersionUID() { 127 // Test for method long java.io.ObjectStreamClass.getSerialVersionUID() 128 ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); 129 assertTrue("getSerialversionUID returned incorrect uid: " 130 + osc.getSerialVersionUID() + " instead of " 131 + DummyClass.getUID(), osc.getSerialVersionUID() == DummyClass 132 .getUID()); 133 } 134 135 /** 136 * @tests java.io.ObjectStreamClass#lookup(java.lang.Class) 137 */ 138 @TestTargetNew( 139 level = TestLevel.COMPLETE, 140 notes = "", 141 method = "lookup", 142 args = {java.lang.Class.class} 143 ) 144 public void test_lookupLjava_lang_Class() { 145 // Test for method java.io.ObjectStreamClass 146 // java.io.ObjectStreamClass.lookup(java.lang.Class) 147 ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); 148 assertTrue("lookup returned wrong class: " + osc.getName(), osc 149 .getName().equals( 150 "tests.api.java.io.ObjectStreamClassTest$DummyClass")); 151 } 152 153 /** 154 * @tests java.io.ObjectStreamClass#toString() 155 */ 156 @TestTargetNew( 157 level = TestLevel.COMPLETE, 158 notes = "", 159 method = "toString", 160 args = {} 161 ) 162 public void test_toString() { 163 // Test for method java.lang.String java.io.ObjectStreamClass.toString() 164 ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); 165 String oscString = osc.toString(); 166 // The previous test was more specific than the spec so it was replaced 167 // with the test below 168 assertTrue("toString returned incorrect string: " + osc.toString(), 169 oscString.indexOf("serialVersionUID") >= 0 170 && oscString.indexOf("999999999999999L") >= 0); 171 ; 172 } 173 @TestTargetNew( 174 level = TestLevel.COMPLETE, 175 notes = "", 176 method = "lookup", 177 args = {java.lang.Class.class} 178 ) 179 public void testSerialization() { 180 ObjectStreamClass osc = ObjectStreamClass.lookup(ObjectStreamClass.class); 181 assertEquals(0, osc.getFields().length); 182 } 183 184 /** 185 * Sets up the fixture, for example, open a network connection. This method 186 * is called before a test is executed. 187 */ 188 protected void setUp() { 189 } 190 191 /** 192 * Tears down the fixture, for example, close a network connection. This 193 * method is called after a test is executed. 194 */ 195 protected void tearDown() { 196 } 197 198// BEGIN android-added 199 @TestTargetNew( 200 level = TestLevel.COMPLETE, 201 notes = "Verifies serialization.", 202 method = "!Serialization", 203 args = {} 204 ) 205 public void testFooSerialVersionUid() { 206 assertEquals(-5887964677443030867L, Foo.serialVersionUID()); 207 } 208 209 /** 210 * An arbitrary class which deliberately tickles various factors affecting 211 * serialVersionUID calculation. 212 */ 213 static abstract class Foo implements Cloneable, Serializable { 214 215 /** All fields except "private static|transient", which these aren't. */ 216 private final String name = "foo"; 217 static final long now; 218 219 /** Presence of static initializer has an affect. */ 220 static { 221 now = System.currentTimeMillis(); 222 } 223 224 /** Non-private constructors. */ 225 Foo() {} 226 protected Foo(int ignored) {} 227 228 /** Non-private methods. */ 229 synchronized static int foo() { return 0; } 230 static int bar() { return 0; } 231 abstract void tee(); 232 protected native synchronized boolean bob(); 233 protected synchronized void tim() {} 234 235 /** Calculates Foo's default serialVersionUID. */ 236 static long serialVersionUID() { 237 return ObjectStreamClass.lookup(Foo.class).getSerialVersionUID(); 238 } 239 } 240 241 public static void main(String[] args) { 242 System.out.println(Foo.serialVersionUID()); 243 } 244// END android-added 245} 246