/*
 * Copyright 2004-2005 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 grails.util;

import java.lang.reflect.Modifier;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.support.GrailsTestSuite;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 
 * 
 * @author Steven Devijver
 * @since Aug 8, 2005
 */
public class RunTests {

	private static Log log = LogFactory.getLog(RunTests.class);
	
	public static void main(String[] args) {
		try {
			ConfigurableApplicationContext appCtx = (ConfigurableApplicationContext)GrailsUtil.bootstrapGrailsFromClassPath();
			GrailsApplication application = (GrailsApplication)appCtx.getBean(GrailsApplication.APPLICATION_ID);

			Class[] allClasses = application.getAllClasses();
	        log.debug("Going through ["+allClasses.length+"] classes");
	        TestSuite s = new TestSuite();
			for (int i = 0; i < allClasses.length; i++) {
				Class clazz = allClasses[i];
				if (TestCase.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {
					log.debug("Adding test [" + clazz.getName() + "]");
					s.addTest(new GrailsTestSuite(appCtx.getBeanFactory(), clazz));
				} else {
					log.debug("[" + clazz.getName() + "] is not a test case.");
				}
			}
			TestRunner.run(s);			
		}
		finally {
			System.exit(0);	
		}		
	}
}

@@ -960,98 +960,443 @@
 ils.
-support.GrailsTestSuite;%0Aimport org.springframework.context.ConfigurableApplicationContext
+commons.spring.GrailsRuntimeConfigurator;%0Aimport org.codehaus.groovy.grails.support.GrailsTestSuite;%0Aimport org.hibernate.Session;%0Aimport org.hibernate.SessionFactory;%0Aimport org.springframework.context.ConfigurableApplicationContext;%0Aimport org.springframework.orm.hibernate3.SessionFactoryUtils;%0Aimport org.springframework.orm.hibernate3.SessionHolder;%0Aimport org.springframework.transaction.support.TransactionSynchronizationManager
 ;%0A%0A/
@@ -2375,16 +2375,324 @@
 %09%7D%0A%09%09%09%7D%0A
+%09%09%09SessionFactory sessionFactory = (SessionFactory)appCtx.getBean(GrailsRuntimeConfigurator.SESSION_FACTORY_BEAN);%0A%09%09%09try %7B%0A%09%09%09%09Session session = SessionFactoryUtils.getSession(sessionFactory, true);%0A%09%09        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); %09%09%09%09%0A%09
 %09%09%09TestR
@@ -2704,19 +2704,255 @@
 .run(s);
+%0A
 %09%09%09
+%7D%0A%09%09%09finally %7B%0A%09%09        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);%0A%09%09        SessionFactoryUtils.releaseSession(sessionHolder.getSession(), sessionFactory);%09%09%09%09%0A%09%09%09%7D
 %0A%09%09%7D%0A%09%09f

fix for GRAILS-268 where unit tests are not using a single hibernate session