|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2023 the original author or authors. |
| 2 | + * Copyright 2012-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.configurationprocessor;
|
18 | 18 |
|
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
19 | 21 | import java.io.InputStream;
|
20 | 22 | import java.io.InputStreamReader;
|
| 23 | +import java.io.UncheckedIOException; |
21 | 24 | import java.util.Arrays;
|
22 | 25 | import java.util.List;
|
23 | 26 | import java.util.concurrent.atomic.AtomicReference;
|
24 | 27 |
|
25 | 28 | import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
|
| 29 | +import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller; |
26 | 30 | import org.springframework.boot.configurationprocessor.test.CompiledMetadataReader;
|
27 | 31 | import org.springframework.boot.configurationprocessor.test.TestConfigurationMetadataAnnotationProcessor;
|
28 | 32 | import org.springframework.boot.configurationsample.ConfigurationProperties;
|
29 | 33 | import org.springframework.boot.configurationsample.NestedConfigurationProperty;
|
| 34 | +import org.springframework.core.test.tools.ResourceFile; |
30 | 35 | import org.springframework.core.test.tools.SourceFile;
|
31 | 36 | import org.springframework.core.test.tools.SourceFiles;
|
32 | 37 | import org.springframework.core.test.tools.TestCompiler;
|
@@ -55,14 +60,33 @@ public TestProject(Class<?>... classes) {
|
55 | 60 | }
|
56 | 61 |
|
57 | 62 | public ConfigurationMetadata compile() {
|
| 63 | + return compile(null); |
| 64 | + } |
| 65 | + |
| 66 | + public ConfigurationMetadata compile(ConfigurationMetadata previousMetadata) { |
58 | 67 | TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor();
|
59 | 68 | TestCompiler compiler = TestCompiler.forSystem().withProcessors(processor);
|
| 69 | + if (previousMetadata != null) { |
| 70 | + compiler = compiler.withResources( |
| 71 | + ResourceFile.of("META-INF/spring-configuration-metadata.json", asBytes(previousMetadata))); |
| 72 | + } |
60 | 73 | AtomicReference<ConfigurationMetadata> configurationMetadata = new AtomicReference<>();
|
61 | 74 | compiler.compile(this.sources,
|
62 | 75 | (compiled) -> configurationMetadata.set(CompiledMetadataReader.getMetadata(compiled)));
|
63 | 76 | return configurationMetadata.get();
|
64 | 77 | }
|
65 | 78 |
|
| 79 | + private byte[] asBytes(ConfigurationMetadata previousMetadata) { |
| 80 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 81 | + try { |
| 82 | + new JsonMarshaller().write(previousMetadata, output); |
| 83 | + } |
| 84 | + catch (IOException ex) { |
| 85 | + throw new UncheckedIOException(ex); |
| 86 | + } |
| 87 | + return output.toByteArray(); |
| 88 | + } |
| 89 | + |
66 | 90 | /**
|
67 | 91 | * Add source code at the end of file, just before last '}'
|
68 | 92 | * @param target the target
|
|
0 commit comments