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.putstatic; 18 19 import dxc.junit.DxTestCase; 20 import dxc.junit.DxUtil; 21 import dxc.junit.opcodes.putstatic.jm.T_putstatic_1; 22 import dxc.junit.opcodes.putstatic.jm.T_putstatic_10; 23 import dxc.junit.opcodes.putstatic.jm.T_putstatic_11; 24 import dxc.junit.opcodes.putstatic.jm.T_putstatic_12; 25 import dxc.junit.opcodes.putstatic.jm.T_putstatic_13; 26 import dxc.junit.opcodes.putstatic.jm.T_putstatic_14; 27 import dxc.junit.opcodes.putstatic.jm.T_putstatic_15; 28 import dxc.junit.opcodes.putstatic.jm.T_putstatic_16; 29 import dxc.junit.opcodes.putstatic.jm.T_putstatic_2; 30 import dxc.junit.opcodes.putstatic.jm.T_putstatic_7; 31 import dxc.junit.opcodes.putstatic.jm.T_putstatic_8; 32 import dxc.junit.opcodes.putstatic.jm.T_putstatic_9; 33 34 public class Test_putstatic extends DxTestCase { 35 36 /** 37 * @title type - int 38 */ 39 public void testN1() { 40 T_putstatic_1 t = new T_putstatic_1(); 41 assertEquals(0, T_putstatic_1.st_i1); 42 t.run(); 43 assertEquals(1000000, T_putstatic_1.st_i1); 44 } 45 46 /** 47 * @title type - double 48 */ 49 public void testN2() { 50 T_putstatic_2 t = new T_putstatic_2(); 51 assertEquals(0d, T_putstatic_2.st_d1); 52 t.run(); 53 assertEquals(1000000d, T_putstatic_2.st_d1); 54 } 55 56 /** 57 * @title modification of final field 58 */ 59 public void testN3() { 60 T_putstatic_12 t = new T_putstatic_12(); 61 assertEquals(0, T_putstatic_12.st_i1); 62 t.run(); 63 assertEquals(1000000, T_putstatic_12.st_i1); 64 } 65 66 /** 67 * @title modification of protected field from subclass 68 */ 69 public void testN4() { 70 // @uses dxc.junit.opcodes.putstatic.jm.T_putstatic_1 71 T_putstatic_14 t = new T_putstatic_14(); 72 assertEquals(0, T_putstatic_14.getProtectedField()); 73 t.run(); 74 assertEquals(1000000, T_putstatic_14.getProtectedField()); 75 } 76 77 /** 78 * @title assignment compatible references 79 */ 80 public void testN5() { 81 T_putstatic_16 t = new T_putstatic_16(); 82 assertNull(T_putstatic_16.o); 83 t.run(); 84 assertEquals("", (String) T_putstatic_16.o); 85 } 86 87 /** 88 * @title attempt to set non-static field 89 */ 90 public void testE1() { 91 try { 92 T_putstatic_7 t = new T_putstatic_7(); 93 t.run(); 94 fail("expected IncompatibleClassChangeError"); 95 } catch (IncompatibleClassChangeError e) { 96 // expected 97 } catch (VerifyError vfe) { 98 // ok for dalvikvm; 99 System.out.print("dvmvfe:"); 100 } 101 } 102 103 /** 104 * @title modification of non-accessible field 105 */ 106 public void testE2() { 107 // @uses dxc.junit.opcodes.putstatic.TestStubs 108 try { 109 T_putstatic_8 t = new T_putstatic_8(); 110 t.run(); 111 fail("expected IllegalAccessError"); 112 } catch (IllegalAccessError iae) { 113 // expected 114 } catch (VerifyError vfe) { 115 // ok for dalvikvm; 116 System.out.print("dvmvfe:"); 117 } 118 } 119 120 /** 121 * @title expected NoClassDefFoundError 122 */ 123 public void testE3() { 124 try { 125 T_putstatic_9 t = new T_putstatic_9(); 126 t.run(); 127 fail("expected NoClassDefFoundError"); 128 } catch (NoClassDefFoundError e) { 129 // expected 130 } catch (VerifyError vfe) { 131 // ok for dalvikvm; 132 System.out.print("dvmvfe:"); 133 } 134 } 135 136 /** 137 * @title expected NoSuchFieldError 138 */ 139 public void testE4() { 140 try { 141 T_putstatic_10 t = new T_putstatic_10(); 142 t.run(); 143 fail("expected NoSuchFieldError"); 144 } catch (NoSuchFieldError e) { 145 // expected 146 } catch (VerifyError vfe) { 147 // ok for dalvikvm; 148 System.out.print("dvmvfe:"); 149 } 150 } 151 152 /** 153 * @title Modification of final field in other class 154 */ 155 public void testE5() { 156 // @uses dxc.junit.opcodes.putstatic.TestStubs 157 try { 158 T_putstatic_11 t = new T_putstatic_11(); 159 t.run(); 160 fail("expected IllegalAccessError"); 161 } catch (IllegalAccessError iae) { 162 // expected 163 } catch (VerifyError vfe) { 164 // ok for dalvikvm; 165 System.out.print("dvmvfe:"); 166 } 167 } 168 169 /** 170 * @title initialization of referenced class throws exception 171 */ 172 public void testE6() { 173 // @uses dxc.junit.opcodes.putstatic.jm.StubInitError 174 T_putstatic_13 t = new T_putstatic_13(); 175 try { 176 t.run(); 177 fail("expected Error"); 178 } catch (Error e) { 179 // expected 180 } 181 } 182 183 // FIXME: "fail" commented out temporarily - check 184 /** 185 * @title modification of superclass' private field from subclass 186 * FIXME: is this a bug in JVM? 187 */ 188 public void testE7() { 189 // @uses dxc.junit.opcodes.putstatic.jm.T_putstatic_1 190 try { 191 assertEquals(0, T_putstatic_15.getPvtField()); 192 T_putstatic_15 t = new T_putstatic_15(); 193 t.run(); 194 assertEquals(12321, T_putstatic_15.getPvtField()); 195 //fail("expected IllegalAccessError"); 196 } catch (IllegalAccessError e) { 197 // expected 198 } catch (VerifyError vfe) { 199 // ok for dalvikvm; 200 System.out.print("dvmvfe:"); 201 } 202 } 203 204 /** 205 * @constraint 4.8.1.12 206 * @title constant pool index 207 */ 208 public void testVFE1() { 209 try { 210 Class.forName("dxc.junit.opcodes.putstatic.jm.T_putstatic_3"); 211 fail("expected a verification exception"); 212 } catch (Throwable t) { 213 DxUtil.checkVerifyException(t); 214 } 215 } 216 217 /** 218 * @constraint 4.8.2.1 219 * @title number of arguments 220 */ 221 public void testVFE2() { 222 try { 223 Class.forName("dxc.junit.opcodes.putstatic.jm.T_putstatic_4"); 224 fail("expected a verification exception"); 225 } catch (Throwable t) { 226 DxUtil.checkVerifyException(t); 227 } 228 } 229 230 /** 231 * @constraint 4.8.2.17 232 * @title type of argument - float instead of 233 * int 234 */ 235 public void testVFE3() { 236 try { 237 Class.forName("dxc.junit.opcodes.putstatic.jm.T_putstatic_5"); 238 fail("expected a verification exception"); 239 } catch (Throwable t) { 240 DxUtil.checkVerifyException(t); 241 } 242 } 243 244 /** 245 * @constraint 4.8.2.17 246 * @title type of argument - assignment 247 * incompatible references 248 */ 249 public void testVFE4() { 250 try { 251 Class.forName("dxc.junit.opcodes.putstatic.jm.T_putstatic_6"); 252 fail("expected a verification exception"); 253 } catch (Throwable t) { 254 DxUtil.checkVerifyException(t); 255 } 256 } 257 258 /** 259 * @constraint 4.8.2.17 260 * @title type of argument - assignment 261 * incompatible values 262 */ 263 public void testVFE5() { 264 try { 265 Class.forName("dxc.junit.opcodes.putstatic.jm.T_putstatic_18"); 266 fail("expected a verification exception"); 267 } catch (Throwable t) { 268 DxUtil.checkVerifyException(t); 269 } 270 } 271 272 273 /** 274 * @constraint 4.8.1.12 275 * @title constant pool type 276 */ 277 public void testVFE6() { 278 try { 279 Class.forName("dxc.junit.opcodes.putstatic.jm.T_putstatic_17"); 280 fail("expected a verification exception"); 281 } catch (Throwable t) { 282 DxUtil.checkVerifyException(t); 283 } 284 } 285 286 } 287