From d67483d12ef1768adda8f87db349915891f3b19a Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 11 Jul 2014 14:37:16 +0200 Subject: [PATCH] Improve some medium tests --- .../org/sonar/server/debt/DebtMediumTest.java | 14 +++- .../ActiveRuleBackendMediumTest.java | 65 ++++++++++--------- .../ws/QProfilesWsMediumTest.java | 1 - 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/server/sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java index b76c7800ff5..6a4434ea5ef 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java @@ -20,11 +20,13 @@ package org.sonar.server.debt; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.server.debt.DebtCharacteristic; import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.server.platform.Platform; import org.sonar.server.tester.ServerTester; import org.sonar.server.user.MockUserSession; @@ -32,8 +34,14 @@ import static org.fest.assertions.Assertions.assertThat; public class DebtMediumTest { - @Rule - public ServerTester serverTester = new ServerTester(); + @ClassRule + public static ServerTester serverTester = new ServerTester(); + + @Before + public void setUp() throws Exception { + serverTester.clearDbAndIndexes(); + serverTester.get(Platform.class).executeStartupTasks(); + } @Test public void find_characteristics() throws Exception { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java index 66662c8203f..3537e1ccaf8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java @@ -21,6 +21,7 @@ package org.sonar.server.qualityprofile; import com.google.common.collect.ImmutableList; import com.google.common.collect.Multimap; +import org.junit.After; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; @@ -66,6 +67,11 @@ public class ActiveRuleBackendMediumTest { index = tester.get(ActiveRuleIndex.class); } + @After + public void after() throws Exception { + dbSession.close(); + } + @Test public void synchronize_index() throws Exception { @@ -232,7 +238,8 @@ public class ActiveRuleBackendMediumTest { // insert and index QualityProfileDto profileDto = QProfileTesting.newXooP1(); db.qualityProfileDao().insert(dbSession, profileDto); - for (int i = 0; i < 100; i++) { + int nb = 100; + for (int i = 0; i < nb; i++) { RuleDto rule = newRuleDto(RuleKey.of("xoo", "S00" + i)); db.ruleDao().insert(dbSession, rule); @@ -244,36 +251,36 @@ public class ActiveRuleBackendMediumTest { // verify index Collection activeRules = index.findByProfile(profileDto.getKey()); - assertThat(activeRules).hasSize(100); + assertThat(activeRules).hasSize(nb); } -// @Test -// public void count_by_profile() { -// QualityProfileDto profileDto1 = QProfileTesting.newXooP1(); -// QualityProfileDto profileDto2 = QProfileTesting.newXooP2(); -// db.qualityProfileDao().insert(dbSession, profileDto1, profileDto2); -// -// RuleKey ruleKey = RuleTesting.XOO_X1; -// RuleDto ruleDto = newRuleDto(ruleKey); -// db.ruleDao().insert(dbSession, ruleDto); -// -// ActiveRuleDto activeRule1 = ActiveRuleDto.createFor(profileDto1, ruleDto).setSeverity(Severity.MAJOR); -// ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(profileDto2, ruleDto).setSeverity(Severity.MAJOR); -// db.activeRuleDao().insert(dbSession, activeRule1, activeRule2); -// dbSession.commit(); -// -// // 0. Test base case -// assertThat(index.countAll()).isEqualTo(2); -// -// // 1. Assert by profileKey -// assertThat(index.countByQualityProfileKey(profileDto1.getKey())).isEqualTo(1); -// -// // 2. Assert by term aggregation; -// Map counts = index.countByField(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY); -// assertThat(counts).hasSize(2); -// assertThat(counts.values()).containsOnly(1L, 1L); -// assertThat(counts.keySet()).containsOnly(profileDto1.getKey().toString(), profileDto2.getKey().toString()); -// } + @Test + public void count_by_profile() { + QualityProfileDto profileDto1 = QProfileTesting.newXooP1(); + QualityProfileDto profileDto2 = QProfileTesting.newXooP2(); + db.qualityProfileDao().insert(dbSession, profileDto1, profileDto2); + + RuleKey ruleKey = RuleTesting.XOO_X1; + RuleDto ruleDto = newRuleDto(ruleKey); + db.ruleDao().insert(dbSession, ruleDto); + + ActiveRuleDto activeRule1 = ActiveRuleDto.createFor(profileDto1, ruleDto).setSeverity(Severity.MAJOR); + ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(profileDto2, ruleDto).setSeverity(Severity.MAJOR); + db.activeRuleDao().insert(dbSession, activeRule1, activeRule2); + dbSession.commit(); + + // 0. Test base case + assertThat(index.countAll()).isEqualTo(2); + + // 1. Assert by profileKey + assertThat(index.countByQualityProfileKey(profileDto1.getKey())).isEqualTo(1); + + // 2. Assert by term aggregation; + Map counts = index.countByField(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY); + assertThat(counts).hasSize(2); + assertThat(counts.values()).containsOnly(1L, 1L); + assertThat(counts.keySet()).containsOnly(profileDto1.getKey().toString(), profileDto2.getKey().toString()); + } @Test public void count_all_by_index_field() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java index b3ca675297d..c99be810262 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java @@ -176,7 +176,6 @@ public class QProfilesWsMediumTest { RuleDto rule1 = createRule(profile.getLanguage(), "world"); createActiveRule(rule0, profile); createActiveRule(rule1, profile); - ; session.commit(); // 0. Assert No Active Rule for profile -- 2.39.5