Skip to content

Fix calculation of base package #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,52 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
* Calculates and returns the base package when given a list of JavaSource
*
* @author Soumya Prakash Behera
*/
@Component
@RequiredArgsConstructor
public class BasePackageCalculator {

private final SbmApplicationProperties sbmApplicationProperties;

public String calculateBasePackage(List<JavaSource> javaSources) {
if(javaSources.isEmpty()) return sbmApplicationProperties.getDefaultBasePackage();
if(javaSources.isEmpty()) {
return sbmApplicationProperties.getDefaultBasePackage();
}

if(javaSources.size() == 1) {
return javaSources.get(0).getPackageName();
}

List<String[]> javaSourcesAsStringArray = new ArrayList<>();

for(JavaSource javaSource : javaSources) {
javaSourcesAsStringArray.add(javaSource.getPackageName().split("\\."));
}

javaSourcesAsStringArray.sort(Comparator.comparingInt(str -> str.length));

List<JavaSource> sortableJavaSources = new ArrayList<>();
sortableJavaSources.addAll(javaSources);
javaSources = sortableJavaSources;
javaSources.sort(Comparator.comparing(js -> js.getPackageName().split("\\.").length));
JavaSource javaSourceInBasePackage = javaSources.get(0);
if(javaSources.size() > 1) {
JavaSource shortestPackage = javaSourceInBasePackage;
String shortestPackageName = shortestPackage.getPackageName();
String[] shortestPackage = javaSourcesAsStringArray.get(0);

Optional<JavaSource> javaSourceInDifferentBasePackage = javaSources.stream()
.filter(js -> !js.getPackageName().startsWith(shortestPackageName))
.findFirst();
StringBuilder sb = new StringBuilder();

if(javaSourceInDifferentBasePackage.isPresent()) {
return sbmApplicationProperties.getDefaultBasePackage();
/*
throw new RuntimeException(String.format("Could not calculate base package. Found at least two conflicting candidates: [%s] and [%s] found in these resources [%s] and [%s]",
javaSources.get(0).getPackageName(),
javaSourceInDifferentBasePackage.get().getPackageName(),
javaSources.get(0).getAbsolutePath(),
javaSourceInDifferentBasePackage.get().getAbsolutePath()));
*/
for(int i = 0; i < shortestPackage.length; i++) {
for (String[] strArray : javaSourcesAsStringArray) {
if(!strArray[i].equals(shortestPackage[i])) {
return sb.isEmpty() ? "" : sb.substring(1);
}
}
sb.append(".").append(shortestPackage[i]);
}

return javaSourceInBasePackage.getPackageName();
return String.join(".", shortestPackage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.sbm.build.impl;

import org.springframework.sbm.build.api.JavaSourceSet;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.java.api.JavaSource;
import org.springframework.sbm.java.api.JavaSourceLocation;
import org.springframework.sbm.project.resource.SbmApplicationProperties;
Expand All @@ -29,6 +30,25 @@

class JavaSourceSetImplTest {

@Test
void getBasePackageShouldUseDefaultPackageApplicationPropertyWhenNoSourcesExist() {
SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
sbmApplicationProperties.setDefaultBasePackage("some.default.packagename");

ProjectContext context = TestProjectContext.buildProjectContext()
.withApplicationProperties(sbmApplicationProperties)
.build();

String packageName = context
.getApplicationModules()
.getRootModule()
.getMainJavaSourceSet()
.getJavaSourceLocation()
.getPackageName();

assertThat(packageName).isEqualTo("some.default.packagename");
}

@Test
void testGetBasePackageShouldReturnDistinctRootPackageIfExists() {
final String sourceCode1 =
Expand All @@ -53,32 +73,28 @@ void testGetBasePackageShouldReturnDistinctRootPackageIfExists() {
}

@Test
void getBasePackageShouldReturnDefaultIfNoDistinctRootPackageExists() {
void getBasePackageShouldReturnEmptyPackageIfNoOtherCommonBasePackageExists() {
final String sourceCode1 =
"package org.springframework.sbm.level1; " +
"package org1.springframework.sbm.level1; " +
"public class Class1 { " +
"} " +
"";
final String sourceCode2 =
"package org.springframework.sbm.anotherLevel1; " +
"package org2.springframework.sbm.anotherLevel1; " +
"public class Class2 { " +
"} " +
"";

SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
sbmApplicationProperties.setDefaultBasePackage("org.springframework.sbm");

JavaSourceSet sut = TestProjectContext.buildProjectContext()
.withDummyRootBuildFile()
.withApplicationProperties(sbmApplicationProperties)
.withJavaSources(sourceCode1, sourceCode2)
.build()
.getApplicationModules()
.getRootModule()
.getMainJavaSourceSet();

JavaSourceLocation location = sut.getJavaSourceLocation();
assertThat(location.getPackageName()).isEqualTo(sbmApplicationProperties.getDefaultBasePackage());
assertThat(location.getPackageName()).isEqualTo("");
assertThat(location.getSourceFolder()).isEqualTo(TestProjectContext.getDefaultProjectRoot().resolve("src/main/java"));
}

Expand All @@ -104,7 +120,7 @@ void testGetBasePackageShouldConsiderLevelNotLength() {
.getMainJavaSourceSet();

JavaSourceLocation location = sut.getJavaSourceLocation();
assertThat(location.getPackageName()).isEqualTo(sbmApplicationProperties.getDefaultBasePackage());
assertThat(location.getPackageName()).isEqualTo("org.springframework");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void calculateBasePackageWithTwoAmbiguousPackages() {

List<JavaSource> javaSources = getJavaSources(package1, package2, package3);

assertThat(sut.calculateBasePackage(javaSources)).isEqualTo(DEFAULT_PACKAGENAME);
assertThat(sut.calculateBasePackage(javaSources)).isEqualTo("");
}

@Test
Expand All @@ -83,10 +83,29 @@ void calculateBasePackageWithOneBasePackage2() {
String package1 = "com.example.foo";
String package2 = "com.example.foo";
String package3 = "com.example.bar";

List<JavaSource> javaSources = getJavaSources(package1, package2, package3);

assertThat(sut.calculateBasePackage(javaSources)).isEqualTo(DEFAULT_PACKAGENAME);
assertThat(sut.calculateBasePackage(javaSources)).isEqualTo("com.example");
}

@Test
void calculateBasePackageWithOneBasePackage3() {
String package1 = "com.acme.some.A.C";
String package2 = "com.acme.other.B";

List<JavaSource> javaSources = getJavaSources(package1, package2);

assertThat(sut.calculateBasePackage(javaSources)).isEqualTo("com.acme");
}

@Test
void calculateBasePackageWithSameBasePackages() {
String package1 = "com.acme.some.B";
String package2 = "com.acme.some.B";

List<JavaSource> javaSources = getJavaSources(package1, package2);

assertThat(sut.calculateBasePackage(javaSources)).isEqualTo("com.acme.some.B");
}

@Test
Expand All @@ -104,7 +123,7 @@ void calculateBasePackageWithAmbiguousBasePackage() {
List<JavaSource> javaSources = getJavaSources(package1, package2, package3);

Path curDir = Path.of(".").toAbsolutePath().normalize();
assertThat(sut.calculateBasePackage(javaSources)).isEqualTo(DEFAULT_PACKAGENAME);
assertThat(sut.calculateBasePackage(javaSources)).isEqualTo("com.example");
}

@NotNull
Expand Down