Skip to content

Commit

Permalink
Fix test for incremental build with type rename
Browse files Browse the repository at this point in the history
Closes gh-38119
  • Loading branch information
wilkinsona committed Nov 7, 2024
1 parent 0be9fd9 commit 03a3425
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-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,6 @@

package org.springframework.boot.configurationprocessor;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
Expand Down Expand Up @@ -74,7 +73,6 @@ void incrementalBuildAnnotationRemoved() {
}

@Test
@Disabled("gh-26271")
void incrementalBuildTypeRenamed() {
TestProject project = new TestProject(FooProperties.class, BarProperties.class);
ConfigurationMetadata metadata = project.compile();
Expand All @@ -85,7 +83,7 @@ void incrementalBuildTypeRenamed() {
assertThat(metadata).doesNotHave(Metadata.withProperty("bar.counter").fromSource(RenamedBarProperties.class));
project.delete(BarProperties.class);
project.add(RenamedBarProperties.class);
metadata = project.compile();
metadata = project.compile(metadata);
assertThat(metadata)
.has(Metadata.withProperty("foo.counter").fromSource(FooProperties.class).withDefaultValue(0));
assertThat(metadata)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-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,17 +16,22 @@

package org.springframework.boot.configurationprocessor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
import org.springframework.boot.configurationprocessor.test.CompiledMetadataReader;
import org.springframework.boot.configurationprocessor.test.TestConfigurationMetadataAnnotationProcessor;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
import org.springframework.core.test.tools.ResourceFile;
import org.springframework.core.test.tools.SourceFile;
import org.springframework.core.test.tools.SourceFiles;
import org.springframework.core.test.tools.TestCompiler;
Expand Down Expand Up @@ -55,14 +60,33 @@ public TestProject(Class<?>... classes) {
}

public ConfigurationMetadata compile() {
return compile(null);
}

public ConfigurationMetadata compile(ConfigurationMetadata previousMetadata) {
TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor();
TestCompiler compiler = TestCompiler.forSystem().withProcessors(processor);
if (previousMetadata != null) {
compiler = compiler.withResources(
ResourceFile.of("META-INF/spring-configuration-metadata.json", asBytes(previousMetadata)));
}
AtomicReference<ConfigurationMetadata> configurationMetadata = new AtomicReference<>();
compiler.compile(this.sources,
(compiled) -> configurationMetadata.set(CompiledMetadataReader.getMetadata(compiled)));
return configurationMetadata.get();
}

private byte[] asBytes(ConfigurationMetadata previousMetadata) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
new JsonMarshaller().write(previousMetadata, output);
}
catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return output.toByteArray();
}

/**
* Add source code at the end of file, just before last '}'
* @param target the target
Expand Down

0 comments on commit 03a3425

Please sign in to comment.