Package tools

Source Code of tools.Chabacc

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2004 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: Chabacc.java 2259 2007-02-16 20:58:37Z mlipp $
*
* $Log$
* Revision 1.5  2007/02/15 13:52:37  drmlipp
* Fixed channel release problem.
*
* Revision 1.4  2007/02/07 16:48:36  mlipp
* Fixed test case.
*
* Revision 1.3  2006/11/03 22:09:26  mlipp
* Added test.
*
* Revision 1.2  2006/09/29 12:32:07  drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.1  2004/08/18 15:18:47  drmlipp
* Update to 1.2
*
* Revision 1.8  2004/03/21 12:38:24  lipp
* Added test cases for mimic as requester process.
*
* Revision 1.7  2004/03/18 09:13:23  lipp
* Added special XML test.
*
* Revision 1.6  2004/02/13 08:25:29  lipp
* Changed channel message data type to Map which is more appropriate.
*
* Revision 1.5  2004/02/12 13:27:46  lipp
* Renamed openChannel to getChannel (channels have no open state).
*
* Revision 1.4  2004/02/06 13:37:35  lipp
* Added channel close notification.
*
* Revision 1.3  2004/02/06 10:25:46  lipp
* Finshed Receiver.
*
* Revision 1.2  2004/01/30 14:36:30  lipp
* Partial implementation of message receipt.
*
* Revision 1.1  2004/01/28 16:11:38  lipp
* Re-implementation of chabacc, Sender working.
*
*/
package tools;

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

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

import javax.security.auth.login.LoginException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;

import de.danet.an.util.EJBUtil;
import de.danet.an.util.XMLUtil;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.util.sax.SAXContentBuffer;

import de.danet.an.workflow.omgcore.ProcessData;
import de.danet.an.workflow.omgcore.WfActivity;
import de.danet.an.workflow.omgcore.WfProcess;
import de.danet.an.workflow.omgcore.WfRequester;
import de.danet.an.workflow.util.SAXEventBufferImpl;

import de.danet.an.workflow.api.Activity;
import de.danet.an.workflow.api.Channel;
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.SAXEventBuffer;
import de.danet.an.workflow.api.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;

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

/**
* Test Cahbacc
*/
public class Chabacc extends TestCase {
    private static UTLoginContext plc = null;
    static {
  try {
      plc = new UTLoginContext();
      plc.login();
  } catch (LoginException e) {
      throw new IllegalStateException (e.getMessage ());
  }
    }

    /**
     * A process directory reference.
     */
    private ProcessDirectory pdd = null;

    /**
     * Constructor of this TestCase
     */
    public Chabacc(String name) {
  super (name);
    }

    /**
     * Stellt diese TestSuite zusammen.
     */
    public static Test suite() {
        TestSuite suite = new TestSuite();
       suite.addTest(new Chabacc("importProcessDefinitions"));
      suite.addTest(new Chabacc("receiveMessage"));
      suite.addTest(new Chabacc("sendMessageAfter"));
      suite.addTest(new Chabacc("sendMessageBefore"));
      suite.addTest(new Chabacc("sendXMLMessage"));
      suite.addTest(new Chabacc("sendSelf"));
      suite.addTest(new Chabacc("echo"));
      suite.addTest(new Chabacc("echoIndirect"));
      suite.addTest(new Chabacc("closeNotification"));
        suite.addTest(new Chabacc("testInEJB"));
        suite.addTest(new Chabacc("testInEJBWithTimeout"));
        return new EJBClientTest (plc, suite);
    }

    private WorkflowService workflowService = null;

    /**
     * Initialisierung.
     */
    protected void setUp() throws Exception {
  try {
      WorkflowServiceFactory wfsf
    = WorkflowServiceFactory.newInstance ();
      workflowService = wfsf.newWorkflowService();
  } catch (FactoryConfigurationError e) {
      throw new IllegalStateException (e.getMessage());
  }
    }

    protected void tearDown() throws Exception {
  workflowService.release (workflowService);
  workflowService = null;
    }

    /**
     * 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("/tools/chabacc.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);
 
    }

    /**
     * Test.
     */
    public void receiveMessage() throws Exception {
  ProcessDefinitionDirectory procDefDir = null;
  ProcessDirectory procDir = null;
        Channel chan = null;
  try {
      procDefDir = workflowService.processDefinitionDirectory();
      procDir = workflowService.processDirectory();
      ProcessMgr pmgr = procDefDir.processMgr
    ("chabacc", "chabacc_test_sender");
      WfProcess process
    = pmgr.createProcess(new DefaultRequester (workflowService));
      chan = workflowService.getChannel(process, "test_channel");
      WfActivity sleepAct = null;
      for (Iterator i = process.steps().iterator(); i.hasNext();) {
    sleepAct = (WfActivity)i.next ();
    if (sleepAct.name().equals("Wait")) {
        break;
    }
      }
      process.start();
      assertTrue (stateReached (sleepAct, "open.running"));
      Map pd = chan.receiveMessage();
      assertTrue ("Hello world!".equals(pd.get("message")));
      assertTrue (stateReached (process, "closed.completed"));
      procDir.removeProcess(process);
  } finally {
            workflowService.release (chan);
      workflowService.release (procDefDir);
      workflowService.release (procDir);
  }
    }

