package com.waldura.struts; import java.util.Arrays; import org.apache.struts.action.ActionForm; public class ActionFormSerializerTest extends junit.framework.TestCase { private final ActionFormSerializer afs = new ActionFormSerializer(); public ActionFormSerializerTest(String name) { super(name); } public void testHexConversion() { int b = afs.hexdigit('0'); assertTrue("hex digit conversion failed", b == 0); b = afs.hexdigit('9'); assertTrue("hex digit conversion failed", b == 9); b = afs.hexdigit('a'); assertTrue("hex digit conversion failed", b == 10); b = afs.hexdigit('f'); assertTrue("hex digit conversion failed", b == 15); } public void testEncode() { final byte[] in = { 'z', 'a', 0xf, 0xa, 127, 'b', 'X', 0, -1, 1 }; final String out = "7a610f0a7f625800ff01"; String s = afs.encode(in); assertEquals("encoding failed", out, s); } public void testFailedDecoding() { assertFailedDecoding(afs, "000"); assertFailedDecoding(afs, "000g"); } public static void assertFailedDecoding(ActionFormSerializer afs, String in) { try { afs.decode(in); fail("decode should fail on odd digits"); } catch (IllegalArgumentException iae) { // this is the success case; it should fail } } public void testDecode() { final String in = "7a610f0a7f625800ff01"; final byte[] out = { 'z', 'a', 0xf, 0xa, 127, 'b', 'X', 0, -1, 1 }; byte[] buf = afs.decode(in); assertTrue("encoding failed", Arrays.equals(out, buf)); } public void testEncodeDecode() { // binary data final byte[] in = { 'z', 'a', 0xf, 0xa, 127, 'b', 'X', 0, -1, 1 }; String temp = afs.encode(in); byte[] out = afs.decode(temp); assertTrue("encoding failed", Arrays.equals(in, out)); } public void testSerializeDeserialize() throws java.io.IOException, ClassNotFoundException { ActionForm orig = new ActionForm() {}; ActionForm copy = afs.deserialize( afs.serialize(orig) ); assertNotNull("serialization failed", copy); } }