package org.treesched.regression;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestResult;

import org.apache.log4j.Logger;
import org.treesched.Schedule;
import org.treesched.ant.TopScheduleElement;

/**
 * 
 */
public class SingleTestSchedule 
implements Test {

    private static Logger logger = Logger.getLogger(SingleTestSchedule.class); 

    private Schedule schedule;
	private List runs = new ArrayList();
	private String name;

	public void setName(String name) {
	    this.name = name;
	}
	
	public String getName() {
	    return this.name;
	}
	
	public void addRun(TestScheduleRun run) throws Exception {
		runs.add(run);
	}
	
	public void addSchedule(TopScheduleElement schedule) {
	    this.schedule = schedule;
	}

	public int countTestCases() {
		
		return runs.size();
	}

	public void run(TestResult tr) {
		
		System.out.println("Running ScheduleTest: " + name);
	
		for (Iterator it = runs.iterator(); it.hasNext(); ) {
			tr.startTest(this);
			TestScheduleRun test = (TestScheduleRun)it.next();
			if (!test.testSchedule(schedule)) {
				tr.addFailure(this, new AssertionFailedError(
					"Not as expected, time " + test.getClass() + ", in test " + name));
			}
			
			tr.endTest(this);
		}
		
	}

}
