GenericTest.java :  » Workflow-Engines » wfmopen-2.1.1 » load » Java Open Source

Java Open Source » Workflow Engines » wfmopen 2.1.1 
wfmopen 2.1.1 » load » GenericTest.java
/*
 * This file is part of the WfMOpen project.
 * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * $Id: GenericTest.java,v 1.2 2006/09/29 12:32:07 drmlipp Exp $
 *
 * $Log: GenericTest.java,v $
 * Revision 1.2  2006/09/29 12:32:07  drmlipp
 * Consistently using WfMOpen as projct name now.
 *
 * Revision 1.1.1.2  2004/08/18 15:17:39  drmlipp
 * Update to 1.2
 *
 * Revision 1.12  2004/01/27 11:45:32  lipp
 * Preserve newlines when reading process definitions.
 *
 * Revision 1.11  2003/11/20 17:25:17  weidauer
 * made cleaning operations static
 *
 * Revision 1.10  2003/11/04 16:49:33  weidauer
 * added initializing of process relevant data
 *
 * Revision 1.9  2003/10/30 16:22:51  weidauer
 * added cleaning suite
 *
 * Revision 1.8  2003/10/30 10:58:28  weidauer
 * fixed removeAllProcesses
 *
 * Revision 1.7  2003/10/28 07:54:05  barzik
 * *** empty log message ***
 *
 * Revision 1.6  2003/10/22 16:10:06  weidauer
 * initial version
 *
 */
package load; 

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

import java.util.Iterator;
import java.util.Collection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.rmi.NoSuchObjectException;

import javax.security.auth.login.LoginException;

import common.STProjectLoginContext;

import de.danet.an.util.junit.EJBClientTest;

import de.danet.an.workflow.api.DefaultRequester;
import de.danet.an.workflow.api.FactoryConfigurationError;
import de.danet.an.workflow.api.Process;
import de.danet.an.workflow.api.ProcessDefinitionDirectory;
import de.danet.an.workflow.api.ProcessDirectory;
import de.danet.an.workflow.api.ProcessMgr;
import de.danet.an.workflow.api.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;
import de.danet.an.workflow.omgcore.WfRequester;
import de.danet.an.workflow.omgcore.ProcessData;


/**
 * This class can be used in two ways: <br>
 * It may define a test suite based on process defintion of process P0 in XPDL
 * file "DefaultForGenericTest.xml" (simple JUnit test) OR <br>
 * It can be used in a generic way by defining a particulary XPDL file 
 * containing the process P0 process defintion (e. g. used for load test) using
 * the appropriate constructor.
 *
 * @author <a href="mailto:weidauer@danet.de">Christian Weidauer</a>
 * @version 1.0
 */
public class GenericTest extends TestCase {

    /**
     * The package id of the process definition of process P0
     */
    private String packageID = null;

    /**
     * The file that contains the process definition of process P0
     */
    private String xpdlFile = null;

    /**
     * The intial process data which is supposed to be set.
     */
    private ProcessData initialProcessData = null;

    private static WorkflowService wfsCache = null;

    protected static WorkflowService workflowService() {
  if (wfsCache == null) {
      try {
    WorkflowServiceFactory wfsf 
        = WorkflowServiceFactory.newInstance ();
    wfsCache = wfsf.newWorkflowService();
      } catch (FactoryConfigurationError e) {
    throw new IllegalStateException (e.getMessage());
      }
  }
  return wfsCache;
    } 

    protected static STProjectLoginContext plc = null;
    static {
  try {
      plc = new STProjectLoginContext();
      plc.login();
  } catch (LoginException e) {
      throw new IllegalStateException (e.getMessage ());
  }
    }


    /**
     * process manager
     */
    private static ProcessMgr processManager = null;

    /**
     * Constructor of this TestCase
     * @param name a <code>String</code> value
     */
    public GenericTest(String name) {
  super(name);
  packageID = "DefaultForGenericTest";
  xpdlFile = "/load/" + packageID + ".xml";
    }

    /**
     * Constructor of this TestCase
     * @param name test case
     * @param packageID ID of package of process P0
     */
    public GenericTest(String name, String packageID) {
  super(name);
  this.packageID = packageID;
  xpdlFile = "/load/" + packageID + ".xml";
    }

    /**
     * Constructor of this TestCase
     * @param name test case
     * @param packageID ID of package of process P0
     * @param initialProcessData process data which is supposed to be set
     */
    public GenericTest(String name, String packageID, 
           ProcessData initialProcessData) {
  super(name);
  this.packageID = packageID;
  xpdlFile = "/load/" + packageID + ".xml";
  this.initialProcessData = initialProcessData;
    }

    private static TestSuite cleaningSuite = new TestSuite("cleaningSuite");
    static {
  cleaningSuite.addTest(new GenericTest("closeAllCloseableProcesses"));
  cleaningSuite.addTest(new GenericTest("waitForNoRunningProcesses"));
  cleaningSuite.addTest(new GenericTest("removeAllProcesses"));
    }

