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