Skip to content
Permalink
Browse files

spring-projectsgh-2313: regression tests for incremental builds

  • Loading branch information...
kdvolder committed Jan 9, 2015
1 parent 103acb1 commit 9c97f72e8eaa08574ae4a894eae7d89db08962b4
@@ -24,7 +24,13 @@
<artifactId>json</artifactId>
</dependency>
<!-- Test -->
<dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
@@ -70,6 +70,8 @@
@SupportedAnnotationTypes({ "*" })
public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor {

public static final String METADATA_PATH = "META-INF/spring-configuration-metadata.json";

static final String CONFIGURATION_PROPERTIES_ANNOTATION = "org.springframework.boot."
+ "context.properties.ConfigurationProperties";

@@ -97,7 +99,7 @@
* On incremental builds, keeps track of the types that where presented to
* the processor. This includes types that are not annotated.
*/
private Set<String> processedSourceTypes;
protected Set<String> processedSourceTypes;

private TypeUtils typeUtils;

@@ -373,7 +375,7 @@ private String getPrefix(AnnotationMirror annotation) {
return values;
}

protected void writeMetaData(ConfigurationMetadata metadata) {
protected ConfigurationMetadata writeMetaData(ConfigurationMetadata metadata) {
metadata = mergeAdditionalMetadata(metadata);
if (isIncremental()) {
mergeOldMetadata(metadata);
@@ -382,10 +384,11 @@ protected void writeMetaData(ConfigurationMetadata metadata) {
try {
FileObject resource = this.processingEnv.getFiler().createResource(
StandardLocation.CLASS_OUTPUT, "",
"META-INF/spring-configuration-metadata.json");
METADATA_PATH);
OutputStream outputStream = resource.openOutputStream();
try {
new JsonMarshaller().write(metadata, outputStream);
return metadata;
}
finally {
outputStream.close();
@@ -395,13 +398,14 @@ protected void writeMetaData(ConfigurationMetadata metadata) {
throw new IllegalStateException(ex);
}
}
return null;
}

protected ConfigurationMetadata readMetadata() {
try {
FileObject resource = this.processingEnv.getFiler().getResource(
StandardLocation.CLASS_OUTPUT, "",
"META-INF/spring-configuration-metadata.json");
METADATA_PATH);
InputStream in = resource.openInputStream();
try {
ConfigurationMetadata data = new ConfigurationMetadata();
@@ -0,0 +1,44 @@
/*
* Copyright 2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;

import java.util.Set;

import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;

public class BuildResult {

public final ConfigurationMetadata metadata;
public final Set<String> processedTypes;
public final boolean isIncremental;

public BuildResult(boolean isIncremental, ConfigurationMetadata metadata,
Set<String> processedTypes) {
this.isIncremental = isIncremental;
this.metadata = metadata;
this.processedTypes = processedTypes;
}

public BuildResult(TestConfigurationMetadataAnnotationProcessor processor) {
this(
processor.isIncremental(),
processor.getMetadata(),
processor.processedSourceTypes
);
}


}
@@ -16,16 +16,27 @@

package org.springframework.boot.configurationprocessor;

import java.io.IOException;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor.METADATA_PATH;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsGroup;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsProperty;

import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import java.io.IOException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.incremental.BarProperties;
import org.springframework.boot.configurationsample.incremental.FooProperties;
import org.springframework.boot.configurationsample.incremental.RenamedBarProperties;
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties;
@@ -46,30 +57,124 @@
import org.springframework.boot.configurationsample.specific.InnerClassRootConfig;
import org.springframework.boot.configurationsample.specific.SimplePojo;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsGroup;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsProperty;

/**
* Tests for {@link ConfigurationMetadataAnnotationProcessor}.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Kris De Volder
*/
public class ConfigurationMetadataAnnotationProcessorTests {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();


@Test
public void incrementalBuild() throws Exception {
TestProject project = new TestProject(temporaryFolder,
FooProperties.class,
BarProperties.class
);
assertFalse(project.getOutputFile(METADATA_PATH).exists());

BuildResult r = project.fullBuild();
assertFalse(r.isIncremental);
assertTrue(project.getOutputFile(METADATA_PATH).exists());

assertThat(r.metadata, containsProperty("foo.counter")
.fromSource(FooProperties.class));
assertThat(r.metadata, containsProperty("bar.counter")
.fromSource(BarProperties.class));

r = project.incrementalBuild(BarProperties.class);
assertTrue(r.isIncremental);
assertTrue(r.processedTypes.contains(BarProperties.class.getName()));
assertFalse(r.processedTypes.contains(FooProperties.class.getName()));

assertThat(r.metadata, containsProperty("foo.counter")
.fromSource(FooProperties.class));
assertThat(r.metadata, containsProperty("bar.counter")
.fromSource(BarProperties.class));

assertTrue(r.processedTypes.contains(BarProperties.class.getName()));
assertFalse(r.processedTypes.contains(FooProperties.class.getName()));

project.addSourceCode(BarProperties.class,
" private String extra;\n" +
" \n" +
" public String getExtra() {\n" +
" return extra;\n" +
" }\n" +
"\n" +
" public void setExtra(String extra) {\n" +
" this.extra = extra;\n" +
" }\n"
);
r = project.incrementalBuild(BarProperties.class);
assertTrue(r.isIncremental);
assertThat(r.metadata, containsProperty("bar.extra"));
assertThat(r.metadata, containsProperty("foo.counter"));
assertThat(r.metadata, containsProperty("bar.counter"));

project.revert(BarProperties.class);
r = project.incrementalBuild(BarProperties.class);
assertTrue(r.isIncremental);
assertThat(r.metadata, not(containsProperty("bar.extra")));
assertThat(r.metadata, containsProperty("foo.counter"));
assertThat(r.metadata, containsProperty("bar.counter"));
}

@Test
public void incremenalBuildAnnotationRemoved() throws Exception {
TestProject project = new TestProject(temporaryFolder,
FooProperties.class,
BarProperties.class
);
BuildResult r = project.fullBuild();
assertThat(r.metadata, containsProperty("foo.counter"));
assertThat(r.metadata, containsProperty("bar.counter"));

project.replaceText(BarProperties.class,
"@ConfigurationProperties",
"//@ConfigurationProperties"
);
r=project.incrementalBuild(BarProperties.class);
assertThat(r.metadata, containsProperty("foo.counter"));
assertThat(r.metadata, not(containsProperty("bar.counter")));
}

@Test
public void incremenalBuildTypeRenamed() throws Exception {
TestProject project = new TestProject(temporaryFolder,
FooProperties.class,
BarProperties.class
);
BuildResult r = project.fullBuild();
assertThat(r.metadata, containsProperty("foo.counter")
.fromSource(FooProperties.class));
assertThat(r.metadata, containsProperty("bar.counter")
.fromSource(BarProperties.class));
assertThat(r.metadata, not(containsProperty("bar.counter")
.fromSource(RenamedBarProperties.class)));

project.delete(BarProperties.class);
project.add(RenamedBarProperties.class);
r=project.incrementalBuild(RenamedBarProperties.class);
assertThat(r.metadata, containsProperty("foo.counter")
.fromSource(FooProperties.class));
assertThat(r.metadata, not(containsProperty("bar.counter")
.fromSource(BarProperties.class)));
assertThat(r.metadata, containsProperty("bar.counter")
.fromSource(RenamedBarProperties.class));


}

@Test
public void notAnnotated() throws Exception {
ConfigurationMetadata metadata = compile(NotAnnotated.class);
assertThat("No config metadata file should have been generated when "
+ "no metadata is discovered", metadata.getItems(), empty());
assertNull("No config metadata file should have been generated when "
+ "no metadata is discovered", metadata);
}

@Test
@@ -272,6 +377,8 @@ public void innerClassAnnotatedGetterConfig() throws Exception {
assertThat(metadata, not(containsProperty("specific.foo")));
}



@Test
public void builderPojo() throws IOException {
ConfigurationMetadata metadata = compile(BuilderPojo.class);
@@ -328,36 +435,4 @@ private ConfigurationMetadata compile(Class<?>... types) throws IOException {
return processor.getMetadata();
}

@SupportedAnnotationTypes({ "*" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
private static class TestConfigurationMetadataAnnotationProcessor extends
ConfigurationMetadataAnnotationProcessor {

static final String CONFIGURATION_PROPERTIES_ANNOTATION = "org.springframework.boot.configurationsample.ConfigurationProperties";

static final String NESTED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.NestedConfigurationProperty";

private ConfigurationMetadata metadata;

@Override
protected String configurationPropertiesAnnotation() {
return CONFIGURATION_PROPERTIES_ANNOTATION;
}

@Override
protected String nestedConfigurationPropertyAnnotation() {
return NESTED_CONFIGURATION_PROPERTY_ANNOTATION;
}

@Override
protected void writeMetaData(ConfigurationMetadata metadata) {
this.metadata = metadata;
}

public ConfigurationMetadata getMetadata() {
return this.metadata;
}

}

}

0 comments on commit 9c97f72

Please sign in to comment.
You can’t perform that action at this time.