001: /* Scheduler.java */
002:
003: package org.quilt.frontend.ant;
004:
005: import java.util.Vector;
006:
007: import org.apache.tools.ant.Project;
008: import org.apache.tools.ant.Task;
009: import org.quilt.framework.QuiltTest;
010:
011: /**
012: * Collects individual and batch tests during initial processing of the
013: * Ant build.xml file, then schedules tests for running. Before running
014: * any tests, batch tests are unpacked and attributes assigned.
015: */
016: public class Scheduler {
017:
018: private QuiltTask task = null;
019: private TaskControl tc = null;
020: private boolean batched = true;
021:
022: /** Vector of QuiltTests */
023: private Vector tests = new Vector();
024: /** Index into that Vector. */
025: private int testIndex = 0;
026:
027: /** Vector of BatchTests */
028: private Vector batchTests = new Vector();
029: /** Index into Vector of BatchTests. */
030: private int batchIndex = 0;
031:
032: /** All tests are clones of this one. */
033: private QuiltTest modelTest = new QuiltTest();
034:
035: /** One-arg constructor called at beginning of run */
036: public Scheduler(QuiltTask t) {
037: task = t;
038: tc = new TaskControl(task);
039: }
040:
041: // SCHEDULING METHODS /////////////////////////////////
042: /**
043: * Ant-compatible method for adding a batch test. When Ant
044: * encounters a <batchtest ... it calls QuiltTask.createBatchTest
045: * to get an instance, then uses the set* methods to set test
046: * parameters for the batch. QuiltTask uses this method to pass
047: * the batch test to the Scheduler.
048: *
049: * @param bt BatchTest instance created by QuiltTask
050: */
051: public void addBatchTest(BatchTest bt) {
052: task.log("--> Scheduler.addBatchTest", Project.MSG_VERBOSE);
053: batchTests.addElement(bt);
054: }
055:
056: /**
057: * Ant-compatible method for adding test to queue. When Ant
058: * counters a <test name=... it uses this method to create
059: * the QuiltTest object and then calls set* methods to set
060: * test parameters.
061: *
062: * @param test QuiltTest structure containing test parameters.
063: */
064: public void addTest(QuiltTest test) {
065: task.log("--> Scheduler.addTest", Project.MSG_VERBOSE);
066: tests.addElement(test);
067: }
068:
069: /**
070: * Zero out indexes into lists of tests and batch tests. This
071: * will be called whenever the user wants to rescan the lists of
072: * tests and batch tests.
073: */
074: public void schedule() {
075: testIndex = 0;
076: batchIndex = 0;
077:
078: task
079: .log(
080: "\n===========================================================\n"
081: + "--> Scheduler.schedule: there are "
082: + tests.size()
083: + " tests and "
084: + batchTests.size()
085: + " batch tests"
086: + "\n===========================================================\n",
087: Project.MSG_DEBUG);
088: }
089:
090: /**
091: * Returns the next test or batch test available.
092: *
093: * @return QuiltTest-compatible structure
094: */
095: public QuiltTest nextTest() {
096: if (testIndex < tests.size()) {
097: return (QuiltTest) tests.elementAt(testIndex++);
098: } else if (batchIndex < batchTests.size()) {
099: return (BatchTest) batchTests.elementAt(batchIndex++);
100: } else {
101: return null;
102: }
103: }
104:
105: /**
106: * Pass through the list of batch tests, creating individual tests
107: * and passing these to the scheduler.
108: */
109: public void unbatch() {
110: task.log("--> Scheduler.unbatch", Project.MSG_VERBOSE);
111: for (int i = 0; i < batchTests.size(); i++) {
112: ((BatchTest) batchTests.elementAt(i)).unbatch(this );
113: }
114: batched = false;
115: batchTests = new Vector();
116: }
117:
118: // GET/SET METHODS //////////////////////////////////////////////
119: /**
120: * Get a reference to the TaskControl object created by the
121: * constructor.
122: * */
123: public TaskControl getTaskControl() {
124: return tc;
125: }
126: }
|