From: Evgeny Mandrikov Date: Fri, 27 May 2011 23:35:21 +0000 (+0400) Subject: SONAR-1922 Improve ProfileEventsSensor X-Git-Tag: 2.9~124 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d1573937217ad18aec5f0cbefd46897aa71879ae;p=sonarqube.git SONAR-1922 Improve ProfileEventsSensor * Create event only when actual change of version or profile was done. * Use only one form for event description: "ProfileA version X is used instead of ProfileB version Y". --- diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProfileEventsSensor.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProfileEventsSensor.java index 76283ba34b9..54cccdd66b7 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProfileEventsSensor.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProfileEventsSensor.java @@ -46,33 +46,44 @@ public class ProfileEventsSensor implements Sensor { if (profile == null) { return; } - String currentProfile = profile.getName(); + + Measure pastProfileMeasure = getPreviousMeasure(project, CoreMetrics.PROFILE); + if (pastProfileMeasure == null) { + return; // first analysis + } + int pastProfileId = pastProfileMeasure.getIntValue(); + Measure pastProfileVersionMeasure = getPreviousMeasure(project, CoreMetrics.PROFILE_VERSION); + final int pastProfileVersion; + if (pastProfileVersionMeasure == null) { // first analysis with versions + pastProfileVersion = 1; + } else { + pastProfileVersion = pastProfileVersionMeasure.getIntValue(); + } + String pastProfile = formatProfileDescription(pastProfileMeasure.getData(), pastProfileVersion); + int currentProfileId = profile.getId(); int currentProfileVersion = profile.getVersion(); + String currentProfile = formatProfileDescription(profile.getName(), currentProfileVersion); - int pastProfileId = getPreviousMeasureValue(project, CoreMetrics.PROFILE, -1); - int pastProfileVersion = getPreviousMeasureValue(project, CoreMetrics.PROFILE, 1); - - if (pastProfileId != currentProfileId) { - // A different profile is used for this project - context.createEvent(project, currentProfile + " V" + currentProfileVersion, - "A different quality profile was used", Event.CATEGORY_PROFILE, null); - } else if (pastProfileVersion != currentProfileVersion) { - // Same profile but new version - context.createEvent(project, currentProfile + " V" + currentProfileVersion, - "A new version of the quality profile was used", Event.CATEGORY_PROFILE, null); + if ((pastProfileId != currentProfileId) || (pastProfileVersion != currentProfileVersion)) { + // A different profile is used for this project or new version of same profile + context.createEvent(project, currentProfile, currentProfile + " is used instead of " + pastProfile, Event.CATEGORY_PROFILE, null); } } - private int getPreviousMeasureValue(Project project, Metric metric, int defaultValue) { + private static String formatProfileDescription(String name, int version) { + return name + " version " + version; + } + + private Measure getPreviousMeasure(Project project, Metric metric) { TimeMachineQuery query = new TimeMachineQuery(project) .setOnlyLastAnalysis(true) .setMetrics(metric); List measures = timeMachine.getMeasures(query); if (measures.isEmpty()) { - return defaultValue; + return null; } - return measures.get(0).getIntValue(); + return measures.get(0); } @Override diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProfileEventsSensorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProfileEventsSensorTest.java index d0b44316eaf..a3095f8b609 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProfileEventsSensorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProfileEventsSensorTest.java @@ -19,11 +19,10 @@ */ package org.sonar.plugins.core.sensors; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; import static org.mockito.Matchers.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import org.junit.Before; import org.junit.Test; @@ -53,6 +52,14 @@ public class ProfileEventsSensorTest { context = mock(SensorContext.class); } + @Test + public void shouldExecute() { + ProfileEventsSensor sensor = new ProfileEventsSensor(null, null); + + assertThat(sensor.shouldExecuteOnProject(project), is(true)); + verifyZeroInteractions(project); + } + @Test public void shouldDoNothingIfNoProfile() throws ParseException { ProfileEventsSensor sensor = new ProfileEventsSensor(null, null); @@ -64,89 +71,78 @@ public class ProfileEventsSensorTest { @Test public void shouldDoNothingIfNoProfileChange() throws ParseException { - RulesProfile profile = mockProfile(1); - TimeMachine timeMachine = mockTM(project, 22.0, 1.0); // Same profile, same version + RulesProfile profile = mockProfileWithVersion(1); + TimeMachine timeMachine = mockTM(project, 22.0, "Foo", 1.0); // Same profile, same version ProfileEventsSensor sensor = new ProfileEventsSensor(profile, timeMachine); sensor.analyse(project, context); - verify(context, never()).createEvent((Resource) anyObject(), anyString(), anyString(), anyString(), (Date) anyObject()); + verifyZeroInteractions(context); } @Test public void shouldCreateEventIfProfileChange() throws ParseException { - RulesProfile profile = mockProfile(1); - TimeMachine timeMachine = mockTM(project, 21.0, 1.0); // Different profile + RulesProfile profile = mockProfileWithVersion(1); + TimeMachine timeMachine = mockTM(project, 21.0, "Bar", 1.0); // Different profile ProfileEventsSensor sensor = new ProfileEventsSensor(profile, timeMachine); sensor.analyse(project, context); - verify(context).createEvent(same(project), eq("Profile V1"), eq("A different quality profile was used"), + verify(context).createEvent(same(project), + eq("Foo version 1"), + eq("Foo version 1 is used instead of Bar version 1"), same(Event.CATEGORY_PROFILE), (Date) anyObject()); } @Test public void shouldCreateEventIfProfileVersionChange() throws ParseException { - RulesProfile profile = mockProfile(2); - TimeMachine timeMachine = mockTM(project, 22.0, 1.0); // Same profile, different version + RulesProfile profile = mockProfileWithVersion(2); + TimeMachine timeMachine = mockTM(project, 22.0, "Foo", 1.0); // Same profile, different version ProfileEventsSensor sensor = new ProfileEventsSensor(profile, timeMachine); sensor.analyse(project, context); - verify(context).createEvent(same(project), eq("Profile V2"), eq("A new version of the quality profile was used"), + verify(context).createEvent(same(project), + eq("Foo version 2"), + eq("Foo version 2 is used instead of Foo version 1"), same(Event.CATEGORY_PROFILE), (Date) anyObject()); } @Test - public void shouldCreateEventIfFirstAnalysis() throws ParseException { - RulesProfile profile = mockProfile(2); + public void shouldNotCreateEventIfFirstAnalysis() throws ParseException { + RulesProfile profile = mockProfileWithVersion(2); TimeMachine timeMachine = mockTM(project, null, null); ProfileEventsSensor sensor = new ProfileEventsSensor(profile, timeMachine); sensor.analyse(project, context); - verify(context).createEvent(same(project), eq("Profile V2"), eq("A different quality profile was used"), - same(Event.CATEGORY_PROFILE), (Date) anyObject()); + verifyZeroInteractions(context); } @Test - public void shouldNotCreateEventIfFirstProfileVersionAndStillV1() throws ParseException { - RulesProfile profile = mockProfile(1); - TimeMachine timeMachine = mockTMWithNullVersion(project, 22.0); + public void shouldCreateEventIfFirstAnalysisWithVersionsAndVersionMoreThan1() throws ParseException { + RulesProfile profile = mockProfileWithVersion(2); + TimeMachine timeMachine = mockTM(project, 22.0, "Foo", null); ProfileEventsSensor sensor = new ProfileEventsSensor(profile, timeMachine); sensor.analyse(project, context); - verify(context, never()).createEvent((Resource) anyObject(), anyString(), anyString(), anyString(), (Date) anyObject()); - } - - @Test - public void shouldCreateEventIfFirstProfileVersionAndMoreThanV1() throws ParseException { - RulesProfile profile = mockProfile(2); - TimeMachine timeMachine = mockTMWithNullVersion(project, 22.0); - ProfileEventsSensor sensor = new ProfileEventsSensor(profile, timeMachine); - - sensor.analyse(project, context); - - verify(context).createEvent(same(project), eq("Profile V2"), eq("A new version of the quality profile was used"), + verify(context).createEvent(same(project), + eq("Foo version 2"), + eq("Foo version 2 is used instead of Foo version 1"), same(Event.CATEGORY_PROFILE), (Date) anyObject()); } - private RulesProfile mockProfile(int version) { + private RulesProfile mockProfileWithVersion(int version) { RulesProfile profile = mock(RulesProfile.class); when(profile.getId()).thenReturn(22); - when(profile.getName()).thenReturn("Profile"); - when(profile.getVersion()).thenReturn(version); // New version + when(profile.getName()).thenReturn("Foo"); + when(profile.getVersion()).thenReturn(version); return profile; } - private TimeMachine mockTM(Project project, double profileValue, double versionValue) { - return mockTM(project, new Measure(CoreMetrics.PROFILE, profileValue), - new Measure(CoreMetrics.PROFILE_VERSION, versionValue)); - } - - private TimeMachine mockTMWithNullVersion(Project project, double profileValue) { - return mockTM(project, new Measure(CoreMetrics.PROFILE, profileValue), null); + private TimeMachine mockTM(Project project, double profileId, String profileName, Double versionValue) { + return mockTM(project, new Measure(CoreMetrics.PROFILE, profileId, profileName), versionValue == null ? null : new Measure(CoreMetrics.PROFILE_VERSION, versionValue)); } private TimeMachine mockTM(Project project, Measure result1, Measure result2) {