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<Measure> measures = timeMachine.getMeasures(query);
if (measures.isEmpty()) {
- return defaultValue;
+ return null;
}
- return measures.get(0).getIntValue();
+ return measures.get(0);
}
@Override
*/
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;
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);
@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) {