/*
 * 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.
 */

package org.springframework.web.context.support;

import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.hamcrest.CoreMatchers.*;

import static org.junit.Assert.*;

/**
 * @author Chris Beams
 */
public class AnnotationConfigWebApplicationContextTests {

	@Test
	public void registerSingleClass() {
		AnnotationConfigWebApplicationContext ctx =
			new AnnotationConfigWebApplicationContext();
		ctx.register(Config.class);
		ctx.refresh();

		TestBean bean = ctx.getBean(TestBean.class);
		assertNotNull(bean);
	}

	@Test
	public void configLocationWithSingleClass() {
		AnnotationConfigWebApplicationContext ctx =
			new AnnotationConfigWebApplicationContext();
		ctx.setConfigLocation(Config.class.getName());
		ctx.refresh();

		TestBean bean = ctx.getBean(TestBean.class);
		assertNotNull(bean);
	}

	@Test
	public void withBeanNameGenerator() {
		AnnotationConfigWebApplicationContext ctx =
			new AnnotationConfigWebApplicationContext();
		ctx.setBeanNameGenerator(new AnnotationBeanNameGenerator() {
			@Override
			public String generateBeanName(BeanDefinition definition,
					BeanDefinitionRegistry registry) {
				return "custom-" + super.generateBeanName(definition, registry);
			}
		});
		ctx.setConfigLocation(Config.class.getName());
		ctx.refresh();
		assertThat(ctx.containsBean("custom-myConfig"), is(true));
	}


	@Configuration("myConfig")
	static class Config {
		@Bean
		public TestBean myTestBean() {
			return new TestBean();
		}
	}

	static class NotConfigurationAnnotated { }

	static class TestBean { }

}

@@ -20,10 +20,10 @@
 2-20
-09
+14
  the
@@ -688,16 +688,17 @@
 t.Test;%0A
+%0A
 import o
@@ -1058,17 +1058,16 @@
 hers.*;%0A
-%0A
 import s
@@ -1120,16 +1120,43 @@
 s Beams%0A
+ * @author Juergen Hoeller%0A
  */%0Apubl
@@ -1291,36 +1291,33 @@
 ionContext ctx =
-%0A%09%09%09
+ 
 new AnnotationCo
@@ -1558,36 +1558,33 @@
 ionContext ctx =
-%0A%09%09%09
+ 
 new AnnotationCo
@@ -1745,32 +1745,337 @@
 Null(bean);%0A%09%7D%0A%0A
+%09@Test%0A%09public void configLocationWithBasePackage() %7B%0A%09%09AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();%0A%09%09ctx.setConfigLocation(%22org.springframework.web.context.support%22);%0A%09%09ctx.refresh();%0A%0A%09%09TestBean bean = ctx.getBean(TestBean.class);%0A%09%09assertNotNull(bean);%0A%09%7D%0A%0A
 %09@Test%0A%09public v
@@ -2149,20 +2149,17 @@
 xt ctx =
-%0A%09%09%09
+ 
 new Anno
@@ -2635,16 +2635,17 @@
 onfig %7B%0A
+%0A
 %09%09@Bean%0A
@@ -2715,52 +2715,8 @@
 %09%7D%0A%0A
-%09static class NotConfigurationAnnotated %7B %7D%0A
 %0A%09st
@@ -2736,14 +2736,15 @@
 stBean %7B
- 
+%0A%09
 %7D%0A%0A%7D%0A

Test for base package specified as config location