View Javadoc

1   package com.flexiblewebsolutions.xdriveunit;
2   
3   import java.io.File;
4   import java.lang.reflect.InvocationTargetException;
5   import java.lang.reflect.Method;
6   
7   import junit.framework.Test;
8   import junit.framework.TestCase;
9   
10  import org.apache.log4j.Logger;
11  
12  import com.flexiblewebsolutions.io.util.FileUtils;
13  import com.flexiblewebsolutions.xml.util.XMLStringParser;
14  
15  /***
16   * Runs a variety of tests from different classes based on an input xml file.
17   * 
18   * @author Donavon Buss
19   */
20  public abstract class ScriptRunner extends TestCase {
21      static Logger logger = Logger.getLogger( ScriptRunner.class.getName() );
22      public abstract File getTestDirectory();
23      
24  	/***
25  	 * Builds a test script based on an xml configuration. 
26  	 * 
27  	 * @return A Test Suite.
28  	 */
29  	public Test initScript( String pConfigFile ) {
30  		File scriptConfig = new File(getTestDirectory(), pConfigFile);
31  		if (scriptConfig.exists()) {
32  
33  			FileUtils fu = new FileUtils();
34  			StringBuffer fileContents = fu.loadXMLFileToString(scriptConfig);
35  			TestCollection script = new TestCollection();
36  			String scriptVal = XMLStringParser.getValueForTag(fileContents,
37  					"testscript");
38  			loadSteps(script, new StringBuffer(scriptVal));
39  			return (script.getTest());
40  		} else {
41  		    logger.error( "Script configuration: " + scriptConfig.getAbsolutePath() + " is required but was not found.");
42  			return null;
43  		}
44  	}
45  
46  	/***
47  	 * Parse the script configuration file.
48  	 * 
49  	 * @param pScript -
50  	 *            TestSuite to be populated with tests.
51  	 * @param pFileContents -
52  	 *            Information from the passed in script config.
53  	 */
54  	private static void loadSteps(TestCollection pScript,
55  			StringBuffer pFileContents) {
56  	    logger.debug( "in ScriptRunner.loadSteps" );
57  		String[] scriptVals = XMLStringParser.getAllNestedTags(pFileContents
58  				.toString());
59  		for (int i = 0; i < scriptVals.length; i++) {
60  //			System.err.println("Script val: " + scriptVals[i]);
61  			String thisTag = scriptVals[i];
62  			if( thisTag.startsWith( "<test" ) )
63  			{
64  				thisTag = XMLStringParser.getValueForTag( new StringBuffer( thisTag ), "test" );
65  			}
66  			
67  			if (thisTag.startsWith("<decorator>")) {
68  			    logger.debug( "parse decorator: " + thisTag );
69  				StringBuffer tagValue = new StringBuffer(XMLStringParser
70  						.getValueForTag(new StringBuffer(thisTag), "decorator"));
71  				TestCollection thisSuite = new TestCollection();
72  				loadSteps(thisSuite, tagValue);
73  				TestDecoratorLoader tdl = new TestDecoratorLoader();
74  				Test newTest = tdl.wrapTestWithDecorator(tagValue, thisSuite
75  						.getTest());
76  				pScript.addTest(newTest);
77  			} else if (thisTag.startsWith("<step>")) {
78  			    logger.debug( "parse step: " + thisTag );
79  				String classname = XMLStringParser.getValueForTag(
80  						new StringBuffer(thisTag), "classname");
81  				String config = XMLStringParser.getValueForTag(new StringBuffer(
82  						thisTag), "configuration");
83  				Test ts = loadTestSuite(classname, config);
84  				pScript.addTest(ts);
85  			}
86  		}
87  		scriptVals = null;
88  	}
89  
90  	/***
91  	 * Use reflection to invoke suite method with specific configuration
92  	 * 
93  	 * @param pClassName
94  	 *            Class name of tests to be run
95  	 * @param pConfig
96  	 *            Configuration file that contains information for this invoke
97  	 * @return Populated test suite
98  	 */
99  	private static Test loadTestSuite(String pClassName, String pConfig) {
100 		Test tests = null;
101 
102 		try {
103 			Class thisClass = Class.forName(pClassName);
104 			Method suiteMethod = thisClass.getMethod("suite",
105 					new Class[] { String.class });
106 			tests = (Test) suiteMethod.invoke(thisClass,
107 					new Object[] { pConfig });
108 		} catch (SecurityException e) {
109 		    logger.error( e.getMessage(), e );
110 		} catch (IllegalArgumentException e) {
111 		    logger.error( e.getMessage(), e );
112 		} catch (ClassNotFoundException e) {
113 		    logger.error( e.getMessage(), e );
114 		} catch (NoSuchMethodException e) {
115 		    logger.error( e.getMessage(), e );
116 		} catch (IllegalAccessException e) {
117 		    logger.error( e.getMessage(), e );
118 		} catch (InvocationTargetException e) {
119 		    logger.error( e.getMessage(), e );
120 		}
121 		return tests;
122 	}
123 
124 }