    /**
     * Test.
     */
    public void sendMessageAfter() throws Exception {
  ProcessDefinitionDirectory procDefDir = null;
  ProcessDirectory procDir = null;
        Channel chan = null;
  try {
      procDefDir = workflowService.processDefinitionDirectory();
      procDir = workflowService.processDirectory();
      ProcessMgr pmgr = procDefDir.processMgr
    ("chabacc", "chabacc_test_receiver");
      WfProcess process
    = pmgr.createProcess(new DefaultRequester (workflowService));
      chan = workflowService.getChannel(process, "test_channel");
      WfActivity recAct = null;
      for (Iterator i = process.steps().iterator(); i.hasNext();) {
    recAct = (WfActivity)i.next ();
    if (recAct.name().equals("Receive")) {
        break;
    }
      }
      assertTrue (recAct != null);
      process.start();
      assertTrue (stateReached (recAct, "open.running"));
      Thread.sleep (500);
      Map pd = new HashMap ();
      pd.put ("message", "Hello world!");
      chan.sendMessage(pd);
      assertTrue (stateReached (recAct, "closed.completed"));
      pd = process.processContext();
      assertTrue ("Hello world!".equals(pd.get("lastMessage")));
      assertTrue (stateReached (process, "closed.completed"));
      procDir.removeProcess(process);
  } finally {
            workflowService.release(chan);
      workflowService.release (procDefDir);
      workflowService.release (procDir);
  }
    }

    /**
     * Test.
     */
    public void sendMessageBefore() throws Exception {
  ProcessDefinitionDirectory procDefDir = null;
  ProcessDirectory procDir = null;
        Channel chan = null;
  try {
      procDefDir = workflowService.processDefinitionDirectory();
      procDir = workflowService.processDirectory();
      ProcessMgr pmgr = procDefDir.processMgr
    ("chabacc", "chabacc_test_receiver");
      WfProcess process
    = pmgr.createProcess(new DefaultRequester (workflowService));
      chan = workflowService.getChannel(process, "test_channel");
      Map pd = new HashMap ();
      pd.put ("message", "Hello world!");
      chan.sendMessage(pd);
      Thread.sleep (500);
      process.start();
      assertTrue (stateReached (process, "closed.completed"));
      pd = process.processContext();
      assertTrue ("Hello world!".equals(pd.get("lastMessage")));
      procDir.removeProcess(process);
  } finally {
            workflowService.release(chan);
      workflowService.release (procDefDir);
      workflowService.release (procDir);
  }
    }

    /**
     * Test.
     */
    public void sendXMLMessage() throws Exception {
  ProcessDefinitionDirectory procDefDir = null;
  ProcessDirectory procDir = null;
        Channel chan = null;
  try {
      procDefDir = workflowService.processDefinitionDirectory();
      procDir = workflowService.processDirectory();
      ProcessMgr pmgr = procDefDir.processMgr
    ("chabacc", "chabacc_test_xml_receiver");
      WfProcess process
    = pmgr.createProcess(new DefaultRequester (workflowService));
      chan = workflowService.getChannel(process, "test_channel");
      DocumentBuilderFactory df = DocumentBuilderFactory.newInstance ();
      Document doc = df.newDocumentBuilder ().newDocument ();
      DocumentFragment frag = doc.createDocumentFragment ();
      frag.appendChild(doc.createElement ("root"));
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer t = tf.newTransformer ();
            SAXEventBufferImpl b = new SAXEventBufferImpl ();
            t.transform (new DOMSource (frag), new SAXResult(b));
            b.pack();
      Map pd = new HashMap ();
      pd.put ("message", b);
      chan.sendMessage(pd);
      Thread.sleep (500);
      process.start();
      assertTrue (stateReached (process, "closed.completed"));
            pd = process.processContext();
            Object o = pd.get("lastXMLMessage");
            assertTrue (o != null);
            assertTrue (o instanceof SAXContentBuffer);
            Node recDoc = ((SAXContentBuffer)o).toW3cDom();
            assertTrue (recDoc instanceof Document);
            assertTrue (((Document)recDoc).getDocumentElement()
                        .getLocalName().equals("root"));
      procDir.removeProcess(process);
  } finally {
            workflowService.release(chan);
      workflowService.release (procDefDir);
      workflowService.release (procDir);
  }
    }

    /**
     * Test.
     */
    public void sendSelf() throws Exception {
  ProcessDefinitionDirectory procDefDir = null;
  ProcessDirectory procDir = null;
  try {
      procDefDir = workflowService.processDefinitionDirectory();
      procDir = workflowService.processDirectory();
      ProcessMgr pmgr = procDefDir.processMgr
    ("chabacc", "chabacc_test_send_self");
      WfProcess process
    = pmgr.createProcess(new DefaultRequester (workflowService));
      process.start();
      assertTrue (stateReached (process, "closed.completed"));
      ProcessData pd = process.processContext();
      assertTrue ("Hello world!".equals(pd.get("lastMessage")));
      procDir.removeProcess(process);
  } finally {
      workflowService.release (procDefDir);
      workflowService.release (procDir);
  }
    }

