diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2018-04-03 13:54:14 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-04-05 20:20:48 +0200 |
commit | c58ff4caa54f4d1da9e3103afc01d64a211aef66 (patch) | |
tree | 91514bc4c0e291629b1429dd51a61730ea2573c9 /tests | |
parent | 492942631b3c2612fd8ed9b03bd7db3261a38e58 (diff) | |
download | sonarqube-c58ff4caa54f4d1da9e3103afc01d64a211aef66.tar.gz sonarqube-c58ff4caa54f4d1da9e3103afc01d64a211aef66.zip |
SONAR-10536 refactoring, no functional changes
Diffstat (limited to 'tests')
4 files changed, 203 insertions, 62 deletions
diff --git a/tests/src/test/java/org/sonarqube/tests/component/ComponentSuite.java b/tests/src/test/java/org/sonarqube/tests/component/ComponentSuite.java index 0384ce9562b..9d08069bc36 100644 --- a/tests/src/test/java/org/sonarqube/tests/component/ComponentSuite.java +++ b/tests/src/test/java/org/sonarqube/tests/component/ComponentSuite.java @@ -31,15 +31,14 @@ import static util.ItUtils.xooPlugin; @Suite.SuiteClasses({ BranchTest.class, CodePageTest.class, - ComponentsWsTest.class + ComponentsWsTest.class, + ModuleTest.class }) public class ComponentSuite { @ClassRule public static final Orchestrator ORCHESTRATOR = ItUtils.newOrchestratorBuilder() - .addPlugin(xooPlugin()) - .build(); } diff --git a/tests/src/test/java/org/sonarqube/tests/component/ModuleTest.java b/tests/src/test/java/org/sonarqube/tests/component/ModuleTest.java new file mode 100644 index 00000000000..ffc3f4e1a47 --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/component/ModuleTest.java @@ -0,0 +1,115 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.sonarqube.tests.component; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.BuildFailureException; +import com.sonar.orchestrator.build.SonarScanner; +import java.io.File; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonarqube.qa.util.Tester; +import org.sonarqube.ws.Organizations; +import org.sonarqube.ws.client.components.ShowRequest; +import org.sonarqube.ws.client.components.TreeRequest; +import org.sonarqube.ws.client.projects.UpdateKeyRequest; +import util.XooProjectBuilder; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; + +public class ModuleTest { + + private static final String PROJECT_KEY = "sample"; + + @ClassRule + public static Orchestrator orchestrator = ComponentSuite.ORCHESTRATOR; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Rule + public Tester tester = new Tester(orchestrator); + + /** + * SONAR-10536 + */ + @Test + public void analyze_disabled_module_as_a_new_project() throws Exception { + Organizations.Organization organization = tester.organizations().generate(); + String projectKey = PROJECT_KEY; + String moduleKey = projectKey + ":module_a"; + + // analyze project "sample" with module "sample:module_a" + File projectWithModule = new XooProjectBuilder(projectKey) + .addModules("module_a") + .build(temp.newFolder()); + analyze(organization, projectWithModule); + assertThat(tester.projects().exists(moduleKey)).isTrue(); + assertThat(countFilesInProject(projectKey)).isEqualTo(2 /* 1 file in project and 1 file in module */); + + // analyze project "sample" without module "sample:module_a". The latter + // is considered as disabled + File projectWithoutModule = new XooProjectBuilder(projectKey) + .build(temp.newFolder()); + analyze(organization, projectWithoutModule); + assertThat(tester.projects().exists(moduleKey)).isFalse(); + assertThat(countFilesInProject(projectKey)).isEqualTo(1 /* 1 file in project */); + + // analyze module_a as a project + File moduleAsProject = new XooProjectBuilder(moduleKey) + .build(temp.newFolder()); + try { + analyze(organization, moduleAsProject); + fail(); + } catch (BuildFailureException e) { + assertThat(e.getResult().getLogs()).contains("The project '" + moduleKey + "' is already defined in SonarQube but as a module of project '" + projectKey + "'"); + assertThat(tester.projects().exists(moduleKey)).isFalse(); + } + + // the only workaround is to rename the key of the disabled module + updateModuleKey(moduleKey, moduleKey + "_old"); + + // module_a can now be analyzed as a project + analyze(organization, moduleAsProject); + assertThat(tester.projects().exists(moduleKey)).isTrue(); + assertThat(countFilesInProject(moduleKey)).isEqualTo(1); + assertThat(tester.wsClient().components().show(new ShowRequest().setComponent(moduleKey)).getComponent().getQualifier()).isEqualTo("TRK"); + } + + private void analyze(Organizations.Organization organization, File projectDir) { + orchestrator.executeBuild(SonarScanner.create(projectDir, + "sonar.organization", organization.getKey(), + "sonar.login", "admin", "sonar.password", "admin")); + } + + private int countFilesInProject(String projectKey) { + TreeRequest request = new TreeRequest().setComponent(projectKey).setQualifiers(asList("FIL")); + return tester.wsClient().components().tree(request).getComponentsCount(); + } + + private void updateModuleKey(String fromKey, String toKey) { + tester.wsClient().projects().updateKey(new UpdateKeyRequest() + .setFrom(fromKey) + .setTo(toKey)); + } +} diff --git a/tests/src/test/java/org/sonarqube/tests/project/ProjectKeyUpdateTest.java b/tests/src/test/java/org/sonarqube/tests/project/ProjectKeyUpdateTest.java index d4f004dd9f1..b1486e5f539 100644 --- a/tests/src/test/java/org/sonarqube/tests/project/ProjectKeyUpdateTest.java +++ b/tests/src/test/java/org/sonarqube/tests/project/ProjectKeyUpdateTest.java @@ -23,16 +23,11 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import java.io.File; import java.io.IOException; -import java.io.OutputStream; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.stream.Collectors; import javax.annotation.CheckForNull; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.StringUtils; import org.junit.After; import org.junit.ClassRule; import org.junit.Rule; @@ -54,6 +49,7 @@ import org.sonarqube.ws.client.components.TreeRequest; import org.sonarqube.ws.client.projects.BulkUpdateKeyRequest; import org.sonarqube.ws.client.projects.UpdateKeyRequest; import util.ItUtils; +import util.XooProjectBuilder; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @@ -398,58 +394,4 @@ public class ProjectKeyUpdateTest { .collect(Collectors.toList()); } - private static class XooProjectBuilder { - private final String key; - private final List<String> moduleKeys = new ArrayList<>(); - private int filesPerModule = 1; - - XooProjectBuilder(String projectKey) { - this.key = projectKey; - } - - XooProjectBuilder addModules(String key, String... otherKeys) { - this.moduleKeys.add(key); - this.moduleKeys.addAll(asList(otherKeys)); - return this; - } - - XooProjectBuilder setFilesPerModule(int i) { - this.filesPerModule = i; - return this; - } - - File build(File dir) { - for (String moduleKey : moduleKeys) { - generateModule(moduleKey, new File(dir, moduleKey), new Properties()); - } - Properties additionalProps = new Properties(); - additionalProps.setProperty("sonar.modules", StringUtils.join(moduleKeys, ",")); - generateModule(key, dir, additionalProps); - return dir; - } - - private void generateModule(String key, File dir, Properties additionalProps) { - try { - File sourceDir = new File(dir, "src"); - FileUtils.forceMkdir(sourceDir); - for (int i = 0; i < filesPerModule; i++) { - File sourceFile = new File(sourceDir, "File" + i + ".xoo"); - FileUtils.write(sourceFile, "content of " + sourceFile.getName()); - } - Properties props = new Properties(); - props.setProperty("sonar.projectKey", key); - props.setProperty("sonar.projectName", key); - props.setProperty("sonar.projectVersion", "1.0"); - props.setProperty("sonar.sources", sourceDir.getName()); - props.putAll(additionalProps); - File propsFile = new File(dir, "sonar-project.properties"); - try (OutputStream output = FileUtils.openOutputStream(propsFile)) { - props.store(output, "generated"); - } - } catch (IOException e) { - throw new IllegalStateException(e); - } - } - } - } diff --git a/tests/src/test/java/util/XooProjectBuilder.java b/tests/src/test/java/util/XooProjectBuilder.java new file mode 100644 index 00000000000..0eaf934572b --- /dev/null +++ b/tests/src/test/java/util/XooProjectBuilder.java @@ -0,0 +1,85 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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 util; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; + +import static java.util.Arrays.asList; + +public class XooProjectBuilder { + private final String key; + private final List<String> moduleKeys = new ArrayList<>(); + private int filesPerModule = 1; + + public XooProjectBuilder(String projectKey) { + this.key = projectKey; + } + + public XooProjectBuilder addModules(String key, String... otherKeys) { + this.moduleKeys.add(key); + this.moduleKeys.addAll(asList(otherKeys)); + return this; + } + + public XooProjectBuilder setFilesPerModule(int i) { + this.filesPerModule = i; + return this; + } + + public File build(File dir) { + for (String moduleKey : moduleKeys) { + generateModule(moduleKey, new File(dir, moduleKey), new Properties()); + } + Properties additionalProps = new Properties(); + additionalProps.setProperty("sonar.modules", StringUtils.join(moduleKeys, ",")); + generateModule(key, dir, additionalProps); + return dir; + } + + private void generateModule(String key, File dir, Properties additionalProps) { + try { + File sourceDir = new File(dir, "src"); + FileUtils.forceMkdir(sourceDir); + for (int i = 0; i < filesPerModule; i++) { + File sourceFile = new File(sourceDir, "File" + i + ".xoo"); + FileUtils.write(sourceFile, "content of " + sourceFile.getName()); + } + Properties props = new Properties(); + props.setProperty("sonar.projectKey", key); + props.setProperty("sonar.projectName", key); + props.setProperty("sonar.projectVersion", "1.0"); + props.setProperty("sonar.sources", sourceDir.getName()); + props.putAll(additionalProps); + File propsFile = new File(dir, "sonar-project.properties"); + try (OutputStream output = FileUtils.openOutputStream(propsFile)) { + props.store(output, "generated"); + } + } catch (IOException e) { + throw new IllegalStateException(e); + } + } +} |