/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.wicket.spring.injection.annot;

import java.lang.reflect.Field;

import junit.framework.TestCase;

import org.apache.wicket.proxy.ILazyInitProxy;
import org.apache.wicket.spring.ISpringContextLocator;
import org.apache.wicket.spring.SpringBeanLocator;
import org.apache.wicket.spring.injection.util.Bean;
import org.apache.wicket.spring.injection.util.Bean2;
import org.apache.wicket.spring.injection.util.Injectable;
import org.apache.wicket.spring.test.ApplicationContextMock;
import org.springframework.context.ApplicationContext;

/**
 * Tests for BeanAnnotLocatorFactory
 * 
 * @author igor
 * 
 */
public class AnnotProxyFieldValueFactoryTest extends TestCase
{
	ISpringContextLocator mockCtxLocator = new ISpringContextLocator()
	{
		private static final long serialVersionUID = 1L;

		public ApplicationContext getSpringContext()
		{
			ApplicationContextMock mock = new ApplicationContextMock();
			mock.putBean(new Bean());
			mock.putBean("somebean", new Bean2());
			return mock;
		}
	};

	Injectable obj = new Injectable();

	AnnotProxyFieldValueFactory factory = new AnnotProxyFieldValueFactory(mockCtxLocator);

	/**
	 * Test the factory
	 * 
	 * @throws Exception
	 */
	public void testFactory() throws Exception
	{
		SpringBeanLocator locator = null;
		Object proxy = null;

		Field field = obj.getClass().getDeclaredField("nobean");
		proxy = factory.getFieldValue(field, obj);
		assertNull(proxy);

		field = obj.getClass().getDeclaredField("beanByClass");
		proxy = factory.getFieldValue(field, obj);
		locator = (SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
		assertTrue(locator.getBeanType().equals(Bean.class));
		assertTrue(locator.getSpringContextLocator() == mockCtxLocator);
		assertTrue(factory.getFieldValue(field, obj) instanceof ILazyInitProxy);

		field = obj.getClass().getDeclaredField("beanByName");
		proxy = factory.getFieldValue(field, obj);
		locator = (SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
		assertTrue(locator.getBeanName().equals("somebean"));
		assertTrue(locator.getBeanType().equals(Bean2.class));
		assertTrue(locator.getSpringContextLocator() == mockCtxLocator);
		assertTrue(factory.getFieldValue(field, obj) instanceof ILazyInitProxy);
	}

	/**
	 * test the cache, make sure the same proxy is returned for the same dependency it represents
	 * 
	 * @throws Exception
	 */
	public void testCache() throws Exception
	{
		Field field = obj.getClass().getDeclaredField("beanByClass");
		Object proxy1 = factory.getFieldValue(field, obj);
		Object proxy2 = factory.getFieldValue(field, obj);
		assertTrue(proxy1 == proxy2);

		field = obj.getClass().getDeclaredField("beanByName");
		proxy1 = factory.getFieldValue(field, obj);
		proxy2 = factory.getFieldValue(field, obj);
		assertTrue(proxy1 == proxy2);
	}

	/**
	 * Test creation fails with null springcontextlocator
	 */
	public void testNullContextLocator()
	{
		try
		{
			new AnnotProxyFieldValueFactory(null);
			fail();
		}
		catch (IllegalArgumentException e)
		{
			// noop
		}
	}

	/**
	 * @throws Exception
	 */
	public void testFailsIfBeanWithIdIsNotFound() throws Exception
	{
		InjectableWithReferenceToNonexistingBean obj = new InjectableWithReferenceToNonexistingBean();
		Field field = obj.getClass().getDeclaredField("nonExisting");
		try
		{
			factory.getFieldValue(field, obj);
			fail();
		}
		catch (RuntimeException e)
		{
		}
	}

	static class InjectableWithReferenceToNonexistingBean
	{
		@SpringBean(name = "nonExisting")
		private Bean nonExisting;
	}
}

@@ -4117,16 +4117,40 @@
 %0A%09%09%7B%0A%09%09%09
+final Bean bean = (Bean)
 factory.
@@ -4176,16 +4176,272 @@
 , obj);%0A
+%09%09%09/*%0A%09%09%09 * returned bean will not be null even though the bean is not found. what we get instead%0A%09%09%09 * is a proxy. we invoke a method on the proxy in order to cause it to try to locate the%0A%09%09%09 * bean and that is when it will fail%0A%09%09%09 */%0A%09%09%09bean.method();%0A
 %09%09%09fail(

fix test