|
| 1 | +package com.coderising.jvm.test; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.junit.After; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +//import com.coderising.jvm.clz.ClassFile; |
| 11 | +//import com.coderising.jvm.clz.ClassIndex; |
| 12 | +//import com.coderising.jvm.cmd.BiPushCmd; |
| 13 | +//import com.coderising.jvm.cmd.ByteCodeCommand; |
| 14 | +//import com.coderising.jvm.cmd.OneOperandCmd; |
| 15 | +//import com.coderising.jvm.cmd.TwoOperandCmd; |
| 16 | +//import com.coderising.jvm.constant.ClassInfo; |
| 17 | +//import com.coderising.jvm.constant.ConstantPool; |
| 18 | +//import com.coderising.jvm.constant.MethodRefInfo; |
| 19 | +//import com.coderising.jvm.constant.NameAndTypeInfo; |
| 20 | +//import com.coderising.jvm.constant.UTF8Info; |
| 21 | +//import com.coderising.jvm.field.Field; |
| 22 | +import com.coderising.jvm.loader.ClassFileLoader; |
| 23 | +//import com.coderising.jvm.method.Method; |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +public class ClassFileloaderTest { |
| 30 | + |
| 31 | + |
| 32 | + private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; |
| 33 | + |
| 34 | + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; |
| 35 | + static String path2 = "C:\temp"; |
| 36 | + |
| 37 | +// static ClassFile clzFile = null; |
| 38 | +// static { |
| 39 | +// ClassFileLoader loader = new ClassFileLoader(); |
| 40 | +// loader.addClassPath(path1); |
| 41 | +// String className = "com.coderising.jvm.test.EmployeeV1"; |
| 42 | +// |
| 43 | +// clzFile = loader.loadClass(className); |
| 44 | +// |
| 45 | +// } |
| 46 | +// |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() throws Exception { |
| 50 | + } |
| 51 | + |
| 52 | + @After |
| 53 | + public void tearDown() throws Exception { |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testClassPath(){ |
| 58 | + |
| 59 | + ClassFileLoader loader = new ClassFileLoader(); |
| 60 | + loader.addClassPath(path1); |
| 61 | + loader.addClassPath(path2); |
| 62 | + |
| 63 | + String clzPath = loader.getClassPath(); |
| 64 | + |
| 65 | + Assert.assertEquals(path1+";"+path2,clzPath); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testClassFileLength() { |
| 71 | + |
| 72 | + ClassFileLoader loader = new ClassFileLoader(); |
| 73 | + loader.addClassPath(path1); |
| 74 | + |
| 75 | + String className = "com.coderising.jvm.test.EmployeeV1"; |
| 76 | + |
| 77 | + byte[] byteCodes = loader.readBinaryCode(className); |
| 78 | + |
| 79 | + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 |
| 80 | + Assert.assertEquals(1056, byteCodes.length); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testMagicNumber(){ |
| 87 | + ClassFileLoader loader = new ClassFileLoader(); |
| 88 | + loader.addClassPath(path1); |
| 89 | + String className = "com.coderising.jvm.test.EmployeeV1"; |
| 90 | + byte[] byteCodes = loader.readBinaryCode(className); |
| 91 | + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; |
| 92 | + |
| 93 | + |
| 94 | + String acctualValue = this.byteToHexString(codes); |
| 95 | + |
| 96 | + Assert.assertEquals("cafebabe", acctualValue); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + private String byteToHexString(byte[] codes ){ |
| 102 | + StringBuffer buffer = new StringBuffer(); |
| 103 | + for(int i=0;i<codes.length;i++){ |
| 104 | + byte b = codes[i]; |
| 105 | + int value = b & 0xFF; |
| 106 | + String strHex = Integer.toHexString(value); |
| 107 | + if(strHex.length()< 2){ |
| 108 | + strHex = "0" + strHex; |
| 109 | + } |
| 110 | + buffer.append(strHex); |
| 111 | + } |
| 112 | + return buffer.toString(); |
| 113 | + } |
| 114 | + |
| 115 | +// add comment for behind test |
| 116 | +// /** |
| 117 | +// * ---------------------------------------------------------------------- |
| 118 | +// */ |
| 119 | +// |
| 120 | +// |
| 121 | +// @Test |
| 122 | +// public void testVersion(){ |
| 123 | +// |
| 124 | +// Assert.assertEquals(0, clzFile.getMinorVersion()); |
| 125 | +// Assert.assertEquals(52, clzFile.getMajorVersion()); |
| 126 | +// |
| 127 | +// } |
| 128 | +// |
| 129 | +// @Test |
| 130 | +// public void testConstantPool(){ |
| 131 | +// |
| 132 | +// |
| 133 | +// ConstantPool pool = clzFile.getConstantPool(); |
| 134 | +// |
| 135 | +// Assert.assertEquals(53, pool.getSize()); |
| 136 | +// |
| 137 | +// { |
| 138 | +// ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); |
| 139 | +// Assert.assertEquals(2, clzInfo.getUtf8Index()); |
| 140 | +// |
| 141 | +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); |
| 142 | +// Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue()); |
| 143 | +// } |
| 144 | +// { |
| 145 | +// ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); |
| 146 | +// Assert.assertEquals(4, clzInfo.getUtf8Index()); |
| 147 | +// |
| 148 | +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); |
| 149 | +// Assert.assertEquals("java/lang/Object", utf8Info.getValue()); |
| 150 | +// } |
| 151 | +// { |
| 152 | +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); |
| 153 | +// Assert.assertEquals("name", utf8Info.getValue()); |
| 154 | +// |
| 155 | +// utf8Info = (UTF8Info) pool.getConstantInfo(6); |
| 156 | +// Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); |
| 157 | +// |
| 158 | +// utf8Info = (UTF8Info) pool.getConstantInfo(7); |
| 159 | +// Assert.assertEquals("age", utf8Info.getValue()); |
| 160 | +// |
| 161 | +// utf8Info = (UTF8Info) pool.getConstantInfo(8); |
| 162 | +// Assert.assertEquals("I", utf8Info.getValue()); |
| 163 | +// |
| 164 | +// utf8Info = (UTF8Info) pool.getConstantInfo(9); |
| 165 | +// Assert.assertEquals("<init>", utf8Info.getValue()); |
| 166 | +// |
| 167 | +// utf8Info = (UTF8Info) pool.getConstantInfo(10); |
| 168 | +// Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); |
| 169 | +// |
| 170 | +// utf8Info = (UTF8Info) pool.getConstantInfo(11); |
| 171 | +// Assert.assertEquals("Code", utf8Info.getValue()); |
| 172 | +// } |
| 173 | +// |
| 174 | +// { |
| 175 | +// MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); |
| 176 | +// Assert.assertEquals(3, methodRef.getClassInfoIndex()); |
| 177 | +// Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); |
| 178 | +// } |
| 179 | +// |
| 180 | +// { |
| 181 | +// NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); |
| 182 | +// Assert.assertEquals(9, nameAndType.getIndex1()); |
| 183 | +// Assert.assertEquals(14, nameAndType.getIndex2()); |
| 184 | +// } |
| 185 | +// //抽查几个吧 |
| 186 | +// { |
| 187 | +// MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); |
| 188 | +// Assert.assertEquals(1, methodRef.getClassInfoIndex()); |
| 189 | +// Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); |
| 190 | +// } |
| 191 | +// |
| 192 | +// { |
| 193 | +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); |
| 194 | +// Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); |
| 195 | +// } |
| 196 | +// } |
| 197 | +// @Test |
| 198 | +// public void testClassIndex(){ |
| 199 | +// |
| 200 | +// ClassIndex clzIndex = clzFile.getClzIndex(); |
| 201 | +// ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); |
| 202 | +// ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); |
| 203 | +// |
| 204 | +// |
| 205 | +// Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); |
| 206 | +// Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); |
| 207 | +// } |
| 208 | +// |
| 209 | +// /** |
| 210 | +// * 下面是第三次JVM课应实现的测试用例 |
| 211 | +// */ |
| 212 | +// @Test |
| 213 | +// public void testReadFields(){ |
| 214 | +// |
| 215 | +// List<Field> fields = clzFile.getFields(); |
| 216 | +// Assert.assertEquals(2, fields.size()); |
| 217 | +// { |
| 218 | +// Field f = fields.get(0); |
| 219 | +// Assert.assertEquals("name:Ljava/lang/String;", f.toString()); |
| 220 | +// } |
| 221 | +// { |
| 222 | +// Field f = fields.get(1); |
| 223 | +// Assert.assertEquals("age:I", f.toString()); |
| 224 | +// } |
| 225 | +// } |
| 226 | +// @Test |
| 227 | +// public void testMethods(){ |
| 228 | +// |
| 229 | +// List<Method> methods = clzFile.getMethods(); |
| 230 | +// ConstantPool pool = clzFile.getConstantPool(); |
| 231 | +// |
| 232 | +// { |
| 233 | +// Method m = methods.get(0); |
| 234 | +// assertMethodEquals(pool,m, |
| 235 | +// "<init>", |
| 236 | +// "(Ljava/lang/String;I)V", |
| 237 | +// "2ab7000c2a2bb5000f2a1cb50011b1"); |
| 238 | +// |
| 239 | +// } |
| 240 | +// { |
| 241 | +// Method m = methods.get(1); |
| 242 | +// assertMethodEquals(pool,m, |
| 243 | +// "setName", |
| 244 | +// "(Ljava/lang/String;)V", |
| 245 | +// "2a2bb5000fb1"); |
| 246 | +// |
| 247 | +// } |
| 248 | +// { |
| 249 | +// Method m = methods.get(2); |
| 250 | +// assertMethodEquals(pool,m, |
| 251 | +// "setAge", |
| 252 | +// "(I)V", |
| 253 | +// "2a1bb50011b1"); |
| 254 | +// } |
| 255 | +// { |
| 256 | +// Method m = methods.get(3); |
| 257 | +// assertMethodEquals(pool,m, |
| 258 | +// "sayHello", |
| 259 | +// "()V", |
| 260 | +// "b2001c1222b60024b1"); |
| 261 | +// |
| 262 | +// } |
| 263 | +// { |
| 264 | +// Method m = methods.get(4); |
| 265 | +// assertMethodEquals(pool,m, |
| 266 | +// "main", |
| 267 | +// "([Ljava/lang/String;)V", |
| 268 | +// "bb000159122b101db7002d4c2bb6002fb1"); |
| 269 | +// } |
| 270 | +// } |
| 271 | +// |
| 272 | +// private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ |
| 273 | +// String methodName = pool.getUTF8String(m.getNameIndex()); |
| 274 | +// String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); |
| 275 | +// String code = m.getCodeAttr().getCode(); |
| 276 | +// Assert.assertEquals(expectedName, methodName); |
| 277 | +// Assert.assertEquals(expectedDesc, methodDesc); |
| 278 | +// Assert.assertEquals(expectedCode, code); |
| 279 | +// } |
| 280 | +// |
| 281 | +// @Test |
| 282 | +// public void testByteCodeCommand(){ |
| 283 | +// { |
| 284 | +// Method initMethod = this.clzFile.getMethod("<init>", "(Ljava/lang/String;I)V"); |
| 285 | +// ByteCodeCommand [] cmds = initMethod.getCmds(); |
| 286 | +// |
| 287 | +// assertOpCodeEquals("0: aload_0", cmds[0]); |
| 288 | +// assertOpCodeEquals("1: invokespecial #12", cmds[1]); |
| 289 | +// assertOpCodeEquals("4: aload_0", cmds[2]); |
| 290 | +// assertOpCodeEquals("5: aload_1", cmds[3]); |
| 291 | +// assertOpCodeEquals("6: putfield #15", cmds[4]); |
| 292 | +// assertOpCodeEquals("9: aload_0", cmds[5]); |
| 293 | +// assertOpCodeEquals("10: iload_2", cmds[6]); |
| 294 | +// assertOpCodeEquals("11: putfield #17", cmds[7]); |
| 295 | +// assertOpCodeEquals("14: return", cmds[8]); |
| 296 | +// } |
| 297 | +// |
| 298 | +// { |
| 299 | +// Method setNameMethod = this.clzFile.getMethod("setName", "(Ljava/lang/String;)V"); |
| 300 | +// ByteCodeCommand [] cmds = setNameMethod.getCmds(); |
| 301 | +// |
| 302 | +// assertOpCodeEquals("0: aload_0", cmds[0]); |
| 303 | +// assertOpCodeEquals("1: aload_1", cmds[1]); |
| 304 | +// assertOpCodeEquals("2: putfield #15", cmds[2]); |
| 305 | +// assertOpCodeEquals("5: return", cmds[3]); |
| 306 | +// |
| 307 | +// } |
| 308 | +// |
| 309 | +// { |
| 310 | +// Method sayHelloMethod = this.clzFile.getMethod("sayHello", "()V"); |
| 311 | +// ByteCodeCommand [] cmds = sayHelloMethod.getCmds(); |
| 312 | +// |
| 313 | +// assertOpCodeEquals("0: getstatic #28", cmds[0]); |
| 314 | +// assertOpCodeEquals("3: ldc #34", cmds[1]); |
| 315 | +// assertOpCodeEquals("5: invokevirtual #36", cmds[2]); |
| 316 | +// assertOpCodeEquals("8: return", cmds[3]); |
| 317 | +// |
| 318 | +// } |
| 319 | +// |
| 320 | +// { |
| 321 | +// Method mainMethod = this.clzFile.getMainMethod(); |
| 322 | +// |
| 323 | +// ByteCodeCommand [] cmds = mainMethod.getCmds(); |
| 324 | +// |
| 325 | +// assertOpCodeEquals("0: new #1", cmds[0]); |
| 326 | +// assertOpCodeEquals("3: dup", cmds[1]); |
| 327 | +// assertOpCodeEquals("4: ldc #43", cmds[2]); |
| 328 | +// assertOpCodeEquals("6: bipush 29", cmds[3]); |
| 329 | +// assertOpCodeEquals("8: invokespecial #45", cmds[4]); |
| 330 | +// assertOpCodeEquals("11: astore_1", cmds[5]); |
| 331 | +// assertOpCodeEquals("12: aload_1", cmds[6]); |
| 332 | +// assertOpCodeEquals("13: invokevirtual #47", cmds[7]); |
| 333 | +// assertOpCodeEquals("16: return", cmds[8]); |
| 334 | +// } |
| 335 | +// |
| 336 | +// } |
| 337 | +// |
| 338 | +// private void assertOpCodeEquals(String expected, ByteCodeCommand cmd){ |
| 339 | +// |
| 340 | +// String acctual = cmd.getOffset()+": "+cmd.getReadableCodeText(); |
| 341 | +// |
| 342 | +// if(cmd instanceof OneOperandCmd){ |
| 343 | +// if(cmd instanceof BiPushCmd){ |
| 344 | +// acctual += " " + ((OneOperandCmd)cmd).getOperand(); |
| 345 | +// } else{ |
| 346 | +// acctual += " #" + ((OneOperandCmd)cmd).getOperand(); |
| 347 | +// } |
| 348 | +// } |
| 349 | +// if(cmd instanceof TwoOperandCmd){ |
| 350 | +// acctual += " #" + ((TwoOperandCmd)cmd).getIndex(); |
| 351 | +// } |
| 352 | +// Assert.assertEquals(expected, acctual); |
| 353 | +// } |
| 354 | + |
| 355 | +} |
0 commit comments