aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java
blob: ddabaaeeb1e0de9f8e0bd492796081f95da5f8cb (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * SonarQube
 * Copyright (C) 2009-2025 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.sonar.scanner.scan.filesystem;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.Nullable;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputFile.Status;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.InputPath;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.scanner.scan.branch.BranchConfiguration;

import static java.util.Optional.ofNullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class InputComponentStoreTest {

  private final SonarRuntime sonarRuntime = mock(SonarRuntime.class);

  @ClassRule
  public static TemporaryFolder temp = new TemporaryFolder();

  @Test
  public void should_add_input_file() throws Exception {
    String rootModuleKey = "struts";
    String subModuleKey = "struts-core";

    File rootBaseDir = temp.newFolder();

    ProjectDefinition moduleDef = ProjectDefinition.create()
      .setKey(subModuleKey).setBaseDir(rootBaseDir).setWorkDir(temp.newFolder());
    ProjectDefinition rootDef = ProjectDefinition.create()
      .setKey(rootModuleKey).setBaseDir(rootBaseDir).setWorkDir(temp.newFolder()).addSubProject(moduleDef);

    DefaultInputProject rootProject = TestInputFileBuilder.newDefaultInputProject(rootDef);
    DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(moduleDef);

    InputComponentStore store = new InputComponentStore(mock(BranchConfiguration.class), sonarRuntime);
    store.put(subModule);

    DefaultInputFile fooFile = new TestInputFileBuilder(rootModuleKey, "src/main/java/Foo.java")
      .setModuleBaseDir(rootBaseDir.toPath())
      .setPublish(true)
      .build();
    store.put(rootProject.key(), fooFile);
    store.put(subModuleKey, new TestInputFileBuilder(rootModuleKey, "src/main/java/Bar.java")
      .setLanguage("bla")
      .setPublish(false)
      .setType(Type.MAIN)
      .setStatus(Status.ADDED)
      .setLines(2)
      .setCharset(StandardCharsets.UTF_8)
      .setModuleBaseDir(temp.newFolder().toPath())
      .build());

    DefaultInputFile loadedFile = (DefaultInputFile) store.getFile(subModuleKey, "src/main/java/Bar.java");
    assertThat(loadedFile.relativePath()).isEqualTo("src/main/java/Bar.java");
    assertThat(loadedFile.charset()).isEqualTo(StandardCharsets.UTF_8);

    assertThat(store.filesByModule(rootModuleKey)).hasSize(1);
    assertThat(store.filesByModule(subModuleKey)).hasSize(1);
    assertThat(store.inputFiles()).hasSize(2);
    for (InputPath inputPath : store.inputFiles()) {
      assertThat(inputPath.relativePath()).startsWith("src/main/java/");
    }

    List<InputFile> toPublish = new LinkedList<>();
    store.allFilesToPublish().forEach(toPublish::add);
    assertThat(toPublish).containsExactly(fooFile);
  }

  static class InputComponentStoreTester extends InputComponentStore {
    InputComponentStoreTester(SonarRuntime sonarRuntime) {
      super(mock(BranchConfiguration.class), sonarRuntime);
    }

    InputFile addFile(String moduleKey, String relpath, @Nullable String language) {
      TestInputFileBuilder fileBuilder = new TestInputFileBuilder(moduleKey, relpath);
      ofNullable(language).ifPresent(fileBuilder::setLanguage);
      DefaultInputFile file = fileBuilder.build();
      put(moduleKey, file);
      return file;
    }

    InputFile addFile(String moduleKey, String relPath) {
      DefaultInputFile file = new TestInputFileBuilder(moduleKey, relPath)
        .build();
      put(moduleKey, file);
      return file;
    }
  }

  @Test
  public void should_add_languages_per_module_and_globally() {
    InputComponentStoreTester tester = new InputComponentStoreTester(sonarRuntime);

    String mod1Key = "mod1";
    tester.addFile(mod1Key, "src/main/java/Foo.java", "java");

    String mod2Key = "mod2";
    tester.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");

    assertThat(tester.languages(mod1Key)).containsExactly("java");
    assertThat(tester.languages(mod2Key)).containsExactly("groovy");
    assertThat(tester.languages()).containsExactlyInAnyOrder("java", "groovy");
  }

  @Test
  public void should_find_files_per_module_and_globally() {
    InputComponentStoreTester tester = new InputComponentStoreTester(sonarRuntime);

    String mod1Key = "mod1";
    InputFile mod1File = tester.addFile(mod1Key, "src/main/java/Foo.java", "java");

    String mod2Key = "mod2";
    InputFile mod2File = tester.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");

    assertThat(tester.filesByModule(mod1Key)).containsExactly(mod1File);
    assertThat(tester.filesByModule(mod2Key)).containsExactly(mod2File);
    assertThat(tester.inputFiles()).containsExactlyInAnyOrder(mod1File, mod2File);
  }

  @Test
  public void stores_not_analysed_c_file_count_in_sq_community_edition() {
    when(sonarRuntime.getEdition()).thenReturn(SonarEdition.COMMUNITY);
    InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime);
    String mod1Key = "mod1";
    underTest.addFile(mod1Key, "src/main/java/Foo.java", "java");
    underTest.addFile(mod1Key, "src/main/c/file1.c");
    underTest.addFile(mod1Key, "src/main/c/file2.c");
    String mod2Key = "mod2";
    underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
    underTest.addFile(mod2Key, "src/main/c/file3.c");

    assertThat(underTest.getNotAnalysedFilesByLanguage()).hasSize(1);
    assertThat(underTest.getNotAnalysedFilesByLanguage()).containsEntry("C", 3);
  }

  @Test
  public void stores_not_analysed_cpp_file_count_in_sq_community_edition() {
    when(sonarRuntime.getEdition()).thenReturn(SonarEdition.COMMUNITY);
    InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime);
    String mod1Key = "mod1";
    underTest.addFile(mod1Key, "src/main/java/Foo.java", "java");
    underTest.addFile(mod1Key, "src/main/c/file1.c");
    underTest.addFile(mod1Key, "src/main/c/file2.cpp");
    underTest.addFile(mod1Key, "src/main/c/file3.cxx");
    underTest.addFile(mod1Key, "src/main/c/file4.c++");
    underTest.addFile(mod1Key, "src/main/c/file5.cc");
    underTest.addFile(mod1Key, "src/main/c/file6.CPP");
    String mod2Key = "mod2";
    underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
    underTest.addFile(mod2Key, "src/main/c/file3.cpp");

    assertThat(underTest.getNotAnalysedFilesByLanguage()).hasSize(2);
    assertThat(underTest.getNotAnalysedFilesByLanguage()).containsEntry("C++", 6);
  }

  @Test
  public void does_not_store_not_analysed_file_counts_in_sq_non_community_editions() {
    when(sonarRuntime.getEdition()).thenReturn(SonarEdition.DEVELOPER);
    InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime);
    String mod1Key = "mod1";
    underTest.addFile(mod1Key, "src/main/java/Foo.java", "java");
    underTest.addFile(mod1Key, "src/main/java/file1.c");
    underTest.addFile(mod1Key, "src/main/java/file2.c");
    String mod2Key = "mod2";
    underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
    underTest.addFile(mod2Key, "src/main/groovy/file4.c");

    assertThat(underTest.getNotAnalysedFilesByLanguage()).isEmpty();
  }
}