aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-06-24 00:15:21 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-06-24 00:26:49 +0200
commit5470fe5d8afd1f29980312dcd90eaf8ecb873155 (patch)
treebe572963bec85161acc44e5bf3ec0a4a88f8799c /sonar-batch
parent78f2909b5ef26d889c16423a05e1e3371244861c (diff)
downloadsonarqube-5470fe5d8afd1f29980312dcd90eaf8ecb873155.tar.gz
sonarqube-5470fe5d8afd1f29980312dcd90eaf8ecb873155.zip
SONAR-5007 add EVENTS.EVENT_DATA to store links on quality profile changes
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java45
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java35
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml14
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml17
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml14
5 files changed, 86 insertions, 39 deletions
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 68aa7d907cd..3774f9f1b5e 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
@@ -19,6 +19,7 @@
*/
package org.sonar.batch.rule;
+import com.google.common.collect.ImmutableSortedMap;
import org.sonar.api.batch.Decorator;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
@@ -33,6 +34,9 @@ import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.KeyValueFormat;
+import org.sonar.batch.index.PersistenceManager;
+import org.sonar.core.UtcDateUtils;
import javax.annotation.CheckForNull;
@@ -43,10 +47,12 @@ public class QProfileEventsDecorator implements Decorator {
private final TimeMachine timeMachine;
private final Languages languages;
+ private final PersistenceManager persistenceManager;
- public QProfileEventsDecorator(TimeMachine timeMachine, Languages languages) {
+ public QProfileEventsDecorator(TimeMachine timeMachine, Languages languages, PersistenceManager pm) {
this.timeMachine = timeMachine;
this.languages = languages;
+ this.persistenceManager = pm;
}
@DependsUpon
@@ -81,31 +87,40 @@ public class QProfileEventsDecorator implements Decorator {
QProfile previousProfile = previousProfiles.get(profile.getKey());
if (previousProfile != null) {
if (profile.getRulesUpdatedAt().after(previousProfile.getRulesUpdatedAt())) {
- markAsUsed(context, profile);
+ markAsChanged(context, previousProfile, profile);
}
} else {
- markAsUsed(context, profile);
+ markAsAdded(context, profile);
}
}
// Detect profiles that are not used anymore
for (QProfile previousProfile : previousProfiles.values()) {
if (!currentProfiles.containsKey(previousProfile.getKey())) {
- markAsUnused(context, previousProfile);
+ markAsRemoved(context, previousProfile);
}
}
}
- private void markAsUnused(DecoratorContext context, QProfile profile) {
- Language language = languages.get(profile.getLanguage());
- String languageName = language != null ? language.getName() : profile.getLanguage();
- context.createEvent("Stop using " + profile.getName() + " (" + languageName + ")", profile.getName() + " no more used for " + languageName, Event.CATEGORY_PROFILE, null);
+ private void markAsChanged(DecoratorContext context, QProfile previousProfile, QProfile profile) {
+ // DecoratorContext does not allow to set event data, so SonarIndex must be used
+ Event event = new Event();
+ event.setName(String.format("Changes in %s", profileLabel(profile)));
+ event.setCategory(Event.CATEGORY_PROFILE);
+ String data = KeyValueFormat.format(ImmutableSortedMap.of(
+ "key", profile.getKey(),
+ "from", UtcDateUtils.formatDateTime(previousProfile.getRulesUpdatedAt()),
+ "to", UtcDateUtils.formatDateTime(profile.getRulesUpdatedAt())));
+ event.setData(data);
+ persistenceManager.saveEvent(context.getResource(), event);
}
- private void markAsUsed(DecoratorContext context, QProfile profile) {
- Language language = languages.get(profile.getLanguage());
- String languageName = language != null ? language.getName() : profile.getLanguage();
- context.createEvent("Use " + profile.getName() + " (" + languageName + ")", profile.getName() + " used for " + languageName, Event.CATEGORY_PROFILE, null);
+ private void markAsRemoved(DecoratorContext context, QProfile profile) {
+ context.createEvent(String.format("Stop using %s", profileLabel(profile)), null, Event.CATEGORY_PROFILE, null);
+ }
+
+ private void markAsAdded(DecoratorContext context, QProfile profile) {
+ context.createEvent(String.format("Use %s", profileLabel(profile)), null, Event.CATEGORY_PROFILE, null);
}
@CheckForNull
@@ -120,6 +135,12 @@ public class QProfileEventsDecorator implements Decorator {
return measures.get(0);
}
+ private String profileLabel(QProfile profile) {
+ Language language = languages.get(profile.getLanguage());
+ String languageName = language != null ? language.getName() : profile.getLanguage();
+ return String.format("'%s' (%s)", profile.getName(), languageName);
+ }
+
@Override
public String toString() {
return getClass().getSimpleName();
diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java
index c0010d823f0..c1afcb1e2ee 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java
@@ -38,6 +38,8 @@
*/
package org.sonar.batch.rule;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.Event;
@@ -48,6 +50,8 @@ import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
+import org.sonar.batch.index.PersistenceManager;
import java.util.Arrays;
import java.util.Date;
@@ -58,6 +62,7 @@ import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.argThat;
public class QProfileEventsDecoratorTest {
@@ -69,11 +74,14 @@ public class QProfileEventsDecoratorTest {
DecoratorContext decoratorContext = mock(DecoratorContext.class);
TimeMachine timeMachine = mock(TimeMachine.class);
Languages languages = mock(Languages.class);
- QProfileEventsDecorator decorator = new QProfileEventsDecorator(timeMachine, languages);
+ PersistenceManager persistenceManager = mock(PersistenceManager.class);
+ QProfileEventsDecorator decorator = new QProfileEventsDecorator(timeMachine, languages, persistenceManager);
@Test
- public void execute_on_all_projects() {
+ public void basic_tests() {
assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
+ assertThat(decorator.toString()).isEqualTo("QProfileEventsDecorator");
+ assertThat(decorator.dependsUpon()).isNotNull();
}
@Test
@@ -104,17 +112,26 @@ public class QProfileEventsDecoratorTest {
decorator.decorate(project, decoratorContext);
- verify(decoratorContext).createEvent(
- eq("Use Java One (Java)"),
- eq("Java One used for Java"),
- same(Event.CATEGORY_PROFILE), any(Date.class));
+ verify(persistenceManager).saveEvent(any(Resource.class), argThat(new BaseMatcher<Event>() {
+ @Override
+ public void describeTo(Description description) {
+ }
+
+ @Override
+ public boolean matches(Object item) {
+ Event event = (Event) item;
+ return event.getCategory().equals(Event.CATEGORY_PROFILE) &&
+ "Changes in 'Java One' (Java)".equals(event.getName()) &&
+ "from=2014-01-15T12:00:00+0000;key=J1;to=2014-02-20T12:00:00+0000".equals(event.getData());
+ }
+ }));
}
@Test
public void generate_event_if_profile_not_used_anymore() {
Measure previousMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA_V1_JSON + "]");
// Different profile
- Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA_OTHER_JSON + "]");
+ Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA_OTHER_JSON + "]");
when(timeMachine.getMeasures(any(TimeMachineQuery.class)))
.thenReturn(Arrays.asList(previousMeasure));
@@ -125,8 +142,8 @@ public class QProfileEventsDecoratorTest {
decorator.decorate(project, decoratorContext);
verify(decoratorContext).createEvent(
- eq("Stop using Java One (Java)"),
- eq("Java One no more used for Java"),
+ eq("Stop using 'Java One' (Java)"),
+ eq((String) null),
same(Event.CATEGORY_PROFILE), any(Date.class));
}
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml b/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml
index 08566ee2bf1..eca7da3d444 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml
@@ -30,9 +30,13 @@
scope="PRJ" qualifier="TRK" created_at="2008-11-09 13:58:00.00" build_date="2008-11-09 13:58:00.00" version="1.2-SNAPSHOT" path=""
status="U" islast="true" depth="0" />
- <events id="2" name="Foo" resource_id="1" snapshot_id="1000" category="Other" event_date="2008-11-03 13:58:00.00" created_at="2008-11-03 13:58:00.00" description=""/>
- <events id="4" name="Bar" resource_id="1" snapshot_id="1001" category="Other" event_date="2008-11-05 13:58:00.00" created_at="2008-11-05 13:58:00.00" description=""/>
- <events id="5" name="Uhh" resource_id="1" snapshot_id="1002" category="Other" event_date="2008-11-07 13:58:00.00" created_at="2008-11-07 13:58:00.00" description=""/>
- <events id="6" name="1.2-SNAPSHOT" resource_id="1" snapshot_id="1003" category="Version" event_date="2008-11-09 13:58:00.00" created_at="2008-11-09 13:58:00.00" description=""/>
+ <events id="2" name="Foo" resource_id="1" snapshot_id="1000" category="Other" event_date="2008-11-03 13:58:00.00" created_at="2008-11-03 13:58:00.00" description=""
+ event_data="[null]"/>
+ <events id="4" name="Bar" resource_id="1" snapshot_id="1001" category="Other" event_date="2008-11-05 13:58:00.00" created_at="2008-11-05 13:58:00.00" description=""
+ event_data="[null]"/>
+ <events id="5" name="Uhh" resource_id="1" snapshot_id="1002" category="Other" event_date="2008-11-07 13:58:00.00" created_at="2008-11-07 13:58:00.00" description=""
+ event_data="[null]"/>
+ <events id="6" name="1.2-SNAPSHOT" resource_id="1" snapshot_id="1003" category="Version" event_date="2008-11-09 13:58:00.00" created_at="2008-11-09 13:58:00.00" description=""
+ event_data="[null]"/>
-</dataset> \ No newline at end of file
+</dataset>
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml b/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml
index 6c282cf30e3..0612f65e11f 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml
@@ -30,12 +30,17 @@
scope="PRJ" qualifier="TRK" created_at="2008-11-09 13:58:00.00" build_date="2008-11-09 13:58:00.00" version="1.2-SNAPSHOT" path=""
status="U" islast="true" depth="0" />
- <events id="1" name="1.0" resource_id="1" snapshot_id="1000" category="Version" event_date="2008-11-02 13:58:00.00" created_at="2008-11-02 13:58:00.00" description=""/>
- <events id="2" name="Foo" resource_id="1" snapshot_id="1000" category="Other" event_date="2008-11-03 13:58:00.00" created_at="2008-11-03 13:58:00.00" description=""/>
+ <events id="1" name="1.0" resource_id="1" snapshot_id="1000" category="Version" event_date="2008-11-02 13:58:00.00" created_at="2008-11-02 13:58:00.00" description=""
+ event_data="[null]"/>
+ <events id="2" name="Foo" resource_id="1" snapshot_id="1000" category="Other" event_date="2008-11-03 13:58:00.00" created_at="2008-11-03 13:58:00.00" description=""
+ event_data="[null]"/>
<!-- The "1.1" version was deleted from the history : -->
<!-- events id="3" name="1.1" resource_id="1" snapshot_id="1001" category="Version" event_date="2008-11-04 13:58:00.00" created_at="2008-11-04 13:58:00.00" description=""/-->
- <events id="4" name="Bar" resource_id="1" snapshot_id="1001" category="Other" event_date="2008-11-05 13:58:00.00" created_at="2008-11-05 13:58:00.00" description=""/>
- <events id="5" name="Uhh" resource_id="1" snapshot_id="1002" category="Other" event_date="2008-11-07 13:58:00.00" created_at="2008-11-07 13:58:00.00" description=""/>
- <events id="6" name="1.2-SNAPSHOT" resource_id="1" snapshot_id="1003" category="Version" event_date="2008-11-09 13:58:00.00" created_at="2008-11-09 13:58:00.00" description=""/>
+ <events id="4" name="Bar" resource_id="1" snapshot_id="1001" category="Other" event_date="2008-11-05 13:58:00.00" created_at="2008-11-05 13:58:00.00" description=""
+ event_data="[null]"/>
+ <events id="5" name="Uhh" resource_id="1" snapshot_id="1002" category="Other" event_date="2008-11-07 13:58:00.00" created_at="2008-11-07 13:58:00.00" description=""
+ event_data="[null]"/>
+ <events id="6" name="1.2-SNAPSHOT" resource_id="1" snapshot_id="1003" category="Version" event_date="2008-11-09 13:58:00.00" created_at="2008-11-09 13:58:00.00" description=""
+ event_data="[null]"/>
-</dataset> \ No newline at end of file
+</dataset>
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml b/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml
index 077b4eea7f0..a708a476c07 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml
@@ -30,11 +30,11 @@
scope="PRJ" qualifier="TRK" created_at="2008-11-09 13:58:00.00" build_date="2008-11-09 13:58:00.00" version="1.2-SNAPSHOT" path=""
status="U" islast="true" depth="0" />
- <events id="1" name="1.0" resource_id="1" snapshot_id="1000" category="Version" event_date="2008-11-02 13:58:00.00" created_at="2008-11-02 13:58:00.00" description=""/>
- <events id="2" name="Foo" resource_id="1" snapshot_id="1000" category="Other" event_date="2008-11-03 13:58:00.00" created_at="2008-11-03 13:58:00.00" description=""/>
- <events id="3" name="1.1" resource_id="1" snapshot_id="1001" category="Version" event_date="2008-11-04 13:58:00.00" created_at="2008-11-04 13:58:00.00" description=""/>
- <events id="4" name="Bar" resource_id="1" snapshot_id="1001" category="Other" event_date="2008-11-05 13:58:00.00" created_at="2008-11-05 13:58:00.00" description=""/>
- <events id="5" name="Uhh" resource_id="1" snapshot_id="1002" category="Other" event_date="2008-11-07 13:58:00.00" created_at="2008-11-07 13:58:00.00" description=""/>
- <events id="6" name="1.2-SNAPSHOT" resource_id="1" snapshot_id="1003" category="Version" event_date="2008-11-09 13:58:00.00" created_at="2008-11-09 13:58:00.00" description=""/>
+ <events id="1" name="1.0" resource_id="1" snapshot_id="1000" category="Version" event_date="2008-11-02 13:58:00.00" created_at="2008-11-02 13:58:00.00" description="" event_data="[null]"/>
+ <events id="2" name="Foo" resource_id="1" snapshot_id="1000" category="Other" event_date="2008-11-03 13:58:00.00" created_at="2008-11-03 13:58:00.00" description="" event_data="[null]"/>
+ <events id="3" name="1.1" resource_id="1" snapshot_id="1001" category="Version" event_date="2008-11-04 13:58:00.00" created_at="2008-11-04 13:58:00.00" description="" event_data="[null]"/>
+ <events id="4" name="Bar" resource_id="1" snapshot_id="1001" category="Other" event_date="2008-11-05 13:58:00.00" created_at="2008-11-05 13:58:00.00" description="" event_data="[null]"/>
+ <events id="5" name="Uhh" resource_id="1" snapshot_id="1002" category="Other" event_date="2008-11-07 13:58:00.00" created_at="2008-11-07 13:58:00.00" description="" event_data="[null]"/>
+ <events id="6" name="1.2-SNAPSHOT" resource_id="1" snapshot_id="1003" category="Version" event_date="2008-11-09 13:58:00.00" created_at="2008-11-09 13:58:00.00" description="" event_data="[null]"/>
-</dataset> \ No newline at end of file
+</dataset>