本文整理汇总了Java中org.sonar.api.measures.CoreMetrics类的典型用法代码示例。如果您正苦于以下问题:Java CoreMetrics类的具体用法?Java CoreMetrics怎么用?Java CoreMetrics使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CoreMetrics类属于org.sonar.api.measures包,在下文中一共展示了CoreMetrics类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetMeasureValue
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Test
public void testGetMeasureValue() throws Exception {
String url = "http://localhost:" + httpServer.port() + "/";
String username = "foo";
String password = "bar";
SonarClient client = new DefaultSonarClient(url, username, password);
Project project = new Project("MY_PROJECT");
Resource file = new File("ElementImpl.java");
file.setKey("ElementImpl.java");
httpServer.stubFor(
get("/api/measures/component?componentKey=MY_PROJECT%3AElementImpl.java&metricKeys=line_coverage")
.withBasicAuth(username, password)
.willReturn(
aResponse()
.withBody(readJsonResource("componentMeasure.json").toJson().getBytes())
)
);
assertEquals(
25.0,
client.getMeasureValue(project, file, CoreMetrics.LINE_COVERAGE),
0.001
);
}
示例2: leaveFile
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Override
public void leaveFile(Tree tree) {
LinesOfCodeVisitor linesOfCodeVisitor = new LinesOfCodeVisitor(tree);
saveMetricOnFile(CoreMetrics.NCLOC, linesOfCodeVisitor.getNumberOfLinesOfCode());
StatementsVisitor statementsVisitor = new StatementsVisitor(tree);
saveMetricOnFile(CoreMetrics.STATEMENTS, statementsVisitor.getNumberOfStatements());
ComplexityInFunctionsVisitor complexityInFunctionsVisitor = new ComplexityInFunctionsVisitor(tree);
saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, complexityInFunctionsVisitor.getComplexityInFunctions());
ComplexityVisitor complexityVisitor = new ComplexityVisitor(tree);
saveMetricOnFile(CoreMetrics.COMPLEXITY, complexityVisitor.getComplexity());
FunctionsVisitor functionsVisitor = new FunctionsVisitor(tree);
saveMetricOnFile(CoreMetrics.FUNCTIONS, functionsVisitor.getNumberOfFunctions());
}
示例3: analyseFile
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
private void analyseFile(InputFile inputFile, Charset charset, SensorContext context) {
log.debug("Analysing file {}", inputFile);
File file = inputFile.file();
Map<CounterType, AtomicInteger> lines = countLines(file, charset);
context.<Integer>newMeasure().on(inputFile)
.withValue(lines.get(CounterType.CODE).get())
.forMetric(CoreMetrics.NCLOC).save();
context.<Integer>newMeasure().on(inputFile)
.withValue(lines.get(CounterType.COMMENT).get())
.forMetric(CoreMetrics.COMMENT_LINES).save();
context.<Integer>newMeasure().on(inputFile)
.withValue(lines.get(CounterType.CLASS).get())
.forMetric(CoreMetrics.CLASSES).save();
context.<Integer>newMeasure().on(inputFile)
.withValue(lines.get(CounterType.FUNCTION).get())
.forMetric(CoreMetrics.FUNCTIONS).save();
}
示例4: leaveFile
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Override
public void leaveFile(Tree tree) {
LinesOfCodeVisitor linesOfCodeVisitor = new LinesOfCodeVisitor(tree);
saveMetricOnFile(CoreMetrics.NCLOC, linesOfCodeVisitor.getNumberOfLinesOfCode());
CommentLinesVisitor commentLinesVisitor = new CommentLinesVisitor(tree);
saveMetricOnFile(CoreMetrics.COMMENT_LINES, commentLinesVisitor.getNumberOfCommentLines());
StatementsVisitor statementsVisitor = new StatementsVisitor(tree);
saveMetricOnFile(CoreMetrics.STATEMENTS, statementsVisitor.getNumberOfStatements());
FunctionsVisitor functionsVisitor = new FunctionsVisitor(tree);
saveMetricOnFile(CoreMetrics.FUNCTIONS, functionsVisitor.getNumberOfFunctions());
ClassesVisitor classesVisitor = new ClassesVisitor(tree);
saveMetricOnFile(CoreMetrics.CLASSES, classesVisitor.getNumberOfClasses());
}
示例5: test
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Test
public void test() throws IOException {
String s = "/** comment **/\r\n" + "select\r\n" + "11\n" + "--aaa\r\n";
File ff = folder.newFile("test.sql");
FileUtils.write(ff, s);
DefaultInputFile f = new DefaultInputFile("test", "test.sql");
f.initMetadata(s);
f.setLanguage(TSQLLanguage.KEY);
SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());
ctxTester.fileSystem().add(f);
AntrlResult result = Antlr4Utils.getFull(s);
AntlrMeasurer m = new AntlrMeasurer();
SourceLine[] lines = new SourceLinesProvider().getLines(new FileInputStream(ff), Charset.defaultCharset());
m.fill(ctxTester, new FillerRequest(f, result.getStream(), result.getTree(), lines));
Assert.assertEquals(2, ctxTester.measure("test:test.sql", CoreMetrics.NCLOC).value().intValue());
Assert.assertEquals(2, ctxTester.measure("test:test.sql", CoreMetrics.COMMENT_LINES).value().intValue());
}
示例6: leaveFile
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Override
public void leaveFile(AstNode astNode) {
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().is(getContext().getFile()));
if (inputFile == null) {
throw new IllegalStateException("InputFile is null, but it should not be.");
}
FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(inputFile);
int fileLength = getContext().peekSourceCode().getInt(PuppetMetric.LINES);
for (int line = 1; line <= fileLength; line++) {
fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0);
fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, linesOfComments.contains(line) ? 1 : 0);
}
fileLinesContext.save();
linesOfCode.clear();
linesOfComments.clear();
}
示例7: test
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Test
public void test() {
File moduleBaseDir = new File("src/test/resources/metrics/");
SensorContextTester context = SensorContextTester.create(moduleBaseDir);
DefaultInputFile inputFile = new DefaultInputFile("moduleKey", "metrics.properties")
.setModuleBaseDir(moduleBaseDir.toPath())
.setLanguage("jproperty")
.setType(InputFile.Type.MAIN);
context.fileSystem().add(inputFile);
MetricsVisitor metricsVisitor = new MetricsVisitor(context);
TreeVisitorContext treeVisitorContext = mock(TreeVisitorContext.class);
when(treeVisitorContext.getFile()).thenReturn(inputFile.file());
when(treeVisitorContext.getTopTree()).thenReturn((PropertiesTree) JavaPropertiesParserBuilder.createParser(Charsets.UTF_8).parse(inputFile.file()));
metricsVisitor.scanTree(treeVisitorContext);
String componentKey = "moduleKey:metrics.properties";
assertThat(context.measure(componentKey, CoreMetrics.NCLOC).value()).isEqualTo(4);
assertThat(context.measure(componentKey, CoreMetrics.STATEMENTS).value()).isEqualTo(4);
assertThat(context.measure(componentKey, CoreMetrics.COMMENT_LINES).value()).isEqualTo(4);
}
示例8: MeasureHolder
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
/**
* Constructs a MeasureHolder from a Measure object.
*
* @param measure used to retrieve the metric name for which the MeasureHolder is built
*/
@SuppressWarnings("unchecked")
public MeasureHolder(final Measure measure) {
final Metric<Serializable> metric = CoreMetrics.getMetric(measure.getMetric());
this.metricName = metric.getName()
.replace(" (%)", "")
.toLowerCase();
String tempValue = null;
if (!measure.hasValue()) {
if (measure.hasPeriods()) {
final PeriodsValue periods = measure.getPeriods();
final PeriodValue periodValue = periods.getPeriodsValue(0);
tempValue = periodValue.getValue();
}
} else {
tempValue = measure.getValue();
}
this.value = tempValue == null ? NA : tempValue + (metric.isPercentageType() ? "%" : "");
}
示例9: test
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Test
public void test() {
File moduleBaseDir = new File("src/test/resources/");
SensorContextTester context = SensorContextTester.create(moduleBaseDir);
DefaultInputFile inputFile = new DefaultInputFile("moduleKey", "metrics.json")
.setModuleBaseDir(moduleBaseDir.toPath())
.setLanguage("json")
.setType(InputFile.Type.MAIN);
context.fileSystem().add(inputFile);
MetricsVisitor metricsVisitor = new MetricsVisitor(context);
TreeVisitorContext treeVisitorContext = mock(TreeVisitorContext.class);
when(treeVisitorContext.getFile()).thenReturn(inputFile.file());
when(treeVisitorContext.getTopTree()).thenReturn((JsonTree) JSONParserBuilder.createParser(Charsets.UTF_8).parse(inputFile.file()));
metricsVisitor.scanTree(treeVisitorContext);
String componentKey = "moduleKey:metrics.json";
assertThat(context.measure(componentKey, CoreMetrics.NCLOC).value()).isEqualTo(6);
assertThat(context.measure(componentKey, CoreMetrics.STATEMENTS).value()).isEqualTo(7);
}
示例10: execute
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Override
public Map<InputFile, Set<Integer>> execute(SensorContext ctx) {
HashMap<InputFile, Set<Integer>> toReturn = new HashMap<>();
Iterable<InputFile> affectedFiles =
ctx
.fileSystem()
.inputFiles(ctx.fileSystem().predicates().hasLanguage(TypeScriptLanguage.LANGUAGE_KEY));
for (InputFile inputFile : affectedFiles) {
Set<Integer> nonCommentLineNumbers = this.getNonCommentLineNumbers(inputFile);
toReturn.put(inputFile, nonCommentLineNumbers);
ctx.<Integer>newMeasure().forMetric(CoreMetrics.NCLOC).on(inputFile).withValue(nonCommentLineNumbers.size()).save();
}
return toReturn;
}
示例11: decorate
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void decorate(Resource resource, DecoratorContext context) {
if (resource.getScope().equals(Scopes.FILE)) {
Map lineHits = new HashMap<Integer, Integer>();
context.saveMeasure(CoreMetrics.LINES_TO_COVER, context.getMeasure(CoreMetrics.LINES).getValue());
context.saveMeasure(CoreMetrics.UNCOVERED_LINES, 0.0);
for (int i = 1; i <= context.getMeasure(CoreMetrics.LINES).getIntValue(); i++) {
lineHits.put(i, 1);
}
context.saveMeasure(new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA).setData(KeyValueFormat.format(lineHits)).setPersistenceMode(PersistenceMode.DATABASE));
}
for (Metric[] entry : CONV) {
applyAndSaveMeasure(entry, context, resource);
}
}
示例12: should_not_save_metrics_with_empty_results
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Test
public void should_not_save_metrics_with_empty_results() throws Exception {
SensorContextTester context = SensorContextTester.create(temp.newFolder());
UnitTestResultsAggregator unitTestResultsAggregator = mock(UnitTestResultsAggregator.class);
UnitTestResults results = mock(UnitTestResults.class);
when(results.tests()).thenReturn(0);
when(results.skipped()).thenReturn(1);
when(results.failures()).thenReturn(2);
when(results.errors()).thenReturn(3);
when(results.executionTime()).thenReturn(null);
when(unitTestResultsAggregator.aggregate(Mockito.any(WildcardPatternFileProvider.class), Mockito.any(UnitTestResults.class))).thenReturn(results);
new UnitTestResultsImportSensor(unitTestResultsAggregator, ProjectDefinition.create()).analyze(context, results);
verify(unitTestResultsAggregator).aggregate(Mockito.any(WildcardPatternFileProvider.class), Mockito.eq(results));
assertThat(context.measures("projectKey"))
.extracting("metric.key", "value")
.containsOnly(
tuple(CoreMetrics.TESTS_KEY, 0),
tuple(CoreMetrics.SKIPPED_TESTS_KEY, 1),
tuple(CoreMetrics.TEST_FAILURES_KEY, 2),
tuple(CoreMetrics.TEST_ERRORS_KEY, 3));
}
示例13: getPointsValue
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
/**
* Retrieves all the necessary code metrics in order to calculate SCORE's
* points metric
*
* @param context
* @returns the points value
*/
public double getPointsValue(final DecoratorContext context) {
double packages = MeasureUtils.getValue(
context.getMeasure(CoreMetrics.PACKAGES), 0.0);
double classes = MeasureUtils.getValue(
context.getMeasure(CoreMetrics.CLASSES), 0.0);
double ncloc = MeasureUtils.getValue(
context.getMeasure(CoreMetrics.NCLOC), 0.0);
double rulesCompliance = MeasureUtils.getValue(
context.getMeasure(CoreMetrics.VIOLATIONS_DENSITY), 0.0);
double docAPI = MeasureUtils.getValue(
context.getMeasure(CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY),
0.0);
double coverage = MeasureUtils.getValue(
context.getMeasure(CoreMetrics.COVERAGE), 0.0);
double packageTangle = MeasureUtils.getValue(
context.getMeasure(CoreMetrics.PACKAGE_TANGLE_INDEX), 0.0);
CalculationComponentList penalties = new CalculationComponentList();
CalculationComponent packageTanglePenalty = new CalculationComponent(packageTangle, MAGNIFY_PACKAGE_TANGLE);
penalties.add(packageTanglePenalty);
return new PointsCalculator(penalties, null).calculateTotalPoints(packages, classes, ncloc, rulesCompliance, docAPI, coverage, packageTangle);
}
示例14: analyse2
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Test
public void analyse2() throws FileNotFoundException {
DefaultFileSystem fs = new DefaultFileSystem(TEST_DIR);
tester.setFileSystem(fs);
DefaultInputFile inputFile = new DefaultInputFile("key", "timeFormatter.lua")
.setType(InputFile.Type.MAIN)
.setLanguage(Lua.KEY)
.initMetadata(new FileMetadata().readMetadata(new FileReader(new File(TEST_DIR, "timeFormatter.lua"))));
fs.add(inputFile);
sensor.execute(tester);
String componentKey = inputFile.key();
assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY_IN_CLASSES).value()).isEqualTo(0);
assertThat(tester.measure(componentKey, CoreMetrics.NCLOC).value()).isEqualTo(0);
assertThat(tester.measure(componentKey, CoreMetrics.COMMENT_LINES).value()).isEqualTo(59);
assertThat(tester.measure(componentKey, CoreMetrics.STATEMENTS).value()).isEqualTo(0);
assertThat(tester.measure(componentKey, CoreMetrics.FUNCTIONS).value()).isEqualTo(0);
assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY).value()).isEqualTo(0);
assertThat(tester.measure(componentKey, CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0");
assertThat(tester.measure(componentKey, CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("1=0;2=0;4=0;6=0;8=0;10=0;12=0");
assertThat(tester.allIssues()).hasSize(0);
}
示例15: testCountLines
import org.sonar.api.measures.CoreMetrics; //导入依赖的package包/类
@Test
public void testCountLines() throws FileNotFoundException {
java.io.File file = TestUtils.getResource("src/main/webapp/user-properties.jsp");
List<Node> nodeList = lexer.parse(new FileReader(file));
assertThat(nodeList.size()).isGreaterThan(100);
// new File("test", "user-properties.jsp");
String relativePath = "test/user-properties.jsp";
WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", relativePath).setModuleBaseDir(new File(".").toPath()));
scanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
assertThat(webSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(227);
assertThat(webSourceCode.getDetailedLinesOfCode().size()).isEqualTo(224);
assertThat(webSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(14);
assertThat(webSourceCode.getDetailedLinesOfComments().size()).isEqualTo(14);
}