    /**
     * Deliver a suite for cleaning the processes (including closing, waiting 
     * for not running and removing). 
     * @return a <code>Test</code> value
     */
    public static TestSuite cleaningSuite() {
  return cleaningSuite;
    }

    /**
     * Construct this test suite. 
     * @return a <code>Test</code> value
     */
    public static Test suite() {
        TestSuite suite = new TestSuite();
     suite.addTest(new GenericTest("prepareForCreateProcess"));
  suite.addTest(new GenericTest("createProcess"));
  suite.addTest(new GenericTest("createAndStartProcess"));
  suite.addTest(new GenericTest("removeAllProcesses"));
        return new EJBClientTest (plc, suite);
    }

    /**
     * Prepare for creating a process.
     * @exception Exception if an error occurs
     */
    public void prepareForCreateProcess() throws Exception {

  // import the process definitions from the XPDL file
  ProcessDefinitionDirectory pdd 
      = workflowService().processDefinitionDirectory();
  InputStream is = getClass().getResourceAsStream(xpdlFile);
  assertTrue (is != null);
  BufferedReader br = new BufferedReader
      (new InputStreamReader(is, "ISO-8859-1"));
  StringBuffer sb = new StringBuffer();
  String st;
  while ((st = br.readLine()) != null) {
      sb.append(st + "\n");
  }
  pdd.importProcessDefinitions(sb.toString()); 
  Collection processDefinitions = pdd.processDefinitions();
  assertTrue (processDefinitions.size() > 0);

  // prepare the creation of process packageID/"P0"
  try {
      processManager = pdd.processMgr(packageID, "P0");
      defReqCache = defaultRequester();
  } catch (Exception e) {
      e.printStackTrace();
  }
    }


    /**
     * Create the process of id="P0".
     * @exception Exception if an error occurs
     */
    public void createProcess() throws Exception {
  Process process = createProcess(packageID, "P0");
    }

    /**
     * Create and start the process of id="P0".
     * @exception Exception if an error occurs
     */
    public void createAndStartProcess() throws Exception {
  Process process = createProcess(packageID, "P0");
  process.start();
    }

    /**
     * Create and start the process of id="P0".
     * @exception Exception if an error occurs
     */
    public void createStartAndInitializeProcess() throws Exception {
  Process process = createProcess(packageID, "P0");
  process.setProcessContext(initialProcessData);
  process.start();
    }

     /**
     * Removes all processes created.
     * @exception Exception if an error occurs
     */
    public static void removeAllProcesses() throws Exception {
  // loop for all processes created
  ProcessDirectory pd = workflowService().processDirectory();
  Collection processes = pd.processes();
  for (Iterator it=processes.iterator(); it.hasNext();) {
      try {
    Process process = (Process)(it.next());
    pd.removeProcess(process);
      } catch (Exception e) {
    // process not in database anymore, so it is not running anymore
      }
  }
    }

     /**
     * Close (terminate, abort) all processes.
     * @exception Exception if an error occurs
     */
    public static void closeAllCloseableProcesses() throws Exception {
  // loop for all processes created
  ProcessDirectory pd = workflowService().processDirectory();
  Collection processes = pd.processes();
  for (Iterator it=processes.iterator(); it.hasNext();) {
      Process process = (Process)(it.next());
      try {
    process.abort();
      } catch (Exception e) {
    try {
        process.terminate();
    } catch (Exception ex) {
    }
      }
  }
    }

     /**
     * Wait until no processes are running anymore.
     * @exception Exception if an error occurs
     */
    public static void waitForNoRunningProcesses() throws Exception {
  // loop for all processes created
  ProcessDirectory pd = workflowService().processDirectory();
  boolean runningProcesses = false;
  do {
      runningProcesses = false;
      Collection processes = pd.processes();
      for (Iterator it=processes.iterator();it.hasNext();) {
    try {
        Process process = (Process)(it.next());
        if (process.state().startsWith("open.running")) {
      runningProcesses = true;
        }    
    } catch (NoSuchObjectException e) {
        // object not database anymore, so it is not running anymore
    }
      }
      Thread.sleep(100);
  } while (runningProcesses);
    }

    private static WfRequester defReqCache = null;

    protected WfRequester defaultRequester ()
  throws RemoteException {
  if (defReqCache == null) {
      defReqCache = new DefaultRequester (workflowService());
  }
  return defReqCache;
    }

    /**
     * Describe <code>createProcess</code> method here.
     *
     * @param pkgId a <code>String</code> value
     * @param prcId a <code>String</code> value
     * @return a <code>Process</code> value
     * @exception Exception if an error occurs
     */
    protected Process createProcess (String pkgId, String prcId)
        throws Exception {
  ProcessDefinitionDirectory pdd 
      = workflowService().processDefinitionDirectory();
  ProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
  Process process = (Process) pmgr.createProcess (defaultRequester());
  return process;
    }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.