aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test/java/util/LoadedProjects.java
blob: eb3565f5f848a8fd867f410d6a9b57b598f7173f (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
/*
 * SonarQube
 * Copyright (C) 2009-2017 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 com.google.common.base.Throwables;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

final class LoadedProjects {

  static final String SONAR_PROJECT_PROPERTIES_FILE_NAME = "sonar-project.properties";

  private final Map<String, ProjectState> projectStatePerProjectKey = new HashMap<>();
  private final Set<String> knownProjects = new HashSet<>();

  public void reset() {
    this.projectStatePerProjectKey.clear();
    this.knownProjects.clear();
  }

  public String load(String projectRelativePath) {
    checkState(!knownProjects.contains(projectRelativePath), "Project at location %s already loaded", projectRelativePath);

    File projectDir = ItUtils.projectDir(projectRelativePath);
    Properties sonarProjectProperties = loadProjectProperties(projectDir);
    ProjectState projectState = new ProjectState(projectDir, sonarProjectProperties);

    register(projectRelativePath, projectState);

    return projectState.getProjectKey();
  }

  public ProjectState getProjectState(String projectKey) {
    ProjectState projectState = this.projectStatePerProjectKey.get(projectKey);
    checkArgument(projectState != null, "Project with key %s is unknown to %s", projectKey, ProjectAnalysisRule.class.getSimpleName());
    return projectState;
  }

  private void register(String projectRelativePath, ProjectState projectState) {
    this.projectStatePerProjectKey.put(projectState.getProjectKey(), projectState);
    this.knownProjects.add(projectRelativePath);
  }

  private static Properties loadProjectProperties(File projectDir) {
    File sonarPropertiesFile = new File(projectDir, SONAR_PROJECT_PROPERTIES_FILE_NAME);
    checkArgument(sonarPropertiesFile.exists(), "Can not locate %s in project %s", SONAR_PROJECT_PROPERTIES_FILE_NAME, projectDir.getAbsolutePath());

    Properties properties = new Properties();
    try {
      properties.load(new FileReader(sonarPropertiesFile));
    } catch (IOException e) {
      Throwables.propagate(e);
    }
    return properties;
  }
}