    /**
     * Test.
     */
    public void echo() throws Exception {
  ProcessDefinitionDirectory procDefDir = null;
  ProcessDirectory procDir = null;
        Channel chan = null;
  try {
      procDefDir = workflowService.processDefinitionDirectory();
      procDir = workflowService.processDirectory();
      ProcessMgr pmgr = procDefDir.processMgr
    ("chabacc", "chabacc_test_rec_send");
      WfProcess process
    = pmgr.createProcess(new DefaultRequester (workflowService));
      chan = workflowService.getChannel(process, "test_channel");
      Map pd = new HashMap ();
      pd.put ("message", "Do you echo?");
      chan.sendMessage(pd);
      process.start();
      pd = chan.receiveMessage();
      assertTrue ("Got: " + pd.get("message"),
      "Do you echo?".equals(pd.get("message")));
      assertTrue (stateReached (process, "closed.completed"));
      procDir.removeProcess(process);
  } finally {
            workflowService.release(chan);
      workflowService.release (procDefDir);
      workflowService.release (procDir);
  }
    }

    /**
     * Test.
     */
    public void echoIndirect() throws Exception {
  ProcessDefinitionDirectory procDefDir = null;
  ProcessDirectory procDir = null;
        Channel chan = null;
  try {
      procDefDir = workflowService.processDefinitionDirectory();
      procDir = workflowService.processDirectory();
      ProcessMgr pmgr = procDefDir.processMgr
    ("chabacc", "chabacc_test_rec_send_indirect");
      WfProcess process
    = pmgr.createProcess(new DefaultRequester (workflowService));
      chan = workflowService.getChannel(process, "test_channel");
      Map pd = new HashMap ();
      pd.put ("message", "Do you echo?");
      chan.sendMessage(pd);
      process.start();
      pd = chan.receiveMessage();
      assertTrue ("Got: " + pd.get("message"),
      "Do you echo?".equals(pd.get("message")));
      assertTrue (stateReached (process, "closed.completed"));
      pd = process.processContext();
      assertTrue (pd.get("feedback").equals ("Got message"));
      // cleanup
      Iterator acts = process.activitiesInState("closed").iterator();
      assertTrue (acts.hasNext ());
      Activity act = null;
      while (true) {
    act = (Activity)acts.next ();
    if (act.name().equals ("Run subflow")) {
        break;
    }
      }
      Iterator subs = act.performers().iterator ();
      assertTrue (subs.hasNext ());
      Process sub = (Process)subs.next ();
       procDir.removeProcess(sub);
       procDir.removeProcess(process);
  } finally {
            workflowService.release(chan);
      workflowService.release (procDefDir);
      workflowService.release (procDir);
  }
    }

    /**
     * Test.
     */
    public void closeNotification() throws Exception {
  ProcessDefinitionDirectory procDefDir = null;
  ProcessDirectory procDir = null;
        Channel chan = null;
  try {
      procDefDir = workflowService.processDefinitionDirectory();
      procDir = workflowService.processDirectory();
      ProcessMgr pmgr = procDefDir.processMgr
    ("chabacc", "chabacc_test_close_notification");
      WfProcess process
    = pmgr.createProcess(new DefaultRequester (workflowService));
      chan = workflowService.getChannel(process, "test_channel");
      process.start();
      Map pd = chan.receiveMessage();
      assertTrue (pd == null);
      assertTrue (stateReached (process, "closed.completed"));
      procDir.removeProcess(process);
  } finally {
            workflowService.release(chan);
      workflowService.release (procDefDir);
      workflowService.release (procDir);
  }
    }

    /**
     * Test.
     */
    public void testInEJB() throws Exception {
        de.danet.an.wfdemo.testejb.Test test
            = (de.danet.an.wfdemo.testejb.Test)EJBUtil.createSession
            (de.danet.an.wfdemo.testejb.TestHome.class, "ejb/WfMOpenTestEJB");
        Map pd = test.startAndWait();
        assertTrue ("Hello world!".equals(pd.get("message")));
    }

    /**
     * Test.
     */
    public void testInEJBWithTimeout() throws Exception {
        de.danet.an.wfdemo.testejb.Test test
            = (de.danet.an.wfdemo.testejb.Test)EJBUtil.createSession
            (de.danet.an.wfdemo.testejb.TestHome.class, "ejb/WfMOpenTestEJB");
        Map pd = test.startAndWait(1000);
        assertTrue ("Hello world!".equals(pd.get("message")));
    }

    private WfProcess createProcess
  (String pkgId, String prcId, WfRequester req)
        throws Exception {
  ProcessDefinitionDirectory procDir = null;
  try {
      procDir = workflowService.processDefinitionDirectory();
      ProcessMgr pmgr = procDir.processMgr(pkgId, prcId);
      return pmgr.createProcess (req);
  } finally {
      workflowService.release (procDir);
  }
    }
 
    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(WfActivity 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 tools.Chabacc

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.