1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dxc.junit.opcodes.getfield; 18 19 import dxc.junit.DxTestCase; 20 import dxc.junit.DxUtil; 21 import dxc.junit.opcodes.getfield.jm.T_getfield_1; 22 import dxc.junit.opcodes.getfield.jm.T_getfield_10; 23 import dxc.junit.opcodes.getfield.jm.T_getfield_11; 24 import dxc.junit.opcodes.getfield.jm.T_getfield_12; 25 import dxc.junit.opcodes.getfield.jm.T_getfield_14; 26 import dxc.junit.opcodes.getfield.jm.T_getfield_16; 27 import dxc.junit.opcodes.getfield.jm.T_getfield_17; 28 import dxc.junit.opcodes.getfield.jm.T_getfield_2; 29 import dxc.junit.opcodes.getfield.jm.T_getfield_5; 30 import dxc.junit.opcodes.getfield.jm.T_getfield_6; 31 import dxc.junit.opcodes.getfield.jm.T_getfield_7; 32 import dxc.junit.opcodes.getfield.jm.T_getfield_8; 33 import dxc.junit.opcodes.getfield.jm.T_getfield_9; 34 import dxc.junit.opcodes.getfield.jm.TestStubs; 35 36 public class Test_getfield extends DxTestCase { 37 private int TestStubField = 123; 38 protected int TestStubFieldP = 0; 39 40 private int privateInt = 456; 41 42 /** 43 * @title type - int 44 */ 45 public void testN1() { 46 T_getfield_1 t = new T_getfield_1(); 47 assertEquals(5, t.run()); 48 } 49 50 /** 51 * @title type - double 52 */ 53 public void testN2() { 54 T_getfield_2 t = new T_getfield_2(); 55 assertEquals(123d, t.run()); 56 } 57 58 /** 59 * @title access protected field from subclass 60 */ 61 public void testN3() { 62 // @uses dxc.junit.opcodes.getfield.jm.T_getfield_1 63 T_getfield_11 t = new T_getfield_11(); 64 assertEquals(10, t.run()); 65 } 66 67 /** 68 * @title assignment compatible references 69 */ 70 public void testN4() { 71 // @uses dxc.junit.opcodes.getfield.jm.TChild 72 // @uses dxc.junit.opcodes.getfield.jm.TSuper 73 T_getfield_14 t = new T_getfield_14(); 74 assertEquals(0, t.run().compareTo("abc")); 75 } 76 77 /** 78 * @title attempt to access static field 79 */ 80 public void testE1() { 81 // @uses dxc.junit.opcodes.getstatic.jm.T_getstatic_1 82 try { 83 T_getfield_5 t = new T_getfield_5(); 84 t.run(); 85 fail("expected IncompatibleClassChangeError"); 86 } catch (IncompatibleClassChangeError e) { 87 // expected 88 } catch (VerifyError vfe) { 89 // ok for dalvikvm; 90 System.out.print("dvmvfe:"); 91 } 92 } 93 94 /** 95 * @title attempt to access of non-accessible private field 96 */ 97 public void testE2() { 98 try { 99 T_getfield_6 t = new T_getfield_6(); 100 int res = t.run(); 101 System.out.println("res:"+res); 102 fail("expected IllegalAccessError"); 103 } catch (IllegalAccessError iae) { 104 // expected 105 } catch (VerifyError vfe) { 106 // ok for dalvikvm; 107 System.out.print("dvmvfe:"); 108 } 109 } 110 111 /** 112 * @title expected NoClassDefFoundError 113 */ 114 public void testE3() { 115 try { 116 // need to include the constructor call into the try-catch block, 117 // since class resolution can take place at any time. 118 // (not only when t.run() is called 119 T_getfield_7 t = new T_getfield_7(); 120 t.run(); 121 fail("expected NoClassDefFoundError"); 122 } catch (NoClassDefFoundError e) { 123 // expected 124 } catch (VerifyError vfe) { 125 // ok for dalvikvm; 126 System.out.print("dvmvfe:"); 127 } 128 } 129 130 /** 131 * @title expected NoSuchFieldError 132 */ 133 public void testE4() { 134 try { 135 T_getfield_8 t = new T_getfield_8(); 136 t.run(); 137 fail("expected NoSuchFieldError"); 138 } catch (NoSuchFieldError e) { 139 // expected 140 } catch (VerifyError vfe) { 141 // ok for dalvikvm; 142 System.out.print("dvmvfe:"); 143 } 144 } 145 146 /** 147 * @title attempt to get int from float field 148 */ 149 public void testE5() { 150 try { 151 T_getfield_10 t = new T_getfield_10(); 152 t.run(); 153 fail("expected NoSuchFieldError"); 154 } catch (NoSuchFieldError e) { 155 // expected 156 } catch (VerifyError vfe) { 157 // ok for dalvikvm; 158 System.out.print("dvmvfe:"); 159 } 160 } 161 162 /** 163 * @title expected NullPointerException 164 */ 165 public void testE6() { 166 T_getfield_9 t = new T_getfield_9(); 167 try { 168 t.run(); 169 fail("expected NullPointerException"); 170 } catch (NullPointerException e) { 171 // expected 172 } 173 } 174 175 //FIXME: "fail" commented out temporarily - check 176 /** 177 * @title attempt to read superclass' private field from subclass 178 * in same package 179 * 180 * FIXME: this seems to be a bug in JDK 1.5? 181 */ 182 public void testE7() { 183 // @uses dxc.junit.opcodes.getfield.jm.T_getfield_1 184 try { 185 T_getfield_12 t = new T_getfield_12(); 186 //fail("expected IllegalAccessError"); 187 } catch (IllegalAccessError e) { 188 // expected 189 } catch (VerifyError vfe) { 190 // ok for dalvikvm; 191 System.out.print("dvmvfe:"); 192 } 193 } 194 195 /** 196 * @title attempt to read private field of a class which was passed 197 * as argument 198 */ 199 public void testE9() { 200 // @uses dxc.junit.opcodes.getfield.jm.TestStubs 201 try { 202 T_getfield_17 t = new T_getfield_17(); 203 t.run(new dxc.junit.opcodes.getfield.jm.TestStubs()); 204 fail("expected IllegalAccessError"); 205 } catch (IllegalAccessError e) { 206 // expected 207 } catch (VerifyError vfe) { 208 // ok for dalvikvm; 209 System.out.print("dvmvfe:"); 210 } 211 } 212 213 214 215 /** 216 * @title attempt to access of non-accessible protected field 217 */ 218 public void testE8() { 219 try { 220 T_getfield_16 t = new T_getfield_16(); 221 t.run(); 222 fail("expected IllegalAccessError"); 223 } catch (IllegalAccessError iae) { 224 // expected 225 } catch (VerifyError vfe) { 226 // ok for dalvikvm; 227 System.out.print("dvmvfe:"); 228 } 229 } 230 231 /** 232 * @constraint 4.8.1.12 233 * @title constant pool index 234 */ 235 public void testVFE1() { 236 try { 237 Class.forName("dxc.junit.opcodes.getfield.jm.T_getfield_4"); 238 fail("expected a verification exception"); 239 } catch (Throwable t) { 240 DxUtil.checkVerifyException(t); 241 } 242 } 243 244 /** 245 * @constraint 4.8.2.5 246 * @title stack size 247 */ 248 public void testVFE2() { 249 try { 250 Class.forName("dxc.junit.opcodes.getfield.jm.T_getfield_3"); 251 fail("expected a verification exception"); 252 } catch (Throwable t) { 253 DxUtil.checkVerifyException(t); 254 } 255 } 256 257 /** 258 * @constraint 4.8.1.12 259 * @title constant pool type 260 */ 261 public void testVFE3() { 262 try { 263 Class.forName("dxc.junit.opcodes.getfield.jm.T_getfield_13"); 264 fail("expected a verification exception"); 265 } catch (Throwable t) { 266 DxUtil.checkVerifyException(t); 267 } 268 } 269 270 /** 271 * @constraint 4.8.1.16 272 * @title assignment incompatible references 273 */ 274 public void testVFE4() { 275 // @uses dxc.junit.opcodes.getfield.jm.TChild 276 // @uses dxc.junit.opcodes.getfield.jm.TSuper 277 try { 278 Class.forName("dxc.junit.opcodes.getfield.jm.T_getfield_15"); 279 fail("expected a verification exception"); 280 } catch (Throwable t) { 281 DxUtil.checkVerifyException(t); 282 } 283 } 284 285 } 286