From a5b2adb90a4afe692fa464d4e531eac19fe7d2bb Mon Sep 17 00:00:00 2001 From: Tom De Leu Date: Wed, 11 Jun 2014 22:09:08 +0200 Subject: [PATCH] CRUNCH-419 Writables.registerComparable does not actually register the WritableComparable --- .../apache/crunch/types/writable/Writables.java | 1 + .../crunch/types/writable/WritablesTest.java | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crunch-core/src/main/java/org/apache/crunch/types/writable/Writables.java b/crunch-core/src/main/java/org/apache/crunch/types/writable/Writables.java index f571f2fe..be2cb1a6 100644 --- a/crunch-core/src/main/java/org/apache/crunch/types/writable/Writables.java +++ b/crunch-core/src/main/java/org/apache/crunch/types/writable/Writables.java @@ -122,6 +122,7 @@ public static void registerComparable(Class clazz, if (WRITABLE_CODES.containsKey(code)) { throw new IllegalArgumentException("Already have writable class assigned to code = " + code); } + WRITABLE_CODES.put(code, clazz); } private static final String WRITABLE_COMPARABLE_CODES = "crunch.writable.comparable.codes"; diff --git a/crunch-core/src/test/java/org/apache/crunch/types/writable/WritablesTest.java b/crunch-core/src/test/java/org/apache/crunch/types/writable/WritablesTest.java index 70099f25..2281473d 100644 --- a/crunch-core/src/test/java/org/apache/crunch/types/writable/WritablesTest.java +++ b/crunch-core/src/test/java/org/apache/crunch/types/writable/WritablesTest.java @@ -42,7 +42,7 @@ import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; -import org.apache.hadoop.io.Writable; +import org.apache.hadoop.io.WritableComparable; import org.apache.hadoop.io.WritableUtils; import org.junit.Test; @@ -172,7 +172,7 @@ public void testTupleN() throws Exception { testInputOutputFn(wt, j, w); } - protected static class TestWritable implements Writable { + protected static class TestWritable implements WritableComparable { String left; int right; @@ -207,6 +207,13 @@ public boolean equals(Object obj) { return true; } + @Override + public int compareTo(TestWritable o) { + int cmp = left.compareTo(o.left); + if (cmp != 0) + return cmp; + return Integer.valueOf(right).compareTo(Integer.valueOf(o.right)); + } } @Test @@ -236,6 +243,12 @@ public void testRegister() throws Exception { assertSame(Writables.records(TestWritable.class), wt); } + @Test + public void testRegisterComparable() throws Exception { + Writables.registerComparable(TestWritable.class); + assertNotNull(Writables.WRITABLE_CODES.inverse().get(TestWritable.class)); + } + @SuppressWarnings({ "unchecked", "rawtypes" }) protected static void testInputOutputFn(PType ptype, Object java, Object writable) { ptype.getInputMapFn().initialize();