Req.java :  » Workflow-Engines » wfmopen-2.1.1 » process » Java Open Source

Java Open Source » Workflow Engines » wfmopen 2.1.1 
wfmopen 2.1.1 » process » Req.java
/*
 * Danet GmbH
 * Beratung und Software-Entwicklung
 * Geschftstelle AN
 *
 * $Id: Req.java,v 1.2 2007/03/27 21:59:42 mlipp Exp $
 *
 * $Log: Req.java,v $
 * Revision 1.2  2007/03/27 21:59:42  mlipp
 * Fixed lots of checkstyle warnings.
 *
 * Revision 1.1.1.3  2004/08/18 15:18:47  drmlipp
 * Update to 1.2
 *
 * Revision 1.9  2004/01/27 11:45:33  lipp
 * Preserve newlines when reading process definitions.
 *
 * Revision 1.8  2003/10/21 21:00:45  lipp
 * Moved EJBClientTest to new junit sub-package.
 *
 * Revision 1.7  2003/10/08 11:52:55  huaiyang
 * make test weblogic compatible.
 *
 * Revision 1.6  2003/04/26 16:46:55  lipp
 * Made unittests and systemtests coexist in eclipse.
 *
 * Revision 1.5  2003/04/26 16:12:35  lipp
 * Moved some classes to reduce package dependencies.
 *
 * Revision 1.4  2003/04/16 19:25:04  lipp
 * Adapted to jdk 1.4
 *
 * Revision 1.3  2003/02/25 17:08:27  lipp
 * Reorganized requester implementation.
 *
 * Revision 1.2  2003/02/07 15:56:19  lipp
 * Implemented Requester notifications.
 *
 * Revision 1.1  2003/02/06 12:47:47  lipp
 * Implemented Requester (no event handling yet).
 *
 */
package process;

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import java.rmi.RemoteException;
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.InvalidPerformerException;
import de.danet.an.workflow.omgcore.WfAuditEvent;
import de.danet.an.workflow.omgcore.WfAuditHandler;
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.Process;
import de.danet.an.workflow.api.ProcessDefinitionDirectory;
import de.danet.an.workflow.api.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;

import common.UTLoginContext;
import junit.framework.*;

/**
 * Zusammenstellung aller TimerObjectTests.
 */
public class Req 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 Req(String name) {
  super (name);
    }

    /**
     * Stellt diese TestSuite zusammen.
     */
    public static Test suite() {
        TestSuite suite = new TestSuite();
     suite.addTest(new Req("importProcessDefinitions"));
     suite.addTest(new Req("testAssoc"));
        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;
    }

    class EventLogger implements WfAuditHandler {

  public List evts = new ArrayList ();
  public Map evtsByProc = new HashMap ();

  /**
   * Describe <code>receiveEvent</code> method here.
   *
   * @param wfAuditEvent a <code>WfAuditEvent</code> value
   * @exception InvalidPerformerException if an error occurs
   * @exception RemoteException if an error occurs
   */
  public void receiveEvent(WfAuditEvent wfAuditEvent)
      throws InvalidPerformerException, RemoteException {
      evts.add (wfAuditEvent);
      String proc = wfAuditEvent.processKey();
      if (!evtsByProc.containsKey(proc)) {
    evtsByProc.put (proc, new ArrayList ());
      }
      ((List)evtsByProc.get (proc)).add (wfAuditEvent);
  }
    }
    

    /**
     * Import the process definitions from a XPDL file
     * unsing the ProcessDefinitionDirectory bean.
     */
    public void importProcessDefinitions() throws Exception {
  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 
      = workflowService().processDefinitionDirectory();
  WfProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
  return pmgr.createProcess (req);
    }

    /**
     * Test association of process and requester.
     */
    public void testAssoc() throws Exception {
  EventLogger evtlog1 = new EventLogger();
  WfRequester cont1 
      = new DefaultRequester(workflowService(), evtlog1);
  Process proc1a = (Process)createProcess("ut-process", "jut1", cont1);
  assertTrue (proc1a.requester().equals (cont1));
  Process proc1b = (Process)createProcess("ut-process", "jut1", cont1);
  assertTrue (proc1b.requester().equals (cont1));
  EventLogger evtlog2 = new EventLogger();
  WfRequester cont2 = new DefaultRequester(workflowService(), evtlog2);
  assertTrue (!cont2.equals (cont1));
  Process proc2 = (Process)createProcess("ut-process", "jut1", cont2);
  assertTrue (proc2.requester().equals (cont2));
  Collection c1 = cont1.performers();
  assertTrue (c1.size() == 2);
  boolean foundA = false;  
  boolean foundB = false;
  for (Iterator i = c1.iterator(); i.hasNext();) {
      Process p = (Process)i.next();
      if (p.key().equals (proc1a.key())) {
    foundA = true;
      }
      if (p.key().equals (proc1b.key())) {
    foundB = true;
      }
  }
  assertTrue (foundA && foundB);
  Thread.sleep (2000);
  assertTrue ("Have " + evtlog1.evts.size() + " events for " + proc1a
        + " and " + proc1b, evtlog1.evts.size() == 4);
  assertTrue ("Have events for " + evtlog1.evtsByProc.keySet().size()
        + " processes", evtlog1.evtsByProc.keySet().size() == 2);
  assertTrue ("Have " + evtlog2.evts.size() + " events for " + proc2,
        evtlog2.evts.size() == 2);
  assertTrue ("Have events for " + evtlog2.evtsByProc.keySet().size()
        + " processes", evtlog2.evtsByProc.keySet().size() == 1);
    }
}

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.