Browse Source

Polishing

pull/639/head
Juergen Hoeller 12 years ago
parent
commit
f617d28eef
  1. 8
      spring-context/src/test/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrarTests.java
  2. 34
      spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java

8
spring-context/src/test/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrarTests.java

@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
@ -47,7 +48,6 @@ public class ImportBeanDefinitionRegistrarTests {
@Test @Test
public void shouldInvokeAwareMethodsInImportBeanDefinitionRegistrar() { public void shouldInvokeAwareMethodsInImportBeanDefinitionRegistrar() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
context.getBean(MessageSource.class); context.getBean(MessageSource.class);
@ -57,19 +57,20 @@ public class ImportBeanDefinitionRegistrarTests {
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment())); assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
} }
@Sample @Sample
@Configuration @Configuration
static class Config { static class Config {
} }
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Import(SampleRegistrar.class) @Import(SampleRegistrar.class)
public static @interface Sample { public static @interface Sample {
} }
static class SampleRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, ResourceLoaderAware, static class SampleRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware { BeanFactoryAware, EnvironmentAware {
@ -102,4 +103,5 @@ public class ImportBeanDefinitionRegistrarTests {
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
} }
} }
} }

34
spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,6 +27,7 @@ import org.hamcrest.Matcher;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
@ -45,7 +46,8 @@ import org.springframework.core.type.AnnotationMetadata;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Matchers.*; import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
/** /**
@ -57,16 +59,17 @@ public class ImportSelectorTests {
static Map<Class<?>, String> importFrom = new HashMap<Class<?>, String>(); static Map<Class<?>, String> importFrom = new HashMap<Class<?>, String>();
@BeforeClass @BeforeClass
public static void clearImportFrom() { public static void clearImportFrom() {
ImportSelectorTests.importFrom.clear(); ImportSelectorTests.importFrom.clear();
} }
@Test @Test
public void importSelectors() { public void importSelectors() {
DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory()); DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory());
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(beanFactory);
beanFactory);
context.register(Config.class); context.register(Config.class);
context.refresh(); context.refresh();
context.getBean(Config.class); context.getBean(Config.class);
@ -91,19 +94,19 @@ public class ImportSelectorTests {
public void correctMetaDataOnIndirectImports() throws Exception { public void correctMetaDataOnIndirectImports() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IndirectConfig.class); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IndirectConfig.class);
Matcher<String> isFromIndirect = equalTo(IndirectImport.class.getName()); Matcher<String> isFromIndirect = equalTo(IndirectImport.class.getName());
System.out.println(importFrom);
assertThat(importFrom.get(ImportSelector1.class), isFromIndirect); assertThat(importFrom.get(ImportSelector1.class), isFromIndirect);
assertThat(importFrom.get(ImportSelector2.class), isFromIndirect); assertThat(importFrom.get(ImportSelector2.class), isFromIndirect);
assertThat(importFrom.get(DeferredImportSelector1.class), isFromIndirect); assertThat(importFrom.get(DeferredImportSelector1.class), isFromIndirect);
assertThat(importFrom.get(DeferredImportSelector2.class), isFromIndirect); assertThat(importFrom.get(DeferredImportSelector2.class), isFromIndirect);
} }
@Configuration @Configuration
@Import(SampleImportSelector.class) @Import(SampleImportSelector.class)
static class AwareConfig { static class AwareConfig {
} }
static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware, static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware { BeanFactoryAware, EnvironmentAware {
@ -138,11 +141,13 @@ public class ImportSelectorTests {
} }
} }
@Sample @Sample
@Configuration @Configuration
static class Config { static class Config {
} }
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Import({ DeferredImportSelector1.class, DeferredImportSelector2.class, @Import({ DeferredImportSelector1.class, DeferredImportSelector2.class,
@ -150,6 +155,7 @@ public class ImportSelectorTests {
public static @interface Sample { public static @interface Sample {
} }
public static class ImportSelector1 implements ImportSelector { public static class ImportSelector1 implements ImportSelector {
@Override @Override
@ -159,6 +165,7 @@ public class ImportSelectorTests {
} }
} }
public static class ImportSelector2 implements ImportSelector { public static class ImportSelector2 implements ImportSelector {
@Override @Override
@ -168,6 +175,7 @@ public class ImportSelectorTests {
} }
} }
public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered { public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered {
@Override @Override
@ -182,6 +190,7 @@ public class ImportSelectorTests {
} }
} }
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
public static class DeferredImportSelector2 implements DeferredImportSelector { public static class DeferredImportSelector2 implements DeferredImportSelector {
@ -190,9 +199,9 @@ public class ImportSelectorTests {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName()); ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { DeferredImportedSelector2.class.getName() }; return new String[] { DeferredImportedSelector2.class.getName() };
} }
} }
@Configuration @Configuration
public static class ImportedSelector1 { public static class ImportedSelector1 {
@ -202,6 +211,7 @@ public class ImportSelectorTests {
} }
} }
@Configuration @Configuration
public static class ImportedSelector2 { public static class ImportedSelector2 {
@ -211,6 +221,7 @@ public class ImportSelectorTests {
} }
} }
@Configuration @Configuration
public static class DeferredImportedSelector1 { public static class DeferredImportedSelector1 {
@ -220,6 +231,7 @@ public class ImportSelectorTests {
} }
} }
@Configuration @Configuration
public static class DeferredImportedSelector2 { public static class DeferredImportedSelector2 {
@ -229,22 +241,24 @@ public class ImportSelectorTests {
} }
} }
@Configuration @Configuration
@Import(IndirectImportSelector.class) @Import(IndirectImportSelector.class)
public static class IndirectConfig { public static class IndirectConfig {
} }
public static class IndirectImportSelector implements ImportSelector { public static class IndirectImportSelector implements ImportSelector {
@Override @Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) { public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] { IndirectImport.class.getName()}; return new String[] {IndirectImport.class.getName()};
} }
} }
@Sample @Sample
public static class IndirectImport { public static class IndirectImport {
} }
} }

Loading…
Cancel
Save