/*
 * Copyright (C) 1999-2007 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
 * 
 * The Regression Test is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package rbsla.regressiontest;

import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import rbsla.wrapper.ProvaWrapper;
import org.mandarax.kernel.ResultSet;
import rbsla.wrapper.KnowledgeBaseWrapper;

import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Base classes for a automatic regression test system for ContractLog rules.
 * The system is able to automaticly execute the test and create a JUnit Report from it.
 */
public class RegressionTest extends TestCase
{

 	
	protected String tc=null; // test case
	protected String lp=null; // logic program
	protected ProvaWrapper wrapper=null;
	
	/**
	 * Constructor for stand-alone test cases which consist of rules, facts and tests. 
	 * @param test  test file
	 */
	public RegressionTest(String test) {
		super("test");
		tc = test;		
		if( tc.equals("test") )
			tc = "examples/test_cases/basics/cutTestCase.prova";
	}
	
	/**
	 * Constructor for a test case which test the specified logic program.
	 * @param file the logic program
	 * @param test the test case
	 */
	public RegressionTest(String file, String test) {
		super("test");		
		lp = file;
		tc = test;
	}
	

	
	protected void setUp() {
		if (lp!=null) {
			wrapper = new ProvaWrapper(lp);
			wrapper.consult("consult("+tc+").");
		}
		else wrapper = new ProvaWrapper(tc);
	}
	
	protected void tearDown() {		
		wrapper.consult("unconsult(\'"+tc+"\').");		
	}
	

	/**
	 * run the test case
	 * @throws Exception
	 */
	public void test() throws Exception {
		setName(tc); // set test case name
		StringBuffer type = new StringBuffer("");
		boolean status = false;
		
		// positive test
		ResultSet rs = wrapper.consult("testSuccess(Name,Message)");
		boolean more = rs.first();
		while (more) {
			Object name = rs.getResult(Object.class, "Name"); 			
			Object message = rs.getResult(Object.class, "Message");
			type=type.append("++++  "+name+" succeeded "+message+"  ++++   |   ");			
			more = rs.next();
			status = true;
	    } 
		
		// negative test
		rs = wrapper.consult("testFailure(Name,Message)");
		more = rs.first();
		while (more) {
			Object name = rs.getResult(Object.class, "Name"); 			
			Object message = rs.getResult(Object.class, "Message");
			type=type.append("----  "+name+" failed - "+message+"  ----   |   ");
			status = false;
			more = rs.next();
	    } 
				    
		
		if (status) assertTrue(type.toString(),true);
		else assertTrue(type.toString(),false);		
		
	}


}