Package process

Source Code of process.Basic

/*
* Danet GmbH
* Beratung und Software-Entwicklung
* Gesch�ftstelle AN
*
* $Id: Basic.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.4  2006/10/13 13:58:32  drmlipp
* Adapted to new environment.
*
* Revision 1.3  2006/10/07 20:41:34  mlipp
* Merged J2EE 1.4 adaptions from test branch.
*
* Revision 1.2  2005/04/22 15:11:07  drmlipp
* Merged changes from 1.3 branch up to 1.3p15.
*
* Revision 1.1.1.3.6.1  2005/04/08 12:32:29  drmlipp
* Added test.
*
* Revision 1.1.1.3  2004/08/18 15:18:47  drmlipp
* Update to 1.2
*
* Revision 1.30  2004/07/18 17:07:32  lipp
* Added check.
*
* Revision 1.29  2004/06/14 19:37:20  lipp
* Fixed assignment functions and cleaned up assignment related
* interfaces.
*
* Revision 1.28  2004/01/27 11:45:33  lipp
* Preserve newlines when reading process definitions.
*
* Revision 1.27  2003/11/18 20:59:12  lipp
* Added ras access.
*
* Revision 1.26  2003/10/30 21:03:49  lipp
* Removed interface EJBObject from ActivityProxy, WebLogic cannot handle
* it.
*
* Revision 1.25  2003/10/21 21:00:45  lipp
* Moved EJBClientTest to new junit sub-package.
*
* Revision 1.24  2003/10/08 11:15:45  huaiyang
* correct compile error.
*
* Revision 1.23  2003/10/08 07:42:34  lipp
* Made tests weblogic compatible.
*
* Revision 1.22  2003/09/19 13:12:29  lipp
* Adapted to closed.completed having a substate.
*
* Revision 1.21  2003/05/14 08:30:07  lipp
* Package import behaviour changed.
*
* Revision 1.20  2003/05/02 14:55:58  lipp
* Resolved some more package dependencies.
*
* Revision 1.19  2003/04/26 16:46:55  lipp
* Made unittests and systemtests coexist in eclipse.
*
* Revision 1.18  2003/04/16 19:25:04  lipp
* Adapted to jdk 1.4
*
* Revision 1.17  2003/02/27 15:14:40  lipp
* Some fixes.
*
* Revision 1.16  2003/02/25 17:08:27  lipp
* Reorganized requester implementation.
*
* Revision 1.15  2003/02/05 15:57:06  lipp
* Replaced DummyRequester with DefaultRequester.
*
* Revision 1.14  2002/11/19 15:14:52  lipp
* New transition manager.
*
* Revision 1.13  2002/11/11 12:20:45  lipp
* Some fixes.
*
* Revision 1.12  2002/11/06 07:54:56  huaiyang
* Add the test of createProcess1AndRemoveProcessDef.
*
* Revision 1.11  2002/10/23 11:36:47  lipp
* Got them running again.
*
* Revision 1.10  2002/10/23 07:29:15  lipp
* Adapted to state handling changes.
*
* Revision 1.9  2002/10/22 17:19:01  lipp
* Still adapting to state handling changes.
*
* Revision 1.8  2002/09/08 18:49:18  lipp
* Proper use of packageId and processId.
*
* Revision 1.7  2002/09/04 20:50:44  lipp
* Adapted to new process manager name schema.
*
* Revision 1.6  2002/08/30 13:37:05  lipp
* Using Workflow engine facade now.
*
* Revision 1.5  2002/08/29 12:49:18  lipp
* Removed no longer needed imports.
*
* Revision 1.4  2002/08/26 20:23:14  lipp
* Lots of method renames.
*
* Revision 1.3  2002/08/21 22:06:48  lipp
* Finished transition to ProcessMgrStub.
*
* Revision 1.2  2002/02/04 16:18:11  huaiyang
* Add the test of activityByKey.
*
* Revision 1.1  2002/02/03 21:41:42  lipp
* Cleaned up unittests.
*
*/
package process;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.Collection;
import java.util.Iterator;

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.security.auth.login.LoginException;

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

import de.danet.an.workflow.omgcore.WfActivity;
import de.danet.an.workflow.omgcore.WfExecutionObject;
import de.danet.an.workflow.omgcore.WfExecutionObject.State;
import de.danet.an.workflow.omgcore.WfProcess;
import de.danet.an.workflow.omgcore.WfProcessMgr;
import de.danet.an.workflow.omgcore.WfRequester;

