Skip to content

Prefer modified resources over the originals in TestCompiler #33850

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

Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -121,14 +121,14 @@ protected URL findResource(String name) {
return createResourceUrl(name, () -> classBytes);
}
}
ResourceFile resourceFile = this.resourceFiles.get(name);
if (resourceFile != null) {
return createResourceUrl(resourceFile.getPath(), resourceFile::getBytes);
}
DynamicResourceFileObject dynamicResourceFile = this.dynamicResourceFiles.get(name);
if (dynamicResourceFile != null && dynamicResourceFile.getBytes() != null) {
return createResourceUrl(dynamicResourceFile.getName(), dynamicResourceFile::getBytes);
}
ResourceFile resourceFile = this.resourceFiles.get(name);
if (resourceFile != null) {
return createResourceUrl(resourceFile.getPath(), resourceFile::getBytes);
}
return super.findResource(name);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -70,13 +70,12 @@ public ClassLoader getClassLoader(Location location) {
}

@Override
public FileObject getFileForOutput(Location location, String packageName,
String relativeName, FileObject sibling) {
ResourceFile resourceFile = this.resourceFiles.get(relativeName);
if (resourceFile != null) {
return new DynamicResourceFileObject(relativeName, resourceFile.getContent());
}
return this.dynamicResourceFiles.computeIfAbsent(relativeName, DynamicResourceFileObject::new);
public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) {
return this.dynamicResourceFiles.computeIfAbsent(relativeName, name -> {
ResourceFile resourceFile = this.resourceFiles.get(name);
return (resourceFile != null) ? new DynamicResourceFileObject(name, resourceFile.getContent())
: new DynamicResourceFileObject(name);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package org.springframework.core.test.tools;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.EnumSet;

Expand Down Expand Up @@ -154,6 +155,23 @@ void getFileForOutputReturnsFile() throws Exception {
"META-INF/second.properties");
}

@Test
void existingResourceFileCanBeUpdated() throws IOException {
try (InputStream input = getResourceOne().openInputStream()) {
assertThat(input).hasContent("a");
}
try (OutputStream output = getResourceOne().openOutputStream()) {
output.write('b');
}
try (InputStream input = getResourceOne().openInputStream()) {
assertThat(input).hasContent("b");
}
}

private FileObject getResourceOne() {
return this.fileManager.getFileForOutput(this.location, "", "com/example/one/resource.one", null);
}

private void writeDummyBytecode(JavaFileObject fileObject) throws IOException {
try (OutputStream outputStream = fileObject.openOutputStream()) {
StreamUtils.copy(DUMMY_BYTECODE, outputStream);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,10 @@

package org.springframework.core.test.tools;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand All @@ -27,6 +30,8 @@
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

import com.example.PublicInterface;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -367,6 +372,14 @@ void getResourceForCompiledBytecode() {
});
}

@Test
void getUpdatedResourceAsStream() {
SourceFile sourceFile = SourceFile.of(HELLO_WORLD);
TestCompiler.forSystem().withResources(ResourceFile.of("com/example/resource", new byte[] { 'a' }))
.withProcessors(new ResourceModifyingProcessor()).compile(sourceFile, compiled -> assertThat(
compiled.getClassLoader().getResourceAsStream("com/example/resource")).hasContent("b"));
}

private void assertSuppliesHelloWorld(Compiled compiled) {
assertThat(compiled.getInstance(Supplier.class).get()).isEqualTo("Hello World!");
}
Expand All @@ -392,4 +405,26 @@ public List<TypeElement> getProcessedAnnotations() {
}
}

@SupportedAnnotationTypes("java.lang.Deprecated")
static class ResourceModifyingProcessor extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
try {
FileObject resource = this.processingEnv.getFiler()
.createResource(StandardLocation.CLASS_OUTPUT, "", "com/example/resource");
try (OutputStream output = resource.openOutputStream()) {
output.write('b');
}
}
catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
return true;
}

}

}