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 dot.junit.opcodes.new_instance; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.new_instance.d.T_new_instance_1; 22 import dot.junit.opcodes.new_instance.d.T_new_instance_3; 23 import dot.junit.opcodes.new_instance.d.T_new_instance_4; 24 import dot.junit.opcodes.new_instance.d.T_new_instance_5; 25 import dot.junit.opcodes.new_instance.d.T_new_instance_8; 26 import dot.junit.opcodes.new_instance.d.T_new_instance_9; 27 28 29 public class Test_new_instance extends DxTestCase { 30 31 /** 32 * @title new String 33 */ 34 public void testN1() { 35 T_new_instance_1 t = new T_new_instance_1(); 36 String s = t.run(); 37 assertNotNull(s); 38 assertEquals(0, s.compareTo("abc")); 39 } 40 41 /** 42 * @title class initialization throws exception 43 */ 44 public void testE1() { 45 try { 46 T_new_instance_3.run(); 47 fail("expected Error"); 48 } catch (Error e) { 49 // expected 50 } 51 } 52 53 /** 54 * @constraint A21 55 * @title attempt to instantiate interface 56 */ 57 public void testE4() { 58 //@uses dot.junit.opcodes.new_instance.d.TestAbstractClass 59 //@uses dot.junit.opcodes.new_instance.d.T_new_instance_8 60 T_new_instance_8 t = new T_new_instance_8(); 61 try { 62 t.run(); 63 fail("expected InstantiationError"); 64 } catch (InstantiationError ie) { 65 // expected 66 } 67 } 68 69 /** 70 * @constraint A21 71 * @title attempt to instantiate abstract 72 * class 73 */ 74 public void testE5() { 75 //@uses dot.junit.opcodes.new_instance.d.TestAbstractClass 76 //@uses dot.junit.opcodes.new_instance.d.T_new_instance_9 77 T_new_instance_9 t = new T_new_instance_9(); 78 try { 79 t.run(); 80 fail("expected Error"); 81 } catch (Error iae) { 82 // expected 83 } 84 } 85 86 /** 87 * @constraint A18 88 * @title constant pool index 89 */ 90 public void testVFE1() { 91 try { 92 Class.forName("dot.junit.opcodes.new_instance.d.T_new_instance_6"); 93 fail("expected a verification exception"); 94 } catch (Throwable t) { 95 DxUtil.checkVerifyException(t); 96 } 97 } 98 99 /** 100 * 101 * @constraint A21 102 * @title attempt to create array using new 103 */ 104 public void testVFE2() { 105 try { 106 Class.forName("dot.junit.opcodes.new_instance.d.T_new_instance_7"); 107 fail("expected a verification exception"); 108 } catch (Throwable t) { 109 DxUtil.checkVerifyException(t); 110 } 111 } 112 113 /** 114 * @constraint B6 115 * @title Attempt to access uninitialized class (before <init> is 116 * called 117 */ 118 public void testVFE3() { 119 try { 120 Class.forName("dot.junit.opcodes.new_instance.d.T_new_instance_2"); 121 fail("expected a verification exception"); 122 } catch (Throwable t) { 123 DxUtil.checkVerifyException(t); 124 } 125 } 126 127 /** 128 * @constraint A23 129 * @title number of registers 130 */ 131 public void testVFE4() { 132 try { 133 Class.forName("dot.junit.opcodes.new_instance.d.T_new_instance_10"); 134 fail("expected a verification exception"); 135 } catch (Throwable t) { 136 DxUtil.checkVerifyException(t); 137 } 138 } 139 140 /** 141 * @constraint n/a 142 * @title Attempt to instantiate array of inaccessible class. 143 */ 144 public void testVFE5() { 145 //@uses dot.junit.opcodes.new_instance.TestStubs 146 //@uses dot.junit.opcodes.new_instance.d.T_new_instance_4 147 try { 148 new T_new_instance_4().run(); 149 fail("expected IllegalAccessError"); 150 } catch (IllegalAccessError t) { 151 } 152 } 153 154 /** 155 * @constraint n/a 156 * @title Attempt to instantiate array of non-existent class. 157 */ 158 public void testVFE6() { 159 try { 160 new T_new_instance_5().run(); 161 fail("expected NoClassDefFoundError"); 162 } catch (NoClassDefFoundError t) { 163 } 164 } 165 166 /** 167 * @constraint B7 168 * @title A register which holds the result of a new-instance instruction must not be used 169 * if the same new-instance instruction is again executed before the instance is initialized 170 */ 171 public void testVFE7() { 172 try { 173 Class.forName("dot.junit.opcodes.new_instance.d.T_new_instance_11"); 174 fail("expected a verification exception"); 175 } catch (Throwable t) { 176 DxUtil.checkVerifyException(t); 177 } 178 } 179 180 /** 181 * @constraint B7 182 * @title A register which holds the result of a new-instance instruction must not be used 183 * if the same new-instance instruction is again executed before the instance is initialized 184 */ 185 public void testVFE8() { 186 try { 187 Class.forName("dot.junit.opcodes.new_instance.d.T_new_instance_12"); 188 fail("expected a verification exception"); 189 } catch (Throwable t) { 190 DxUtil.checkVerifyException(t); 191 } 192 } 193 } 194