From b829b43462b950bae7345eced0d9a80d4d486c14 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 16 Apr 2014 19:07:12 +0200 Subject: [PATCH] First draft of Medium test --- .../org/sonar/server/tester/MediumTest.java | 103 ++++++++++++++++++ .../org/sonar/server/tester/ServerTester.java | 64 +++++++++++ 2 files changed, 167 insertions(+) create mode 100644 sonar-server/src/test/java/org/sonar/server/tester/MediumTest.java create mode 100644 sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java diff --git a/sonar-server/src/test/java/org/sonar/server/tester/MediumTest.java b/sonar-server/src/test/java/org/sonar/server/tester/MediumTest.java new file mode 100644 index 00000000000..17717a0dbb3 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/tester/MediumTest.java @@ -0,0 +1,103 @@ +/* + * 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.tester; + +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.sonar.core.permission.GlobalPermissions; +import org.sonar.core.rule.RuleDao; +import org.sonar.core.rule.RuleDto; +import org.sonar.server.debt.DebtModelService; +import org.sonar.server.rule.RuleQuery; +import org.sonar.server.rule.RuleRegistry; +import org.sonar.server.rule.Rules; +import org.sonar.server.user.MockUserSession; + +import static org.fest.assertions.Assertions.assertThat; + +@Ignore +public class MediumTest { + + ServerTester serverTester = new ServerTester(); + + @Before + public void before() throws Exception { + serverTester.start(); + } + + @After + public void after() throws Exception { + serverTester.stop(); + } + + @Test + public void find_characteristics() throws Exception { + DebtModelService debtModelService = serverTester.get(DebtModelService.class); + + assertThat(debtModelService.allCharacteristics()).hasSize(39); + } + + @Test + public void create_characteristic() throws Exception { + MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); + + DebtModelService debtModelService = serverTester.get(DebtModelService.class); + debtModelService.create("NEW ONE", null); + + assertThat(debtModelService.allCharacteristics()).hasSize(40); + } + + @Test + public void create_another_characteristic() throws Exception { + MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); + + DebtModelService debtModelService = serverTester.get(DebtModelService.class); + debtModelService.create("NEW TWO", null); + + assertThat(debtModelService.allCharacteristics()).hasSize(40); + } + + @Test + public void create_rule() throws Exception { + Rules rules = serverTester.get(Rules.class); + assertThat(rules.find(RuleQuery.builder().build()).results()).isEmpty(); + + RuleDto ruleDto = new RuleDto().setRepositoryKey("repo").setRuleKey("key").setSeverity("MAJOR"); + serverTester.get(RuleDao.class).insert(ruleDto); + serverTester.get(RuleRegistry.class).reindex(ruleDto); + + assertThat(rules.find(RuleQuery.builder().build()).results()).hasSize(1); + } + + @Test + public void create_another_rule() throws Exception { + Rules rules = serverTester.get(Rules.class); + assertThat(rules.find(RuleQuery.builder().build()).results()).isEmpty(); + + RuleDto ruleDto = new RuleDto().setRepositoryKey("repo2").setRuleKey("key2").setSeverity("MAJOR"); + serverTester.get(RuleDao.class).insert(ruleDto); + serverTester.get(RuleRegistry.class).reindex(ruleDto); + + assertThat(rules.find(RuleQuery.builder().build()).results()).hasSize(1); + } +} 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 new file mode 100644 index 00000000000..5f2f1f26f1e --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java @@ -0,0 +1,64 @@ +/* + * 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.tester; + +import org.apache.commons.io.FileUtils; +import org.sonar.api.CoreProperties; +import org.sonar.server.db.migrations.DatabaseMigrator; +import org.sonar.server.platform.Platform; + +import java.io.File; +import java.util.Properties; +import java.util.UUID; + +public class ServerTester { + + private File temp; + + private Platform platform; + + public ServerTester() { + platform = new Platform(); + } + + public void start() { + temp = new File("target/" + UUID.randomUUID().toString()); + temp.mkdirs(); + + Properties properties = new Properties(); + properties.setProperty(CoreProperties.SONAR_HOME, temp.getAbsolutePath()); + properties.setProperty("sonar.jdbc.dialect", "h2"); + properties.setProperty("sonar.jdbc.url", "jdbc:h2:" + temp.getAbsolutePath() + "/h2"); + + platform.init(properties); + ((DatabaseMigrator) platform.getComponent(DatabaseMigrator.class)).createDatabase(); + platform.doStart(); + } + + public void stop() { + platform.doStop(); + FileUtils.deleteQuietly(temp); + } + + public E get(Class component) { + return platform.getContainer().getComponentByType(component); + } +} -- 2.39.5