⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logpersistencetest.java

📁 一个java工作流引擎
💻 JAVA
字号:
package org.jbpm.model.log.impl;

import java.util.*;
import org.jbpm.*;
import org.jbpm.util.log.*;
import org.jbpm.model.definition.*;
import org.jbpm.model.definition.impl.*;
import org.jbpm.model.execution.impl.*;
import org.jbpm.persistence.hibernate.*;
import org.jbpm.service.*;
import junit.framework.*;

public class LogPersistenceTest extends TestCase {

  static { TestHelper.initLogging(); }
  
  private HibernateTestHelper hibernate = null;
	private Date now = new Date();
  
  public void setUp() throws Exception {
    hibernate = new HibernateTestHelper();
    hibernate.startTransaction();
  }

  public void testEvent() throws Exception {
		TokenImpl token = new TokenImpl();
    token.setName("the token");

    InvocationLogImpl savedEvent = token.add( new InvocationLogImpl(ServiceMethod.START_PROCESS_INSTANCE, "testrunner" ) );
    savedEvent.setDate( now );
    hibernate.getSession().save( token ); // the token cascades the save to the event
    
    hibernate.commitTransaction();
    hibernate.startTransaction();
    
    InvocationLogImpl retrievedEvent = (InvocationLogImpl) hibernate.getSession().load( InvocationLogImpl.class, savedEvent.getId() );
    assertNotNull( retrievedEvent );
    assertNotSame( savedEvent, retrievedEvent );
    assertEquals( "the token", retrievedEvent.getToken().getName() );
    assertSame( ServiceMethod.START_PROCESS_INSTANCE, retrievedEvent.getServiceMethod() );
    assertEquals( "testrunner", retrievedEvent.getActorId() );
    assertEquals( now, savedEvent.getDate() );
  }
  
  public void testEventOrdering() throws Exception {
    TokenImpl token = new TokenImpl();
    token.setName("the token");
    
    // suppose the following sequence of service methods is called
    ServiceMethod[] serviceMethods = new ServiceMethod[]{
        ServiceMethod.START_PROCESS_INSTANCE,
        ServiceMethod.END_OF_STATE,
        ServiceMethod.CANCEL_TOKEN,
        ServiceMethod.CANCEL_PROCESS_INSTANCE,
        ServiceMethod.END_OF_STATE,
        ServiceMethod.CANCEL_TOKEN,
        ServiceMethod.CANCEL_PROCESS_INSTANCE,
        ServiceMethod.CANCEL_TOKEN,
        ServiceMethod.CANCEL_PROCESS_INSTANCE,
        ServiceMethod.END_OF_STATE,
        ServiceMethod.CANCEL_TOKEN,
        ServiceMethod.CANCEL_PROCESS_INSTANCE,
        ServiceMethod.START_PROCESS_INSTANCE,
        ServiceMethod.START_PROCESS_INSTANCE,
        ServiceMethod.CANCEL_PROCESS_INSTANCE,
        ServiceMethod.CANCEL_PROCESS_INSTANCE,
        ServiceMethod.END_OF_STATE,
        ServiceMethod.CANCEL_TOKEN,
        ServiceMethod.START_PROCESS_INSTANCE,
        ServiceMethod.START_PROCESS_INSTANCE,
        ServiceMethod.END_OF_STATE,
        ServiceMethod.END_OF_STATE
    };
    
    // then we create a sequece of events in the same order
    for ( int i = 0; i < serviceMethods.length; i++ ) {
      token.add( new InvocationLogImpl( serviceMethods[i] ) );
    }
    
    // check the sequence before it is stored in the db
    int i = 0;
    Date previous = new Date(0);
    for ( Iterator iter = token.getInvocationLogs().iterator(); iter.hasNext();) {
      InvocationLogImpl event = (InvocationLogImpl) iter.next();
      assertSame( serviceMethods[i], event.getServiceMethod() );
      assertTrue( event.getDate().after( previous ) );
      previous = event.getDate();
      i++;
    }

    // store and retrieve the token
    hibernate.getSession().save( token ); // the token cascades the save to the event
    hibernate.commitTransaction();
    hibernate.startTransaction();
    token = (TokenImpl) hibernate.getSession().load( TokenImpl.class, token.getId() );
    
    // check the sequence after it is stored and retrieved from the db
    i = 0;
    previous = new Date(0);
    for ( Iterator iter = token.getInvocationLogs().iterator(); iter.hasNext();) {
      InvocationLogImpl event = (InvocationLogImpl) iter.next();
      assertSame( serviceMethods[i], event.getServiceMethod() );
      assertTrue( event.getDate().after( previous ) );
      previous = event.getDate();
      i++;
    }
  }
/*
  public void testAllLogsInProcessInstance() throws Exception {
    ProcessInstanceImpl pi = new ProcessInstanceImpl();
    TokenImpl root = new TokenImpl();
    root.setProcessInstance( pi );
    pi.setRoot( root );
    
    root.add( new InvocationLogImpl() );
   
    root.add( new ActionLogImpl() );
    root.add( new AssignmentLogImpl() );
    root.add( new EndOfTokenLogImpl() );
    root.add( new MessageLogImpl() );
    root.add( new VariableCreationLogImpl() );
    root.add( new VariableUpdateLogImpl( new VariableInstanceImpl(), "old", null, "new", null ) );
    
    hibernate.getSession().save( pi ); // the token cascades the save to the event and the logs
    hibernate.commitTransaction();
    hibernate.startTransaction();
    root = (TokenImpl) hibernate.getSession().load( TokenImpl.class, root.getId() );
    
    System.out.println( "logs: " + ((InvocationLogImpl)root.getInvocationLogs().iterator().next()).getExecutionLogs() );
  }
  */
  
