2017-10-06 73 views
0

我正在爲一個研究項目測試測試代碼。我們先從測試用例如以編程方式從測試用例中刪除try/catch塊

@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); 
} 

斷言語句被註釋掉.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); 
    } 
} 
+1

使用'--no-迴歸assertions'與Randoop應消除需要刪除的聲明。 – vinegarbin

+0

當然,我們的腳本也能夠處理預先編寫的測試用例,在這種情況下,我們需要手動對其進行評論。另外,通過運行帶有斷言的原始測試來確保測試工作正常,這很好。 – Loren

回答

0

我發現了有效的解決方案。它不會刪除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(); 
      } 
     } 
    }