Package process

Source Code of process.Deadline

/*
* 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: Deadline.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.2  2006/09/29 12:32:07  drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.1  2003/12/19 13:01:43  drmlipp
* Updated to 1.1rc1
*
* Revision 1.11  2003/12/02 15:59:28  lipp
* Fixed timing problem.
*
* Revision 1.10  2003/11/04 08:37:32  schlue
* Deadline tests for block activities and subflows added.
*
* Revision 1.9  2003/10/21 21:00:45  lipp
* Moved EJBClientTest to new junit sub-package.
*
* Revision 1.8  2003/10/08 12:39:40  huaiyang
* make test weblogic compatible.
*
* Revision 1.7  2003/09/26 14:41:37  schlue
* New test cases for deadline added.
*
* Revision 1.6  2003/09/25 14:58:27  schlue
* Deadline tests fixed.
*
* Revision 1.5  2003/09/25 14:37:47  schlue
* Deadline tests fixed.
*
* Revision 1.4  2003/09/25 13:57:28  lipp
* Fixed.
*
* Revision 1.3  2003/09/25 13:41:42  schlue
* Deadline tests added.
*
* Revision 1.2  2003/09/19 15:05:00  schlue
* Call to stateReached fixed.
*
* Revision 1.1  2003/09/19 15:01:34  schlue
* Initial deadline test added (not working yet).
*
*
*
*/
package process;

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

import junit.framework.Test;
import junit.framework.TestSuite;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.workflow.api.Activity;
import de.danet.an.workflow.api.DefaultRequester;
import de.danet.an.workflow.api.ImportException;
import de.danet.an.workflow.api.PrioritizedMessage;
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.WfProcess;
import de.danet.an.workflow.omgcore.WfRequester;
import de.danet.an.workflow.omgcore.ProcessData;

/**
* Test of Deadlines within workflow processes.
* After completion, all previously created processes and process definitions
* are removed.
* @author <a href="mailto:schlueter@danet.de">Holger Schlueter</a>
* @version 1.0
*/
public class Deadline extends WfMOpenTestCase {
    /**
     * Access to process definition directory (singleton)
     */
    private ProcessDefinitionDirectory defDir = null;

    /**
     * Access to process directory (singleton)
     */
    private ProcessDirectory procDir = null;

    /**
     * Access to default requester (singleton)
     */
    private WfRequester requester = null;

    /**
     * Constructor of this TestCase
     * @param name a <code>String</code> value
     */
    public Deadline(String name) {
  super (name);
    }

    /**
     * Construct this test suite.
     * @return a <code>Test</code> value
     */
    public static Test suite() {
        TestSuite suite = new TestSuite();
  suite.addTest(new Deadline("timeoutSynchr"));
  suite.addTest(new Deadline("timeoutASynchr"));
  suite.addTest(new Deadline("timeoutLoop"));
  suite.addTest(new Deadline("timeoutSuspend"));
  suite.addTest(new Deadline("timeoutBlock"));
  suite.addTest(new Deadline("timeoutSubflow"));
        return new EJBClientTest (plc, suite);
    }

    /**
     * Test synchronous deadline execution. Explicit named exception are
     * tested as well as default exception if no match is found.
     * @exception Exception if an error occurs
     */
    public void timeoutSynchr() throws Exception {
  ProcessMgr mgr = defDir.processMgr("SystemTest", "timeoutSynchr");
  WfProcess proc = mgr.createProcess(requester);
  String procKey = proc.key();
  proc.start();
  assertTrue(stateReached(proc, "closed.completed"));
  ProcessData data = proc.processContext();
  String path = (String)data.get("TransitionPath");
  assertTrue(path, path.equals("PATH:act1:act2:to"));
  procDir.removeProcess(proc);

  mgr = defDir.processMgr("SystemTest", "timeoutSynchrDefault");
  proc = mgr.createProcess(requester);
  procKey = proc.key();
  proc.start();
  assertTrue(stateReached(proc, "closed.completed"));
  data = proc.processContext();
  path = (String)data.get("TransitionPath");
  assertTrue(path, path.equals("PATH:act1:act2:def"));
  procDir.removeProcess(proc);
    }

