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

📄 variableinstancetest.java

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

import java.util.*;
import org.jbpm.model.definition.impl.*;

import junit.framework.*;

public class VariableInstanceTest extends TestCase {
  
  public void testFindNonExistingType() {
    DefinitionImpl definition = new DefinitionImpl( "test definition" );
    assertNull( definition.findTypeByVariableName( "non existing variable name" ) );
    assertNull( definition.findTypeByVariableName( null ) );
    assertNull( definition.findTypeByObject( "a string value" ) );
    assertNull( definition.findTypeByObject( null ) );
  }
  
  public void testFindTypeByValueClass() {
    DefinitionImpl definition = new DefinitionImpl( "test definition" );
    TypeImpl stringType = new TypeImpl( "java.lang.String" );
    definition.add( stringType );
    assertSame( stringType, definition.findTypeByObject( "a string value" ) );
  }
  
  public void testFindTypeByOverridenValueClass() {
    DefinitionImpl definition = new DefinitionImpl( "test definition" );
    TypeImpl mapTestType = new TypeImpl( "org.jbpm.model.execution.impl.MapTestType" );
    definition.add( mapTestType );
    TypeImpl mapType = new TypeImpl( "java.util.Map" );
    definition.add( mapType );

    assertSame( mapTestType, definition.findTypeByObject( new MapTestType() ) );
    assertSame( mapType, definition.findTypeByObject( new HashMap() ) );
  }
  
  public void testFindTypeByVariableName() {
    DefinitionImpl definition = new DefinitionImpl( "test definition" );
    TypeImpl stringType = new TypeImpl( "java.lang.String" );
    definition.add( new VariableImpl( stringType, "the string variable" ) );
    definition.add( stringType );

    assertSame( stringType, definition.findTypeByVariableName( "the string variable" ) );
  }
  
  public void testSerialization() {
    
    // create the definition
    DefinitionImpl definition = new DefinitionImpl( "test definition" );
		TypeImpl stringType = new TypeImpl( "java.lang.String" );
		DelegationImpl stringSerializerDelegation = new DelegationImpl( definition, "org.jbpm.model.execution.impl.TestStringSerializer" );
		stringType.setSerializerDelegation( stringSerializerDelegation );
		definition.add( stringType );

    // create a variable instance with string type.
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setType( stringType );
    
    assertNull( variableInstance.getValue() );
    String cachedValue = "I'm cached";
		variableInstance.setValue( cachedValue );
    assertEquals( "SERIALIZEDTEXT[I'm cached]", variableInstance.getSerializedValue() );
    assertSame( cachedValue, variableInstance.getValue() );

    // create another variable instance with string type.
    variableInstance = new VariableInstanceImpl();
    variableInstance.setType( stringType );
    variableInstance.setSerializedValue( "SERIALIZEDTEXT[beam me up, scotty]" );
    assertEquals( "beam me up, scotty", variableInstance.getValue() );
  }
  
  public void testVariableScope() {
    // create the definition
    DefinitionImpl definition = new DefinitionImpl( "test definition" );
    
    // create the process instance with 1 child flow
    ProcessInstanceImpl processInstance = new ProcessInstanceImpl( definition );
    TokenImpl root = (TokenImpl) processInstance.getRoot();
    addVariableInstance( root, "root variable", "root value" );
    addVariableInstance( root, "overridden variable", "invisible value" );
    TokenImpl child = root.add( new TokenImpl( root, "the child flow" ) );
    addVariableInstance( child, "child variable", "child value" );
    addVariableInstance( child, "overridden variable", "visible value" );
    
    assertEquals( "root value", child.getVariable( "root variable" ) );
    assertEquals( "child value", child.getVariable( "child variable" ) );
    assertEquals( "visible value", child.getVariable( "overridden variable" ) );
  }
  
	private void addVariableInstance( TokenImpl token, String name, String value ) {
    VariableInstanceImpl variableInstance = token.add( new VariableInstanceImpl( name, token, DefaultType.STRING ) );
    variableInstance.setValue( value );
  }
  
  public void testDefaultStringType() {
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setDefaultType( DefaultType.STRING );
    
    String testTextValue = "test-text-value-test-text-value-test-text-value-tsstxtvltextlvarltesiclva... damn";
    variableInstance.setValue( testTextValue );
    assertEquals( testTextValue, variableInstance.getSerializedValue() );
    assertSame( testTextValue, variableInstance.getValue() );
  }
  
  public void testStringTypeException() {
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setDefaultType( DefaultType.STRING );
    try {
      variableInstance.setValue( new Long( 3 ) );
      fail( "should have thrown an IllegalArgumentException" );
    } catch (IllegalArgumentException e) {
      // OK
    }
  }
  
  public void testDefaultLongType() {
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setDefaultType( DefaultType.LONG );
    
    Long three = new Long( 3 );
    variableInstance.setValue( three );
    
    assertEquals( "3", variableInstance.getSerializedValue() );
    assertSame( three, variableInstance.getValue() );
  }
  
  public void testLongTypeException() {
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setDefaultType( DefaultType.LONG );
    try {
      variableInstance.setValue( "whooops" );
      fail( "should have thrown an IllegalArgumentException" );
    } catch (IllegalArgumentException e) {
      // OK
    }
  }
  
  public void testDefaultDoubleType() {
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setDefaultType( DefaultType.DOUBLE );

    Double pipi = new Double( Math.PI * Math.PI );
    variableInstance.setValue( pipi );
    assertEquals( pipi.toString(), variableInstance.getSerializedValue() );
    assertSame( pipi, variableInstance.getValue() );
  }
  
  public void testDoubleTypeException() {
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setDefaultType( DefaultType.DOUBLE );
    try {
      variableInstance.setValue( "whooops" );
      fail( "should have thrown an IllegalArgumentException" );
    } catch (IllegalArgumentException e) {
      // OK
    }
  }
  
  public void testDefaultDateType() {
    Date now = new Date();
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setDefaultType( DefaultType.DATE );
    variableInstance.setValue( now );
    assertEquals( new Long( now.getTime() ).toString(), variableInstance.getSerializedValue() );
    assertSame( now, variableInstance.getValue() );
  }
  
  public void testDateTypeException() {
    VariableInstanceImpl variableInstance = new VariableInstanceImpl();
    variableInstance.setDefaultType( DefaultType.DOUBLE );
    try {
      variableInstance.setValue( "whooops" );
      fail( "should have thrown an IllegalArgumentException" );
    } catch (IllegalArgumentException e) {
      // OK
    }
  }
  
}

⌨️ 快捷键说明

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