3 * Copyright (C) 2009-2018 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v75;
22 import java.sql.SQLException;
23 import java.util.List;
25 import javax.annotation.Nullable;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.resources.Qualifiers;
29 import org.sonar.api.resources.Scopes;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.utils.internal.TestSystem2;
32 import org.sonar.core.util.SequenceUuidFactory;
33 import org.sonar.core.util.UuidFactory;
34 import org.sonar.db.CoreDbTester;
36 import static java.lang.String.valueOf;
37 import static org.assertj.core.api.Assertions.assertThat;
39 public class MigrateModulePropertiesTest {
40 private final static long NOW = 50_000_000_000L;
43 public CoreDbTester db = CoreDbTester.createForSchema(MigrateModulePropertiesTest.class, "schema.sql");
44 private System2 system2 = new TestSystem2().setNow(NOW);
45 private UuidFactory uuidFactory = new SequenceUuidFactory();
46 private MigrateModuleProperties underTest = new MigrateModuleProperties(db.database(), system2);
49 public void multi_module_project_migration() throws SQLException {
50 setupMultiModuleProject();
54 verifyMultiModuleProjectMigration();
58 public void migration_is_reentrant() throws SQLException {
59 setupMultiModuleProject();
64 verifyMultiModuleProjectMigration();
65 List<Map<String, Object>> remainingProperties = db.select("select ID from properties");
66 assertThat(remainingProperties).hasSize(3);
70 public void single_module_project_migration() throws SQLException {
71 String project2Uuid = uuidFactory.create();
72 insertComponent(5, project2Uuid, null, project2Uuid, Qualifiers.PROJECT, "Single module project");
73 insertProperty(9, 5, "sonar.coverage.exclusions", "SingleModuleA.java");
74 insertProperty(10, 5, "sonar.cp.exclusions", "SingleModuleB.java");
78 List<Map<String, Object>> properties = db.select("select ID, TEXT_VALUE, CLOB_VALUE " +
80 "where PROP_KEY='sonar.subprojects.settings.removed' and RESOURCE_ID = 5");
81 assertThat(properties).hasSize(0);
82 List<Map<String, Object>> remainingProperties = db.select("select ID from properties");
83 assertThat(remainingProperties).hasSize(2);
87 public void properties_do_not_leak_between_projects() throws SQLException {
88 setupMultiModuleProject();
89 setupSecondMultiModuleProject();
93 verifyMultiModuleProjectMigration();
94 verifySecondMultiModuleProjectMigration();
97 private void insertComponent(long id, String uuid, @Nullable String rootUuid, String projectUuid, String qualifier, String name) {
102 "ROOT_UUID", rootUuid,
103 "PROJECT_UUID", projectUuid,
104 "SCOPE", Scopes.PROJECT,
105 "QUALIFIER", qualifier,
109 private void insertProperty(long id, long componentId, String key, String value) {
113 "RESOURCE_ID", componentId,
119 private void setupMultiModuleProject() {
120 String projectUuid = uuidFactory.create();
121 String moduleUuid = uuidFactory.create();
122 String subModule1Uuid = uuidFactory.create();
123 String subModule2Uuid = uuidFactory.create();
124 insertComponent(1, projectUuid, null, projectUuid, Qualifiers.PROJECT, "Multi-module project");
125 insertComponent(2, moduleUuid, projectUuid, projectUuid, Qualifiers.MODULE, "Module");
126 insertComponent(3, subModule1Uuid, moduleUuid, projectUuid, Qualifiers.MODULE, "Submodule 1");
127 insertComponent(4, subModule2Uuid, moduleUuid, projectUuid, Qualifiers.MODULE, "Submodule 2");
128 insertProperty(1, 1, "sonar.coverage.exclusions", "Proj1.java");
129 insertProperty(2, 1, "sonar.cpd.exclusions", "Proj2.java");
130 insertProperty(3, 2, "sonar.coverage.exclusions", "ModuleA.java");
131 insertProperty(4, 2, "sonar.cpd.exclusions", "ModuleB.java");
132 insertProperty(5, 3, "sonar.coverage.exclusions", "Module1A.java");
133 insertProperty(6, 3, "sonar.cpd.exclusions", "Moddule1B.java");
134 insertProperty(7, 4, "sonar.coverage.exclusions", "Module2A.java");
135 insertProperty(8, 4, "sonar.cpd.exclusions", "Module2B.java");
138 private void verifyMultiModuleProjectMigration() {
139 final String expectedResult = "# previous settings for sub-project Multi-module project::Module\n" +
140 "sonar.coverage.exclusions=ModuleA.java\n" +
141 "sonar.cpd.exclusions=ModuleB.java\n" +
143 "# previous settings for sub-project Multi-module project::Submodule 1\n" +
144 "sonar.coverage.exclusions=Module1A.java\n" +
145 "sonar.cpd.exclusions=Moddule1B.java\n" +
147 "# previous settings for sub-project Multi-module project::Submodule 2\n" +
148 "sonar.coverage.exclusions=Module2A.java\n" +
149 "sonar.cpd.exclusions=Module2B.java\n";
151 List<Map<String, Object>> properties = db.select("select ID, TEXT_VALUE, CLOB_VALUE " +
153 "where PROP_KEY='sonar.subprojects.settings.removed' and RESOURCE_ID = 1");
155 assertThat(properties).hasSize(1);
156 Map<String, Object> project1Property = properties.get(0);
157 assertThat(project1Property.get("TEXT_VALUE")).isNull();
158 assertThat(project1Property.get("CLOB_VALUE")).isEqualTo(expectedResult);
161 private void setupSecondMultiModuleProject() {
162 String project3Uuid = uuidFactory.create();
163 String singleModuleUuid = uuidFactory.create();
164 insertComponent(6, project3Uuid, null, project3Uuid, Qualifiers.PROJECT, "Another multi-module project");
165 insertComponent(7, singleModuleUuid, project3Uuid, project3Uuid, Qualifiers.MODULE, "Module X");
166 insertProperty(11, 6, "sonar.coverage.exclusions", "InRoot.java");
167 insertProperty(12, 7, "sonar.coverage.exclusions", "InModule.java");
170 private void verifySecondMultiModuleProjectMigration() {
171 final String expectedResult = "# previous settings for sub-project Another multi-module project::Module X\n" +
172 "sonar.coverage.exclusions=InModule.java\n";
174 List<Map<String, Object>> properties = db.select("select ID, TEXT_VALUE, CLOB_VALUE " +
176 "where PROP_KEY='sonar.subprojects.settings.removed' and RESOURCE_ID = 6");
178 assertThat(properties).hasSize(1);
179 Map<String, Object> project2Property = properties.get(0);
180 assertThat(project2Property.get("TEXT_VALUE")).isNull();
181 assertThat(project2Property.get("CLOB_VALUE")).isEqualTo(expectedResult);