private static Class<List<Object>> getCls(List<Object> instance) { return TypeExtractor.getForObject(instance).getTypeClass(); }
@Test(expected=InvalidTypesException.class) public void testEitherFromObjectException() { Either<String, Tuple1<Integer>> either = Either.Left("test"); TypeExtractor.getForObject(either); }
@Test(expected = CompositeType.InvalidFieldReferenceException.class) public void testIllegalFlatTuple() { Tuple2<String, Integer> t = Tuple2.of("aa", 5); TupleTypeInfo<Tuple2<String, Integer>> tpeInfo = (TupleTypeInfo<Tuple2<String, Integer>>) TypeExtractor.getForObject(t); FieldAccessorFactory.getAccessor(tpeInfo, "illegal", null); }
public TypeSerializerFormatTest(int numberOfTuples, long blockSize, int parallelism) { super(numberOfTuples, blockSize, parallelism); resultType = TypeExtractor.getForObject(getRecord(0)); serializer = resultType.createSerializer(new ExecutionConfig()); }
@Test public void testRecursivePojoObjectTypeExtraction() { TypeInformation<Recursive1Pojo> type = TypeExtractor.getForObject(new Recursive1Pojo()); assertTrue("Extracted type is not a Pojo type but should be.", type instanceof CompositeType); }
private <T extends Tuple> void runTests(int length, T... instances) { try { TupleTypeInfo<T> tupleTypeInfo = (TupleTypeInfo<T>) TypeExtractor.getForObject(instances[0]); TypeSerializer<T> serializer = tupleTypeInfo.createSerializer(new ExecutionConfig()); Class<T> tupleClass = tupleTypeInfo.getTypeClass(); if(tupleClass == Tuple0.class) { length = 1; } TupleSerializerTestInstance<T> test = new TupleSerializerTestInstance<T>(serializer, tupleClass, length, instances); test.testAll(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); Assert.fail(e.getMessage()); } } }
public ListSink() throws Exception { super(new SimpleCommitter(), TypeExtractor.getForObject(new Tuple1<>(1)).createSerializer(new ExecutionConfig()), "job"); }
public ListSink2() throws Exception { super(new FailingCommitter(), TypeExtractor.getForObject(new Tuple1<>(1)).createSerializer(new ExecutionConfig()), "job"); }
@Test public void testPojoAllPublic() { TypeInformation<?> typeForClass = TypeExtractor.createTypeInfo(AllPublic.class); checkAllPublicAsserts(typeForClass); TypeInformation<?> typeForObject = TypeExtractor.getForObject(new AllPublic() ); checkAllPublicAsserts(typeForObject); }
@Test(expected = CompositeType.InvalidFieldReferenceException.class) public void testIllegalTupleInPojoInTuple() { Tuple2<String, Foo> t = Tuple2.of("aa", new Foo(8, Tuple2.of("ddd", 9L), (short) 2)); TupleTypeInfo<Tuple2<String, Foo>> tpeInfo = (TupleTypeInfo<Tuple2<String, Foo>>) TypeExtractor.getForObject(t); FieldAccessorFactory.getAccessor(tpeInfo, "illegal.illegal.illegal", null); }
@Test public void testPojoExtendingTuple() { TypeInformation<?> typeForClass = TypeExtractor.createTypeInfo(FromTuple.class); checkFromTuplePojo(typeForClass); FromTuple ft = new FromTuple(); ft.f0 = ""; ft.f1 = ""; ft.f2 = 0L; TypeInformation<?> typeForObject = TypeExtractor.getForObject(ft); checkFromTuplePojo(typeForObject); }
@Test public void testPojoWC() { TypeInformation<?> typeForClass = TypeExtractor.createTypeInfo(WC.class); checkWCPojoAsserts(typeForClass); WC t = new WC(); t.complex = new ComplexNestedClass(); TypeInformation<?> typeForObject = TypeExtractor.getForObject(t); checkWCPojoAsserts(typeForObject); }
@Test public void testRow() { Row row = new Row(2); row.setField(0, "string"); row.setField(1, 15); TypeInformation<Row> rowInfo = TypeExtractor.getForObject(row); Assert.assertEquals(rowInfo.getClass(), RowTypeInfo.class); Assert.assertEquals(2, rowInfo.getArity()); Assert.assertEquals( new RowTypeInfo( BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO), rowInfo); Row nullRow = new Row(2); TypeInformation<Row> genericRowInfo = TypeExtractor.getForObject(nullRow); Assert.assertEquals(genericRowInfo, new GenericTypeInfo<>(Row.class)); }
@Test public void testSimpleType() { TypeInformation<?> ti = TypeExtractor.createTypeInfo(IntLike.class); assertEquals(INT_TYPE_INFO, ti); ti = TypeExtractor.getForClass(IntLike.class); assertEquals(INT_TYPE_INFO, ti); ti = TypeExtractor.getForObject(new IntLike()); assertEquals(INT_TYPE_INFO, ti); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testValue() { // use getKeyExtractorType() KeySelector<?, ?> function = new KeySelector<StringValue, StringValue>() { private static final long serialVersionUID = 1L; @Override public StringValue getKey(StringValue value) { return null; } }; TypeInformation<?> ti = TypeExtractor.getKeySelectorTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<StringValue>(){})); Assert.assertFalse(ti.isBasicType()); Assert.assertFalse(ti.isTupleType()); Assert.assertTrue(ti instanceof ValueTypeInfo); Assert.assertEquals(ti.getTypeClass(), StringValue.class); // use getForClass() Assert.assertTrue(TypeExtractor.getForClass(StringValue.class) instanceof ValueTypeInfo); Assert.assertEquals(TypeExtractor.getForClass(StringValue.class).getTypeClass(), ti.getTypeClass()); // use getForObject() StringValue v = new StringValue("Hello"); Assert.assertTrue(TypeExtractor.getForObject(v) instanceof ValueTypeInfo); Assert.assertEquals(TypeExtractor.getForObject(v).getTypeClass(), ti.getTypeClass()); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testBasicType() { // use getGroupReduceReturnTypes() RichGroupReduceFunction<?, ?> function = new RichGroupReduceFunction<Boolean, Boolean>() { private static final long serialVersionUID = 1L; @Override public void reduce(Iterable<Boolean> values, Collector<Boolean> out) throws Exception { // nothing to do } }; TypeInformation<?> ti = TypeExtractor.getGroupReduceReturnTypes(function, (TypeInformation) Types.BOOLEAN); Assert.assertTrue(ti.isBasicType()); Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, ti); Assert.assertEquals(Boolean.class, ti.getTypeClass()); // use getForClass() Assert.assertTrue(TypeExtractor.getForClass(Boolean.class).isBasicType()); Assert.assertEquals(ti, TypeExtractor.getForClass(Boolean.class)); // use getForObject() Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, TypeExtractor.getForObject(true)); }
@Test public void testStringArrayWritable() { StringArrayWritable[] data = new StringArrayWritable[]{ new StringArrayWritable(new String[]{}), new StringArrayWritable(new String[]{""}), new StringArrayWritable(new String[]{"a", "a"}), new StringArrayWritable(new String[]{"a", "b"}), new StringArrayWritable(new String[]{"c", "c"}), new StringArrayWritable(new String[]{"d", "f"}), new StringArrayWritable(new String[]{"d", "m"}), new StringArrayWritable(new String[]{"z", "x"}), new StringArrayWritable(new String[]{"a", "a", "a"}) }; WritableTypeInfo<StringArrayWritable> writableTypeInfo = (WritableTypeInfo<StringArrayWritable>) TypeExtractor.getForObject(data[0]); WritableSerializer<StringArrayWritable> writableSerializer = (WritableSerializer<StringArrayWritable>) writableTypeInfo.createSerializer(new ExecutionConfig()); SerializerTestInstance<StringArrayWritable> testInstance = new SerializerTestInstance<StringArrayWritable>(writableSerializer, writableTypeInfo.getTypeClass(), -1, data); testInstance.testAll(); } }
@Test public void testEitherHierarchy() { MapFunction<?, ?> function = new EitherMapper<Boolean>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes((MapFunction) function, BasicTypeInfo.BOOLEAN_TYPE_INFO); TypeInformation<?> expected = new EitherTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.BOOLEAN_TYPE_INFO); Assert.assertEquals(expected, ti); function = new EitherMapper2(); ti = TypeExtractor.getMapReturnTypes((MapFunction) function, BasicTypeInfo.STRING_TYPE_INFO); expected = new EitherTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, new TupleTypeInfo(BasicTypeInfo.INT_TYPE_INFO)); Assert.assertEquals(expected, ti); function = new EitherMapper3(); ti = TypeExtractor.getMapReturnTypes((MapFunction) function, expected); Assert.assertEquals(expected, ti); Either<String, Tuple1<Integer>> either = new Either2(); ti = TypeExtractor.getForObject(either); Assert.assertEquals(expected, ti); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testSubclassOfTuple() { // use getJoinReturnTypes() RichFlatJoinFunction<?, ?, ?> function = new RichFlatJoinFunction<CustomTuple, String, CustomTuple>() { private static final long serialVersionUID = 1L; @Override public void join(CustomTuple first, String second, Collector<CustomTuple> out) throws Exception { out.collect(null); } }; TypeInformation<?> ti = TypeExtractor.getFlatJoinReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<String, Integer>>(){}), (TypeInformation) Types.STRING); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(1)); Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti).getTypeClass()); // use getForObject() CustomTuple t = new CustomTuple("hello", 1); TypeInformation<?> ti2 = TypeExtractor.getForObject(t); Assert.assertTrue(ti2.isTupleType()); Assert.assertEquals(2, ti2.getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(1)); Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti2).getTypeClass()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleOfValues() { // use getMapReturnTypes() RichMapFunction<?, ?> function = new RichMapFunction<Tuple2<StringValue, IntValue>, Tuple2<StringValue, IntValue>>() { private static final long serialVersionUID = 1L; @Override public Tuple2<StringValue, IntValue> map(Tuple2<StringValue, IntValue> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<StringValue, IntValue>>(){})); Assert.assertFalse(ti.isBasicType()); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(StringValue.class, ((TupleTypeInfo<?>) ti).getTypeAt(0).getTypeClass()); Assert.assertEquals(IntValue.class, ((TupleTypeInfo<?>) ti).getTypeAt(1).getTypeClass()); // use getForObject() Tuple2<StringValue, IntValue> t = new Tuple2<StringValue, IntValue>(new StringValue("x"), new IntValue(1)); TypeInformation<?> ti2 = TypeExtractor.getForObject(t); Assert.assertFalse(ti2.isBasicType()); Assert.assertTrue(ti2.isTupleType()); Assert.assertEquals(((TupleTypeInfo<?>) ti2).getTypeAt(0).getTypeClass(), StringValue.class); Assert.assertEquals(((TupleTypeInfo<?>) ti2).getTypeAt(1).getTypeClass(), IntValue.class); }