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

📄 filemgrtest.java

📁 一个java工作流引擎
💻 JAVA
字号:
package org.jbpm.persistence.filemgr;

import java.io.*;
import java.util.*;
import org.jbpm.*;
import org.jbpm.model.definition.impl.*;
import org.jbpm.par.*;
import org.jbpm.persistence.*;
import org.jbpm.service.*;
import org.jbpm.util.io.*;
import junit.framework.*;

public class FileMgrTest extends TestCase {

  static { TestHelper.initLogging(); }
  
  public void testFileSystemFileMgr() throws Exception {
    FileMgr fileMgr = new FileSystemFileMgr( "target/filesystem" );
    fileMgr.storeBytes( new Long(1), "org/jbpm/test/FileSytemFileMgrTest.bin", create5KbBinaryData() );
    byte[] retrievedBytes = fileMgr.retrieveBytes( new Long(1), "org/jbpm/test/FileSytemFileMgrTest.bin" );
    assertEquals( new String(create5KbBinaryData()), new String( retrievedBytes ) );
  }

  public void testDatabaseFileMgr() throws Exception {
    FileMgr fileMgr = new DatabaseFileMgr( new JbpmConfiguration( new Properties() ) );
    fileMgr.storeBytes( new Long(1), "org/jbpm/test/DatabaseFileMgrTest.bin", create5KbBinaryData() );
    byte[] retrievedBytes = fileMgr.retrieveBytes( new Long(1), "org/jbpm/test/DatabaseFileMgrTest.bin" );
    assertEquals( new String(create5KbBinaryData()), new String( retrievedBytes ) );
  }

  public void testClassLoading() throws Exception {
    // get the bytes of the class LoadableClass
    InputStream in = this.getClass().getResourceAsStream( "/org/jbpm/persistence/filemgr/LoadableClass.class" );
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IoUtil.transfer( in, out );
    byte[] loadableClassBytes = out.toByteArray();
    
    // create a file manager
    FileMgr fileMgr = new FileSystemFileMgr( "target/filesystem" );
    // store the bytes of the loadable class
    fileMgr.storeBytes( new Long(1), "classes/org/jbpm/persistence/filemgr/LoadableClass.class", loadableClassBytes );

    // initialise the filemgr in the DefinitionClassLoader
    DefinitionClassLoader.setFileMgr( fileMgr );
    
    // get the root classloader
    ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
    while ( ( systemClassLoader.getParent() != null )
            && ( systemClassLoader != systemClassLoader.getParent() ) ) {
      systemClassLoader = systemClassLoader.getParent();
    }

    // check if the root classloader doesn't find the class
    try {
      systemClassLoader.loadClass( "org.jbpm.persistence.filemgr.LoadableClass" ).newInstance();
      fail( "if the LoadableClass is found by a parent classloader, this test has no purpose" );
    } catch ( ClassNotFoundException e ) {
      // OK
    }
      
    // create the definition class loader as a child of the system classloader
    ClassLoader definitionClassLoader = new DefinitionClassLoader( systemClassLoader, new Long(1) );
    
    // load the class using the definition class loader
    Object object = definitionClassLoader.loadClass( "org.jbpm.persistence.filemgr.LoadableClass" ).newInstance();
    assertNotNull( object );
    assertEquals( "a loadable instance", object.toString() );
  }

  public void testArchiveParser() throws Exception {
    Long definitionId = null;
    String fileName = "somedir/somebinarydata.hex";
    
    JbpmConfiguration jbpmConfiguration = new JbpmConfiguration( new Properties() );
    JbpmServiceLocator jbpmServiceLocator = new JbpmServiceLocator( jbpmConfiguration );
    
    ArchiveBuilder archiveBuilder = new ArchiveBuilder( this.getClass().getClassLoader().getResourceAsStream("process/stateprocess.xml") );
    byte[] fiveKOfBinaryData = create5KbBinaryData();
    archiveBuilder.add( fileName, fiveKOfBinaryData );
    
    DefinitionService definitionService = new DefinitionServiceImpl( jbpmConfiguration );
    definitionService.deployProcessArchive( archiveBuilder.getJarInputStream() );
    
    definitionId = jbpmServiceLocator.getExecutionService().getLatestDefinition( "the state process" ).getId();
     
    FileMgr fileMgr = (FileMgr) jbpmConfiguration.instantiate( "jbpm.file.mgr", FileMgr.class );
    byte[] retrievedBytes = fileMgr.retrieveBytes( definitionId, fileName );
    assertEquals( new String( fiveKOfBinaryData ), new String( retrievedBytes ) );
  }

  private static byte[] create5KbBinaryData() {
    byte[] binaryData = new byte[ 1024*5 ];
    Arrays.fill( binaryData, (byte) ' ' );
    byte[] firstPart = "these are the first bytes of the binary data".getBytes();
    System.arraycopy( firstPart, 0, binaryData, 0, firstPart.length );
    return binaryData;
  }
 
}

⌨️ 快捷键说明

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