    /**
     * Test asynchronous deadline execution.
     * @exception Exception if an error occurs
     */
    public void timeoutASynchr() throws Exception {
  ProcessMgr mgr = defDir.processMgr("SystemTest", "timeoutASynchr");
  WfProcess proc = mgr.createProcess(requester);
  String procKey = proc.key();
  proc.start();
  assertTrue(stateReached(proc, "closed.completed"));
  ProcessData data = proc.processContext();
  String path = (String)data.get("TransitionPath");
  assertTrue(path, path.equals("PATH:act1:to1:to2:to3"));
  procDir.removeProcess(proc);
    }

    /**
     * Test deadline execution in combination with loops.
     * @exception Exception if an error occurs
     */
    public void timeoutLoop() throws Exception {
  ProcessMgr mgr = defDir.processMgr("SystemTest", "timeoutLoop");
  WfProcess proc = mgr.createProcess(requester);
  String procKey = proc.key();
  proc.start();
  assertTrue(stateReached(proc, "closed.completed"));
  ProcessData data = proc.processContext();
  String path = (String)data.get("TransitionPath");
  Long incr = (Long)data.get("increment");
  assertTrue("I: " + incr + "; " + path,
       path.equals("PATH:start:loop:to:loop:to:loop:end"));
  procDir.removeProcess(proc);
    }

    /**
     * Test deadline execution in combination with suspension of
     * activities and/or the process.
     * @exception Exception if an error occurs
     */
    public void timeoutSuspend() throws Exception {
  ProcessMgr mgr = defDir.processMgr("SystemTest", "suspendStartMan");
  WfProcess proc = mgr.createProcess(requester);
  String procKey = proc.key();
  proc.start();
  assertTrue(stateReached(proc, "closed.completed"));
  ProcessData data = proc.processContext();
  String path = (String)data.get("TransitionPath");
  assertTrue(path,
       path.equals("PATH:start:t1:t2:end"));
  procDir.removeProcess(proc);

  mgr = defDir.processMgr("SystemTest", "suspendEndMan");
  proc = mgr.createProcess(requester);
  procKey = proc.key();
  proc.start();
  assertTrue(stateReached(proc, "closed.completed"));
  data = proc.processContext();
  path = (String)data.get("TransitionPath");
  assertTrue(path,
       path.equals("PATH:start:a1:t1:a2:t2:end"));
  procDir.removeProcess(proc);

  // One test case with two variants:
  // 1. activity is suspended: (deadline is triggered after resume)
  mgr = defDir.processMgr("SystemTest", "suspendAbsolute");
  proc = mgr.createProcess(requester);
  procKey = proc.key();
  proc.start();
  Activity act = null;
  Iterator i = proc.steps().iterator();
  while (i.hasNext()) {
      act = (Activity)i.next();
      if (act.name().equals("A1")) {
    break;
      }
  }
  assertTrue(act.state(), stateReached(act, "open.running"));
  Thread.sleep(1000); // allow some time to invoke first tool
  act.suspend();
  data = proc.processContext();
  path = (String)data.get("TransitionPath");
  assertTrue(path, path.equals("PATH:start:a1"));
  assertTrue(act.state(),
       stateReached(act, "open.not_running.suspended"));
  Thread.sleep(15000);
  Date timestampRes = new Date();
  act.resume();
  assertTrue(act.state(),
       stateReached(act, "closed.completed.abandoned"));
  assertTrue(stateReached(proc, "closed.completed"));
  data = proc.processContext();
  path = (String)data.get("TransitionPath");
  Date timestampTo = (Date)data.get("timestamp_to");
  assertTrue(path,
       path.equals("PATH:start:a1:t1:end"));
  assertTrue( timestampTo + ":" + timestampRes,
        timestampTo.after(timestampRes) );
  procDir.removeProcess(proc);

  // 2. whole process is suspended: (deadline is triggered before resume
  // but cannot be started until process is resumed)
  proc = mgr.createProcess(requester);
  procKey = proc.key();
  proc.start();
  act = null;
  i = proc.steps().iterator();
  while (i.hasNext()) {
      act = (Activity)i.next();
      if (act.name().equals("A1")) {
    break;
      }
  }
  assertTrue(act.state(), stateReached(act, "open.running"));
  Thread.sleep(1000); // allow some time to invoke first tool
  proc.suspend();
  data = proc.processContext();
  path = (String)data.get("TransitionPath");
  assertTrue(path, path.equals("PATH:start:a1"));
  assertTrue(act.state(),
       stateReached(act, "closed.completed.abandoned"));
  Thread.sleep(10000);
  act = null;
  i = proc.steps().iterator();
  while (i.hasNext()) {
      act = (Activity)i.next();
      if (act.name().equals("TIMEOUT1")) {
    break;
      }
  }
  assertTrue(act.state(),
       stateReached(act, "open.not_running.not_started"));
  timestampRes = new Date();
  proc.resume();
  assertTrue(act.state(),
       stateReached(act, "closed.completed"));
  assertTrue(act.state(),
       !proc.state().equals("closed.completed.abandoned"));
  assertTrue(stateReached(proc, "closed.completed"));
  data = proc.processContext();
  path = (String)data.get("TransitionPath");
  timestampTo = (Date)data.get("timestamp_to");
  assertTrue(path,
       path.equals("PATH:start:a1:t1:end"));
  assertTrue( timestampTo + ":" + timestampRes,
        !timestampTo.before(timestampRes) );
  procDir.removeProcess(proc);
    }

