如何单元测试hadoop的可写性

1u4esq0p  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(327)

我实现了 WritableComparable 我自己的,但我找不到好的wasy单元测试 write 以及 readFields 方法。
有什么想法吗?

s1ag04yj

s1ag04yj1#

也许您可以找到更简单的方法来测试可写性,但是手动执行序列化/反序列化也可以。例如:
myutils.java文件:

...
import org.apache.commons.io.IOUtils;
...
public static byte[] serialize(Writable writable) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataOut = null;
        try {
            dataOut = new DataOutputStream(out);
            writable.write(dataOut);
            return out.toByteArray();
        } 
        finally {
            IOUtils.closeQuietly(dataOut);
        }
    }

public static <T extends Writable> T asWritable(byte[] bytes, Class<T> clazz)
            throws IOException {
        T result = null;
        DataInputStream dataIn = null;
        try {
            result = clazz.newInstance();
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            dataIn = new DataInputStream(in);
            result.readFields(dataIn);
        } catch (InstantiationException e) {
            // should not happen
            assert false;
        } catch (IllegalAccessException e) {
            // should not happen
            assert false;
        } finally {
            IOUtils.closeQuietly(dataIn);
        }
        return result;
    }

然后在你的测试课上:

CustomWritable record = ... ; //your initialized Writable
byte[] serializedBytes = MyUtils.serialize(record);

CustomWritable deserialized = 
  MyUtils.asWritable(serializedBytes, CustomWritable.class);

assertEquals("Value mismatch!", record.getFieldA(), deserialized.getFieldA());
...

相关问题