@Test public void testPairs() throws Exception { AvroType<Pair<String, String>> at = Avros.pairs(Avros.strings(), Avros.strings()); Pair<String, String> j = Pair.of("a", "b"); GenericData.Record w = new GenericData.Record(at.getSchema()); w.put(0, new Utf8("a")); w.put(1, new Utf8("b")); testInputOutputFn(at, j, w); }
@Test public void testCollections() throws Exception { Collection<String> j = Lists.newArrayList(); j.add("a"); j.add("b"); Schema collectionSchema = Schema.createArray(Avros.strings().getSchema()); GenericData.Array<Utf8> w = new GenericData.Array<Utf8>(2, collectionSchema); w.add(new Utf8("a")); w.add(new Utf8("b")); testInputOutputFn(Avros.collections(Avros.strings()), j, w); }
@Test @SuppressWarnings("rawtypes") public void testTriples() throws Exception { AvroType at = Avros.triples(Avros.strings(), Avros.strings(), Avros.strings()); Tuple3 j = Tuple3.of("a", "b", "c"); GenericData.Record w = new GenericData.Record(at.getSchema()); w.put(0, new Utf8("a")); w.put(1, new Utf8("b")); w.put(2, new Utf8("c")); testInputOutputFn(at, j, w); }
@Test @SuppressWarnings("rawtypes") public void testTableOf() throws Exception { AvroType at = Avros.tableOf(Avros.strings(), Avros.strings()); Pair<String, String> j = Pair.of("a", "b"); org.apache.avro.mapred.Pair w = new org.apache.avro.mapred.Pair(at.getSchema()); w.put(0, new Utf8("a")); w.put(1, new Utf8("b")); // TODO update this after resolving the o.a.a.m.Pair.equals issue initialize(at); assertEquals(j, at.getInputMapFn().map(w)); org.apache.avro.mapred.Pair converted = (org.apache.avro.mapred.Pair) at.getOutputMapFn().map(j); assertEquals(w.key(), converted.key()); assertEquals(w.value(), converted.value()); }
@Test public void testPairEquals() throws Exception { AvroType<Pair<Long, ByteBuffer>> at1 = Avros.pairs(Avros.longs(), Avros.bytes()); AvroType<Pair<Long, ByteBuffer>> at2 = Avros.pairs(Avros.longs(), Avros.bytes()); assertEquals(at1, at2); assertEquals(at1.hashCode(), at2.hashCode()); }
/** * Determine if the wrapped type is a specific data avro type or wraps one. * * @return true if the wrapped type is a specific data type or wraps one */ public boolean hasSpecific() { if (Avros.isPrimitive(this)) { return false; } if (!this.subTypes.isEmpty()) { for (PType<?> subType : this.subTypes) { AvroType<?> atype = (AvroType<?>) subType; if (atype.hasSpecific()) { return true; } } return false; } return SpecificRecord.class.isAssignableFrom(typeClass); }
/** * Determine if the wrapped type is a reflection-based avro type or wraps one. * * @return true if the wrapped type is a reflection-based type or wraps one. */ public boolean hasReflect() { if (Avros.isPrimitive(this)) { return false; } if (!this.subTypes.isEmpty()) { for (PType<?> subType : this.subTypes) { if (((AvroType<?>) subType).hasReflect()) { return true; } } return false; } return !(typeClass.equals(GenericData.Record.class) || SpecificRecord.class.isAssignableFrom(typeClass)); }
@Test public void testBytes() throws Exception { byte[] bytes = new byte[] {17, 26, -98}; ByteBuffer bb = ByteBuffer.wrap(bytes); testInputOutputFn(Avros.bytes(), bb, bb); }
@Test public void testBooleans() throws Exception { boolean j = true; testInputOutputFn(Avros.booleans(), j, j); }
@Test public void testDoubles() throws Exception { double j = Double.MIN_VALUE; testInputOutputFn(Avros.doubles(), j, j); }
@Test public void testFloats() throws Exception { float j = Float.MIN_VALUE; testInputOutputFn(Avros.floats(), j, j); }
@Test public void testLongs() throws Exception { long j = Long.MAX_VALUE; testInputOutputFn(Avros.longs(), j, j); }
@Test public void testInts() throws Exception { int j = 55; testInputOutputFn(Avros.ints(), j, j); }
@Test public void testStrings() throws Exception { String s = "abc"; Utf8 w = new Utf8(s); testInputOutputFn(Avros.strings(), s, w); }
@Test public void testNulls() throws Exception { Void n = null; testInputOutputFn(Avros.nulls(), n, n); }