  public void testVariableUpdateLog() throws Exception {
    TokenImpl token = new TokenImpl();
    token.setName("the token");
    token.add( new InvocationLogImpl( ServiceMethod.START_PROCESS_INSTANCE, "testrunner" ) );

    VariableInstanceImpl variableInstance = new VariableInstanceImpl( "varname", token, DefaultType.LONG );
    variableInstance.setValue( new Long( 1 ) );
    variableInstance.setValue( new Long( 2 ) );
    variableInstance.setValue( new Long( 36 ) );
    
    // store and retrieve the token
    hibernate.getSession().save( token ); // the token cascades the save to the event and the logs
    hibernate.commitTransaction();
    hibernate.startTransaction();
    token = (TokenImpl) hibernate.getSession().load( TokenImpl.class, token.getId() );
    variableInstance = (VariableInstanceImpl) hibernate.getSession().load( VariableInstanceImpl.class, variableInstance.getId() );
    
    assertNotNull( token );
    assertNotNull( token.getInvocationLogs() );
    assertEquals( 1, token.getInvocationLogs().size() );
    InvocationLogImpl event = (InvocationLogImpl) token.getInvocationLogs().iterator().next();

    assertNotNull( event.getExecutionLogs() );
    List logs = event.getExecutionLogs();
    assertEquals( 3, logs.size() );
    
    checkVariableUpdateLog(event, 0, null, new Long(1));
    checkVariableUpdateLog(event, 1, new Long(1), new Long(2));
    checkVariableUpdateLog(event, 2, new Long(2), new Long(36));

    // since we only added update logs to the event, the logs of the event
    // must be return the same sorted collection as the update-logs of the variable instance
    assertEquals( new ArrayList(event.getExecutionLogs()), new ArrayList( variableInstance.getUpdateLogs() ) );
  }
    
  private void checkVariableUpdateLog(InvocationLogImpl event, int index, Long oldValue, Long newValue) {
		VariableUpdateLogImpl log = (VariableUpdateLogImpl) event.getExecutionLogs().get(index);
    assertEquals( new Integer(index), log.getIndex() );
    assertEquals( "varname", log.getVariableName() );
    assertSame( event, log.getInvocationLog() );
    if ( oldValue != null ) {
      assertEquals( oldValue.toString(), log.getOldValueText() );
      assertEquals( oldValue, log.getOldValue() );
    } else {
      assertNull( log.getOldValueText() );
      assertNull( log.getOldValue() );
    }
    assertEquals( newValue.toString(), log.getNewValueText() );
    assertEquals( newValue, log.getNewValue() );
	}
  
