本文整理汇总了Java中org.sonar.api.batch.SensorContext类的典型用法代码示例。如果您正苦于以下问题:Java SensorContext类的具体用法?Java SensorContext怎么用?Java SensorContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SensorContext类属于org.sonar.api.batch包,在下文中一共展示了SensorContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeOn
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
@Override
public void executeOn(Project project, SensorContext context) {
if (!settings.getBoolean(DISABLED)) {
String room = settings.getString(ROOM);
String token = settings.getString(TOKEN);
if (isBlank(room) || isBlank(token)) {
LOGGER.warn("No Room or token information available. No notification is send");
return;
}
String postUrl = String.format(settings.getString(URL_TEMPLATE), room, token);
HipChatHttpClient hipChatHttpClient = new HipChatHttpClient(postUrl);
HipChatMessageBuilder messageBuilder = new SonarHipChatMessageBuilder(project, settings, projectIssues);
hipChatHttpClient.sendPreNotification(settings.getString(PRE_MESSAGE), messageBuilder);
hipChatHttpClient.sendStatusNotification(messageBuilder);
}
}
示例2: analyse
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
@Override
public void analyse(Project project, SensorContext sensorContext) {
// configure the lexer
final PageLexer lexer = new PageLexer();
// configure page scanner and the visitors
final HtmlAstScanner scanner = setupScanner();
for (java.io.File file : fileSystem.files(FileQuery.onSource().onLanguage(TanaguruConstants.LANGUAGE_KEY))) {
File resource = File.fromIOFile(file, project);
WebSourceCode sourceCode = new WebSourceCode(file, resource);
FileReader reader = null;
try {
reader = new FileReader(file);
List<Node> nodeList = lexer.parse(reader);
scanner.scan(nodeList, sourceCode, fileSystem.sourceCharset());
saveMetrics(sensorContext, sourceCode);
saveLineLevelMeasures(resource, sourceCode);
} catch (Exception e) {
LOG.error("Can not analyze file " + file.getAbsolutePath(), e);
} finally {
IOUtils.closeQuietly(reader);
}
}
}
示例3: analyse
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
@Test
public void analyse() {
// Setup
SensorContext sensorContext = mock(SensorContext.class);
// Run
fs.add(createInputFile("setup.properties", TextLanguage.LANGUAGE_KEY));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(final InvocationOnMock invocation) throws Throwable {
TextSourceFile sourceFile = (TextSourceFile)invocation.getArguments()[0];
sourceFile.addViolation(new TextIssue(mock(RuleKey.class), 1, "rule violated"));
return null;
}
}).when(textCheckMock).validate(Mockito.any(TextSourceFile.class), Mockito.matches("com.mycorp.projectA.service:service-do-X"));
sensor.analyse(project, sensorContext);
verify(mockIssuable).addIssue(Mockito.isA(Issue.class));
}
示例4: analyse
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
public void analyse(Project project, SensorContext sensorContext) {
if (CodeReviewInitialize.reviewResult == null || CodeReviewInitialize.reviewResult.getProjectBasedir() == null) return;
logger.debug("Project basedir : " + CodeReviewInitialize.reviewResult.getProjectBasedir());
File projectBasedir = new File(CodeReviewInitialize.reviewResult.getProjectBasedir());
for (RuleType rule : CodeReviewInitialize.reviewResult.getRule()) {
if (rule.getResult().size() > 0) {
logger.debug("Found one non conformity : " + rule.getRule().getKey());
RuleKey ruleKey = RuleKey.of("FCCodeReviewRepository", rule.getRule().getKey());
for (Result result : rule.getResult()) {
File resourceToFind = new File(projectBasedir.getAbsolutePath() + result.getResource());
InputFile resource = fs.inputFile(fs.predicates().hasPath(projectBasedir.getAbsolutePath() + result.getResource()));
if (resource != null) {
logger.debug("Resource found : " + resourceToFind.getAbsolutePath());
Issuable issuable = perspectives.as(Issuable.class, resource);
issuable.addIssue(issuable.newIssueBuilder().ruleKey(ruleKey).line(1).build());
} else {
logger.debug("Resource not found : " + resourceToFind.getAbsolutePath());
}
}
}
}
}
示例5: getValidResource
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
private Resource getValidResource(Project module, SensorContext context, CtcMeasure measure) {
Resource resource;
java.io.File file = measure.getSOURCE();
if (file == null) {
resource = module;
} else if (!file.exists()) {
resource = null;
} else {
LOG.debug("FileName: {}", measure.getSOURCE());
resource = File.fromIOFile(measure.getSOURCE(), module);
resource = context.getResource(resource);
if (resource == null) {
LOG.error("File not mapped to resource!");
}
}
return resource;
}
示例6: analyse
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
public void analyse(Project project, SensorContext context) {
this.project = project;
this.context = context;
Collection<SquidAstVisitor<LexerlessGrammar>> squidChecks = annotationCheckFactory.getChecks();
List<SquidAstVisitor<LexerlessGrammar>> visitors = Lists.newArrayList(squidChecks);
//TODO: enable
visitors.add(new FileLinesVisitor(project, fileLinesContextFactory));
this.scanner = PlSQLAstScanner.create(createConfiguration(project), visitors.toArray(new SquidAstVisitor[visitors.size()]));
scanner.scanFiles(InputFileUtils.toFiles(project.getFileSystem().mainFiles(PlSQL.KEY)));
Collection<SourceCode> squidSourceFiles = scanner.getIndex().search(new QueryByType(SourceFile.class));
save(squidSourceFiles);
}
示例7: saveMetrics
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
private void saveMetrics(org.sonar.api.resources.File sonarFile, SensorContext context, SourceCode sourceCode) {
for (Issue issue : sourceCode.getIssues()) {
logger.finer("Saving issue: " + issue);
Issuable issuable = perspectives.as(Issuable.class, sonarFile);
if (issuable != null) {
org.sonar.api.issue.Issue sIssue = issuable.newIssueBuilder()
.ruleKey(issue.rule())
.line(issue.line())
.message(issue.message())
.build();
issuable.addIssue(sIssue);
}
}
for (Measure measure : sourceCode.getMeasures()) {
logger.finer("Saving measure: " + measure);
context.saveMeasure(sourceCode.getResource(), measure);
}
for (Dependency dependency : sourceCode.getDependencies()) {
logger.finer("Saving dependency: " + dependency);
context.saveDependency(dependency);
}
}
示例8: analyse
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
public void analyse(final Project project, final SensorContext sensorContext) {
LOGGER.info("SpringSensor analyse start");
springChecks = SpringRulesRepository.createChecks(profile);
LOGGER.info("SpringSensor analyse checks.size(): " + springChecks.size());
for (InputFile inputfile : SpringPlugin.getFiles(project)) {
try {
org.sonar.api.resources.File resource = SpringProjectFileSystem.fromIOFile(inputfile, project);
if(resource != null && resource.getName().endsWith(".xml")) {
checkXmlFile(project, sensorContext, resource);
} else if(resource != null && resource.getName().endsWith(".java")) {
checkJavaFile();
}
} catch (Exception e) {
LOGGER.error("SpringSensor analyse exception", e);
}
}
LOGGER.info("SpringSensor analyse end");
}
示例9: buildStatusCheckersAreExecutedAfterOtherPostJobs
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
@Test
public void buildStatusCheckersAreExecutedAfterOtherPostJobs() {
BuildBreaker checker = new BuildBreaker() {
public void executeOn(Project project, SensorContext context) {
}
};
ScannerExtensionDictionnary selector = newSelector(new FakePostJob(), checker, new FakePostJob());
List extensions = Lists.newArrayList(selector.select(PostJob.class, null, true));
assertThat(extensions).hasSize(3);
assertThat(extensions.get(2)).isEqualTo(checker);
}
示例10: setUp
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
@Before
public void setUp() {
handler = mock(SensorsPhaseHandler.class);
sensor = mock(Sensor.class);
dict = mock(ScannerExtensionDictionnary.class);
project = mock(Project.class);
SensorsPhaseHandler[] handlers = {handler};
context = mock(SensorContext.class);
executor = new AllSensorsExecutor(context, dict, project, handlers);
when(dict.select(Sensor.class, project, true)).thenReturn(Collections.singletonList(sensor));
}
示例11: analyse
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
@Override
public void analyse(Project module, SensorContext context) {
for (InputFile file : fileSystem.inputFiles(fileSystem.predicates().doesNotMatchPathPatterns(config.coverageExclusions()))) {
analyseFile(module, context, file);
}
// We assume the root module is always the last module, so that the overall data is correct
if (module.isRoot()) {
analyseRootProject(module);
}
}
示例12: fetchMeasure
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
private Integer fetchMeasure(SensorContext context, Resource resource, Metric<Integer> metric) {
Measure<Integer> measure = context.getMeasure(resource, metric);
if (measure != null) {
return measure.value();
}
LOGGER.warn("Could not retrieve measure of {} for {}", metric.getKey(), resource);
return null;
}
示例13: analyseFile
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
private void analyseFile(Project module, SensorContext context, InputFile file) {
Integer linesToCover = null;
Integer uncoveredLines = null;
Resource fileResource = context.getResource(file);
linesToCover = fetchMeasure(context, fileResource, CoreMetrics.LINES_TO_COVER);
uncoveredLines = fetchMeasure(context, fileResource, CoreMetrics.UNCOVERED_LINES);
// get lines_to_cover, uncovered_lines
if ((linesToCover != null) && (uncoveredLines != null)) {
Double previousCoverage = sonar.getMeasureValue(module, fileResource, CoreMetrics.LINE_COVERAGE);
double coverage = calculateCoverage(linesToCover, uncoveredLines);
coverageProjectStore.updateMeasurements(linesToCover, uncoveredLines);
if (previousCoverage == null) {
return;
}
// The API returns the coverage rounded.
// So we can only report anything if the rounded value has changed,
// otherwise we could report false positives.
LOGGER.debug("Previous/current file coverage on {}: {} / {}",
fileResource.getPath(), previousCoverage, coverage);
if (roundedPercentageGreaterThan(previousCoverage, coverage)) {
addIssue(file, coverage, previousCoverage);
}
}
}
示例14: testAnalyse
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
@Test
public void testAnalyse() {
File fsDir = new File("somepath");
fs = new DefaultFileSystem(fsDir);
InputFile fileA = new DefaultInputFile("a.java").setAbsolutePath("a.java");
InputFile fileB = new DefaultInputFile("b.java").setAbsolutePath("b.java");
fs.add(fileA);
fs.add(fileB);
fs.addLanguages("java");
SensorContext context = mock(SensorContext.class);
coverageProjectStore = new CoverageProjectStore();
sonarClient = mock(SonarClient.class);
sensor = new CoverageSensor(fs, rp, config, makeRules(true, true, fs), coverageProjectStore, sonarClient);
mockPreviousCoverage(sonarClient, project, project, 100, 0);
mockFileCoverages(context, sonarClient, fileA, project,
100, 60,
100, 50
);
mockFileCoverages(context, sonarClient, fileB, project,
100, 70,
100, 50
);
sensor.analyse(project, context);
assertEquals(35.0, coverageProjectStore.getProjectCoverage(),0.01);
verify(issuable, times(3)).addIssue(any());
verify(sonarClient, times(3)).getMeasureValue(any(), any(), any());
verifyNoMoreInteractions(sonarClient);
}
示例15: testAnalyseWithExclusions
import org.sonar.api.batch.SensorContext; //导入依赖的package包/类
@Test
public void testAnalyseWithExclusions() {
File fsDir = new File("somepath");
fs = new DefaultFileSystem(fsDir);
InputFile fileA = new DefaultInputFile("a.java").setAbsolutePath("a.java");
InputFile fileB = new DefaultInputFile("b.java").setAbsolutePath("b.java");
fs.add(fileA);
fs.add(fileB);
fs.addLanguages("java");
SensorContext context = mock(SensorContext.class);
coverageProjectStore = new CoverageProjectStore();
sonarClient = mock(SonarClient.class);
mockFileCoverages(context, sonarClient, fileA, project,
100, 60,
100, 50
);
mockFileCoverages(context, sonarClient, fileB, project,
100, 70,
100, 50
);
sensor = new CoverageSensor(fs, rp, config, makeRules(true, false, null), coverageProjectStore, sonarClient);
settings.setProperty(CoreProperties.PROJECT_COVERAGE_EXCLUSIONS_PROPERTY, "a.java");
sensor.analyse(project, context);
verify(sonarClient, times(2)).getMeasureValue(any(), any(), any());
}