Package net.fp.rp.drools

Source Code of net.fp.rp.drools.SpringDecisionTableLoaderTest$TestThread

package net.fp.rp.drools;

import java.io.File;

import junit.framework.TestCase;
import net.fp.rp.common.exception.RpException;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
* Tests the functionality provided by the decision table loader
* Uses the config file @see SpringDecisionTableLoaderTest-AppContext.xml
* @author Paul
*
*/
public class SpringDecisionTableLoaderTest extends TestCase {

  /**
   * Where to find the Spring Context that we use for testing
   */
  public static final String TEST_APP_CONTEXT="core/test/net/fp/rp/drools/SpringDecisionTableLoaderTest-AppContext.xml";
 
  /**
   * The Name of the Spring Bean under test
   */
  public static final String TEST_BEAN="SpringDecisionTableLoaderBean";
 
   /** Logger for this class and subclasses */
    protected final Logger log = Logger.getLogger(getClass());
 
    //The application context used by this test
    ApplicationContext context = null;
   
  public void setUp() throws Exception{
   
   
    context = new FileSystemXmlApplicationContext(TEST_APP_CONTEXT);
   
  }
 
  /**
   * Carry out a simple load of rules test
   *
   */
    public void testInitialise() throws Throwable{
        SpringDecisionTableLoader myBean  = (SpringDecisionTableLoader) context.getBean(TEST_BEAN);
         log.debug(""+myBean.getExcelFileName());
         //test
         File myFile = new File(".");
         log.debug(myFile.getAbsolutePath());
         myFile = new File(myBean.getExcelFileName());
        myBean.initialize();
       
     
    }
 
    /**
   * Carry out a simple rules test
   *
   */
    public void testSimpleDecision() throws Throwable{
       
       //Get the DecisionTableBean
        SpringDecisionTableLoader droolsBean  = (SpringDecisionTableLoader) context.getBean(TEST_BEAN);
        droolsBean.initialize();
     
        //Setup test values
        SimpleFlowBean testBean = new SimpleFlowBean();
        testBean.setInput1("test");
       
        //Apply the Rules to this bean
        testBean.setInput2("TestInput2");
       testBean=(SimpleFlowBean)droolsBean.executeDecisionTable(testBean,false);
       assertEquals("TestOutput2",testBean.getOutput());
   
        //Clear working memory between tests
       testBean.setInput1("test");
        testBean.setInput2("TestInput1");
        testBean=(SimpleFlowBean)droolsBean.executeDecisionTable(testBean,false);
        assertEquals("TestOutput1",testBean.getOutput());    
       
        log.debug(testBean);
     
    }
   
  /**
   * Carry out a multi-threaded test on our simple decision table
   */
  public void testMultiThread() throws Throwable{
   
    //Load our drools bean
      SpringDecisionTableLoader mainDroolsBean  = (SpringDecisionTableLoader) context.getBean(TEST_BEAN);
      mainDroolsBean.initialize();
   
    //Create and start set of threads
    TestThread[] threads = new TestThread[5];
    for (int a=0;a<threads.length;a++){
      threads[a] = new TestThread();
      threads[a].droolsBean = mainDroolsBean;
      threads[a].input1Value="test"+(a+1);
      threads[a].input2Value="TestInput"+(a+1);
      threads[a].expectedOutputValue="TestOutput"+(a+1);
     
    };
    for (int a=0;a<threads.length;a++){
      threads[a].start();
    }
   
    //Wait until all threads are complete
    for (int a=0;a<threads.length;a++){
      while(!threads[a].complete){
        //loop until thread a complete
      }
    }
   
  log.debug("Multithread-test successfully completed")
 
   
  }
 
  /**
   * Inner class used in the testMultiThread() method
   * @author paul.browne
   *
   */
  class TestThread extends Thread{
   
    //Local Variables
     SpringDecisionTableLoader droolsBean=null;
     String input1Value =null;
     String input2Value =null;
     String expectedOutputValue =null;
     boolean complete=false;
     int a=0;
   
    public void run(){
     
     
      //Create Simple Bean
       SimpleFlowBean testBean = new SimpleFlowBean();
       testBean.setInput1("test");
      try{
        for( a=0; a<100; a++){
         
              //Apply the Rules to this bean
              testBean.setInput2(input2Value);
             testBean=(SimpleFlowBean)droolsBean.executeDecisionTable(testBean,false);
             assertEquals("Assertion Failed in Thread input2Value:"+input2Value+" Iteration:"+a,expectedOutputValue,testBean.getOutput());
             //log.debug("Test Passed Input2Value:"+input2Value+" Iteration:"+a);
       
             //Yield so another test can run
             sleep(5); //PB comment out - we get mix of threads without this wait
        }
      } catch (RpException rpe){
        log.debug("Exception in Thread. Input2Value:"+input2Value+" Iteration:"+a,rpe);
      }catch (InterruptedException ie){
        log.debug("Exception in Thread. Input2Value:"+input2Value+" Iteration:"+a,ie);
      } finally{
        complete=true;
      }
     
    }
   
  }
 
 
 
}
TOP

Related Classes of net.fp.rp.drools.SpringDecisionTableLoaderTest$TestThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.