/*
 * 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) {
		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);
		System.exit(0);
	}
}

@@ -1244,16 +1244,25 @@
 args) %7B%0A
+%09%09try %7B%0A%09
 %09%09Config
@@ -1372,16 +1372,17 @@
 th();%0A%09%09
+%09
 GrailsAp
@@ -1478,11 +1478,10 @@
 D);%0A
-%09%09
 %0A
+%09
 %09%09Cl
@@ -1528,16 +1528,17 @@
 sses();%0A
+%09
         
@@ -1597,16 +1597,17 @@
 sses%22);%0A
+%09
         
@@ -1639,16 +1639,17 @@
 te();%0A%09%09
+%09
 for (int
@@ -1686,16 +1686,17 @@
  i++) %7B%0A
+%09
 %09%09%09Class
@@ -1722,16 +1722,17 @@
 %5Bi%5D;%0A%09%09%09
+%09
 if (Test
@@ -1815,24 +1815,25 @@
 s())) %7B%0A%09%09%09%09
+%09
 log.debug(%22A
@@ -1872,16 +1872,17 @@
 + %22%5D%22);%0A
+%09
 %09%09%09%09s.ad
@@ -1944,16 +1944,17 @@
 z));%0A%09%09%09
+%09
 %7D else %7B
@@ -1954,16 +1954,17 @@
  else %7B%0A
+%09
 %09%09%09%09log.
@@ -2026,14 +2026,17 @@
 %0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A
+%09
 %09%09Te
@@ -2051,17 +2051,37 @@
 .run(s);
-%0A
+%09%09%09%0A%09%09%7D%0A%09%09finally %7B%0A%09
 %09%09System
@@ -2089,14 +2089,21 @@
 exit(0);
+%09%0A%09%09%7D%09%09
 %0A%09%7D%0A%7D%0A

put exit into finally block to avoid the process continuing when an exception occurs fixing GRAILS-276