Skip to content

Commit 606ea49

Browse files
committed
Add a test to cover the merging of an additional metadata file
This test covers the code path that caused spring-projectsgh-2361 and also checks that, when an additional metadata file is found, it’s correctly merged with the other metadata. Closes spring-projectsgh-2361
1 parent 65acaf8 commit 606ea49

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

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

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -16,16 +16,23 @@
1616

1717
package org.springframework.boot.configurationprocessor;
1818

19+
import java.io.File;
20+
import java.io.FileInputStream;
21+
import java.io.FileWriter;
1922
import java.io.IOException;
2023

2124
import javax.annotation.processing.SupportedAnnotationTypes;
2225
import javax.annotation.processing.SupportedSourceVersion;
2326
import javax.lang.model.SourceVersion;
2427

28+
import org.json.JSONArray;
29+
import org.json.JSONObject;
30+
import org.junit.Before;
2531
import org.junit.Rule;
2632
import org.junit.Test;
2733
import org.junit.rules.TemporaryFolder;
2834
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
35+
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
2936
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
3037
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
3138
import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties;
@@ -59,12 +66,20 @@
5966
*
6067
* @author Stephane Nicoll
6168
* @author Phillip Webb
69+
* @author Andy Wilkinson
6270
*/
6371
public class ConfigurationMetadataAnnotationProcessorTests {
6472

6573
@Rule
6674
public TemporaryFolder temporaryFolder = new TemporaryFolder();
6775

76+
private TestCompiler compiler;
77+
78+
@Before
79+
public void createCompiler() throws IOException {
80+
this.compiler = new TestCompiler(this.temporaryFolder);
81+
}
82+
6883
@Test
6984
public void notAnnotated() throws Exception {
7085
ConfigurationMetadata metadata = compile(NotAnnotated.class);
@@ -307,6 +322,36 @@ public void lombokExplicitProperties() throws Exception {
307322
assertSimpleLombokProperties(metadata, LombokExplicitProperties.class, "explicit");
308323
}
309324

325+
@Test
326+
public void mergingOfAdditionalMetadata() throws Exception {
327+
File metaInfFolder = new File(this.compiler.getOutputLocation(), "META-INF");
328+
metaInfFolder.mkdirs();
329+
File additionalMetadataFile = new File(metaInfFolder,
330+
"additional-spring-configuration-metadata.json");
331+
additionalMetadataFile.createNewFile();
332+
333+
JSONObject property = new JSONObject();
334+
property.put("name", "foo");
335+
property.put("type", "java.lang.String");
336+
property.put("sourceType", AdditionalMetadata.class.getName());
337+
JSONArray properties = new JSONArray();
338+
properties.put(property);
339+
JSONObject additionalMetadata = new JSONObject();
340+
additionalMetadata.put("properties", properties);
341+
FileWriter writer = new FileWriter(additionalMetadataFile);
342+
additionalMetadata.write(writer);
343+
writer.flush();
344+
345+
ConfigurationMetadata metadata = compile(SimpleProperties.class);
346+
347+
assertThat(metadata, containsProperty("simple.comparator"));
348+
349+
assertThat(metadata,
350+
containsProperty("foo", String.class)
351+
.fromSource(AdditionalMetadata.class));
352+
353+
}
354+
310355
private void assertSimpleLombokProperties(ConfigurationMetadata metadata,
311356
Class<?> source, String prefix) {
312357
assertThat(metadata, containsGroup(prefix).fromSource(source));
@@ -324,13 +369,13 @@ private void assertSimpleLombokProperties(ConfigurationMetadata metadata,
324369

325370
private ConfigurationMetadata compile(Class<?>... types) throws IOException {
326371
TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor();
327-
new TestCompiler(this.temporaryFolder).getTask(types).call(processor);
372+
this.compiler.getTask(types).call(processor);
328373
return processor.getMetadata();
329374
}
330375

331376
@SupportedAnnotationTypes({ "*" })
332377
@SupportedSourceVersion(SourceVersion.RELEASE_6)
333-
private static class TestConfigurationMetadataAnnotationProcessor extends
378+
private class TestConfigurationMetadataAnnotationProcessor extends
334379
ConfigurationMetadataAnnotationProcessor {
335380

336381
static final String CONFIGURATION_PROPERTIES_ANNOTATION = "org.springframework.boot.configurationsample.ConfigurationProperties";
@@ -351,7 +396,23 @@ protected String nestedConfigurationPropertyAnnotation() {
351396

352397
@Override
353398
protected void writeMetaData(ConfigurationMetadata metadata) {
354-
this.metadata = metadata;
399+
super.writeMetaData(metadata);
400+
try {
401+
File metadataFile = new File(
402+
ConfigurationMetadataAnnotationProcessorTests.this.compiler
403+
.getOutputLocation(),
404+
"META-INF/spring-configuration-metadata.json");
405+
if (metadataFile.isFile()) {
406+
this.metadata = new JsonMarshaller().read(new FileInputStream(
407+
metadataFile));
408+
}
409+
else {
410+
this.metadata = metadata;
411+
}
412+
}
413+
catch (IOException e) {
414+
throw new RuntimeException("Failed to read metadata from disk", e);
415+
}
355416
}
356417

357418
public ConfigurationMetadata getMetadata() {
@@ -360,4 +421,8 @@ public ConfigurationMetadata getMetadata() {
360421

361422
}
362423

424+
private static class AdditionalMetadata {
425+
426+
}
427+
363428
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -35,13 +35,16 @@
3535
*
3636
* @author Stephane Nicoll
3737
* @author Phillip Webb
38+
* @author Andy Wilkinson
3839
*/
3940
public class TestCompiler {
4041

4142
private final JavaCompiler compiler;
4243

4344
private final StandardJavaFileManager fileManager;
4445

46+
private final File outputLocation;
47+
4548
public TestCompiler(TemporaryFolder temporaryFolder) throws IOException {
4649
this(ToolProvider.getSystemJavaCompiler(), temporaryFolder);
4750
}
@@ -50,7 +53,8 @@ public TestCompiler(JavaCompiler compiler, TemporaryFolder temporaryFolder)
5053
throws IOException {
5154
this.compiler = compiler;
5255
this.fileManager = compiler.getStandardFileManager(null, null, null);
53-
Iterable<? extends File> temp = Arrays.asList(temporaryFolder.newFolder());
56+
this.outputLocation = temporaryFolder.newFolder();
57+
Iterable<? extends File> temp = Arrays.asList(this.outputLocation);
5458
this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, temp);
5559
this.fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, temp);
5660
}
@@ -61,6 +65,10 @@ public TestCompilationTask getTask(Class<?>... types) {
6165
null, null, null, javaFileObjects));
6266
}
6367

68+
public File getOutputLocation() {
69+
return this.outputLocation;
70+
}
71+
6472
private Iterable<? extends JavaFileObject> getJavaFileObjects(Class<?>... types) {
6573
File[] files = new File[types.length];
6674
for (int i = 0; i < types.length; i++) {

0 commit comments

Comments
 (0)