Merge branch 'master' into websocket-stomp
This commit is contained in:
@@ -367,6 +367,16 @@ public abstract class AbstractAnnotationTests {
|
||||
assertSame(r2, secondary.get(o).get());
|
||||
}
|
||||
|
||||
public void testPutRefersToResult(CacheableService<?> service) throws Exception {
|
||||
Long id = Long.MIN_VALUE;
|
||||
TestEntity entity = new TestEntity();
|
||||
Cache primary = cm.getCache("primary");
|
||||
assertNull(primary.get(id));
|
||||
assertNull(entity.getId());
|
||||
service.putRefersToResult(entity);
|
||||
assertSame(entity, primary.get(id).get());
|
||||
}
|
||||
|
||||
public void testMultiCacheAndEvict(CacheableService<?> service) {
|
||||
String methodName = "multiCacheAndEvict";
|
||||
|
||||
@@ -621,6 +631,16 @@ public abstract class AbstractAnnotationTests {
|
||||
testMultiPut(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutRefersToResult() throws Exception {
|
||||
testPutRefersToResult(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassPutRefersToResult() throws Exception {
|
||||
testPutRefersToResult(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiCacheAndEvict() {
|
||||
testMultiCacheAndEvict(cs);
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.springframework.cache.annotation.Caching;
|
||||
public class AnnotatedClassCacheableService implements CacheableService<Object> {
|
||||
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
|
||||
public static final AtomicLong nullInvocations = new AtomicLong();
|
||||
|
||||
@Override
|
||||
@@ -164,4 +165,11 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
|
||||
public Object multiUpdate(Object arg1) {
|
||||
return arg1;
|
||||
}
|
||||
|
||||
@Override
|
||||
@CachePut(value="primary", key="#result.id")
|
||||
public TestEntity putRefersToResult(TestEntity arg1) {
|
||||
arg1.setId(Long.MIN_VALUE);
|
||||
return arg1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,4 +70,6 @@ public interface CacheableService<T> {
|
||||
T multiConditionalCacheAndEvict(Object arg1);
|
||||
|
||||
T multiUpdate(Object arg1);
|
||||
|
||||
TestEntity putRefersToResult(TestEntity arg1);
|
||||
}
|
||||
|
||||
@@ -170,4 +170,11 @@ public class DefaultCacheableService implements CacheableService<Long> {
|
||||
public Long multiUpdate(Object arg1) {
|
||||
return Long.valueOf(arg1.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
@CachePut(value="primary", key="#result.id")
|
||||
public TestEntity putRefersToResult(TestEntity arg1) {
|
||||
arg1.setId(Long.MIN_VALUE);
|
||||
return arg1;
|
||||
}
|
||||
}
|
||||
|
||||
56
spring-context/src/test/java/org/springframework/cache/config/TestEntity.java
vendored
Normal file
56
spring-context/src/test/java/org/springframework/cache/config/TestEntity.java
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.cache.config;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Simple test entity for use with caching tests.
|
||||
*
|
||||
* @author Michael Pl<50>d
|
||||
*/
|
||||
public class TestEntity {
|
||||
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return ObjectUtils.nullSafeHashCode(this.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (obj instanceof TestEntity) {
|
||||
return ObjectUtils.nullSafeEquals(this.id, ((TestEntity) obj).id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
89
spring-context/src/test/java/org/springframework/cache/interceptor/SimpleKeyGeneratorTests.java
vendored
Normal file
89
spring-context/src/test/java/org/springframework/cache/interceptor/SimpleKeyGeneratorTests.java
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.cache.interceptor;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link SimpleKeyGenerator} and {@link SimpleKey}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class SimpleKeyGeneratorTests {
|
||||
|
||||
private SimpleKeyGenerator generator = new SimpleKeyGenerator();
|
||||
|
||||
@Test
|
||||
public void noValues() throws Exception {
|
||||
Object k1 = generator.generate(null, null, new Object[] {});
|
||||
Object k2 = generator.generate(null, null, new Object[] {});
|
||||
Object k3 = generator.generate(null, null, new Object[] { "different" });
|
||||
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
|
||||
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
|
||||
assertThat(k1, equalTo(k2));
|
||||
assertThat(k1, not(equalTo(k3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleValue() throws Exception {
|
||||
Object k1 = generator.generate(null, null, new Object[] { "a" });
|
||||
Object k2 = generator.generate(null, null, new Object[] { "a" });
|
||||
Object k3 = generator.generate(null, null, new Object[] { "different" });
|
||||
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
|
||||
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
|
||||
assertThat(k1, equalTo(k2));
|
||||
assertThat(k1, not(equalTo(k3)));
|
||||
assertThat(k1, equalTo((Object) "a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleValues() throws Exception {
|
||||
Object k1 = generator.generate(null, null, new Object[] { "a", 1, "b" });
|
||||
Object k2 = generator.generate(null, null, new Object[] { "a", 1, "b" });
|
||||
Object k3 = generator.generate(null, null, new Object[] { "b", 1, "a" });
|
||||
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
|
||||
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
|
||||
assertThat(k1, equalTo(k2));
|
||||
assertThat(k1, not(equalTo(k3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleNullValue() throws Exception {
|
||||
Object k1 = generator.generate(null, null, new Object[] { null });
|
||||
Object k2 = generator.generate(null, null, new Object[] { null });
|
||||
Object k3 = generator.generate(null, null, new Object[] { "different" });
|
||||
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
|
||||
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
|
||||
assertThat(k1, equalTo(k2));
|
||||
assertThat(k1, not(equalTo(k3)));
|
||||
assertThat(k1, instanceOf(SimpleKey.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiplNullValues() throws Exception {
|
||||
Object k1 = generator.generate(null, null, new Object[] { "a", null, "b", null });
|
||||
Object k2 = generator.generate(null, null, new Object[] { "a", null, "b", null });
|
||||
Object k3 = generator.generate(null, null, new Object[] { "a", null, "b" });
|
||||
assertThat(k1.hashCode(), equalTo(k2.hashCode()));
|
||||
assertThat(k1.hashCode(), not(equalTo(k3.hashCode())));
|
||||
assertThat(k1, equalTo(k2));
|
||||
assertThat(k1, not(equalTo(k3)));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -16,16 +16,21 @@
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import example.scannable.DefaultNamedComponent;
|
||||
import org.junit.Test;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import example.scannable.DefaultNamedComponent;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -81,6 +86,22 @@ public class AnnotationBeanNameGeneratorTests {
|
||||
assertEquals(expectedGeneratedBeanName, beanName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerateBeanNameFromMetaComponentWithStringValue() {
|
||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
||||
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromStringMeta.class);
|
||||
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
|
||||
assertEquals("henry", beanName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerateBeanNameFromMetaComponentWithNonStringValue() {
|
||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
||||
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class);
|
||||
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
|
||||
assertEquals("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta", beanName);
|
||||
}
|
||||
|
||||
|
||||
@Component("walden")
|
||||
private static class ComponentWithName {
|
||||
@@ -96,4 +117,19 @@ public class AnnotationBeanNameGeneratorTests {
|
||||
private static class AnonymousComponent {
|
||||
}
|
||||
|
||||
@Service("henry")
|
||||
private static class ComponentFromStringMeta {
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Component
|
||||
public @interface NonStringMetaComponent {
|
||||
long value();
|
||||
}
|
||||
|
||||
@NonStringMetaComponent(123)
|
||||
private static class ComponentFromNonStringMeta {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -41,7 +41,8 @@ public class AsmCircularImportDetectionTests extends AbstractCircularImportDetec
|
||||
new StandardEnvironment(),
|
||||
new DefaultResourceLoader(),
|
||||
new AnnotationBeanNameGenerator(),
|
||||
new DefaultListableBeanFactory());
|
||||
new DefaultListableBeanFactory(),
|
||||
null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,55 +16,80 @@
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
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 java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test for {@link Conditional} beans.
|
||||
* Tests for {@link Conditional} beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public class ConfigurationClassWithConditionTests {
|
||||
|
||||
private final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@After
|
||||
public void closeContext() {
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conditionalOnBeanMatch() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
public void conditionalOnMissingBeanMatch() throws Exception {
|
||||
ctx.register(BeanOneConfiguration.class, BeanTwoConfiguration.class);
|
||||
ctx.refresh();
|
||||
assertTrue(ctx.containsBean("bean1"));
|
||||
assertFalse(ctx.containsBean("bean2"));
|
||||
assertFalse(ctx.containsBean("configurationClassWithConditionTests.BeanTwoConfiguration"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conditionalOnMissingBeanNoMatch() throws Exception {
|
||||
ctx.register(BeanTwoConfiguration.class);
|
||||
ctx.refresh();
|
||||
assertFalse(ctx.containsBean("bean1"));
|
||||
assertTrue(ctx.containsBean("bean2"));
|
||||
assertTrue(ctx.containsBean("configurationClassWithConditionTests.BeanTwoConfiguration"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conditionalOnBeanMatch() throws Exception {
|
||||
ctx.register(BeanOneConfiguration.class, BeanThreeConfiguration.class);
|
||||
ctx.refresh();
|
||||
assertTrue(ctx.containsBean("bean1"));
|
||||
assertTrue(ctx.containsBean("bean3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conditionalOnBeanNoMatch() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(BeanTwoConfiguration.class);
|
||||
ctx.register(BeanThreeConfiguration.class);
|
||||
ctx.refresh();
|
||||
assertTrue(ctx.containsBean("bean2"));
|
||||
assertFalse(ctx.containsBean("bean1"));
|
||||
assertFalse(ctx.containsBean("bean3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metaConditional() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ConfigurationWithMetaCondition.class);
|
||||
ctx.refresh();
|
||||
assertTrue(ctx.containsBean("bean"));
|
||||
@@ -72,7 +97,6 @@ public class ConfigurationClassWithConditionTests {
|
||||
|
||||
@Test
|
||||
public void nonConfigurationClass() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(NonConfigurationClass.class);
|
||||
ctx.refresh();
|
||||
thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
@@ -81,13 +105,38 @@ public class ConfigurationClassWithConditionTests {
|
||||
|
||||
@Test
|
||||
public void methodConditional() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ConditionOnMethodConfiguration.class);
|
||||
ctx.refresh();
|
||||
thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
assertNull(ctx.getBean(ExampleBean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void importsNotCreated() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ImportsNotCreated.class);
|
||||
ctx.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void importsNotLoaded() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ImportsNotLoaded.class);
|
||||
ctx.refresh();
|
||||
assertThat(ctx.containsBeanDefinition("a"), equalTo(false));
|
||||
assertThat(ctx.containsBeanDefinition("b"), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sensibleConditionContext() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.setResourceLoader(new DefaultResourceLoader());
|
||||
ctx.setClassLoader(getClass().getClassLoader());
|
||||
ctx.register(SensibleConditionContext.class);
|
||||
ctx.refresh();
|
||||
assertThat(ctx.getBean(ExampleBean.class), instanceOf(ExampleBean.class));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanOneConfiguration {
|
||||
@Bean
|
||||
@@ -105,6 +154,15 @@ public class ConfigurationClassWithConditionTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Conditional(HasBeanOneCondition.class)
|
||||
static class BeanThreeConfiguration {
|
||||
@Bean
|
||||
public ExampleBean bean3() {
|
||||
return new ExampleBean();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@MetaConditional("test")
|
||||
static class ConfigurationWithMetaCondition {
|
||||
@@ -135,6 +193,19 @@ public class ConfigurationClassWithConditionTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class HasBeanOneCondition implements ConfigurationCondition {
|
||||
|
||||
@Override
|
||||
public ConfigurationPhase getConfigurationPhase() {
|
||||
return ConfigurationPhase.REGISTER_BEAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return context.getBeanFactory().containsBeanDefinition("bean1");
|
||||
}
|
||||
}
|
||||
|
||||
static class MetaConditionalFilter implements Condition {
|
||||
|
||||
@Override
|
||||
@@ -167,6 +238,112 @@ public class ConfigurationClassWithConditionTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Never
|
||||
@Import({ ConfigurationNotCreated.class, RegistrarNotCreated.class, ImportSelectorNotCreated.class })
|
||||
static class ImportsNotCreated {
|
||||
static {
|
||||
if (true) throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ConfigurationNotCreated {
|
||||
static {
|
||||
if (true) throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
static class RegistrarNotCreated implements ImportBeanDefinitionRegistrar {
|
||||
static {
|
||||
if (true) throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
|
||||
BeanDefinitionRegistry registry) {
|
||||
}
|
||||
}
|
||||
|
||||
static class ImportSelectorNotCreated implements ImportSelector {
|
||||
|
||||
static {
|
||||
if (true) throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||
return new String[] {};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Never
|
||||
@Import({ ConfigurationNotLoaded.class, RegistrarNotLoaded.class, ImportSelectorNotLoaded.class })
|
||||
static class ImportsNotLoaded {
|
||||
static {
|
||||
if (true) throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ConfigurationNotLoaded {
|
||||
@Bean
|
||||
public String a() {
|
||||
return "a";
|
||||
}
|
||||
}
|
||||
|
||||
static class RegistrarNotLoaded implements ImportBeanDefinitionRegistrar {
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
|
||||
BeanDefinitionRegistry registry) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
static class ImportSelectorNotLoaded implements ImportSelector {
|
||||
|
||||
@Override
|
||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||
return new String[] { SelectedConfigurationNotLoaded.class.getName() };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class SelectedConfigurationNotLoaded {
|
||||
@Bean
|
||||
public String b() {
|
||||
return "b";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Conditional(SensibleConditionContextCondition.class)
|
||||
static class SensibleConditionContext {
|
||||
@Bean
|
||||
ExampleBean exampleBean() {
|
||||
return new ExampleBean();
|
||||
}
|
||||
}
|
||||
|
||||
static class SensibleConditionContextCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
assertThat(context.getApplicationContext(), notNullValue());
|
||||
assertThat(context.getBeanFactory(), notNullValue());
|
||||
assertThat(context.getClassLoader(), notNullValue());
|
||||
assertThat(context.getEnvironment(), notNullValue());
|
||||
assertThat(context.getRegistry(), notNullValue());
|
||||
assertThat(context.getResourceLoader(), notNullValue());
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class ExampleBean {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -16,26 +16,27 @@
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link ImportBeanDefinitionRegistrar}.
|
||||
*
|
||||
@@ -53,6 +54,7 @@ public class ImportBeanDefinitionRegistrarTests {
|
||||
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory()));
|
||||
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
|
||||
assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
|
||||
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
|
||||
}
|
||||
|
||||
@Sample
|
||||
@@ -69,11 +71,12 @@ public class ImportBeanDefinitionRegistrarTests {
|
||||
}
|
||||
|
||||
static class SampleRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, ResourceLoaderAware,
|
||||
BeanFactoryAware {
|
||||
BeanFactoryAware, EnvironmentAware {
|
||||
|
||||
static ClassLoader classLoader;
|
||||
static ResourceLoader resourceLoader;
|
||||
static BeanFactory beanFactory;
|
||||
static Environment environment;
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
@@ -90,6 +93,11 @@ public class ImportBeanDefinitionRegistrarTests {
|
||||
SampleRegistrar.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
SampleRegistrar.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
|
||||
}
|
||||
|
||||
@@ -16,11 +16,6 @@
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -28,12 +23,27 @@ import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrarTests.SampleRegistrar;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link ImportSelector} and {@link DeferredImportSelector}.
|
||||
*
|
||||
@@ -50,10 +60,62 @@ public class ImportSelectorTests {
|
||||
context.refresh();
|
||||
context.getBean(Config.class);
|
||||
InOrder ordered = inOrder(beanFactory);
|
||||
ordered.verify(beanFactory).registerBeanDefinition(eq("a"), any(BeanDefinition.class));
|
||||
ordered.verify(beanFactory).registerBeanDefinition(eq("b"), any(BeanDefinition.class));
|
||||
ordered.verify(beanFactory).registerBeanDefinition(eq("d"), any(BeanDefinition.class));
|
||||
ordered.verify(beanFactory).registerBeanDefinition(eq("c"), any(BeanDefinition.class));
|
||||
ordered.verify(beanFactory).registerBeanDefinition(eq("a"), (BeanDefinition) anyObject());
|
||||
ordered.verify(beanFactory).registerBeanDefinition(eq("b"), (BeanDefinition) anyObject());
|
||||
ordered.verify(beanFactory).registerBeanDefinition(eq("d"), (BeanDefinition) anyObject());
|
||||
ordered.verify(beanFactory).registerBeanDefinition(eq("c"), (BeanDefinition) anyObject());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAwareMethodsInImportSelector() {
|
||||
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
|
||||
context.getBean(MessageSource.class);
|
||||
|
||||
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory()));
|
||||
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
|
||||
assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
|
||||
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(SampleImportSelector.class)
|
||||
static class AwareConfig {
|
||||
|
||||
}
|
||||
|
||||
static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
|
||||
BeanFactoryAware, EnvironmentAware {
|
||||
|
||||
static ClassLoader classLoader;
|
||||
static ResourceLoader resourceLoader;
|
||||
static BeanFactory beanFactory;
|
||||
static Environment environment;
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
SampleRegistrar.classLoader = classLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
SampleRegistrar.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
SampleRegistrar.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
SampleRegistrar.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||
return new String[] {};
|
||||
}
|
||||
}
|
||||
|
||||
@Sample
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -16,9 +16,15 @@
|
||||
|
||||
package org.springframework.context.annotation.componentscan.simple;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SimpleComponent {
|
||||
|
||||
@Bean
|
||||
public String exampleBean() {
|
||||
return "example";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -193,8 +193,8 @@ public class ScopingTests {
|
||||
@Test
|
||||
public void testScopedConfigurationBeanDefinitionCount() throws Exception {
|
||||
// count the beans
|
||||
// 6 @Beans + 1 Configuration + 2 scoped proxy + 1 importRegistry
|
||||
assertEquals(10, ctx.getBeanDefinitionCount());
|
||||
// 6 @Beans + 1 Configuration + 2 scoped proxy + 1 importRegistry + 1 enhanced config post processor
|
||||
assertEquals(11, ctx.getBeanDefinitionCount());
|
||||
}
|
||||
|
||||
// /**
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.context.annotation.configuration;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests for SPR-10668.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class Spr10668Tests {
|
||||
|
||||
@Test
|
||||
public void testSelfInjectHierarchy() throws Exception {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
ChildConfig.class);
|
||||
assertNotNull(context.getBean(MyComponent.class));
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class ParentConfig implements BeanFactoryAware {
|
||||
|
||||
@Autowired(required = false)
|
||||
MyComponent component;
|
||||
|
||||
public ParentConfig() {
|
||||
System.out.println("Parent " + getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
System.out.println("BFA " + getClass());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class ChildConfig extends ParentConfig {
|
||||
|
||||
@Bean
|
||||
public MyComponentImpl myComponent() {
|
||||
return new MyComponentImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static interface MyComponent {
|
||||
}
|
||||
|
||||
public static class MyComponentImpl implements MyComponent {
|
||||
}
|
||||
}
|
||||
@@ -265,4 +265,23 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
||||
thrown.expect(IllegalStateException.class);
|
||||
ppc.getAppliedPropertySources();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleLocationsWithDefaultResolvedValue() throws Exception {
|
||||
// SPR-10619
|
||||
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
||||
ClassPathResource doesNotHave = new ClassPathResource("test.properties", getClass());
|
||||
ClassPathResource setToTrue = new ClassPathResource("placeholder.properties", getClass());
|
||||
ppc.setLocations(new Resource[] { doesNotHave, setToTrue });
|
||||
ppc.setIgnoreResourceNotFound(true);
|
||||
ppc.setIgnoreUnresolvablePlaceholders(true);
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.registerBeanDefinition("testBean",
|
||||
genericBeanDefinition(TestBean.class)
|
||||
.addPropertyValue("jedi", "${jedi:false}")
|
||||
.getBeanDefinition());
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
assertThat(bf.getBean(TestBean.class).isJedi(), equalTo(true));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
targetName=wrappedAssemblerOne
|
||||
logicName=logicTwo
|
||||
realLogicName=realLogic
|
||||
jedi=true
|
||||
|
||||
Reference in New Issue
Block a user