    /**
     * Test deadline execution within block activity.
     * @exception Exception if an error occurs
     */
    public void timeoutBlock() throws Exception {
  ProcessMgr mgr = defDir.processMgr("SystemTest", "timeoutBlock");
  WfProcess proc = mgr.createProcess(requester);
  String procKey = proc.key();
  proc.start();
  assertTrue(stateReached(proc, "closed.completed"));
  ProcessData data = proc.processContext();
  String path = (String)data.get("TransitionPath");
  assertTrue(path,
       path.equals("PATH:start:a1:t1:a3:a2:t0:end"));
  procDir.removeProcess(proc);
    }

    /**
     * Test deadline execution within subflows.
     * @exception Exception if an error occurs
     */
    public void timeoutSubflow() throws Exception {
  ProcessMgr mgr = defDir.processMgr("SystemTest", "timeoutSubflow");
  WfProcess proc = mgr.createProcess(requester);
  String procKey = proc.key();
  proc.start();
  assertTrue(stateReached(proc, "closed.completed"));
  ProcessData data = proc.processContext();
  String path = (String)data.get("TransitionPath");
  assertTrue(path,
       path.equals("PATH:start:a1:t1:a3:a2:t0:end"));
  procDir.removeProcess(proc);
    }

    /**
     * Initialisation.
     * The <code>setUp</code> method defines the way a state change is
     * realized. Override this method to change this way.
     * @exception Exception if an error occurs
     */
    protected void setUp() throws Exception {
  super.setUp();
  WorkflowService wfs = WorkflowServiceFactory.newInstance()
      .newWorkflowService();
  try {
      defDir = wfs.processDefinitionDirectory();
  } catch(RemoteException exc) {
      System.err.println("Process definition directory not accessible: "
             + exc.getMessage());
      System.exit(-1);
  }

  procDir = wfs.processDirectory();
  requester = new DefaultRequester(wfs);
  importProcessDefinition("/process/deadline.xml");
    }

    private void importProcessDefinition(String name) throws Exception {
  StringBuffer processDefinition = new StringBuffer();
  InputStream is = getClass().getResourceAsStream(name);
  BufferedReader in = new BufferedReader
      (new InputStreamReader(is, "ISO-8859-1"));
  String line = null;
  while ((line = in.readLine())!= null) {
      processDefinition.append(line+"\n");
  }
  try {defDir.importProcessDefinitions(processDefinition.toString());
  } catch (ImportException exc) {
      Iterator msg = exc.messages().iterator();
      while (msg.hasNext()) {
    System.out.println(((PrioritizedMessage)msg.next())
           .message());
      }
  } 
    }

    private boolean stateReached(WfProcess proc,
         String procState)
    throws Exception {
  boolean test = true;
  boolean stateReached = false;
  int maxRetries = 100;
  while (test){
      if (maxRetries-- > 0) {
    if (proc.state().startsWith(procState)) {
        stateReached = true;
        test = false;
    } else {
        Thread.sleep(500);
    }
      } else {
    test = false;
      }
  } 
  return stateReached;
    }

    private boolean stateReached(Activity act,
         String actState)
    throws Exception {
  boolean test = true;
  boolean stateReached = false;
  int maxRetries = 100;
  while (test){
      if (maxRetries-- > 0) {
    if (act.state().startsWith(actState)) {
        stateReached = true;
        test = false;
    } else {
        Thread.sleep(500);
    }
      } else {
    test = false;
      }
  } 
  return stateReached;
    }
}
TOP

Related Classes of process.Deadline

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.