Ejemplo n.º 1
0
 @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"));
   }
 }
Ejemplo n.º 2
0
  @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);
  }
Ejemplo n.º 3
0
 @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");
   }
 }
Ejemplo n.º 4
0
  @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);
  }
Ejemplo n.º 5
0
  @Test
  public void testInvokeUsingStandardizedLoadVariableReference() {
    Context context = ContextBuilder.create().addVariable("s", String.class).getContext();

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

    assertEquals("Failed using load() passing a variable reference", "s.toUpperCase()", s);
  }
Ejemplo n.º 6
0
 @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");
   }
 }
Ejemplo n.º 7
0
  @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);
  }
Ejemplo n.º 8
0
  @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
    }
  }
Ejemplo n.º 9
0
  @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);
  }