You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
sonar-keyware/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/CustomCxxRulesDefinitionTes...

91 lines
2.9 KiB

/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称:28所 C++ 信息安全性设计准则
* 项目描述:用于检查C++源代码的安全性设计准则的Sonarqube插件
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
*/
package com.keyware.sonar.cxx;
import com.sonar.cxx.sslr.api.Grammar;
import org.junit.jupiter.api.Test;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.resources.Language;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import org.sonar.cxx.tag.Tag;
import static org.assertj.core.api.Assertions.assertThat;
class CustomCxxRulesDefinitionTest {
private static final Language LANGUAGE = new CxxLanguage(new MapSettings().asConfig());
private static final String REPOSITORY_NAME = "Custom Rule Repository";
private static final String REPOSITORY_KEY = "CustomRuleRepository";
private static final String RULE_NAME = "This is my custom rule";
private static final String RULE_KEY = "MyCustomRule";
@Test
void test() {
var rulesDefinition = new MyCustomPlSqlRulesDefinition();
var context = new RulesDefinition.Context();
rulesDefinition.define(context);
RulesDefinition.Repository repository = context.repository(REPOSITORY_KEY);
assertThat(repository.name()).isEqualTo(REPOSITORY_NAME);
assertThat(repository.language()).isEqualTo(LANGUAGE.getKey());
assertThat(repository.rules()).hasSize(1);
RulesDefinition.Rule alertUseRule = repository.rule(RULE_KEY);
assertThat(alertUseRule).isNotNull();
assertThat(alertUseRule.name()).isEqualTo(RULE_NAME);
for (var rule : repository.rules()) {
for (var param : rule.params()) {
assertThat(param.description()).as("description for " + param.key()).isNotEmpty();
}
}
}
public static class MyCustomPlSqlRulesDefinition extends CustomCxxRulesDefinition {
@Override
public String repositoryName() {
System.out.println(REPOSITORY_NAME);
return REPOSITORY_NAME;
}
@Override
public String repositoryKey() {
return REPOSITORY_KEY;
}
@SuppressWarnings("rawtypes")
@Override
public Class[] checkClasses() {
return new Class[]{MyCustomRule.class};
}
@Override
public Language getLanguage() {
return LANGUAGE;
}
}
@Rule(
key = RULE_KEY,
name = RULE_NAME,
description = "desc",
tags = {Tag.BUG})
public class MyCustomRule extends SquidCheck<Grammar> {
@RuleProperty(
key = "customParam",
description = "Custom parameter",
defaultValue = "value")
public String customParam = "value";
}
}