enumtest.java
来自「一个java工作流引擎」· Java 代码 · 共 72 行
JAVA
72 行
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 + =
减小字号Ctrl + -
显示快捷键?