簡體   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