import de.danet.an.workflow.api.DefaultRequester;
import de.danet.an.workflow.api.FactoryConfigurationError;
import de.danet.an.workflow.api.MethodInvocationBatch;
import de.danet.an.workflow.api.Process;
import de.danet.an.workflow.api.ProcessDefinition;
import de.danet.an.workflow.api.ProcessDefinitionDirectory;
import de.danet.an.workflow.api.ProcessDirectory;
import de.danet.an.workflow.api.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;

import de.danet.an.workflow.ejbs.admin.ProcessDefinitionDirectoryHome;
import de.danet.an.workflow.ejbs.core.WfActivityHome;

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

/**
* Zusammenstellung aller TimerObjectTests.
*/
public class Basic extends TestCase {

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

    /**
     * Konstruktor zum Erzeugen eines TestCase
     */
    public Basic(String name) {
  super (name);
    }

    /**
     * Stellt diese TestSuite zusammen.
     */
    public static Test suite() {
        TestSuite suite = new TestSuite();
     suite.addTest(new Basic("importProcessDefinitions"));
        suite.addTest(new Basic("createProcess2"));
      suite.addTest(new Basic("createProcess3"));
      suite.addTest(new Basic("createAndStartProcess1"));
      suite.addTest(new Basic("createAndStartProcess2"));
    suite.addTest(new Basic("createAndStartProcess3"));
    suite.addTest(new Basic("createAndStartProcessBatched"));
     suite.addTest(new Basic("createProcess1AndRemoveProcessDef"));
        return new EJBClientTest (plc, suite);
    }

    private static WorkflowService wfsCache = null;

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

