Skip to content

Commit 2e819c8

Browse files
committed
More Windows fixes
1 parent f506ef6 commit 2e819c8

File tree

53 files changed

+1135
-1152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1135
-1152
lines changed

components/openrewrite-spring-recipes/src/test/java/org/springframework/sbm/SpringBoot23To24MigrationTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void recipeUpdatesBootDependenciesAndParentVersion() throws IOException {
126126

127127
sut.apply(projectContext);
128128

129-
assertThat(expectedPom).isEqualTo(projectContext.getBuildFile().print());
129+
assertThat(expectedPom).isEqualToNormalizingNewlines(projectContext.getBuildFile().print());
130130

131131
SpringBootApplicationProperties applicationProperties = projectContext.search(new SpringBootApplicationPropertiesResourceListFilter()).get(0);
132132
assertThat(applicationProperties.hasChanges()).isFalse();
@@ -200,7 +200,7 @@ public void recipeJpaInitPropertyAdded() throws IOException {
200200
);
201201

202202
SpringBootApplicationProperties applicationProperties = projectContext.search(new SpringBootApplicationPropertiesResourceListFilter()).get(0);
203-
assertThat(applicationProperties.getProperty("spring.datasource.initialization-order").get()).isEqualTo("after-jpa");
203+
assertThat(applicationProperties.getProperty("spring.datasource.initialization-order")).hasValue("after-jpa");
204204

205205
verify(ui).askUserYesOrNo(InitDataSourceAfterJpaInitAction.QUESTION);
206206
verifyNoMoreInteractions(ui);
@@ -218,7 +218,7 @@ public void recipeJpaInitPropertyAddedForSchemaSql() throws IOException {
218218
// sut.apply(projectContext);
219219
//
220220
// SpringBootApplicationProperties applicationProperties = projectContext.getFilteredResources(new SpringBootApplicationPropertiesResourceListFilter()).get(0);
221-
// assertThat(applicationProperties.getProperty("spring.datasource.initialization-order").get()).isEqualTo("after-jpa");
221+
// assertThat(applicationProperties.getProperty("spring.datasource.initialization-order")).hasValue("after-jpa");
222222
//
223223
// verify(ui).askUserYesOrNo(InitDataSourceAfterJpaInitAction.QUESTION);
224224
// verifyNoMoreInteractions(ui);

components/recipe-test-support/src/main/java/org/springframework/sbm/test/RecipeIntegrationTestSupport.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.nio.file.Files;
3535
import java.nio.file.Path;
3636
import java.util.List;
37-
import java.util.stream.Collectors;
3837
import java.util.stream.Stream;
3938

4039
import static org.assertj.core.api.Assertions.assertThat;
@@ -50,6 +49,7 @@ public static Path getResultDir(String toDirName) {
5049
}
5150

5251
private static final String TARGET_DIR = "./target/testcode";
52+
private static final String TEMPLATES = "./src/main/resources/templates";
5353
@Setter(AccessLevel.PRIVATE)
5454
private Path rootDir;
5555
@Setter(AccessLevel.PRIVATE)
@@ -68,10 +68,10 @@ public static RecipeIntegrationTestSupport withTargetDir(Path targetDir) {
6868
public static RecipeIntegrationTestSupport initializeProject(Path sourceDir, String targetDirName) {
6969
try {
7070
Resource classPathResource = new FileSystemResource(sourceDir.toString());
71-
File to = getResultDir(targetDirName).toFile();
71+
File to = getResultDir(targetDirName).toFile().getCanonicalFile();
7272
FileUtils.deleteDirectory(to);
7373
FileUtils.forceMkdir(to);
74-
FileUtils.copyDirectory(classPathResource.getFile(), to);
74+
FileUtils.copyDirectory(classPathResource.getFile().getCanonicalFile(), to);
7575
RecipeIntegrationTestSupport recipeIntegrationTestSupport = new RecipeIntegrationTestSupport();
7676
recipeIntegrationTestSupport.setRootDir(to.toPath());
7777
recipeIntegrationTestSupport.setSourceDir(sourceDir);
@@ -91,9 +91,11 @@ public static RecipeIntegrationTestSupport initializeProject(String targetDirNam
9191

9292
public void andApplyRecipe(String recipeName) {
9393
SpringBeanProvider.run(ctx -> {
94-
if (new File("./src/main/resources/templates").exists()) {
95-
Configuration configuration = ctx.getBean("configuration", Configuration.class); // FIXME: two freemarker configurations exist
96-
configuration.setDirectoryForTemplateLoading(new File("./src/main/resources/templates"));
94+
Path templatesPath = Path.of(TEMPLATES).normalize().toAbsolutePath();
95+
if (Files.exists(templatesPath)) {
96+
// FIXME: two freemarker configurations exist
97+
Configuration configuration = ctx.getBean("configuration", Configuration.class);
98+
configuration.setDirectoryForTemplateLoading(templatesPath.toFile().getCanonicalFile());
9799
}
98100

99101
ScanCommand scanCommand = ctx.getBean(ScanCommand.class);
@@ -108,7 +110,7 @@ public void andApplyRecipe(String recipeName) {
108110
Recipe recipe = recipesFound.stream()
109111
.filter(r -> r.getName().equals(recipeName))
110112
.findFirst()
111-
.orElseThrow(() -> new RuntimeException("Could not find recipe '" + recipeName + "' in list of recipes found: " + recipesFound));
113+
.orElseThrow(() -> new RuntimeException("Could not find recipe '%s' in list of recipes found: %s".formatted(recipeName, recipesFound)));
112114
ApplyCommand applyCommand = ctx.getBean(ApplyCommand.class);
113115
applyCommand.execute(projectContext, recipe.getName());
114116
},
@@ -134,12 +136,12 @@ public void andApplyRecipeComparingWithExpected(String recipeName) {
134136
List<Path> result;
135137
try (Stream<Path> walk = Files.walk(expectedProject)) {
136138
result = walk.filter(Files::isRegularFile)
137-
.map(f -> expectedProject.relativize(f))
138-
.collect(Collectors.toList());
139+
.map(expectedProject::relativize)
140+
.toList();
139141
}
140142

141-
result.stream()
142-
.forEach(r -> assertThat(expectedProject.resolve(r)).hasSameTextualContentAs(migratedProject.resolve(r)));
143+
result.forEach(r -> assertThat(expectedProject.resolve(r).normalize().toAbsolutePath())
144+
.hasSameTextualContentAs(migratedProject.resolve(r).normalize().toAbsolutePath()));
143145
} catch (Exception exception) {
144146
throw new RuntimeException(exception);
145147
}

components/sbm-core/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@
172172
<artifactId>jackson-dataformat-xml</artifactId>
173173
<version>2.14.1</version>
174174
</dependency>
175+
<dependency>
176+
<groupId>org.openrewrite</groupId>
177+
<artifactId>rewrite-test</artifactId>
178+
<scope>test</scope>
179+
</dependency>
175180

176181
</dependencies>
177182
<build>

components/sbm-core/src/main/java/org/springframework/sbm/build/migration/actions/AddMinimalPomXml.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.sbm.engine.recipe.AbstractAction;
3131

3232
import java.io.ByteArrayInputStream;
33+
import java.io.File;
3334
import java.io.StringWriter;
3435
import java.nio.charset.StandardCharsets;
3536
import java.nio.file.Path;
@@ -59,7 +60,7 @@ public class AddMinimalPomXml extends AbstractAction {
5960
@Override
6061
public void apply(ProjectContext context) {
6162
String projectDir = context.getProjectRootDirectory().toString();
62-
String projectName = projectDir.replace(" ", "-").substring(projectDir.lastIndexOf("/") + 1);
63+
String projectName = projectDir.replace(" ", "-").substring(projectDir.lastIndexOf(File.separator) + 1);
6364
Map<String, String> params = new HashMap<>();
6465
params.put("groupId", "com.example.change");
6566
params.put("artifactId", projectName);

components/sbm-core/src/main/java/org/springframework/sbm/common/migration/actions/DocumentationExtraction.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.sbm.java.api.Member;
2424
import org.springframework.sbm.java.impl.OpenRewriteType;
2525

26+
import java.io.File;
2627
import java.nio.file.Path;
2728
import java.util.List;
2829

@@ -36,7 +37,7 @@ public void apply(ProjectContext context) {
3637
Path sourcePath = module.getBaseJavaSourceLocation().getSourceFolder();
3738
Path sourceDir = rootDirectory.relativize(sourcePath);
3839
module.getMainJavaSources().stream()
39-
.filter(js -> js.getResource().getAbsolutePath().endsWith(sourceDir.resolve(js.getPackageName().replace(".", "/") + "/" + js.getResource().getAbsolutePath().getFileName())))
40+
.filter(js -> filterPath(js, sourceDir))
4041
.map(JavaSource::getTypes)
4142
.flatMap(List::stream)
4243
.filter(t -> OpenRewriteType.class.isAssignableFrom(t.getClass()))
@@ -52,6 +53,12 @@ public void apply(ProjectContext context) {
5253

5354
}
5455

56+
private static boolean filterPath(JavaSource js, Path sourceDir) {
57+
String packageName = js.getPackageName().replace(".", File.separator);
58+
Path resolved = sourceDir.resolve(packageName).resolve(js.getResource().getAbsolutePath().getFileName());
59+
return js.getResource().getAbsolutePath().endsWith(resolved);
60+
}
61+
5562
private boolean filterActions(OpenRewriteType javaSource) {
5663
Class<?> aClass = getaClass(javaSource);
5764
return Action.class.isAssignableFrom(aClass);

components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class ProjectContext {
5454
private final RewriteMigrationResultMerger resultMerger;
5555

5656
public ProjectContext(JavaRefactoringFactory javaRefactoringFactory, Path projectRootDirectory, ProjectResourceSet projectResources, BasePackageCalculator basePackageCalculator, JavaParser javaParser, ExecutionContext executionContext, RewriteMigrationResultMerger resultMerger) {
57-
this.projectRootDirectory = projectRootDirectory.toAbsolutePath();
57+
this.projectRootDirectory = projectRootDirectory.normalize().toAbsolutePath();
5858
this.projectResources = projectResources;
5959
this.javaRefactoringFactory = javaRefactoringFactory;
6060
this.basePackageCalculator = basePackageCalculator;

components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ResourceParser.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.context.ApplicationEventPublisher;
3535
import org.springframework.core.io.Resource;
3636
import org.springframework.sbm.engine.events.StartedScanningProjectResourceEvent;
37+
import org.springframework.sbm.utils.LinuxWindowsPathUnifier;
3738
import org.springframework.stereotype.Component;
3839

3940
import java.io.IOException;
@@ -107,7 +108,7 @@ public List<SourceFile> parse(Path baseDir, List<Resource> relevantResources, Li
107108
parserAndParserInputMappings.put(plainTextParser, new ArrayList<>());
108109

109110
parserInputs.forEach(r -> {
110-
Parser parser = parserAndParserInputMappings.keySet().stream()
111+
Parser<?> parser = parserAndParserInputMappings.keySet().stream()
111112
.filter(p -> p.accept(r))
112113
.findFirst()
113114
.orElseThrow(() -> new RuntimeException("Could not find matching parser for " + r.getPath()));
@@ -141,15 +142,15 @@ private Stream<SourceFile> getSourceFileStream(Path baseDir, ExecutionContext ct
141142
.getValue()
142143
.stream()
143144
.map(resource -> (List<SourceFile>) parseSingleResource(baseDir, ctx, e, resource))
144-
.flatMap(elem -> Stream.ofNullable(elem))
145+
.flatMap(Stream::ofNullable)
145146
.flatMap(List::stream);
146147
}
147148

148149
private List<? extends SourceFile> parseSingleResource(Path baseDir, ExecutionContext ctx, Map.Entry<Parser<? extends SourceFile>, List<Parser.Input>> e, Parser.Input resource) {
149150
try {
150151
return e.getKey().parseInputs(List.of(resource), baseDir, ctx);
151152
} catch(Exception ex) {
152-
if(resource.getPath().toString().contains("src/test/resources")) {
153+
if (LinuxWindowsPathUnifier.unifyPath(resource.getPath()).contains("src/test/resources")) {
153154
log.error("Could not parse resource '%s' using parser %s. Exception was: %s".formatted(resource.getPath(), e.getKey().getClass().getName(), ex.getMessage()));
154155
return null;
155156
} else {

components/sbm-core/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSet.java

+2-49
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
import java.nio.file.Path;
2121
import java.util.ArrayList;
22-
import java.util.Iterator;
2322
import java.util.List;
24-
import java.util.stream.Collectors;
2523
import java.util.stream.Stream;
2624

2725
// TODO: make package private
@@ -65,63 +63,18 @@ public int size() {
6563
return projectResources.size();
6664
}
6765

68-
// /**
69-
// * @deprecated use {@link ProjectContext#getFilteredResources(ProjectResourcesFilter)}
70-
// * with {@link org.springframework.sbm.project.resource.filter.GenericTypeFilter}
71-
// */
72-
// @Deprecated(forRemoval = true)
73-
// public <T> List<RewriteSourceFileHolder<T>> getProjections(Class<T> projectionClass) {
74-
// return typeFilteredList(projectionClass);
75-
// }
76-
7766
public int indexOf(Path absolutePath) {
7867
return projectResources.stream()
7968
.map(ProjectResource::getAbsolutePath)
80-
.collect(Collectors.toList())
69+
.toList()
8170
.indexOf(absolutePath);
8271
}
8372

8473
void clearDeletedResources() {
85-
Iterator<RewriteSourceFileHolder<? extends SourceFile>> iterator = this.projectResources.iterator();
86-
while(iterator.hasNext()) {
87-
RewriteSourceFileHolder<? extends SourceFile> current = iterator.next();
88-
if(current.isDeleted()) {
89-
iterator.remove();
90-
}
91-
}
74+
this.projectResources.removeIf(BaseProjectResource::isDeleted);
9275
}
9376

9477
public Stream<RewriteSourceFileHolder<? extends SourceFile>> streamIncludingDeleted() {
9578
return projectResources.stream();
9679
}
97-
//
98-
// public Stream<ProjectResource> filteredStream(ProjectResourceFilter filter) {
99-
// return projectResources.stream()
100-
// .filter(filter::satisfies);
101-
// }
102-
103-
// public <T extends ProjectResource> Stream<T> classFilteredStream(Class<T> clazz) {
104-
// return filteredStream(pr -> clazz.isAssignableFrom(pr.getClass()))
105-
// .map(clazz::cast);
106-
// }
107-
//
108-
// public <T> Stream<RewriteSourceFileHolder<T>> typeFilteredStream(Class<T> type) {
109-
// return filteredStream(pr -> Objects.nonNull(pr.getType()) && type.isAssignableFrom(pr.getType()))
110-
// .map(pr -> (RewriteSourceFileHolder<T>)pr);
111-
// }
112-
//
113-
// public List<ProjectResource> filteredList(ProjectResourceFilter filter) {
114-
// return filteredStream(filter)
115-
// .collect(Collectors.toList());
116-
// }
117-
//
118-
// public <T extends ProjectResource> List<T> classFilteredList(Class<T> clazz) {
119-
// return classFilteredStream(clazz)
120-
// .collect(Collectors.toList());
121-
// }
122-
//
123-
// public <T> List<RewriteSourceFileHolder<T>> typeFilteredList(Class<T> type) {
124-
// return typeFilteredStream(type)
125-
// .collect(Collectors.toList());
126-
// }
12780
}

components/sbm-core/src/test/java/org/springframework/sbm/build/Issue54Test.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@
1515
*/
1616
package org.springframework.sbm.build;
1717

18-
import org.assertj.core.api.Assertions;
1918
import org.intellij.lang.annotations.Language;
2019
import org.junit.jupiter.api.Test;
2120
import org.openrewrite.maven.MavenParser;
22-
import org.openrewrite.maven.internal.RawPom;
2321
import org.openrewrite.maven.tree.Dependency;
2422
import org.openrewrite.maven.tree.MavenResolutionResult;
25-
import org.openrewrite.maven.tree.Pom;
2623
import org.openrewrite.xml.tree.Xml;
2724
import org.springframework.sbm.GitHubIssue;
2825
import org.springframework.sbm.engine.context.ProjectContext;
2926
import org.springframework.sbm.project.resource.TestProjectContext;
3027

31-
import java.io.ByteArrayInputStream;
3228
import java.util.List;
3329

3430
import static org.assertj.core.api.Assertions.assertThat;
@@ -83,9 +79,9 @@ void scanMultiModuleProjectWithOptionalPropertyProvidedByParent() {
8379

8480
List<Xml.Document> poms = MavenParser.builder().build().parse(pomA, pomB);
8581

86-
assertThat(
87-
poms.get(1).getMarkers().findFirst(MavenResolutionResult.class).get().getPom().getProperties().get("boolean-variable")
88-
).isEqualTo("false");
82+
assertThat(poms.get(1).getMarkers().findFirst(MavenResolutionResult.class))
83+
.hasValueSatisfying(mrr -> assertThat(mrr.getPom().getProperties()).containsEntry("boolean-variable", "false")
84+
);
8985

9086
List<Dependency> requestedDependencies = poms.get(1).getMarkers().findFirst(MavenResolutionResult.class).get().getPom().getRequestedDependencies();
9187

components/sbm-core/src/test/java/org/springframework/sbm/project/buildfile/OpenRewriteMavenBuildFileTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2134,9 +2134,9 @@ void deserializePluginConfiguration() {
21342134
plugin.getArtifactId().equals("maven-compiler-plugin"))
21352135
.findAny().orElseThrow();
21362136

2137-
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("source").get()).isEqualTo("${source}");
2138-
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("target").get()).isEqualTo("17");
2139-
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("fork").get()).isEqualTo("false");
2137+
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("source")).hasValue("${source}");
2138+
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("target")).hasValue("17");
2139+
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("fork")).hasValue("false");
21402140
}
21412141

21422142
@Test

components/sbm-core/src/test/java/org/springframework/sbm/project/parser/ForgivingParsingOfTestResourcesTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
2424
import org.springframework.sbm.project.resource.TestProjectContext;
2525
import org.springframework.sbm.test.TestProjectContextInfo;
26+
import org.springframework.sbm.utils.LinuxWindowsPathUnifier;
2627

2728
import java.util.List;
2829

@@ -53,9 +54,9 @@ void test_renameMe() {
5354
List<RewriteSourceFileHolder<? extends SourceFile>> parsedResources = context.getProjectResources().list();
5455
assertThat(parsedResources).hasSize(3);
5556
assertThat(parsedResources.get(0).getSourcePath().toString()).isEqualTo("pom.xml");
56-
assertThat(parsedResources.get(1).getSourcePath().toString()).isEqualTo("src/test/resources/one.yaml");
57+
assertThat(LinuxWindowsPathUnifier.unifyPath(parsedResources.get(1).getSourcePath())).isEqualTo("src/test/resources/one.yaml");
5758
// src/test/resources/error.yaml is ignored
58-
assertThat(parsedResources.get(2).getSourcePath().toString()).isEqualTo("src/test/resources/three.yaml");
59+
assertThat(LinuxWindowsPathUnifier.unifyPath(parsedResources.get(2).getSourcePath())).isEqualTo("src/test/resources/three.yaml");
5960
ParsingExecutionContextView contextView = ParsingExecutionContextView.view(projectContextInfo.executionContext());
6061
assertThat(contextView.getParseFailures()).hasSize(1);
6162
assertThat(contextView.getParseFailures().get(0).getText()).isEqualTo("""

components/sbm-core/src/test/java/org/springframework/sbm/project/parser/RewriteXmlParserTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void parse() {
4242
String xml = "<foo>content</foo>";
4343
RewriteSourceFileHolder<Xml.Document> parse = sut.parse(Path.of(".").toAbsolutePath(), Path.of("some.xml"), xml);
4444
assertThat(parse.getSourceFile().getRoot().getName()).isEqualTo("foo");
45-
assertThat(parse.getSourceFile().getRoot().getValue().get()).isEqualTo("content");
45+
assertThat(parse.getSourceFile().getRoot().getValue()).hasValue("content");
4646
}
4747

4848
@Test
@@ -51,7 +51,7 @@ void testParse() {
5151
List<RewriteSourceFileHolder<Xml.Document>> rewriteSourceFileHolders = sut.parse(List.of(file), Path.of("./testcode").toAbsolutePath().normalize(), new RewriteExecutionContext());
5252
RewriteSourceFileHolder<Xml.Document> sourceFileHolder = rewriteSourceFileHolders.get(0);
5353
assertThat(sourceFileHolder.getSourceFile().getRoot().getName()).isEqualTo("shiporder");
54-
assertThat(sourceFileHolder.getSourceFile().getRoot().getChild("orderperson").get().getValue().get()).isEqualTo("John Smith");
54+
assertThat(sourceFileHolder.getSourceFile().getRoot().getChild("orderperson").get().getValue()).hasValue("John Smith");
5555
}
5656

5757
@Test

0 commit comments

Comments
 (0)