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

📄 enumtest.java

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

import java.io.*;
import junit.framework.*;

public class EnumTest extends TestCase {
  
  private static class TestEnum extends Enum {
    
    public static TestEnum TEST_ENUM_ONE = new TestEnum( "one", "First test enum" ); 
    public static TestEnum TEST_ENUM_TWO = new TestEnum( "two", "Second test enum" ); 
    public static TestEnum TEST_ENUM_THREE = new TestEnum( "three", "Third test enum" );
    
    private String text = null;

    private TestEnum(String id, String text) {
      super(id);
      this.text = text;
    }
    
    public String getText() {
     return text; 
    }
  }

  public void testIdTextConversion() {
    assertEquals( "one", TestEnum.TEST_ENUM_ONE.getId() );
    assertEquals( "First test enum", TestEnum.TEST_ENUM_ONE.getText() );
  }
  
  public void testEquality() {
    assertSame( TestEnum.TEST_ENUM_ONE, Enum.findById( TestEnum.class, "one" ) );
  }
  
  public void testNonExisting() {
    // this should not throw exceptions
    assertNull( Enum.findById( TestEnum.class, "non-existing id" ) );
    assertNull( Enum.findById( TestEnum.class, null ) );
  }
  
  public void testExceptions() {
    try {
      new TestEnum( null, "null id is not allowed" );
      fail();
    } catch ( NullPointerException e ) {
      //OK 
    }

    try {
      new TestEnum( "three", "duplicate id is not allowed" );
      fail();
    } catch ( IllegalArgumentException e ) {
      //OK 
    }
  }
  
  public void testSerialization() throws Exception {
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    ObjectOutputStream objectsOut = new ObjectOutputStream( bytesOut );
    objectsOut.writeObject( TestEnum.TEST_ENUM_TWO );
    objectsOut.writeObject( TestEnum.TEST_ENUM_ONE );
    objectsOut.writeObject( TestEnum.TEST_ENUM_THREE );

    ByteArrayInputStream bytesIn = new ByteArrayInputStream( bytesOut.toByteArray() );
    ObjectInputStream objectsIn = new ObjectInputStream( bytesIn );
    
    assertSame( TestEnum.TEST_ENUM_TWO, objectsIn.readObject() );
    assertSame( TestEnum.TEST_ENUM_ONE, objectsIn.readObject() );
    assertSame( TestEnum.TEST_ENUM_THREE, objectsIn.readObject() );
  }
}

⌨️ 快捷键说明

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