    /**
     * Import the process definitions from a XPDL file
     * unsing the ProcessDefinitionDirectory bean.
     */
    public void importProcessDefinitions() throws Exception {
  // Create process definition directory bean
        ProcessDefinitionDirectory pdd
            = workflowService().processDefinitionDirectory();

  InputStream is = getClass()
            .getResourceAsStream("/process/testXPDL.xml");
  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);
 
    }

    private WfProcess createProcess
  (String pkgId, String prcId, WfRequester req)
        throws Exception {
  ProcessDefinitionDirectory pdd = null;
  try {
      pdd = workflowService().processDefinitionDirectory ();
      WfProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
      return pmgr.createProcess (req);
  } finally {
      workflowService().release (pdd);
  }
    }
 
    /**
     * Create a new process for the given type and start it.
     */
    public void createAndStartProcess1() throws Exception {
  WfRequester cont = new DefaultRequester(workflowService());
  WfProcess process = createProcess("ut-process", "jut1", cont);
  assertTrue(process.state().startsWith("open.not_running.not_started"));
  process.start();
  assertTrue(process.state().startsWith("open.running"));
  // get the activities
  Collection c = process.steps();
  Iterator it = c.iterator();
  while (it.hasNext()) {
      WfActivity a = (WfActivity)it.next();
      assertTrue(a.state() != null);
  }
    }

    /**
     * Create a new process for the given type and start it.
     */
    public void createAndStartProcess2() throws Exception {
  WfRequester cont = new DefaultRequester(workflowService());
  WfProcess process = createProcess("ut-process", "jut2", cont);
  assertTrue(process.state().startsWith("open.not_running.not_started"));
  process.start();
  assertTrue(process.state().startsWith("open.running"));
  // get the activities
  Collection c = process.steps();
  Iterator it = c.iterator();
  while (it.hasNext()) {
      WfActivity a = (WfActivity)it.next();
      assertTrue(a.state() != null);
  }
    }

    /**
     * Create a new process for the given type and start it.
     */
    public void createAndStartProcess3() throws Exception {
  WfRequester cont = new DefaultRequester(workflowService());
  WfProcess process = createProcess("ut-process", "jut3", cont);
  assertTrue(process.state().startsWith("open.not_running.not_started"));
  process.start();
  Thread.sleep(2500);
  assertTrue(process + " should be completed, is " + process.state(),
       process.state().startsWith("closed.completed"));
  // get the activities
  Collection c = process.steps();
  Iterator it = c.iterator();
  int cnt = 0;
  while (it.hasNext()) {
      WfActivity a = (WfActivity)it.next();
      if (a.state().startsWith
    (WfExecutionObject.ClosedState.COMPLETED.toString())) {
    cnt += 1;
      }
  }
  assertTrue (cnt == 5);
    }

    /**
     * Create a new process for the given type and start it.
     */
    public void createAndStartProcessBatched() throws Exception {
  WfRequester cont = new DefaultRequester(workflowService());

  ProcessDefinitionDirectory pdd = null;
  WfProcessMgr pmgr = null;
  try {
      pdd = workflowService().processDefinitionDirectory ();
      pmgr = pdd.processMgr("ut-process", "jut1");
  } finally {
      workflowService().release (pdd);
  }
  MethodInvocationBatch mib = new MethodInvocationBatch();
  mib.addInvocation
      (pmgr, "createProcess", new String[]
    {"de.danet.an.workflow.omgcore.WfRequester"},
       new Object[]{new DefaultRequester(workflowService())});
  mib.addInvocation(-1, "key", null, null, false);
  mib.addInvocation(-2, "start", null, null, false);
  MethodInvocationBatch.Result mir = (MethodInvocationBatch.Result)
      workflowService().executeBatch(mib);
  if (mir.hasExceptions ()) {
      Exception e = mir.firstException ();
      assertTrue ("Problem executing batch: " + e.getMessage(), false);
  }
  Object[] results = mir.results();
  WfProcess proc = (WfProcess)results[0];
  assertTrue (proc.key().equals (results[1]));
    }

    /**
     * Create a new process for the given type.
     */
    public void createProcess2() throws Exception {
  WfRequester cont = new DefaultRequester(workflowService());
   WfProcess process = createProcess("ut-process", "jut2", cont);
  assertTrue(process.state().startsWith("open.not_running.not_started"));
    process.start();
  Thread.sleep (1000);
  assertTrue(process.state().startsWith("open.running"));
  int i = 9;
  int resCnt = 0;
  while (i-- > 0) {
      boolean resd = false;
      for (Iterator it = process.steps().iterator(); it.hasNext();) {
    WfActivity a = (WfActivity)it.next();
    if (a.state().startsWith ("open.not_running.suspended")) {
        a.resume ();
        resd = true;
        resCnt += 1;
        Thread.sleep (500);
    }
      }
      if (!resd) {
    break;
      }
  }
  assertTrue (i >= 0);
  assertTrue (resCnt == 8);
    }

    /**
     * Create a new process for the given type.
     */
    public void createProcess3() throws Exception {
  WfRequester cont = new DefaultRequester(workflowService());
  WfProcess process = createProcess("ut-process", "jut3", cont);
  assertTrue(process.state().startsWith("open.not_running.not_started"));
    process.start();
  Thread.sleep (5000);
  assertTrue("Process state should be closed.completed.normal, is "
       + process.state(),
       process.state().startsWith("closed.completed.normal"));
  assertTrue("Process workflow state should be closed, is "
       + process.workflowState(),
       process.workflowState().equals (State.CLOSED));
    }

    /**
     * Remove a given process from the database.
     */
    public void removeProcess3() throws Exception {
  WfRequester cont = new DefaultRequester(workflowService());
  WfProcess process = createProcess("ut-process", "jut999", cont);
  ProcessDirectory pd = null;
  try {
      pd = workflowService().processDirectory ();
      pd.removeProcess ((Process)process);
  } finally {
      workflowService().release (pd);
  }
    }

    /**
     * Create a new process and then remove its process definition.
     */
    public void createProcess1AndRemoveProcessDef() throws Exception
  ProcessDefinitionDirectory pdd = null;
  try {
      WfRequester cont = new DefaultRequester(workflowService());
      Process process = (Process)createProcess("ut-process", "jut1", cont);
      assertTrue(process.state().startsWith("open.not_running.not_started"));
      process.start();
      assertTrue(process.state().startsWith("open.running"));
      // remove its process definition
      pdd = workflowService().processDefinitionDirectory ();
      ProcessDefinition pd
    = pdd.lookupProcessDefinition("ut-process", "jut1");
      assertTrue(pd!=null);
      pdd.removeProcessDefinition("ut-process", "jut1");
      boolean gotEx = false;
      try {
    pd = pdd.lookupProcessDefinition("ut-process", "jut1");
      } catch (Exception ex) {
    gotEx = true;
      }
      assertTrue(gotEx);
      // check the process definition of the process
      ProcessDefinition procDef = process.processDefinition();
      assertTrue(procDef.packageId().equals("ut-process"));
      assertTrue(procDef.processId().equals("jut1"));
      // import the process definition again.
      importProcessDefinitions();
      pd = pdd.lookupProcessDefinition("ut-process", "jut1");
      assertTrue(pd!=null);
  } finally {
      workflowService().release (pdd);
  }
    }

    public static void importProcessDefinitions(String filename)
  throws Exception {
        ProcessDefinitionDirectory pdd
            = WorkflowServiceFactory.newInstance()
            .newWorkflowService().processDefinitionDirectory();
  InputStream is = Basic.class.getResourceAsStream(filename);
  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);
    }
}
TOP

Related Classes of process.Basic

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.