BeanMethodTests.java 7.0 KB
Newer Older
C
Chris Beams 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2002-2009 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.
 */
16
package org.springframework.context.annotation;
C
Chris Beams 已提交
17 18 19

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
20
import static org.springframework.context.annotation.AsmUtils.*;
C
Chris Beams 已提交
21
import static org.springframework.context.annotation.ScopedProxyMode.*;
22
import static org.springframework.context.annotation.StandardScopes.*;
C
Chris Beams 已提交
23 24 25 26 27 28 29 30

import java.lang.reflect.Modifier;

import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.beans.factory.parsing.FailFastProblemReporter;
import org.springframework.beans.factory.parsing.Location;
import org.springframework.beans.factory.parsing.ProblemReporter;
31
import org.springframework.context.annotation.Bean;
32 33 34
import org.springframework.context.annotation.BeanMethod;
import org.springframework.context.annotation.ConfigurationClass;
import org.springframework.context.annotation.ModelClass;
C
Chris Beams 已提交
35 36
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
37
import org.springframework.util.ClassUtils;
C
Chris Beams 已提交
38 39 40 41 42 43 44 45 46 47 48


/**
 * Unit tests for {@link BeanMethod}.
 *
 * @author Chris Beams
 */
public class BeanMethodTests {
	
	private ProblemReporter problemReporter = new FailFastProblemReporter();
	private String beanName = "foo";
49
	private Bean beanAnno = createMutableAnnotation(Bean.class, ClassUtils.getDefaultClassLoader());
C
Chris Beams 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	private ModelClass returnType = new ModelClass("FooType");
	private ConfigurationClass declaringClass = new ConfigurationClass();
	{ declaringClass.setName("test.Config"); }

	@Test
	public void testWellFormedMethod() {
		BeanMethod beanMethod = new BeanMethod(beanName, 0, returnType, beanAnno);

		assertThat(beanMethod.getName(), sameInstance(beanName));
		assertThat(beanMethod.getModifiers(), equalTo(0));
		assertThat(beanMethod.getReturnType(), sameInstance(returnType));
		assertThat(beanMethod.getAnnotation(Bean.class), sameInstance(beanAnno));
		assertThat(beanMethod.getAnnotation(Override.class), nullValue());
		assertThat(beanMethod.getRequiredAnnotation(Bean.class), sameInstance(beanAnno));
		try {
			beanMethod.getRequiredAnnotation(Override.class);
			fail("expected IllegalArgumentException ex");
		} catch (IllegalArgumentException ex) { /* expected */ }

		// must call setDeclaringClass() before calling getLocation()
		try {
			beanMethod.getLocation();
			fail("expected IllegalStateException ex");
		} catch (IllegalStateException ex) { /* expected */ }


		beanMethod.setDeclaringClass(declaringClass);
		assertThat(beanMethod.getDeclaringClass(), sameInstance(declaringClass));

		beanMethod.setSource(12); // indicating a line number
		assertEquals(beanMethod.getSource(), 12);

		Location location = beanMethod.getLocation();
		assertEquals(location.getResource(), new ClassPathResource("test/Config"));
		assertEquals(location.getSource(), 12);

		// should validate without throwing as this is a well-formed method
		beanMethod.validate(problemReporter);
	}

	@Test
	public void finalMethodsAreIllegal() {
		BeanMethod beanMethod = new BeanMethod(beanName, Modifier.FINAL, returnType, beanAnno);
		beanMethod.setDeclaringClass(declaringClass);
		try {
			beanMethod.validate(problemReporter);
			fail("should have failed due to final bean method");
		} catch (BeanDefinitionParsingException ex) {
			assertTrue(ex.getMessage().contains("remove the final modifier"));
		}
	}

	@Test
	public void privateMethodsAreIllegal() {
		BeanMethod beanMethod = new BeanMethod(beanName, Modifier.PRIVATE, returnType, beanAnno);
		beanMethod.setDeclaringClass(declaringClass);
		try {
			beanMethod.validate(problemReporter);
			fail("should have failed due to private bean method");
		} catch (BeanDefinitionParsingException ex) {
			assertTrue(ex.getMessage().contains("increase the method's visibility"));
		}
	}

	@Test
	public void singletonInterfaceScopedProxiesAreIllegal() {
		Scope scope = SingletonInterfaceProxy.class.getAnnotation(Scope.class);
		BeanMethod beanMethod = new BeanMethod(beanName, 0, returnType, beanAnno, scope);
		beanMethod.setDeclaringClass(declaringClass);
		try {
			beanMethod.validate(problemReporter);
			fail("should have failed due to singleton with scoped proxy");
		} catch (Exception ex) {
			assertTrue(ex.getMessage().contains("cannot be created for singleton/prototype beans"));
		}
	}

	@Test
	public void singletonTargetClassScopedProxiesAreIllegal() {
		Scope scope = SingletonTargetClassProxy.class.getAnnotation(Scope.class);
		BeanMethod beanMethod = new BeanMethod(beanName, 0, returnType, beanAnno, scope);
		beanMethod.setDeclaringClass(declaringClass);
		try {
			beanMethod.validate(problemReporter);
			fail("should have failed due to singleton with scoped proxy");
		} catch (Exception ex) {
			assertTrue(ex.getMessage().contains("cannot be created for singleton/prototype beans"));
		}
	}

	@Test
	public void singletonsSansProxyAreLegal() {
		Scope scope = SingletonNoProxy.class.getAnnotation(Scope.class);
		BeanMethod beanMethod = new BeanMethod(beanName, 0, returnType, beanAnno, scope);
		beanMethod.setDeclaringClass(declaringClass);
		beanMethod.validate(problemReporter); // should validate without problems - it's legal
	}

	@Test
	public void prototypeInterfaceScopedProxiesAreIllegal() {
		Scope scope = PrototypeInterfaceProxy.class.getAnnotation(Scope.class);
		BeanMethod beanMethod = new BeanMethod(beanName, 0, returnType, beanAnno, scope);
		beanMethod.setDeclaringClass(declaringClass);
		try {
			beanMethod.validate(problemReporter);
			fail("should have failed due to prototype with scoped proxy");
		} catch (Exception ex) {
			assertTrue(ex.getMessage().contains("cannot be created for singleton/prototype beans"));
		}
	}

	@Test
	public void sessionInterfaceScopedProxiesAreLegal() {
163
		Scope scope = SessionInterfaceProxy.class.getAnnotation(Scope.class);
C
Chris Beams 已提交
164 165
		BeanMethod beanMethod = new BeanMethod(beanName, 0, returnType, beanAnno, scope);
		beanMethod.setDeclaringClass(declaringClass);
166
		beanMethod.validate(problemReporter); // should validate without problems - it's legal
C
Chris Beams 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	}

	@Scope(value=SINGLETON, proxyMode=INTERFACES)
	private class SingletonInterfaceProxy { }

	@Scope(value=SINGLETON, proxyMode=TARGET_CLASS)
	private class SingletonTargetClassProxy { }

	@Scope(value=SINGLETON, proxyMode=NO)
	private class SingletonNoProxy { }

	@Scope(value=PROTOTYPE, proxyMode=INTERFACES)
	private class PrototypeInterfaceProxy { }

	@Scope(value=SESSION, proxyMode=INTERFACES)
	private class SessionInterfaceProxy { }
}