  public void testMessageLogs() throws Exception {
    TokenImpl token = new TokenImpl();
    token.setName("the token");
    token.add( new InvocationLogImpl( ServiceMethod.START_PROCESS_INSTANCE, "testrunner" ) );
    token.add( new MessageLogImpl(Level.INFO, "the first message log") );
    token.add( new MessageLogImpl(Level.ERROR, "the second message log") );

    // store and retrieve the token
    hibernate.getSession().save( token ); // the token cascades the save to the event and the logs
    hibernate.commitTransaction();
    hibernate.startTransaction();
    token = (TokenImpl) hibernate.getSession().load( TokenImpl.class, token.getId() );
    
    assertNotNull( token );
    assertNotNull( token.getInvocationLogs() );
    assertEquals( 1, token.getInvocationLogs().size() );
    InvocationLogImpl event = (InvocationLogImpl) token.getInvocationLogs().iterator().next();

    Iterator iter = event.getExecutionLogs().iterator();
    MessageLogImpl messageLog = (MessageLogImpl) iter.next();
    assertSame( Level.INFO, messageLog.getLevel() );
    assertEquals( "the first message log", messageLog.getMessage() );
    assertEquals( new Integer(0), messageLog.getIndex() );
    assertSame( event, messageLog.getInvocationLog() );
    
    messageLog = (MessageLogImpl) iter.next();
    assertSame( Level.ERROR, messageLog.getLevel() );
    assertEquals( "the second message log", messageLog.getMessage() );
    assertEquals( new Integer(1), messageLog.getIndex() );
    assertSame( event, messageLog.getInvocationLog() );
  }

  public void testAssignmentLogs() throws Exception {
    
    StateImpl state = new StateImpl();
    state.setName( "the state" );
    
    TokenImpl token = new TokenImpl();
    token.setName("the token");
    token.add( new InvocationLogImpl( ServiceMethod.START_PROCESS_INSTANCE, "testrunner" ) );
    token.add( new AssignmentLogImpl(null, "the first assigned swimlane", state) );
    token.add( new AssignmentLogImpl(null, "the second assigned swimlane", state) );
    
    // store and retrieve the token
    hibernate.getSession().save( token ); // the token cascades the save to the event and the logs
    hibernate.getSession().save( state );
    hibernate.commitTransaction();
    hibernate.startTransaction();
    token = (TokenImpl) hibernate.getSession().load( TokenImpl.class, token.getId() );
    
    assertNotNull( token );
    assertNotNull( token.getInvocationLogs() );
    assertEquals( 1, token.getInvocationLogs().size() );
    InvocationLogImpl event = (InvocationLogImpl) token.getInvocationLogs().iterator().next();

    Iterator iter = event.getExecutionLogs().iterator();
    AssignmentLogImpl assignmentLog = (AssignmentLogImpl) iter.next();
    assertEquals( "the state", assignmentLog.getState().getName() );
    assertEquals( "the first assigned swimlane", assignmentLog.getActorId() );
    assertEquals( new Integer(0), assignmentLog.getIndex() );
    assertSame( event, assignmentLog.getInvocationLog() );
    
    assignmentLog = (AssignmentLogImpl) iter.next();
    assertEquals( "the state", assignmentLog.getState().getName() );
    assertEquals( "the second assigned swimlane", assignmentLog.getActorId() );
    assertEquals( new Integer(1), assignmentLog.getIndex() );
    assertSame( event, assignmentLog.getInvocationLog() );
  }
  
  public void testActionLogs() throws Exception {
    
    StateImpl state = new StateImpl();
    state.setName( "the state" );

    DelegationImpl delegation = new DelegationImpl(null, "the name of the action class");
    
    ActionImpl action = new ActionImpl(EventType.PROCESS_START, delegation );
    action.setElement( state );
    
    TokenImpl token = new TokenImpl();
    token.setName("the token");
    token.add( new InvocationLogImpl( ServiceMethod.START_PROCESS_INSTANCE, "testrunner" ) );
    token.add( new ActionLogImpl(action) );
    
    // store and retrieve the token
    hibernate.getSession().save( token ); // the token cascades the save to the event and the logs
    hibernate.getSession().save( state );
    hibernate.getSession().save( action );
    hibernate.commitTransaction();
    hibernate.startTransaction();
    token = (TokenImpl) hibernate.getSession().load( TokenImpl.class, token.getId() );
    
    assertNotNull( token );
    assertNotNull( token.getInvocationLogs() );
    assertEquals( 1, token.getInvocationLogs().size() );
    InvocationLogImpl event = (InvocationLogImpl) token.getInvocationLogs().iterator().next();

    Iterator iter = event.getExecutionLogs().iterator();
    ActionLogImpl actionLog = (ActionLogImpl) iter.next();
    assertEquals( "the state", actionLog.getAction().getElement().getName() );
    assertEquals( new Integer(0), actionLog.getIndex() );
    assertSame( event, actionLog.getInvocationLog() );
  }
  
  public void tearDown() throws Exception {
    hibernate.commitTransaction();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -