diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-08-27 10:31:46 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-08-27 10:31:46 +0200 |
commit | b5e8fd0ef143c9309c30854ce6d8a1d19c2b138d (patch) | |
tree | be35b3d2b27d4d7e484c942026fd4e30aab83634 /sonar-batch | |
parent | 400386a676630f8ac4e51e568981c652bc11b56f (diff) | |
download | sonarqube-b5e8fd0ef143c9309c30854ce6d8a1d19c2b138d.tar.gz sonarqube-b5e8fd0ef143c9309c30854ce6d8a1d19c2b138d.zip |
Fix some quality flaws
Diffstat (limited to 'sonar-batch')
12 files changed, 57 insertions, 42 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/DefaultHighlightingBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/DefaultHighlightingBuilder.java index 1399f66a438..6682e32536c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/DefaultHighlightingBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/DefaultHighlightingBuilder.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.highlighting; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; + import com.google.common.base.Preconditions; import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; import org.sonar.batch.index.ComponentDataCache; diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilder.java index aba6bd66029..80e784d4211 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilder.java @@ -19,10 +19,11 @@ */ package org.sonar.batch.highlighting; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; + import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Ordering; import org.elasticsearch.common.collect.Sets; -import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; import javax.annotation.Nullable; @@ -52,7 +53,7 @@ public class SyntaxHighlightingDataBuilder { return syntaxHighlightingRuleSet; } - public SyntaxHighlightingDataBuilder registerHighlightingRule(int startOffset, int endOffset, HighlightingBuilder.TypeOfText typeOfText) { + public SyntaxHighlightingDataBuilder registerHighlightingRule(int startOffset, int endOffset, TypeOfText typeOfText) { SyntaxHighlightingRule syntaxHighlightingRule = SyntaxHighlightingRule.create(startOffset, endOffset, typeOfText); this.syntaxHighlightingRuleSet.add(syntaxHighlightingRule); diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRule.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRule.java index 9ccf5758557..bcfab67f476 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRule.java @@ -19,7 +19,7 @@ */ package org.sonar.batch.highlighting; -import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; import java.io.Serializable; @@ -27,15 +27,15 @@ public class SyntaxHighlightingRule implements Serializable { private final int startPosition; private final int endPosition; - private final HighlightingBuilder.TypeOfText textType; + private final TypeOfText textType; - private SyntaxHighlightingRule(int startPosition, int endPosition, HighlightingBuilder.TypeOfText textType) { + private SyntaxHighlightingRule(int startPosition, int endPosition, TypeOfText textType) { this.startPosition = startPosition; this.endPosition = endPosition; this.textType = textType; } - public static SyntaxHighlightingRule create(int startPosition, int endPosition, HighlightingBuilder.TypeOfText textType) { + public static SyntaxHighlightingRule create(int startPosition, int endPosition, TypeOfText textType) { return new SyntaxHighlightingRule(startPosition, endPosition, textType); } @@ -47,7 +47,7 @@ public class SyntaxHighlightingRule implements Serializable { return endPosition; } - public HighlightingBuilder.TypeOfText getTextType() { + public TypeOfText getTextType() { return textType; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java index 4cc9ea53ab8..3d69f717c6b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java @@ -19,10 +19,11 @@ */ package org.sonar.batch.highlighting; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; + import com.persistit.Value; import com.persistit.encoding.CoderContext; import com.persistit.encoding.ValueCoder; -import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; class SyntaxHighlightingRuleValueCoder implements ValueCoder { @@ -38,7 +39,7 @@ class SyntaxHighlightingRuleValueCoder implements ValueCoder { public Object get(Value value, Class clazz, CoderContext context) { int startPosition = value.getInt(); int endPosition = value.getInt(); - HighlightingBuilder.TypeOfText type = HighlightingBuilder.TypeOfText.values()[value.getInt()]; + TypeOfText type = TypeOfText.values()[value.getInt()]; return SyntaxHighlightingRule.create(startPosition, endPosition, type); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java index 2905cc6b0d7..bdb052ddc1e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; + import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,7 +32,6 @@ import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputPath; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.sensor.duplication.DuplicationGroup; -import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.batch.sensor.measure.Measure; import org.sonar.api.batch.sensor.symbol.Symbol; @@ -291,12 +292,12 @@ public class BatchMediumTester { * @param charIndex 0-based offset in file */ @CheckForNull - public List<HighlightingBuilder.TypeOfText> highlightingTypeFor(InputFile file, int charIndex) { + public List<TypeOfText> highlightingTypeFor(InputFile file, int charIndex) { SyntaxHighlightingData syntaxHighlightingData = highlightingPerFile.get(file); if (syntaxHighlightingData == null) { return null; } - List<HighlightingBuilder.TypeOfText> result = new ArrayList<HighlightingBuilder.TypeOfText>(); + List<TypeOfText> result = new ArrayList<TypeOfText>(); for (SyntaxHighlightingRule sortedRule : syntaxHighlightingData.syntaxHighlightingRuleSet()) { if (sortedRule.getStartPosition() <= charIndex && sortedRule.getEndPosition() > charIndex) { result.add(sortedRule.getTextType()); @@ -341,6 +342,7 @@ public class BatchMediumTester { } public FakeGlobalReferentialsLoader add(Metric metric) { + Boolean optimizedBestValue = metric.isOptimizedBestValue(); ref.metrics().add(new org.sonar.batch.protocol.input.Metric(metricId, metric.key(), metric.getType().name(), @@ -351,7 +353,7 @@ public class BatchMediumTester { metric.getUserManaged(), metric.getWorstValue(), metric.getBestValue(), - metric.isOptimizedBestValue())); + optimizedBestValue != null ? optimizedBestValue : false)); metricId++; return this; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java index 28c1fea24c3..3de4a4b985b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java @@ -84,7 +84,20 @@ public class QProfileEventsDecorator implements Decorator { Measure currentMeasure = context.getMeasure(CoreMetrics.QUALITY_PROFILES); Map<String, QProfile> currentProfiles = UsedQProfiles.fromJson(currentMeasure.getData()).profilesByKey(); - // Detect new profiles or updated profiles + detectNewOrUpdatedProfiles(context, previousProfiles, currentProfiles); + + detectNoMoreUsedProfiles(context, previousProfiles, currentProfiles); + } + + private void detectNoMoreUsedProfiles(DecoratorContext context, Map<String, QProfile> previousProfiles, Map<String, QProfile> currentProfiles) { + for (QProfile previousProfile : previousProfiles.values()) { + if (!currentProfiles.containsKey(previousProfile.getKey())) { + markAsRemoved(context, previousProfile); + } + } + } + + private void detectNewOrUpdatedProfiles(DecoratorContext context, Map<String, QProfile> previousProfiles, Map<String, QProfile> currentProfiles) { for (QProfile profile : currentProfiles.values()) { QProfile previousProfile = previousProfiles.get(profile.getKey()); if (previousProfile != null) { @@ -95,13 +108,6 @@ public class QProfileEventsDecorator implements Decorator { markAsAdded(context, profile); } } - - // Detect profiles that are not used anymore - for (QProfile previousProfile : previousProfiles.values()) { - if (!currentProfiles.containsKey(previousProfile.getKey())) { - markAsRemoved(context, previousProfile); - } - } } private void markAsChanged(DecoratorContext context, QProfile previousProfile, QProfile profile) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultSensorContext.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultSensorContext.java index afd21a8d358..74b13c2e43e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultSensorContext.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultSensorContext.java @@ -130,8 +130,9 @@ public class DefaultSensorContext implements SensorContext { @Override public void addMeasure(Measure<?> measure) { - if (measure.inputFile() != null) { - measureCache.put(def.getKey(), ComponentKeys.createEffectiveKey(def.getKey(), measure.inputFile()), (DefaultMeasure) measure); + InputFile inputFile = measure.inputFile(); + if (inputFile != null) { + measureCache.put(def.getKey(), ComponentKeys.createEffectiveKey(def.getKey(), inputFile), (DefaultMeasure) measure); } else { measureCache.put(def.getKey(), def.getKey(), (DefaultMeasure) measure); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java index b290c9a0dc9..0fbedcaeb22 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java +++ b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java @@ -19,7 +19,8 @@ */ package org.sonar.batch.source; -import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; + import org.sonar.api.component.Component; import org.sonar.api.source.Highlightable; import org.sonar.batch.highlighting.SyntaxHighlightingDataBuilder; @@ -71,7 +72,7 @@ public class DefaultHighlightable implements Highlightable { @Override public HighlightingBuilder highlight(int startOffset, int endOffset, String typeOfText) { - TypeOfText type = org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.forCssClass(typeOfText); + TypeOfText type = org.sonar.api.batch.sensor.highlighting.TypeOfText.forCssClass(typeOfText); builder.registerHighlightingRule(startOffset, endOffset, type); return this; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/highlighting/DefaultHighlightingBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/highlighting/DefaultHighlightingBuilderTest.java index aefc5afd732..dcfdb8a1430 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/highlighting/DefaultHighlightingBuilderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/highlighting/DefaultHighlightingBuilderTest.java @@ -21,7 +21,7 @@ package org.sonar.batch.highlighting; import org.junit.Test; import org.mockito.ArgumentCaptor; -import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; import org.sonar.batch.index.ComponentDataCache; import org.sonar.core.source.SnapshotDataTypes; diff --git a/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilderTest.java index 4b192a47731..c132dcba477 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilderTest.java @@ -27,9 +27,9 @@ import org.junit.rules.ExpectedException; import java.util.Collection; import static org.fest.assertions.Assertions.assertThat; -import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.CLASSIC_COMMENT; -import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.CPP_DOC; -import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.KEYWORD; +import static org.sonar.api.batch.sensor.highlighting.TypeOfText.COMMENT; +import static org.sonar.api.batch.sensor.highlighting.TypeOfText.CPP_DOC; +import static org.sonar.api.batch.sensor.highlighting.TypeOfText.KEYWORD; public class SyntaxHighlightingDataBuilderTest { @@ -42,12 +42,12 @@ public class SyntaxHighlightingDataBuilderTest { public void setUpSampleRules() { SyntaxHighlightingDataBuilder highlightingDataBuilder = new SyntaxHighlightingDataBuilder(); - highlightingDataBuilder.registerHighlightingRule(0, 10, CLASSIC_COMMENT); + highlightingDataBuilder.registerHighlightingRule(0, 10, COMMENT); highlightingDataBuilder.registerHighlightingRule(10, 12, KEYWORD); highlightingDataBuilder.registerHighlightingRule(24, 38, KEYWORD); highlightingDataBuilder.registerHighlightingRule(42, 50, KEYWORD); highlightingDataBuilder.registerHighlightingRule(24, 65, CPP_DOC); - highlightingDataBuilder.registerHighlightingRule(12, 20, CLASSIC_COMMENT); + highlightingDataBuilder.registerHighlightingRule(12, 20, COMMENT); highlightingRules = highlightingDataBuilder.getSyntaxHighlightingRuleSet(); } @@ -61,7 +61,7 @@ public class SyntaxHighlightingDataBuilderTest { public void should_order_by_start_then_end_offset() throws Exception { assertThat(highlightingRules).onProperty("startPosition").containsOnly(0, 10, 12, 24, 24, 42); assertThat(highlightingRules).onProperty("endPosition").containsOnly(10, 12, 20, 38, 65, 50); - assertThat(highlightingRules).onProperty("textType").containsOnly(CLASSIC_COMMENT, KEYWORD, CLASSIC_COMMENT, KEYWORD, CPP_DOC, KEYWORD); + assertThat(highlightingRules).onProperty("textType").containsOnly(COMMENT, KEYWORD, COMMENT, KEYWORD, CPP_DOC, KEYWORD); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataTest.java b/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataTest.java index 75a78d1becd..c7ab820408c 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataTest.java @@ -25,9 +25,9 @@ import org.junit.Test; import java.util.List; import static org.fest.assertions.Assertions.assertThat; -import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.CLASSIC_COMMENT; -import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.CPP_DOC; -import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.KEYWORD; +import static org.sonar.api.batch.sensor.highlighting.TypeOfText.COMMENT; +import static org.sonar.api.batch.sensor.highlighting.TypeOfText.CPP_DOC; +import static org.sonar.api.batch.sensor.highlighting.TypeOfText.KEYWORD; public class SyntaxHighlightingDataTest { @@ -35,9 +35,9 @@ public class SyntaxHighlightingDataTest { public void should_serialize_rules_to_string() throws Exception { List<SyntaxHighlightingRule> orderedHighlightingRules = Lists.newArrayList( - SyntaxHighlightingRule.create(0, 10, CLASSIC_COMMENT), + SyntaxHighlightingRule.create(0, 10, COMMENT), SyntaxHighlightingRule.create(10, 12, KEYWORD), - SyntaxHighlightingRule.create(12, 20, CLASSIC_COMMENT), + SyntaxHighlightingRule.create(12, 20, COMMENT), SyntaxHighlightingRule.create(24, 38, KEYWORD), SyntaxHighlightingRule.create(24, 65, CPP_DOC), SyntaxHighlightingRule.create(42, 50, KEYWORD) diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java index b55a2125cbe..b6ce7fb7d52 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest.highlighting; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; + import com.google.common.collect.ImmutableMap; import org.apache.commons.io.FileUtils; import org.junit.After; @@ -28,7 +30,6 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.rules.TestName; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; import org.sonar.batch.mediumtest.BatchMediumTester; import org.sonar.batch.mediumtest.BatchMediumTester.TaskResult; import org.sonar.xoo.XooPlugin; @@ -87,10 +88,10 @@ public class HighlightingMediumTest { .start(); InputFile file = result.inputFiles().get(0); - assertThat(result.highlightingTypeFor(file, 0)).containsExactly(HighlightingBuilder.TypeOfText.STRING); - assertThat(result.highlightingTypeFor(file, 9)).containsExactly(HighlightingBuilder.TypeOfText.STRING); + assertThat(result.highlightingTypeFor(file, 0)).containsExactly(TypeOfText.STRING); + assertThat(result.highlightingTypeFor(file, 9)).containsExactly(TypeOfText.STRING); assertThat(result.highlightingTypeFor(file, 10)).isEmpty(); - assertThat(result.highlightingTypeFor(file, 11)).containsExactly(HighlightingBuilder.TypeOfText.KEYWORD); + assertThat(result.highlightingTypeFor(file, 11)).containsExactly(TypeOfText.KEYWORD); } @@ -126,7 +127,7 @@ public class HighlightingMediumTest { System.out.println("Duration: " + (System.currentTimeMillis() - start)); InputFile file = result.inputFiles().get(0); - assertThat(result.highlightingTypeFor(file, 0)).containsExactly(HighlightingBuilder.TypeOfText.STRING); + assertThat(result.highlightingTypeFor(file, 0)).containsExactly(TypeOfText.STRING); } |