@Test public void testInvokeWithParameterizedClassAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("set", Set.class, StatementBuilder.create().invokeStatic(Foo.class, "baz", Set.class)) .toJavaString(); assertEquals("Failed to generate method invocation with variable return type inferred from Class<T>", "Set set = Foo.baz(Set.class);", s); }
@Test public void testInvokeWithParameterizedListAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("list", new TypeLiteral<List<String>>() { }) .declareVariable("str", String.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("list"))) .toJavaString(); assertEquals("Failed to generate method invocation with variable return type inferred from List<T>", "String str = Foo.bar(list);", s); }
@Test public void testInvokeWithParameterizedMapAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("map", new TypeLiteral<Map<String, Integer>>() { }) .declareVariable("val", Integer.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("map"))) .toJavaString(); assertEquals("Failed to generate method invocation with variable return type inferred from Map<K, V>", "Integer val = Foo.bar(map);", s); }
@Test public void testInvokeWithVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("s", String.class) .declareVariable("str", String.class, StatementBuilder.create().invokeStatic(Foo.class, "foo", Variable.get("s"))) .toJavaString(); assertEquals("Failed to generate method invocation using variable return type", "String str = Foo.foo(s);", s); }
@Test public void testDeclareVariableWithObjectInitializationUsingSuperClassType() { String declaration = ContextBuilder.create() .declareVariable("str", Object.class) .initializeWith(ObjectBuilder.newInstanceOf(String.class).withParameters("abc")) .toJavaString(); assertEquals("failed to generate variable declaration using an objectbuilder initialization with parameters", "Object str = new String(\"abc\");", declaration); try { Stmt.declareVariable("str", Integer.class, ObjectBuilder.newInstanceOf(String.class).withParameters("abc")) .toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } try { Stmt.declareVariable("str", String.class, ObjectBuilder.newInstanceOf(Object.class)) .toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } }
@Test public void testInvokeWithNestedParameterizedListAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("n", int.class) .declareVariable("list", new TypeLiteral<List<List<Map<String, Integer>>>>() { }) .declareVariable("str", String.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("n"), Variable.get("list"))) .toJavaString(); assertEquals("Failed to generate method invocation with variable return type inferred from nested List<T>", "String str = Foo.bar(n, list);", s); }
@Test public void testDeclareVariableWithObjectInitializationWithStringTypeInference() { final Context ctx = Context.create(); final String s = StatementBuilder.create(ctx) .declareVariable("str", ObjectBuilder.newInstanceOf(String.class)).toJavaString(); assertEquals("failed to generate variable declaration with object initialization and string type inference", "String str = new String();", s); final VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), str.getType()); }
@Test public void testInvokeWithInvalidVariableReturnType() { try { StatementBuilder.create(Context.create().autoImport()) .declareVariable("list", new TypeLiteral<List<String>>() { }) .declareVariable("n", Integer.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("list"))) .toJavaString(); fail("expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected assertEquals("Wrong exception message", "java.lang.Integer is not assignable from java.lang.String", e.getMessage()); } }
@Test public void testDeclareVariableWithImplicitTypeConversion() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", Integer.class, "10").generate(ctx); assertEquals("failed to generate variable declaration with implicit type conversion", "Integer n = 10;", s); final VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(Integer.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral(10), n.getValue()); try { StatementBuilder.create().declareVariable("n", Integer.class, "abc").toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException ive) { // expected assertTrue(ive.getCause() instanceof NumberFormatException); } }