Skip to content

Commit 03ca080

Browse files
Review changes
1 parent 77f09c9 commit 03ca080

File tree

8 files changed

+125
-54
lines changed

8 files changed

+125
-54
lines changed

java-checks-test-sources/default/src/test/java/checks/tests/SpringBootEmptyMethodsCheckSample.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
class SpringBootSanityTest {
88
// Compliant, first time we encounter a spring sanity test
99
@Test
10-
void contextLoads() {
11-
}
10+
void contextLoads() {}
1211

1312
// Noncompliant@+2
1413
@Test

java-frontend/src/main/java/org/sonar/java/DefaultModuleMetadata.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616
*/
1717
package org.sonar.java;
1818

19-
import javax.annotation.CheckForNull;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
2021
import org.sonar.api.batch.bootstrap.ProjectDefinition;
2122
import org.sonar.api.config.Configuration;
2223
import org.sonar.java.model.JavaVersionImpl;
24+
import org.sonar.java.utils.ModuleMetadataUtils;
2325
import org.sonar.plugins.java.api.JavaVersion;
2426
import org.sonar.plugins.java.api.internal.ModuleMetadata;
2527

2628
import static org.sonar.java.SonarComponents.SONAR_IGNORE_UNNAMED_MODULE_FOR_SPLIT_PACKAGE;
2729

2830
public class DefaultModuleMetadata implements ModuleMetadata {
2931

32+
private static final Logger LOG = LoggerFactory.getLogger(DefaultModuleMetadata.class);
33+
3034
private final JavaVersion javaVersion;
3135
private final ProjectDefinition projectDefinition;
3236
private final boolean ignoreUnnamedModuleForSplitPackage;
@@ -44,30 +48,16 @@ public JavaVersion javaVersion() {
4448

4549
@Override
4650
public String moduleKey() {
47-
var root = getRootProject();
48-
if (root != null && projectDefinition != null) {
49-
var rootBase = root.getBaseDir().toPath();
50-
var moduleBase = projectDefinition.getBaseDir().toPath();
51-
return rootBase.relativize(moduleBase).toString().replace('\\', '/');
51+
var moduleKey = ModuleMetadataUtils.getModuleKey(projectDefinition);
52+
if (moduleKey.isEmpty()) {
53+
LOG.warn("Unable to determine module key, using empty string as fallback");
5254
}
53-
return "";
55+
return moduleKey;
5456
}
5557

5658
@Override
5759
public boolean shouldIgnoreUnnamedModuleForSplitPackage() {
5860
return ignoreUnnamedModuleForSplitPackage;
5961
}
6062

61-
@CheckForNull
62-
private ProjectDefinition getRootProject() {
63-
ProjectDefinition current = projectDefinition;
64-
if (current == null) {
65-
return null;
66-
}
67-
while (current.getParent() != null) {
68-
current = current.getParent();
69-
}
70-
return current;
71-
}
72-
7363
}

java-frontend/src/main/java/org/sonar/java/SonarComponents.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.sonar.java.model.LineUtils;
6969
import org.sonar.java.reporting.AnalyzerMessage;
7070
import org.sonar.java.reporting.JavaIssue;
71+
import org.sonar.java.utils.ModuleMetadataUtils;
7172
import org.sonar.plugins.java.api.CheckRegistrar;
7273
import org.sonar.plugins.java.api.JavaCheck;
7374
import org.sonar.plugins.java.api.JavaFileScanner;
@@ -513,7 +514,7 @@ private static long computeIdealBatchSize() {
513514
}
514515

515516
public File projectLevelWorkDir() {
516-
var root = getRootProject();
517+
var root = ModuleMetadataUtils.getRootProject(projectDefinition);
517518
if (root != null) {
518519
return root.getWorkDir();
519520
} else {
@@ -527,25 +528,7 @@ public File projectLevelWorkDir() {
527528
* @return A key representing the module
528529
*/
529530
public String getModuleKey() {
530-
var root = getRootProject();
531-
if (root != null && projectDefinition != null) {
532-
var rootBase = root.getBaseDir().toPath();
533-
var moduleBase = projectDefinition.getBaseDir().toPath();
534-
return rootBase.relativize(moduleBase).toString().replace('\\', '/');
535-
}
536-
return "";
537-
}
538-
539-
@CheckForNull
540-
private ProjectDefinition getRootProject() {
541-
ProjectDefinition current = projectDefinition;
542-
if (current == null) {
543-
return null;
544-
}
545-
while (current.getParent() != null) {
546-
current = current.getParent();
547-
}
548-
return current;
531+
return ModuleMetadataUtils.getModuleKey(projectDefinition);
549532
}
550533

551534
public boolean canSkipUnchangedFiles() throws ApiMismatchException {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java.utils;
18+
19+
import javax.annotation.CheckForNull;
20+
import javax.annotation.Nullable;
21+
import org.sonar.api.batch.bootstrap.ProjectDefinition;
22+
23+
public class ModuleMetadataUtils {
24+
25+
private ModuleMetadataUtils() {
26+
// utility class
27+
}
28+
29+
public static String getModuleKey(@Nullable ProjectDefinition projectDefinition) {
30+
var root = getRootProject(projectDefinition);
31+
if (root != null && projectDefinition != null) {
32+
var rootBase = root.getBaseDir().toPath();
33+
var moduleBase = projectDefinition.getBaseDir().toPath();
34+
return rootBase.relativize(moduleBase).toString().replace('\\', '/');
35+
}
36+
return "";
37+
}
38+
39+
@CheckForNull
40+
public static ProjectDefinition getRootProject(ProjectDefinition projectDefinition) {
41+
ProjectDefinition current = projectDefinition;
42+
if (current == null) {
43+
return null;
44+
}
45+
while (current.getParent() != null) {
46+
current = current.getParent();
47+
}
48+
return current;
49+
}
50+
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
@ParametersAreNonnullByDefault
18+
package org.sonar.java.utils;
19+
20+
import javax.annotation.ParametersAreNonnullByDefault;
21+

java-frontend/src/test/java/org/sonar/java/DefaultModuleMetadataTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
*/
1717
package org.sonar.java;
1818

19-
import java.io.File;
2019
import java.util.Optional;
2120
import org.junit.jupiter.api.Test;
22-
import org.sonar.api.batch.bootstrap.ProjectDefinition;
2321
import org.sonar.api.config.Configuration;
2422

2523
import static org.assertj.core.api.Assertions.assertThat;
2624
import static org.mockito.Mockito.doReturn;
2725
import static org.mockito.Mockito.mock;
26+
import static org.sonar.java.TestUtils.mockProjectDefinition;
2827

2928
class DefaultModuleMetadataTest {
3029

@@ -67,23 +66,13 @@ void testWithShouldIgnoreUnnamed() {
6766
assertThat(defaultModuleMetadata.shouldIgnoreUnnamedModuleForSplitPackage()).isTrue();
6867
}
6968

70-
private ProjectDefinition mockProjectDefinition() {
71-
var rootProj = mock(ProjectDefinition.class);
72-
doReturn(new File("/foo/bar/proj")).when(rootProj).getBaseDir();
73-
var childModule = mock(ProjectDefinition.class);
74-
doReturn(new File("/foo/bar/proj/pmodule/cmodule")).when(childModule).getBaseDir();
75-
doReturn(rootProj).when(childModule).getParent();
76-
77-
return childModule;
78-
}
79-
8069
private Configuration mockConfiguration(String... keysAndValues) {
8170
Configuration configuration = mock(Configuration.class);
8271
for (int i = 0; i < keysAndValues.length; i++) {
8372
String key = keysAndValues[i++];
8473
String value = keysAndValues[i];
8574
doReturn(Optional.of(value)).when(configuration).get(key);
86-
if(value.equals("true") || value.equals("false")) {
75+
if (value.equals("true") || value.equals("false")) {
8776
doReturn(Optional.of(Boolean.valueOf(value))).when(configuration).getBoolean(key);
8877
}
8978
}

java-frontend/src/test/java/org/sonar/java/TestUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import java.nio.file.Files;
2222
import java.util.List;
2323
import java.util.stream.Stream;
24+
import org.sonar.api.batch.bootstrap.ProjectDefinition;
2425
import org.sonar.api.batch.fs.InputFile;
2526
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
2627

2728
import static java.nio.charset.StandardCharsets.UTF_8;
2829
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import static org.mockito.Mockito.doReturn;
2931
import static org.mockito.Mockito.mock;
3032
import static org.mockito.Mockito.when;
3133

@@ -144,4 +146,15 @@ public static SonarComponents mockSonarComponents() {
144146
when(mock.jspChecks()).thenReturn(List.of());
145147
return mock;
146148
}
149+
150+
public static ProjectDefinition mockProjectDefinition() {
151+
var rootProj = mock(ProjectDefinition.class);
152+
doReturn(new File("/foo/bar/proj")).when(rootProj).getBaseDir();
153+
var childModule = mock(ProjectDefinition.class);
154+
doReturn(new File("/foo/bar/proj/pmodule/cmodule")).when(childModule).getBaseDir();
155+
doReturn(rootProj).when(childModule).getParent();
156+
157+
return childModule;
158+
}
159+
147160
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.sonar.java.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
7+
import static org.junit.jupiter.api.Assertions.assertSame;
8+
import static org.sonar.java.TestUtils.mockProjectDefinition;
9+
10+
class ModuleMetadataUtilsTest {
11+
12+
@Test
13+
void getModuleKey() {
14+
var projectDefinition = mockProjectDefinition();
15+
assertThat(ModuleMetadataUtils.getModuleKey(projectDefinition)).isEqualTo("pmodule/cmodule");
16+
assertThat(ModuleMetadataUtils.getModuleKey(null)).isEmpty();
17+
}
18+
19+
@Test
20+
void getRootProject() {
21+
var projectDefinition = mockProjectDefinition();
22+
assertSame(ModuleMetadataUtils.getRootProject(projectDefinition), ModuleMetadataUtils.getRootProject(projectDefinition.getParent()));
23+
assertNull(ModuleMetadataUtils.getRootProject(null));
24+
}
25+
}

0 commit comments

Comments
 (0)