1 package com.flexiblewebsolutions.xdriveunit;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.util.HashMap;
6
7 import junit.framework.Test;
8 import junit.framework.TestCase;
9
10 import org.apache.log4j.Logger;
11
12 import com.flexiblewebsolutions.exceptions.MissingConfigException;
13 import com.flexiblewebsolutions.io.util.FileUtils;
14 import com.flexiblewebsolutions.xml.util.XMLStringParser;
15
16 /***
17 * A TestCase implementation that uses XML files to drive test behavior
18 *
19 * @author Donavon Buss
20 */
21 public abstract class XDriveTestCase extends TestCase implements
22 XDriveTest
23 {
24 static Logger logger = Logger.getLogger( XDriveTestCase.class.getName() );
25
26 /***
27 * Data that will be available to test methods during execution.
28 */
29 protected StringBuffer _TestInput = null;
30
31 /***
32 * Name of thread for differentiating test instances.
33 */
34 protected String _ThreadName = null;
35
36 /***
37 * Define behavior of test based on options
38 */
39 private HashMap _TestOptions = null;
40
41 /***
42 * Init the web client test case
43 *
44 * @param pName -
45 * Name of test to be run
46 * @param pTestInput -
47 * data needed for input during test
48 * @param pTestOptions -
49 * different options that control behavior of all tests
50 * @param pTheadName -
51 * User supplied name of thread running test
52 */
53 public XDriveTestCase( String pName, StringBuffer pTestInput,
54 StringBuffer pTestOptions, String pThreadName )
55 {
56 super( pName );
57 _TestInput = pTestInput;
58
59 if( pTestOptions != null )
60 {
61 setupTestOptions( pTestOptions );
62 }
63 _ThreadName = pThreadName;
64 }
65
66 protected HashMap getTestOptions()
67 {
68 if( _TestOptions == null )
69 {
70 _TestOptions = new HashMap();
71 }
72 return( _TestOptions );
73 }
74
75 /***
76 * Internal method only. Needed for loading parameters for test lookup.
77 *
78 */
79 protected XDriveTestCase()
80 {
81 super();
82 }
83
84 /***
85 * Parse out the passed in xml string for different options to be applied to
86 * all tests
87 *
88 * @param pTestOptions -
89 * xml containing test options
90 */
91 protected void setupTestOptions( StringBuffer pTestOptions )
92 {
93 logger.debug( "setupTestOptions" );
94 String testOptions = XMLStringParser.getValueForTag( pTestOptions,
95 "testOptions" );
96 if( testOptions != null )
97 {
98
99 String[] variables = XMLStringParser.getMatchingNestedTags(
100 testOptions, "testVariable" );
101
102 _TestOptions = new HashMap( variables.length );
103
104 for( int i = 0; i < variables.length; i++ )
105 {
106 StringBuffer variableData = new StringBuffer( variables[i] );
107 String varName = XMLStringParser.getValueForTag( variableData,
108 "name" );
109
110
111 String varVal = XMLStringParser.getValueForTag( variableData,
112 "value" );
113 _TestOptions.put( varName, varVal );
114 }
115 variables = null;
116 }
117 }
118
119 /***
120 * Retrieve property from Map and return it's boolean value
121 *
122 * @param pPropName
123 * @return true or false
124 */
125 protected boolean checkBooleanParameter( String pPropName )
126 {
127 boolean returnVal = false;
128 String val = (String) getTestOptions().get( pPropName );
129
130 if( val != null && val.equalsIgnoreCase( "true" ) )
131 {
132 returnVal = true;
133 }
134 else
135 {
136 returnVal = false;
137 }
138 val = null;
139 return ( returnVal );
140 }
141
142 /***
143 * Creates the test suite using an XML configuration file.
144 *
145 * @param pConfigFile -
146 * Relative path of file from TestDirectory setting or current
147 * directory
148 * @return TestSuite
149 */
150 protected Test init( String pConfigFile ) throws MissingConfigException
151 {
152 if( pConfigFile == null )
153 {
154 logger.error( "Path to configuration file was not specified." );
155 throw new MissingConfigException(
156 "Path to configuration file was not specified." );
157 }
158 try
159 {
160 File configFile = getFile( pConfigFile );
161 FileUtils fu = new FileUtils();
162 StringBuffer configText = fu.loadXMLFileToString( configFile );
163 return ( init( configText, "" ) );
164 }
165 catch( FileNotFoundException e )
166 {
167 logger.error( e.getMessage(), e );
168 throw new MissingConfigException( e.getMessage() );
169 }
170 }
171
172 /***
173 * Attempts to load requested file from getTestDirectory and then from the
174 * current directory
175 *
176 * @param pFilePath -
177 * relative path to file
178 * @return Path to the located file
179 * @throws FileNotFoundException
180 */
181 protected File getFile( String pFilePath ) throws FileNotFoundException
182 {
183 File returnFile = null;
184 File testDirFile = new File( getTestDirectory(), pFilePath );
185 if( !testDirFile.exists() )
186 {
187 File thisDirFile = new File( pFilePath );
188 if( !testDirFile.exists() )
189 {
190 throw new FileNotFoundException( "Could not find file in "
191 + testDirFile.getAbsolutePath() + " or "
192 + thisDirFile.getAbsolutePath() );
193 }
194 else
195 {
196 returnFile = thisDirFile;
197 }
198 thisDirFile = null;
199 }
200 else
201 {
202 returnFile = testDirFile;
203 }
204 testDirFile = null;
205 return ( returnFile );
206 }
207
208 /***
209 * Builds a test suite based on a configuration String.
210 *
211 * @param pConfigInfo
212 * StringBuffer which contains XML configuration for the test.
213 * @param pThreadName
214 * Name to be used to differeniate different threads running same
215 * test.
216 * @return Suite of tests to be run.
217 */
218 protected Test init( StringBuffer pConfigInfo, String pThreadName )
219 {
220 TestSuiteLoader tsl = new TestSuiteLoader();
221 Test returnSuite = null;
222
223 try
224 {
225 returnSuite = tsl.buildSuite( pConfigInfo, pThreadName,
226 getClassName(), getTestDirectory() );
227 }
228 catch( FileNotFoundException e )
229 {
230 logger.error( "Could not load configuration info from " + pConfigInfo, e );
231 e.printStackTrace();
232 }
233 return ( returnSuite );
234 }
235 }