@Test
 public void testInvokeWithUndefinedVariable() {
   try {
     // param2 undefined
     StatementBuilder.create()
         .declareVariable("obj", Object.class)
         .declareVariable("param", String.class)
         .loadVariable("obj")
         .invoke("undefinedMethod", Variable.get("param"), Variable.get("param2"))
         .toJavaString();
     fail("expected OutOfScopeException");
   } catch (OutOfScopeException oose) {
     // expected
     assertTrue(oose.getMessage().contains("param2"));
   }
 }
  @Test
  public void testChainedInvocations() {
    String s =
        StatementBuilder.create()
            .declareVariable("i", Integer.class)
            .declareVariable("regex", String.class)
            .declareVariable("replacement", String.class)
            .loadVariable("i")
            .invoke("toString")
            .invoke("replaceAll", Variable.get("regex"), Variable.get("replacement"))
            .toJavaString();

    assertEquals(
        "Failed to generate chained invocations on variable",
        "i.toString().replaceAll(regex, replacement)",
        s);
  }
 @Test
 public void testInvokeChainedUndefinedMethod() {
   try {
     StatementBuilder.create()
         .declareVariable("s", String.class)
         .declareVariable("regex", String.class)
         .declareVariable("replacement", String.class)
         .loadVariable("s")
         .invoke("replaceAll", Variable.get("regex"), Variable.get("replacement"))
         .invoke("undefinedMethod", Variable.get("regex"), Variable.get("replacement"))
         .toJavaString();
     fail("expected UndefinedMethodException");
   } catch (UndefinedMethodException udme) {
     // expected
     assertEquals("Wrong exception thrown", udme.getMethodName(), "undefinedMethod");
   }
 }
  @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 testInvokeUsingStandardizedLoadVariableInstance() {
    Context context = ContextBuilder.create().addVariable("s", String.class).getContext();

    Variable v = Variable.create("s", String.class);
    String s = StatementBuilder.create(context).load(v).invoke("toUpperCase").toJavaString();

    assertEquals("Failed using load() passing a variable instance", "s.toUpperCase()", s);
  }
 @Test
 public void testInvokeUndefinedMethodOnVariable() {
   try {
     StatementBuilder.create()
         .declareVariable("obj", Object.class)
         .declareVariable("param", String.class)
         .loadVariable("obj")
         .invoke("undefinedMethod", Variable.get("param"))
         .toJavaString();
     fail("expected UndefinedMethodException");
   } catch (UndefinedMethodException udme) {
     // expected
     assertEquals("Wrong exception thrown", udme.getMethodName(), "undefinedMethod");
   }
 }
  @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 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
    }
  }
  @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);
  }