/*
 * Copyright 2008-2012 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.broadleafcommerce.test;

import org.broadleafcommerce.common.extensibility.context.MergeClassPathXMLApplicationContext;
import org.broadleafcommerce.common.extensibility.context.StandardConfigLocations;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import java.lang.management.ManagementFactory;

@TransactionConfiguration(transactionManager = "blTransactionManager", defaultRollback = true)
@TestExecutionListeners(inheritListeners = false, value = {MergeDependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, MergeTransactionalTestExecutionListener.class})
public abstract class BaseTest extends AbstractTestNGSpringContextTests {

	private static MergeClassPathXMLApplicationContext mergeContext = null;
	
	public static MergeClassPathXMLApplicationContext getContext() {
			
		try {
			if (mergeContext == null) {
				String[] contexts = StandardConfigLocations.retrieveAll(StandardConfigLocations.TESTCONTEXTTYPE);
				
				String[] additionalContexts = (ManagementFactory.getRuntimeMXBean().getInputArguments().contains("-Dlegacy=true")) 
						? new String[]{"bl-applicationContext-test-legacy.xml"} 
						: new String[]{};
				
				mergeContext = new MergeClassPathXMLApplicationContext(contexts, additionalContexts);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return mergeContext;
	}
	
	@PersistenceContext(unitName = "blPU")
    protected EntityManager em;

}

@@ -649,24 +649,67 @@
 erce.test;%0A%0A
+import org.apache.commons.lang.ArrayUtils;%0A
 import org.b
@@ -791,16 +791,16 @@
 ontext;%0A
-
 import o
@@ -1183,16 +1183,115 @@
 ation;%0A%0A
+import java.lang.management.ManagementFactory;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import j
@@ -1372,56 +1372,8 @@
 t;%0A%0A
-import java.lang.management.ManagementFactory;%0A%0A
 @Tra
@@ -1745,79 +1745,237 @@
  %7B%0A%0A
-%09private static MergeClassPathXMLApplicationContext mergeContext = null
+    @PersistenceContext(unitName = %22blPU%22)%0A    protected EntityManager em;%0A%0A    protected static MergeClassPathXMLApplicationContext mergeContext = null;%0A%0A    protected static List%3CString%3E moduleContexts = new ArrayList%3CString%3E()
 ;%0A%09%0A
@@ -2040,20 +2040,16 @@
 ext() %7B%0A
-%09%09%09%0A
 %09%09try %7B%0A
@@ -2392,16 +2392,130 @@
 ing%5B%5D%7B%7D;
+%0A%0A                additionalContexts = (String%5B%5D) ArrayUtils.addAll(additionalContexts, moduleContexts.toArray());
 %0A%09%09%09%09%0A%09%09
@@ -2671,16 +2671,17 @@
 e);%0A%09%09%7D%0A
+%0A
 %09%09return
@@ -2702,81 +2702,101 @@
 %0A%09%7D%0A
-%09%0A%09@PersistenceContext(unitName = %22blPU%22)%0A    protected EntityManager em;
+%0A    protected static List%3CString%3E getModuleContexts() %7B%0A        return moduleContexts;%0A    %7D
 %0A%0A%7D%0A

Add support for module specific configurations in integration tests