Skip to content

Commit 94e8102

Browse files
committed
Fix test for incremental build of renamed properties class
Previously, the .class file for the renamed properties class was on the class path of the compilation in two places: 1. The output directory of the test's previous compilation 2. The output directory of the compilation of src/test/java of spring-boot-configuration-processor The first of these locations is addressed by updating TestProject. The .class file is now deleted from the project's output location at the same time as the .java file is deleted from its source location. The second of these locations is addressed by configuring the class path of the compiler to include a copy of the result of compiling src/test/java of spring-boot-configuration-processor. From this copy entries can then be deleted as needed without destabilizing other tests. Closes gh-26271
1 parent c999d98 commit 94e8102

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/IncrementalBuildMetadataGenerationTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.configurationprocessor;
1818

19-
import org.junit.jupiter.api.Disabled;
2019
import org.junit.jupiter.api.Test;
2120

2221
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
@@ -74,7 +73,6 @@ void incrementalBuildAnnotationRemoved() throws Exception {
7473
}
7574

7675
@Test
77-
@Disabled("gh-26271")
7876
void incrementalBuildTypeRenamed() throws Exception {
7977
TestProject project = new TestProject(this.tempDir, FooProperties.class, BarProperties.class);
8078
ConfigurationMetadata metadata = project.fullBuild();

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,15 +23,18 @@
2323
import java.io.InputStream;
2424
import java.io.InputStreamReader;
2525
import java.io.StringReader;
26+
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.HashSet;
2829
import java.util.LinkedHashSet;
30+
import java.util.List;
2931
import java.util.Set;
3032

3133
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
3234
import org.springframework.boot.configurationprocessor.test.TestConfigurationMetadataAnnotationProcessor;
3335
import org.springframework.boot.configurationsample.ConfigurationProperties;
3436
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
37+
import org.springframework.boot.testsupport.BuildOutput;
3538
import org.springframework.boot.testsupport.compiler.TestCompiler;
3639
import org.springframework.boot.testsupport.compiler.TestCompiler.TestCompilationTask;
3740
import org.springframework.util.Assert;
@@ -56,21 +59,41 @@ public class TestProject {
5659
* Contains copies of the original source so we can modify it safely to test
5760
* incremental builds.
5861
*/
59-
private File sourceDirectory;
62+
private final File sourceDirectory;
6063

61-
private TestCompiler compiler;
64+
private final TestCompiler compiler;
6265

63-
private Set<File> sourceFiles = new LinkedHashSet<>();
66+
private final Set<File> sourceFiles = new LinkedHashSet<>();
67+
68+
private final File classPathDirectory;
6469

6570
public TestProject(File tempDirectory, Class<?>... classes) throws IOException {
6671
this.sourceDirectory = new File(tempDirectory, "src");
67-
this.compiler = new TestCompiler(new File(tempDirectory, "build")) {
72+
File outputDirectory = new File(tempDirectory, "build");
73+
File testClasses = new BuildOutput(TestProject.class).getTestClassesLocation();
74+
this.classPathDirectory = new File(tempDirectory, "classPath");
75+
FileSystemUtils.copyRecursively(testClasses, this.classPathDirectory);
76+
this.compiler = new TestCompiler(outputDirectory) {
6877

6978
@Override
7079
protected File getSourceDirectory() {
7180
return TestProject.this.sourceDirectory;
7281
}
7382

83+
@Override
84+
protected Iterable<? extends File> prepareClassPath(Iterable<? extends File> classPath) {
85+
List<File> updatedClassPath = new ArrayList<>();
86+
for (File entry : classPath) {
87+
if (!entry.equals(testClasses)) {
88+
updatedClassPath.add(entry);
89+
}
90+
else {
91+
updatedClassPath.add(TestProject.this.classPathDirectory);
92+
}
93+
}
94+
return updatedClassPath;
95+
}
96+
7497
};
7598
Set<Class<?>> contents = new HashSet<>(Arrays.asList(classes));
7699
contents.addAll(Arrays.asList(ALWAYS_INCLUDE));
@@ -143,13 +166,17 @@ public void addSourceCode(Class<?> target, InputStream snippetStream) throws Exc
143166
}
144167

145168
/**
146-
* Delete source file for given class from project.
169+
* Delete from the project the source {@code .java} file and any compiled
170+
* {@code .class} file for the given class.
147171
* @param type the class to delete
148172
*/
149173
public void delete(Class<?> type) {
150174
File target = getSourceFile(type);
151175
target.delete();
152176
this.sourceFiles.remove(target);
177+
String fileName = type.getName().replace(".", "/") + ".class";
178+
new File(this.compiler.getOutputLocation(), fileName).delete();
179+
new File(this.classPathDirectory, fileName).delete();
153180
}
154181

155182
/**

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/compiler/TestCompiler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public TestCompiler(JavaCompiler compiler, File outputLocation) throws IOExcepti
6262
Iterable<? extends File> temp = Collections.singletonList(this.outputLocation);
6363
this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, temp);
6464
this.fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, temp);
65+
Iterable<? extends File> classPath = this.fileManager.getLocation(StandardLocation.CLASS_PATH);
66+
this.fileManager.setLocation(StandardLocation.CLASS_PATH, prepareClassPath(classPath));
6567
}
6668

6769
public TestCompilationTask getTask(Collection<File> sourceFiles) {
@@ -103,6 +105,10 @@ protected File getSourceDirectory() {
103105
return SOURCE_DIRECTORY;
104106
}
105107

108+
protected Iterable<? extends File> prepareClassPath(Iterable<? extends File> classPath) {
109+
return classPath;
110+
}
111+
106112
/**
107113
* A compilation task.
108114
*/

0 commit comments

Comments
 (0)