繁体   English   中英

以编程方式从测试用例中删除try / catch块

[英]Programatically Remove try/catch blocks from test cases

我正在为一个研究项目检测测试代码。 我们从测试用例开始,例如

@Test
public void test025() throws Throwable {
    if (debug)
        System.out.format("%n%s%n", "RegressionTest1.test025");
    java.lang.Double[] d_array2 = new java.lang.Double[] { 1.0d, (-1.0d) };
    org.apache.commons.math.linear.ArrayRealVector arrayRealVector3 = new org.apache.commons.math.linear.ArrayRealVector(d_array2);
    org.apache.commons.math.linear.ArrayRealVector arrayRealVector4 = new org.apache.commons.math.linear.ArrayRealVector(d_array2);
    try {
        org.apache.commons.math.linear.ArrayRealVector arrayRealVector7 = new org.apache.commons.math.linear.ArrayRealVector(d_array2, (int) '4', (int) ' ');
        org.junit.Assert.fail("Expected exception of type org.apache.commons.math.exception.NumberIsTooLargeException");
    } catch (org.apache.commons.math.exception.NumberIsTooLargeException e) {
    }
    org.junit.Assert.assertNotNull(d_array2);
}

assert语句使用以下命令从.java文件中注释掉:

original = original.replace("org.junit.Assert.", "//org.junit.Assert.");
original = original.replace("assert", "//assert");

然后,使用javassist对它们进行记录以记录所有异常(以确保它们在测试用例的所有运行中都发生):

    for (CtMethod testmethod : methods) {
        if (testmethod.getName().startsWith("test")) {
            try {
                testmethod.insertBefore("semanticrt.statedump.dumpObjectState.setDumpTestCase(\""+testClassName+ "." + testmethod.getName()+"\");");
                CtClass etype = null;
                try {
                    etype = pool.get("java.lang.Exception");
                } catch (NotFoundException e) {
                    e.printStackTrace();
                }

                String exceptionCode = "{ semanticrt.statedump.dumpObjectState.dumpExceptionToFile($e, " +
                        "\"" + outputFileRoot + "."+ testmethod.getName() + ".exception\", false); throw $e; }\n";
                testmethod.addCatch(exceptionCode, etype);
            } catch (CannotCompileException e) {
                System.out.println(testmethod);
                e.printStackTrace();
            }

        }
    }

产生:

@Test
public void test025() throws Throwable {
    try {
        dumpObjectState.setDumpTestCase("RegressionTest1.test025");
        if(debug) {
            System.out.format("%n%s%n", new Object[]{"RegressionTest1.test025"});
        }

        Double[] var1 = new Double[]{Double.valueOf(1.0D), Double.valueOf(-1.0D)};
        new ArrayRealVector(var1);
        new ArrayRealVector(var1);

        try {
            new ArrayRealVector(var1, 52, 32);
        } catch (NumberIsTooLargeException var6) {
            ;
        }

    } catch (Exception var7) {
        dumpObjectState.dumpExceptionToFile(var7, "/home/loren/repos/d4jBugs/Math_45/version/XMLOut//out-RegressionTest1.test025.exception", false);
    }
}

我的问题是NumberIsTooLargeException被代码中剩下的try / catch块吞没了。 (注意:Exception类可以是任何类,这只是一个有问题的情况的示例。)因此,我需要一种方法来消除生成测试用例中的所有try / catch块,然后再将其包装在我的类中。

有谁知道如何使用javassist或良好的正则表达式来执行此操作,我可以在删除它们之前在.java文件上运行它?

我想结束于:

@Test
public void test025() throws Throwable {
    try {
        dumpObjectState.setDumpTestCase("RegressionTest1.test025");
        if(debug) {
            System.out.format("%n%s%n", new Object[]{"RegressionTest1.test025"});
        }

        Double[] var1 = new Double[]{Double.valueOf(1.0D), Double.valueOf(-1.0D)};
        new ArrayRealVector(var1);
        new ArrayRealVector(var1);

        new ArrayRealVector(var1, 52, 32);

    } catch (Exception var7) {
        dumpObjectState.dumpExceptionToFile(var7, "/home/loren/repos/d4jBugs/Math_45/version/XMLOut//out-RegressionTest1.test025.exception", false);
    }
}

我找到了可行的解决方案。 它不会删除try / catch块,但是会在catch的开头添加一个throw,因此它提供了所需的功能。

testmethod.instrument(
    new ExprEditor() {
        public void edit(Handler m)
                throws CannotCompileException
        {
            m.insertBefore("throw $1;");
        }
    }
);

这使得整个循环:

    for (CtMethod testmethod : methods) {
        if (testmethod.getName().startsWith("test")) {
            try {
                testmethod.instrument(
                    new ExprEditor() {
                        public void edit(Handler m)
                                throws CannotCompileException
                        {
                            m.insertBefore("throw $1;");
                        }
                    }
                );
                testmethod.insertBefore("semanticrt.statedump.dumpObjectState.setDumpTestCase(\""+testClassName+ "." + testmethod.getName()+"\");");
                CtClass etype = null;
                try {
                    etype = pool.get("java.lang.Exception");
                } catch (NotFoundException e) {
                    e.printStackTrace();
                }
                // See addCatch() https://jboss-javassist.github.io/javassist/tutorial/tutorial2.html
                String exceptionCode = "{ semanticrt.statedump.dumpObjectState.dumpExceptionToFile($e, " +
                        "\"" + outputFileRoot + "."+ testmethod.getName() + ".exception\", false); return; }\n";
                testmethod.addCatch(exceptionCode, etype);
            } catch (CannotCompileException e) {
                System.out.println(testmethod);
                e.printStackTrace();
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM