1 package com.flexiblewebsolutions.xdriveunit.loadtest;
2
3 import java.io.File;
4 import java.util.Iterator;
5 import java.util.Vector;
6
7 import org.apache.log4j.Logger;
8
9 import com.flexiblewebsolutions.io.util.FileUtils;
10 import com.flexiblewebsolutions.xdriveunit.DelayTimer;
11 import com.flexiblewebsolutions.xdriveunit.LoadWebXDriveTestCase;
12
13 /***
14 * Create threads to run concurrent tests.
15 *
16 * @author Donavon Buss
17 */
18 public abstract class ThreadedTestRunner
19 {
20 static Logger logger = Logger.getLogger( ThreadedTestRunner.class.getName() );
21 /***
22 * The name of the test suite to be run.
23 */
24 private String _TestMode = null;
25
26 /***
27 * A list of threads to be run
28 */
29 protected Vector _AllThreads = new Vector();
30
31 /***
32 * Flag that determines if test will actually be run or not
33 */
34 protected boolean _RunTest = true;
35
36 /***
37 * Timer that determines thread start offset
38 */
39 protected DelayTimer _DelayTimer = new DelayTimer();
40
41 /***
42 * Returns a file path used for temporary files.
43 * @return Path where temporary files are stored.
44 */
45 protected abstract File getTmpDirectory();
46
47 /***
48 * Iterate through vector of StringBuffer input files and create a thread to
49 * process each one.
50 *
51 * @param pXML -
52 * List of StringBuffers which contain XML needed for test
53 * @param pClassName -
54 * Class of test class
55 * @param pConfigTemplate -
56 * Config file to be passed to test containing test methods, etc.
57 */
58 public void runLoadTest( Vector pXML, LoadWebXDriveTestCase pTestCase,
59 String pConfigTemplate, ThreadCallback pTracer )
60 {
61 File writeDir = getTmpDirectory();
62 pTracer.setTmpDir( writeDir );
63
64 ThreadTracker tt = new ThreadTracker( pXML.size(), pTracer );
65
66 int users = 1;
67 for( Iterator it = pXML.iterator(); it.hasNext(); users++ )
68 {
69 String index = "0000" + users;
70
71 String trueIndex = index.substring( index.length() - 4,
72 index.length() );
73 index = null;
74 String threadName = "User" + trueIndex;
75
76 String inputFile = threadName + _TestMode + ".xml";
77 StringBuffer thisXML = (StringBuffer) it.next();
78 FileUtils fu = new FileUtils();
79 fu.writeFile( new File( writeDir, inputFile ), thisXML );
80 fu = null;
81 ThreadTestLoadUtils ltu = new ThreadTestLoadUtils();
82
83 StringBuffer buildConfig = ltu.createConfigFile( pConfigTemplate,
84 writeDir + "/" + inputFile );
85 ltu = null;
86 thisXML = null;
87
88 if( _RunTest )
89 {
90 logger.debug( "Starting Thread: " + threadName );
91 TestThread thread = new TestThread( pTestCase,
92 threadName, buildConfig, tt );
93 thread.start();
94
95 _DelayTimer.clickDelay();
96 }
97 }
98 }
99
100 /***
101 * Retrieves name for this test run
102 *
103 * @return test run name used for output file naming
104 */
105 public String getTestMode()
106 {
107 return _TestMode;
108 }
109
110 /***
111 * Sets name for this test run
112 *
113 * @param pTestMode
114 * name of test run to be added to output files
115 */
116 public void setTestMode( String pTestMode )
117 {
118 _TestMode = pTestMode;
119 }
120 }