/*
 * Copyright 2002-2007 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.test.context.junit4;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Arrays;
import java.util.Collection;

import junit.framework.JUnit4TestAdapter;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.beans.Employee;
import org.springframework.beans.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

/**
 * Simple JUnit 4 based unit test which demonstrates how to use JUnit's
 * {@link Parameterized} Runner in conjunction with
 * {@link ContextConfiguration @ContextConfiguration}, the
 * {@link DependencyInjectionTestExecutionListener}, and a
 * {@link TestContextManager} to provide dependency injection to a
 * <em>parameterized test instance</em>.
 *
 * @author Sam Brannen
 * @since 2.1
 */
@RunWith(Parameterized.class)
@ContextConfiguration
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })
public class ParameterizedDependencyInjectionTests implements ApplicationContextAware {

	private ApplicationContext applicationContext;

	private final String employeeBeanName;

	private final String employeeName;

	@Autowired
	private Pet pet;

	private final TestContextManager testContextManager;


	public ParameterizedDependencyInjectionTests(final String employeeBeanName, final String employeeName)
	        throws Exception {
		this.testContextManager = new TestContextManager(getClass());
		this.employeeBeanName = employeeBeanName;
		this.employeeName = employeeName;
	}

	@Parameters
	public static Collection<String[]> employeeData() {
		return Arrays.asList(new String[][] { { "employee1", "John Smith" }, { "employee2", "Jane Smith" } });
	}

	// XXX Remove suite() once we've migrated to Ant 1.7 with JUnit 4 support.
	public static junit.framework.Test suite() {
		return new JUnit4TestAdapter(ParameterizedDependencyInjectionTests.class);
	}

	@Before
	public void injectDependencies() throws Exception {

		this.testContextManager.prepareTestInstance(this);
	}

	public final void setApplicationContext(final ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	@Test
	public final void verifyPetAndEmployee() {

		// Verifying dependency injection:
		assertNotNull("The pet field should have been autowired.", this.pet);

		// Verifying 'parameterized' support:
		final Employee employee = (Employee) this.applicationContext.getBean(this.employeeBeanName);
		assertEquals("Verifying the name of the employee configured as bean [" + this.employeeBeanName + "].",
		        this.employeeName, employee.getName());
	}

}

@@ -757,16 +757,44 @@
 tNull;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -838,16 +838,39 @@
 lection;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -923,22 +923,81 @@
 g.junit.
-Before
+AfterClass;%0Aimport org.junit.Before;%0Aimport org.junit.BeforeClass
 ;%0Aimport
@@ -2289,16 +2289,93 @@
 ware %7B%0A%0A
+%09private static final List%3CEmployee%3E employees = new ArrayList%3CEmployee%3E();%0A%0A
 %09private
@@ -3238,16 +3238,94 @@
 s);%0A%09%7D%0A%0A
+%09@BeforeClass%0A%09public static void clearEmployees() %7B%0A%09%09employees.clear();%0A%09%7D%0A%0A
 %09@Before
@@ -3873,16 +3873,43 @@
 nName);%0A
+%09%09employees.add(employee);%0A
 %09%09assert
@@ -4059,10 +4059,222 @@
 ));%0A%09%7D%0A%0A
+%09@AfterClass%0A%09public static void verifyNumParameterizedRuns() %7B%0A%09%09assertEquals(%22Verifying the number of times the parameterized test method was executed.%22,%0A%09%09        employeeData().size(), employees.size());%0A%09%7D%0A%0A
 %7D%0A

Verifying the number of times the parameterized test method was executed.