package com.sprint.jvm.loader;
import com.sprint.jvm.clz.ClassFile;
import com.sprint.jvm.clz.ClassIndex;
import com.sprint.jvm.constant.*;
import org.junit.Test;
import org.junit.Assert;
public class ClassFileLoaderTest {
private static final String FULL_QUALIFIED_CLASS_NAME = "com/sprint/jvm/loader/EmployeeV1";
static String path1 = "/home/sprint/java/code/coding2017/group11/1178243325/week07/build/classes/test";
static String path2 = "/home/sprint/xxx";
static ClassFile clzFile = null;
static {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.sprint.jvm.loader.EmployeeV1";
clzFile = loader.loadClass(className);
clzFile.print();
}
@Test
public void testClassPath(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
loader.addClassPath(path2);
String clzPath = loader.getClassPath();
Assert.assertEquals(path1+";"+path2,clzPath);
}
@Test
public void testClassFileLength() {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.sprint.jvm.loader.EmployeeV1";
byte[] byteCodes = loader.readBinaryCode(className);
// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大
Assert.assertEquals(1050, byteCodes.length);
}
@Test
public void testMagicNumber(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.sprint.jvm.loader.EmployeeV1";
byte[] byteCodes = loader.readBinaryCode(className);
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]};
String acctualValue = this.byteToHexString(codes);
Assert.assertEquals("cafebabe", acctualValue);
}
private String byteToHexString(byte[] codes ){
StringBuffer buffer = new StringBuffer();
for(int i=0;i<codes.length;i++){
byte b = codes[i];
int value = b & 0xFF;
String strHex = Integer.toHexString(value);
if(strHex.length()< 2){
strHex = "0" + strHex;
}
buffer.append(strHex);
}
return buffer.toString();
}
/**
* ----------------------------------------------------------------------
*/
@Test
public void testVersion(){
Assert.assertEquals(0, clzFile.getMinorVersion());
Assert.assertEquals(52, clzFile.getMajorVersion());
}
@Test
public void testConstantPool(){
ConstantPool pool = clzFile.getConstantPool();
Assert.assertEquals(53, pool.getSize());
for (int i = 0; i < 54; i++) {
System.out.println(pool.getConstantInfo(i));
}
{
ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1);
Assert.assertEquals(2, clzInfo.getUtf8Index());
UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2);
Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue());
}
{
ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3);
Assert.assertEquals(4, clzInfo.getUtf8Index());
UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4);
Assert.assertEquals("java/lang/Object", utf8Info.getValue());
}
{
UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5);
Assert.assertEquals("name", utf8Info.getValue());
utf8Info = (UTF8Info) pool.getConstantInfo(6);
Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue());
utf8Info = (UTF8Info) pool.getConstantInfo(7);
Assert.assertEquals("age", utf8Info.getValue());
utf8Info = (UTF8Info) pool.getConstantInfo(8);
Assert.assertEquals("I", utf8Info.getValue());
utf8Info = (UTF8Info) pool.getConstantInfo(9);
Assert.assertEquals("<init>", utf8Info.getValue());
utf8Info = (UTF8Info) pool.getConstantInfo(10);
Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue());
utf8Info = (UTF8Info) pool.getConstantInfo(11);
Assert.assertEquals("Code", utf8Info.getValue());
}
{
MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12);
Assert.assertEquals(3, methodRef.getClassInfoIndex());
Assert.assertEquals(13, methodRef.getNameAndTypeIndex());
}
{
NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13);
Assert.assertEquals(9, nameAndType.getIndex1());
Assert.assertEquals(14, nameAndType.getIndex2());
}
//抽查几个吧
{
MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45);
Assert.assertEquals(1, methodRef.getClassInfoIndex());
Assert.assertEquals(46, methodRef.getNameAndTypeIndex());
}
{
UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53);
Assert.assertEquals("EmployeeV1.java", utf8Info.getValue());
}
}
@Test
public void testClassIndex(){
ClassIndex clzIndex = clzFile.getClassIndex();
ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex());
ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex());
Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName());
Assert.assertEquals("java/lang/Object", superClassInfo.getClassName());
}
}