簡體   English   中英

SonarQube測試自定義Java規則無法顯示語義信息

[英]SonarQube Test Custom Java Rule fails to display semantic information

我正在學習SonarQube的API,試圖擴展java插件規則。 我成功地遵循了本教程

現在我想構建一個簡單的分析,檢查單元測試中是否使用了toString()方法。

public class TS_SensitiveEqualityCheck extends BaseTreeVisitor implements JavaFileScanner {

  private final Deque<Boolean> methodContainsToStringInAssert = new ArrayDeque<Boolean>();
  private final Deque<Boolean> inUnitTest = new ArrayDeque<Boolean>();

  private JavaFileScannerContext context;

  @Override
  public void scanFile(final JavaFileScannerContext context) {
    this.context = context;
    scan(context.getTree());
  }

  @Override
  public void visitMethod(MethodTree methodTree) {
    if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.ABSTRACT)) {
      return;
    }
    boolean isUnitTest = isUnitTest(methodTree);
    inUnitTest.push(isUnitTest);
    System.out.println("For method " + methodTree.simpleName() 
    + " found [isUnitTest | isViolation] :  " 
    + String.valueOf(isUnitTest));
    methodContainsToStringInAssert.push(false);
    super.visitMethod(methodTree);
    inUnitTest.pop();
    Boolean isViolation = methodContainsToStringInAssert.pop();
    System.out.println("For method " + methodTree.simpleName() 
            + " found [isUnitTest | isViolation] :  " 
            + String.valueOf(isUnitTest) + " " 
            + String.valueOf(isViolation) );
    if (isUnitTest && isViolation) { 
      context.reportIssue(this, methodTree.simpleName(), "This test method uses unsafe equality checking!");
    }
  }

  @Override
  public void visitMethodInvocation(MethodInvocationTree mit) {
    if (!inUnitTest()) {
      return;
    }
    Symbol mis = mit.symbol();
    System.out.println(mis.name()); // null when encountering an assertion.
    if (mis.name() != null && mis.name().equals("toString")) {
        setTrue(methodContainsToStringInAssert);
    }
    super.visitMethodInvocation(mit);
  }

  private boolean inUnitTest() {
    return !inUnitTest.isEmpty() && inUnitTest.peek();
  }

  private static void setTrue(Deque<Boolean> collection) {
    if (collection != null && !collection.peek()) {
      collection.pop();
      collection.push(true);
    }
  }

  private static boolean isUnitTest(MethodTree methodTree) {
    JavaSymbol.MethodJavaSymbol symbol = (JavaSymbol.MethodJavaSymbol) methodTree.symbol();
    while (symbol != null) {
      if (symbol.metadata().isAnnotatedWith("org.junit.Test")) {
        return true;
      }
      symbol = symbol.overriddenSymbol();
    }
    Symbol.TypeSymbol enclosingClass = methodTree.symbol().enclosingClass();
    return (enclosingClass != null 
            // && enclosingClass.type().isSubtypeOf("junit.framework.TestCase")  // errors!!! does not get the package name of the class!!!
            && methodTree.simpleName().name().startsWith("test"));
  }

}

對於給定的測試文件,SonarQube找不到任何斷言方法調用! 只有方法B.is()給出一個結果,即mit.symbol().name != null 誰能解釋為什么會出錯? 這是用作測試的文件:

import junit.framework.TestCase;
    import javax.annotation.Nullable;

public class AssertionsInTestsCheckTestJunit3 extends TestCase { public void testCompliant() { B b = new B(); b.is(); org.junit.Assert.assertTrue(b.is()); } public void testNoncompliant() { // Noncompliant org.junit.Assert.assertTrue(this.toString().equals("")); } public void testNoncompliant2() { // Noncompliant org.junit.Assert.assertEquals(this.toString(), ""); } public void testNoncompliant3() { // Noncompliant org.junit.Fail.fail(this.toString()); doWork(); } @Nullable public Test notAtest() { compliant1(); } } public class B { public boolean is() { return true; } } </pre></code>

請注意,此代碼的作用並不重要!

Java Analyzer需要源文件中使用的庫的字節代碼才能完成語義模型。 沒有它,大多數可以檢索的語義信息都會丟失。

在您的測試文件中,您使用的是junit 但是,缺少與junit相關的二進制文件,因為您很可能不會向檢查驗證程序提供庫。 該部分尚未在教程中描述。

默認情況下,沒有為檢查驗證程序提供外部庫,因為教程在當前狀態下不需要外部庫。 解釋如何使用外部源的部分尚未編寫,但一旦完成,它應該可以通過以下鏈接獲得: 如何測試需要外部二進制文件的源

現在,為了解決您的問題並總結教程中將要呈現的內容,您需要執行以下操作:

為了在分析文件時使用足夠的字節代碼,您必須向check checkifier提供junit二進制文件 有多種方法可以做到這一點,但最簡單的方法可能是將jar放在項目的專用位置: target/test-jars 默認情況下,檢查驗證程序將查找此文件夾。

您可以通過更改項目根目錄下的pom.xml文件來自動添加任何所需的庫:

  1. 在插件根目錄的pom.xml文件中,您應該看到一個注釋部分: pom.xml#L108 (來自教程中使用的模板)
  2. 取消評論此部分代碼。 請注意,對於該示例,它已包含apache commons-collections的二進制文件。
 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
      <execution>
        <id>copy</id>
        <phase>test-compile</phase>
        <goals>
          <goal>copy</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>org.apache.commons</groupId>
              <artifactId>commons-collections4</artifactId>
              <version>4.0</version>
              <type>jar</type>
            </artifactItem>
          </artifactItems>
          <outputDirectory>${project.build.directory}/test-jars</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
  1. 替換<artifactItem>代替使用junit,或者只是添加一個新工件:
  <artifactItem>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <type>jar</type>
  </artifactItem>
  1. 使用mvn clean install -DskipTests重新構建項目。 具有所需版本的junit jar將由maven下載,並放在target/test-jars文件夾中。

  2. 重新運行測試。 應該檢測問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM