- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
正在学习SonarQube的API,尝试扩展java插件规则。我跟着这个tutorial成功。
现在我想构建一个简单的分析来检查 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;<p></p>
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 分析器需要源文件中使用的库的字节码来完成语义模型。没有它,大部分可以检索的语义信息都会丢失。
在您的测试文件中,您使用的是 junit。但是,缺少与 junit 相关的二进制文件,因为您很可能没有向检查验证程序提供库。这部分还没有在教程中描述。
默认情况下,不向检查验证程序提供任何外部库,因为教程在当前状态下不需要外部库。解释如何使用外部资源的部分尚未编写,但一旦完成,应该可以通过以下链接获得:How to test sources requiring external binaries .
现在,要解决您的问题并总结教程中将要介绍的内容,您必须执行以下操作:
为了在分析文件时使用足够的字节码,您必须向检查验证程序提供junit 二进制文件。有多种方法可以做到这一点,但最简单的方法可能是通过将 jar 放在项目中的专用位置来提供它:target/test-jars
.默认情况下,检查验证程序将在此文件夹中查找。
您可以通过更改项目根目录下的 pom.xml 文件来自动添加任何所需的库:
<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>
<artifactItem>
改用 junit,或者简单地添加一个新的工件: <artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<type>jar</type>
</artifactItem>
使用 mvn clean install -DskipTests
重新构建项目.具有所需版本的 junit jar 将由 maven 下载,并放置在您的 target/test-jars
中。文件夹。
重新运行测试。应该检测到问题。
关于java - SonarQube Test Custom Java Rule 显示语义信息失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37609704/
在 makefile 中,我可以从另一个规则调用一个规则吗? 类似于: rule1: echo "bye" rule2: date rule3: @ec
我想创建一个只匹配外部链接的 CSS 选择器。问题是它似乎不适用于 :not() 中的多个规则。我不能使用多个 not ( example: ":not():not()") 因为那变成了 ||而不是
我正在动态创建 CSS 动画,所以我需要在我的文档中插入一个 CSS 计时函数。如: @-webkit-keyframes slide { from { -webkit-transfo
错误内容: Error: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): HookW
我有这样的文件结构: ---- _base-map.scss ---- _child-map-1.scss ---- _child-map-2.scss ---- _child-map-3.scss
我正在利用 engine.Host 类创建自己的规则引擎实例,并通过 JSON 文件加载规则并调用 set_rulesets() 方法。这一切都运行良好。 持久规则:https://pypi.org/
在无服务器(https://www.serverless.com/framework/docs/providers/aws/events/event-bridge),的EventBridge文档中提到
在无服务器(https://www.serverless.com/framework/docs/providers/aws/events/event-bridge),的EventBridge文档中提到
系统要求我使用最常用的网络规则和应用程序规则配置 Azure 防火墙策略规则集合。 我收集了以下详细信息,其中捕获了最常用的网络规则和应用程序规则。但是我不确定我是否遗漏了任何被认为是最常见的规则?
我想做的是在运行时从 ABNF 语法文件创建一个解析器。我已经在 qi::grammar 中实现了所有 ABNF 规则,如下所示: typedef /*qi::rule or struct conta
对于复杂的多边形(即:自相交),选择缠绕填充规则还是奇偶填充规则会影响多边形的填充方式。 但对于非相交多边形,缠绕或奇偶填充规则之间是否存在任何性能差异。我知道这将是特定于实现的,但哪种算法对于非复杂
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 8 个月前
我正在尝试将 TypeScript 编译添加到现有的 Javascript 项目中。 AFAIK 这应该是可能的(甚至很容易),并且您可以通过代码库逐步传播 TS。不幸的是,这不是我所看到的——在添加
尝试将 Firebase 部署到其托管服务时。我还使用 firebase 工具来发布安全规则。我看到此错误消息: $ firebase deploy Security Rules Error - s
我在使用 ANTLR4 解析某些 SQL 类型的字符串时遇到问题。解析后的字符串是: WHERE a <> 17106 AND b BETWEEN c AND d AND e BTW(f, g) 这是
我已经在 Android 中创建了一个模块以在我的主应用程序中使用,并且似乎有两个文件Consumer-rules.pro 和 proguard-rules.pro。 我想了解以下内容 所有模块代码都
我正在尝试在另一个自定义规则中加载自定义规则,这两个规则均由 Parasoft 规则向导创建。 以下代码是作为方法放在调用规则中的python片段: def somePythonMethod(node
像许多其他问题一样,我正在尝试使用 Boost.Spirit.Qi 将简单语法解析为结构树。 我会尽量提炼我正在尝试做的事情,以尽可能最简单的情况。我有: struct Integer { int
我的samtools_dup规则存在一些问题。 它在/data/mypipeline.smk的第201行中显示“SyntaxError: 规则samtools_dup中的run/shell/scrip
有人可以向我解释一下这两种情况下负载均衡器(v2)后端实际发生的情况吗: 应用入站 NAT 规则。 应用负载平衡规则。 最佳答案 当您有 1 台后端服务器或者您知道要访问哪个后端服务器时,您将使用 N
我是一名优秀的程序员,十分优秀!