diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/ActuatorConfigurationClassTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/ActuatorConfigurationClassTests.java new file mode 100644 index 0000000000..bc0a606a6d --- /dev/null +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/ActuatorConfigurationClassTests.java @@ -0,0 +1,27 @@ +/* + * Copyright 2012-2014 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.actuate; + +import org.springframework.boot.test.AbstractConfigurationClassTests; + +/** + * Tests for the actuator module's @Configuration classes + * @author Andy Wilkinson + */ +public class ActuatorConfigurationClassTests extends AbstractConfigurationClassTests { + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java index d8642c8f0b..24a436000f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java @@ -101,7 +101,7 @@ public class JacksonAutoConfiguration { @Bean @ConditionalOnMissingBean - JodaModule jacksonJodaModule() { + public JodaModule jacksonJodaModule() { return new JodaModule(); } @@ -114,7 +114,7 @@ public class JacksonAutoConfiguration { @Bean @ConditionalOnMissingBean - JSR310Module jacksonJsr310Module() { + public JSR310Module jacksonJsr310Module() { return new JSR310Module(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java index 2151dff4d1..1a0438dab2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java @@ -58,7 +58,8 @@ public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean - RedisConnectionFactory redisConnectionFactory() throws UnknownHostException { + public RedisConnectionFactory redisConnectionFactory() + throws UnknownHostException { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(this.properties.getHost()); factory.setPort(this.properties.getPort()); @@ -79,7 +80,8 @@ public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean - RedisConnectionFactory redisConnectionFactory() throws UnknownHostException { + public RedisConnectionFactory redisConnectionFactory() + throws UnknownHostException { JedisConnectionFactory factory = createJedisConnectionFactory(); factory.setHostName(this.properties.getHost()); factory.setPort(this.properties.getPort()); @@ -124,7 +126,7 @@ public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean(name = "redisTemplate") - RedisOperations redisTemplate( + public RedisOperations redisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate template = new RedisTemplate(); @@ -134,7 +136,7 @@ public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) - StringRedisTemplate stringRedisTemplate( + public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigureConfigurationClassTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigureConfigurationClassTests.java new file mode 100644 index 0000000000..ffb50bdb08 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigureConfigurationClassTests.java @@ -0,0 +1,27 @@ +/* + * Copyright 2012-2014 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.autoconfigure; + +import org.springframework.boot.test.AbstractConfigurationClassTests; + +/** + * Tests for the autoconfigure module's @Configuration classes + * @author Andy Wilkinson + */ +public class AutoConfigureConfigurationClassTests extends AbstractConfigurationClassTests { + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/test/AbstractConfigurationClassTests.java b/spring-boot/src/test/java/org/springframework/boot/test/AbstractConfigurationClassTests.java new file mode 100644 index 0000000000..0e76e80da4 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/test/AbstractConfigurationClassTests.java @@ -0,0 +1,94 @@ +/* + * Copyright 2012-2014 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.test; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +import org.junit.Test; +import org.springframework.asm.Opcodes; +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.core.type.MethodMetadata; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; + +import static org.junit.Assert.assertEquals; + +/** + * @author Andy Wilkinson + */ +public abstract class AbstractConfigurationClassTests { + + private ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + + @Test + public void allBeanMethodsArePublic() throws IOException, ClassNotFoundException { + Set nonPublicBeanMethods = new HashSet(); + for (AnnotationMetadata configurationClass : findConfigurationClasses()) { + Set beanMethods = configurationClass + .getAnnotatedMethods(Bean.class.getName()); + for (MethodMetadata methodMetadata : beanMethods) { + if (!isPublic(methodMetadata)) { + nonPublicBeanMethods.add(methodMetadata.getDeclaringClassName() + "." + + methodMetadata.getMethodName()); + } + } + } + + assertEquals("Found non-public @Bean methods: " + nonPublicBeanMethods, 0, + nonPublicBeanMethods.size()); + } + + private Set findConfigurationClasses() throws IOException { + Set configurationClasses = new HashSet(); + Resource[] resources = this.resolver.getResources("classpath*:" + + getClass().getPackage().getName().replace(".", "/") + "/**/*.class"); + + for (Resource resource : resources) { + if (!isTestClass(resource)) { + MetadataReader metadataReader = new SimpleMetadataReaderFactory() + .getMetadataReader(resource); + AnnotationMetadata annotationMetadata = metadataReader + .getAnnotationMetadata(); + if (annotationMetadata.getAnnotationTypes().contains( + Configuration.class.getName())) { + configurationClasses.add(annotationMetadata); + } + } + } + return configurationClasses; + } + + private boolean isTestClass(Resource resource) throws IOException { + return resource.getFile().getAbsolutePath().contains("target/test-classes"); + } + + private boolean isPublic(MethodMetadata methodMetadata) { + int access = (Integer) new DirectFieldAccessor(methodMetadata) + .getPropertyValue("access"); + + return (access & Opcodes.ACC_PUBLIC) != 0; + } + +}