具体代码在:这里(求打星!!)
Testing strategy
使用等价类和边界值的测试思想。
鉴于新增的情况主要出现在三个具体应用的实现CircularOrbit接口的类中,此处具体对这三个类的测试策略进行描述。
三个类中按3.1中定义的三种Exception的类型进行等价类划分,并对这些异常写了相应的三个.txt测试文件。通过读文件时捕获异常并对异常的类进行比对、对日志文件中记录的情况进行字符串比对等操作,确定原java文件是否正确。
测试用例设计
错误文档的构造:
@Test
public void testWrongtxt()
{
CircularOrbit<CentralObject, PhysicalObject> cir = CircularOrbit.empty("Q3");
try {
cir.readfile("test AtomicStructureWrong.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
cir = CircularOrbit.empty("Q3");
try {
cir.readfile("test AtomicStructureWrong2.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
cir = CircularOrbit.empty("Q3");
cir.islegal();
try {
cir.readfile("test AtomicStructureWrong3.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
}
@Test
public void testWrongtxt()
{
CircularOrbit< CentralObject, PhysicalObject> cir = CircularOrbit.empty("Q5");
try {
cir.readfile("test SocialNetworkCircleWrong.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
try {
cir.islegal();
} catch (Exception e) {
// TODO: handle exception
}
cir = CircularOrbit.empty("Q5");
try {
cir.readfile("test SocialNetworkCircleWrong2.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
cir = CircularOrbit.empty("Q5");
try {
cir.readfile("test SocialNetworkCircleWrong3.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
cir = CircularOrbit.empty("Q5");
try {
cir.addcenterobject(null);
} catch (Throwable e) {
// TODO: handle exception
}
try {
cir.deletecentralobj(null);
} catch (Throwable e) {
// TODO: handle exception
}
try {
cir.deletecontactco(null, null);
} catch (Throwable e) {
// TODO: handle exception
}
try {
cir.deleteorbitobj(null);
} catch (Throwable e) {
// TODO: handle exception
}
try {
cir.deletecontactoo(null, null);
} catch (Throwable e) {
// TODO: handle exception
}
try {
cir.addtoorbit(null, null);
} catch (Throwable e) {
// TODO: handle exception
}
try {
cir.addcontactoo(null, null, null);
} catch (Throwable e) {
// TODO: handle exception
}
try {
cir.addcontactco(null, null, null);
} catch (Throwable e) {
// TODO: handle exception
}
}
@Test
public void testWrongtxt()
{
CircularOrbit< CentralObject, PhysicalObject> cir = CircularOrbit.empty("Q2");
try {
cir.readfile("test StellarSystemWrong.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
cir = CircularOrbit.empty("Q2");
try {
cir.readfile("test StellarSystemWrong2.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
cir = CircularOrbit.empty("Q2");
try {
cir.readfile("test StellarSystemWrong3.txt");
} catch (ReadFileFailException e) {
ReadFileFailException exp = new ReadFileFailException();
assertEquals(exp.getClass(), e.getClass());
}catch (Exception e) {
System.out.println("The path is not valid, please re-enter it.");
}
}
测试运行结果与EclEmma覆盖度报告
SpotBugs tool
发现了哪些错误,每种错误代表什么不良的编程习惯
对代码修改,消除这些错误。
整体检测后发现这一个错误(出现在三个不同的应用中)
bug描述:
说明RuntimeException与Exception的处理最好要分开进行,否则会有很大的安全隐患,因为RuntimeException多是不可处理的,程序代码中出现的错误,而Exception是可以处理的,可预见的错误,二者不可同日而语。