From: Simon Brandhof Date: Thu, 29 May 2014 08:54:56 +0000 (+0200) Subject: SONAR-5007 complete tests X-Git-Tag: 4.4-RC1~736 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7ad21ac0a91a218fa3e34534bda3cbfc702d5ec7;p=sonarqube.git SONAR-5007 complete tests --- diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java index 4bb2964cfec..b1346dd9e28 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java @@ -72,10 +72,10 @@ public class QProfileService implements ServerComponent { public Multimap bulkActivate(RuleQuery ruleQuery, QualityProfileKey profile) { - return ruleActivator.activateByRuleQuery(ruleQuery, profile); + return ruleActivator.bulkActivate(ruleQuery, profile); } public Multimap bulkDeactivate(RuleQuery ruleQuery, QualityProfileKey profile) { - return ruleActivator.deActivateByRuleQuery(ruleQuery, profile); + return ruleActivator.bulkDeactivate(ruleQuery, profile); } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java index 1344623830a..fc3e5909bcf 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java @@ -35,7 +35,6 @@ import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.utils.TimeProfiler; import org.sonar.api.utils.ValidationMessages; import org.sonar.core.persistence.DbSession; -import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualityprofile.db.ActiveRuleKey; import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.qualityprofile.db.QualityProfileKey; @@ -108,7 +107,7 @@ public class RegisterQualityProfiles implements ServerComponent { } session.commit(); } finally { - MyBatis.closeQuietly(session); + session.close(); profiler.stop(); } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java index b231371a5cb..4fcb3b8cd41 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java @@ -220,18 +220,18 @@ public class RuleActivator implements ServerComponent { } } - public Multimap activateByRuleQuery(RuleQuery ruleQuery, QualityProfileKey profile) { + public Multimap bulkActivate(RuleQuery ruleQuery, QualityProfileKey profile) { verifyPermission(UserSession.get()); RuleIndex ruleIndex = index.get(RuleIndex.class); Multimap results = ArrayListMultimap.create(); DbSession dbSession = db.openSession(false); try { + // TODO pb because limited to QueryOptions.MAX_LIMIT RuleResult result = ruleIndex.search(ruleQuery, QueryOptions.DEFAULT.setOffset(0) .setLimit(Integer.MAX_VALUE) - .setFieldsToReturn(ImmutableSet.of("template", "severity")) - ); + .setFieldsToReturn(ImmutableSet.of("template", "severity"))); for (Rule rule : result.getHits()) { if (!rule.template()) { @@ -252,7 +252,7 @@ public class RuleActivator implements ServerComponent { return results; } - public Multimap deActivateByRuleQuery(RuleQuery ruleQuery, QualityProfileKey profile) { + public Multimap bulkDeactivate(RuleQuery ruleQuery, QualityProfileKey profile) { verifyPermission(UserSession.get()); RuleIndex ruleIndex = index.get(RuleIndex.class); Multimap results = ArrayListMultimap.create(); @@ -261,6 +261,7 @@ public class RuleActivator implements ServerComponent { try { RuleResult result = ruleIndex.search(ruleQuery, QueryOptions.DEFAULT.setOffset(0) + // TODO pb because limited to QueryOptions.MAX_LIMIT .setLimit(Integer.MAX_VALUE) .setFieldsToReturn(ImmutableSet.of("template", "severity")) ); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java index ebf3da2978a..2bd98428b47 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java @@ -99,12 +99,12 @@ public class BulkRuleActivationActions implements ServerComponent { } private void bulkActivate(Request request, Response response) throws Exception { - Multimap results = service.activateByRuleQuery(createRuleQuery(request), readKey(request)); + Multimap results = service.bulkActivate(createRuleQuery(request), readKey(request)); writeResponse(results, response); } private void bulkDeactivate(Request request, Response response) throws Exception { - Multimap results = service.deActivateByRuleQuery(createRuleQuery(request), readKey(request)); + Multimap results = service.bulkDeactivate(createRuleQuery(request), readKey(request)); writeResponse(results, response); } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java index 0b06587ec6f..5c8a474a3d3 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java @@ -101,6 +101,7 @@ public class ActiveRuleBackendMediumTest { dbSession.commit(); // verify db + assertThat(db.activeRuleDao().getByKey(dbSession, activeRule.getKey())).isNotNull(); List persistedDtos = db.activeRuleDao().findByRule(dbSession, ruleDto); assertThat(persistedDtos).hasSize(1); diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java index bf78b005a9c..de11c3be6b8 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java @@ -48,12 +48,12 @@ import static org.fest.assertions.Fail.fail; public class RuleActivatorMediumTest { + static final QualityProfileKey PROFILE_KEY = QualityProfileKey.of("MyProfile", "xoo"); + @Rule public ServerTester tester = new ServerTester() .addComponents(XooRulesDefinition.class, JavaRulesDefinition.class); - static final QualityProfileKey PROFILE_KEY = QualityProfileKey.of("MyProfile", "xoo"); - DbClient db; DbSession dbSession; RuleActivator ruleActivator; diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/db/ActiveRuleDaoTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/db/ActiveRuleDaoTest.java deleted file mode 100644 index 7255f8e879d..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/db/ActiveRuleDaoTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.qualityprofile.db; - - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.System2; -import org.sonar.core.persistence.AbstractDaoTestCase; -import org.sonar.core.persistence.DbSession; -import org.sonar.core.qualityprofile.db.ActiveRuleDto; -import org.sonar.core.qualityprofile.db.QualityProfileDao; -import org.sonar.core.qualityprofile.db.QualityProfileDto; -import org.sonar.core.rule.RuleDto; -import org.sonar.server.rule.db.RuleDao; - -import static org.fest.assertions.Assertions.assertThat; - -public class ActiveRuleDaoTest extends AbstractDaoTestCase{ - - ActiveRuleDao activeRuleDao; - RuleDao ruleDao; - QualityProfileDao qualityProfileDao; - DbSession session; - - - @Before - public void setUp() throws Exception { - session = getMyBatis().openSession(false); - ruleDao = new RuleDao(System2.INSTANCE); - qualityProfileDao = new QualityProfileDao(getMyBatis()); - activeRuleDao = new ActiveRuleDao(qualityProfileDao, ruleDao, System2.INSTANCE); - } - - @After - public void tearDown() throws Exception { - session.close(); - } - - @Test - public void get_by_key() throws Exception { - - QualityProfileDto profile = QualityProfileDto.createFor("profile","xoo"); - qualityProfileDao.insert(session, profile); - - RuleDto rule = RuleDto.createFor(RuleKey.of("repo","rule")); - ruleDao.insert(session, rule); - - ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(profile, rule) - .setSeverity("BLOCKER"); - activeRuleDao.insert(session, activeRuleDto); - - session.commit(); - - ActiveRuleDto result = activeRuleDao.getNullableByKey(session, activeRuleDto.getKey()); - - assertThat(result).isNotNull(); - assertThat(result.getKey()).isNotNull(); - } -} diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java index 4bab5f494f9..f470e9e0eac 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java @@ -32,7 +32,6 @@ import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.rule.RuleParamType; import org.sonar.check.Cardinality; import org.sonar.core.persistence.DbSession; -import org.sonar.core.persistence.MyBatis; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.technicaldebt.db.CharacteristicDto; @@ -64,7 +63,7 @@ public class RuleBackendMediumTest { public void before() { tester.clearDbAndIndexes(); db = tester.get(DbClient.class); - dbSession = tester.get(MyBatis.class).openSession(false); + dbSession = tester.get(DbClient.class).openSession(false); } @After @@ -317,7 +316,7 @@ public class RuleBackendMediumTest { // 2. assert find does not get REMOVED assertThat(index.search(new RuleQuery(), QueryOptions.DEFAULT) - .getRules()).hasSize(1); + .getRules()).hasSize(1); } private RuleDto newRuleDto(RuleKey ruleKey) { diff --git a/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java index 77f4212b750..7049503d317 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java @@ -32,7 +32,6 @@ import org.sonar.api.rule.Severity; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.check.Cardinality; import org.sonar.core.persistence.DbSession; -import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; import org.sonar.core.qualityprofile.db.QualityProfileDto; @@ -60,16 +59,16 @@ public class RuleIndexMediumTest { @ClassRule public static ServerTester tester = new ServerTester(); - MyBatis myBatis = tester.get(MyBatis.class); RuleDao dao = tester.get(RuleDao.class); RuleIndex index = tester.get(RuleIndex.class); - DbClient dbClient = tester.get(DbClient.class); + DbClient db; DbSession dbSession; @Before public void before() { tester.clearDbAndIndexes(); - dbSession = myBatis.openSession(false); + db = tester.get(DbClient.class); + dbSession = db.openSession(false); } @After @@ -88,6 +87,7 @@ public class RuleIndexMediumTest { assertThat(rule.htmlDescription()).isEqualTo(ruleDto.getDescription()); assertThat(rule.key()).isEqualTo(ruleDto.getKey()); + //TODO // assertThat(rule.debtSubCharacteristicKey()) // .isEqualTo(ruleDto.getDefaultSubCharacteristicId().toString()); assertThat(rule.debtRemediationFunction().type().name()) @@ -286,11 +286,11 @@ public class RuleIndexMediumTest { @Test public void search_by_characteristics() throws InterruptedException { CharacteristicDto char1 = DebtTesting.newCharacteristicDto("char1"); - dbClient.debtCharacteristicDao().insert(char1, dbSession); + db.debtCharacteristicDao().insert(char1, dbSession); CharacteristicDto char11 = DebtTesting.newCharacteristicDto("char11") .setParentId(char1.getId()); - dbClient.debtCharacteristicDao().insert(char11, dbSession); + db.debtCharacteristicDao().insert(char11, dbSession); dbSession.commit(); dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")) @@ -412,10 +412,9 @@ public class RuleIndexMediumTest { @Test public void fail_sort_by_language() throws InterruptedException { - try { // Sorting on a field not tagged as sortable - RuleQuery query = new RuleQuery().setSortField(RuleNormalizer.RuleField.LANGUAGE); + new RuleQuery().setSortField(RuleNormalizer.RuleField.LANGUAGE); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).isEqualTo("Field 'lang' is not sortable!"); @@ -426,14 +425,14 @@ public class RuleIndexMediumTest { public void search_by_profile() throws InterruptedException { QualityProfileDto qualityProfileDto1 = QualityProfileDto.createFor("profile1", "java"); QualityProfileDto qualityProfileDto2 = QualityProfileDto.createFor("profile2", "java"); - dbClient.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2); + db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2); RuleDto rule1 = newRuleDto(RuleKey.of("java", "S001")); RuleDto rule2 = newRuleDto(RuleKey.of("java", "S002")); RuleDto rule3 = newRuleDto(RuleKey.of("java", "S003")); dao.insert(dbSession, rule1, rule2, rule3); - dbClient.activeRuleDao().insert( + db.activeRuleDao().insert( dbSession, ActiveRuleDto.createFor(qualityProfileDto1, rule1) .setSeverity("BLOCKER"), @@ -477,7 +476,7 @@ public class RuleIndexMediumTest { String value = "//expression[primary/qualifiedIdentifier[count(IDENTIFIER) = 2]/IDENTIFIER[2]/@tokenValue = 'firstOf' and primary/identifierSuffix/arguments/expression[not(primary) or primary[not(qualifiedIdentifier) or identifierSuffix]]]"; QualityProfileDto profile = QualityProfileDto.createFor("name", "Language"); - dbClient.qualityProfileDao().insert(dbSession, profile); + db.qualityProfileDao().insert(dbSession, profile); RuleDto rule = newRuleDto(RuleKey.of("java", "S001")); dao.insert(dbSession, rule); @@ -492,16 +491,13 @@ public class RuleIndexMediumTest { .setSeverity("BLOCKER"); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param); - dbClient.activeRuleDao().insert(dbSession, activeRule); - dbClient.activeRuleDao().addParam(dbSession, activeRule, activeRuleParam); + db.activeRuleDao().insert(dbSession, activeRule); + db.activeRuleDao().addParam(dbSession, activeRule, activeRuleParam); dbSession.commit(); assertThat(index.getByKey(rule.getKey()).params().get(0).defaultValue()).isEqualTo(value); - - } - @Test public void search_by_tag() throws InterruptedException { dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")).setTags(ImmutableSet.of("tag1"))); diff --git a/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java b/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java index d9156a2dcf7..8c7b6812118 100644 --- a/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java +++ b/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java @@ -75,7 +75,7 @@ public class ServerTester extends ExternalResource { Properties properties = new Properties(); properties.putAll(initialProps); - properties.setProperty(IndexProperties.TYPE,IndexProperties.ES_TYPE.MEMORY.name()); + properties.setProperty(IndexProperties.TYPE, IndexProperties.ES_TYPE.MEMORY.name()); properties.setProperty(CoreProperties.SONAR_HOME, homeDir.getAbsolutePath()); properties.setProperty(DatabaseProperties.PROP_URL, "jdbc:h2:" + homeDir.getAbsolutePath() + "/h2"); for (Map.Entry entry : System.getProperties().entrySet()) {