aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java
blob: 3d8d75407876b831c311aa3231c5e803874a75ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * 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.batch.index;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
import org.sonar.api.resources.Project;
import org.sonar.jpa.test.AbstractDbUnitTestCase;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class ResourceKeyMigrationTest extends AbstractDbUnitTestCase {

  @Rule
  public ExpectedException thrown = ExpectedException.none();

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  Project multiModuleProject, phpModule, javaModule;
  Iterable<InputFile> javaInputFiles;
  Iterable<InputFile> phpInputFiles;
  File baseDir;

  @Before
  public void before() throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");

    multiModuleProject = newProject("root", "java");
    multiModuleProject.setName("Root").setAnalysisDate(format.parse("25/12/2010"));

    phpModule = newProject("a", "php");
    phpModule.setName("A").setAnalysisDate(format.parse("25/12/2010"));
    phpModule.setParent(multiModuleProject);
    phpModule.setPath("/moduleA");

    javaModule = newProject("b", "java");
    javaModule.setName("B").setAnalysisDate(format.parse("25/12/2010"));
    javaModule.setParent(multiModuleProject);
    javaModule.setPath("/moduleB");

    baseDir = temp.newFolder();

    javaInputFiles = (Iterable) Arrays.asList(
      newInputFile(javaModule, "src/main/java/org/foo/Bar.java", "org.foo.Bar", false),
      newInputFile(javaModule, "src/main/java/RootBar.java", "[default].RootBar", false),
      newInputFile(javaModule, "src/test/java/org/foo/BarTest.java", "org.foo.BarTest", true));

    phpInputFiles = (Iterable) Arrays.asList(
      newInputFile(phpModule, "org/foo/Bar.php", "org/foo/Bar.php", false),
      newInputFile(phpModule, "RootBar.php", "RootBar.php", false),
      newInputFile(phpModule, "test/org/foo/BarTest.php", "org/foo/BarTest.php", true));

  }

  private DefaultInputFile newInputFile(Project module, String path, String deprecatedKey, boolean isTest) {
    File file = new File(baseDir, path);
    String effectiveKey = module.getKey() + ":" + path;
    String deprecatedEffectiveKey = module.getKey() + ":" + deprecatedKey;
    return new DeprecatedDefaultInputFile(path)
      .setDeprecatedKey(deprecatedEffectiveKey)
      .setFile(file)
      .setKey(effectiveKey)
      .setType(isTest ? InputFile.Type.TEST : InputFile.Type.MAIN);
  }

  @Test
  public void shouldMigrateResourceKeys() {
    setupData("shouldMigrateResourceKeys");

    Logger logger = mock(Logger.class);
    ResourceKeyMigration migration = new ResourceKeyMigration(getSession(), logger);
    migration.checkIfMigrationNeeded(multiModuleProject);

    migration.migrateIfNeeded(javaModule, javaInputFiles);
    migration.migrateIfNeeded(phpModule, phpInputFiles);

    verify(logger).info("Component {} changed to {}", "b:org.foo.Bar", "b:src/main/java/org/foo/Bar.java");
    verify(logger).warn("Directory with key b:org/foo matches both b:src/main/java/org/foo and b:src/test/java/org/foo. First match is arbitrary chosen.");
    verify(logger).info("Component {} changed to {}", "b:org.foo.BarTest", "b:src/test/java/org/foo/BarTest.java");
    verify(logger).info("Component {} changed to {}", "b:[default].RootBar", "b:src/main/java/RootBar.java");
    verify(logger).info("Component {} changed to {}", "b:org/foo", "b:src/main/java/org/foo");
    verify(logger).info("Component {} changed to {}", "b:[root]", "b:src/main/java");

    checkTables("shouldMigrateResourceKeys", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects");
  }

  private static Project newProject(String key, String language) {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty("sonar.language", language);
    return new Project(key).setConfiguration(configuration).setAnalysisType(Project.AnalysisType.DYNAMIC);
  }

}