@Test public void testInvokeStaticMethod() { String s = StatementBuilder.create() .invokeStatic(Integer.class, "getInteger", "123") .toJavaString(); assertEquals("Failed to generate static method invocation", "Integer.getInteger(\"123\")", s); }
@Test public void testInvokeOnLiteral() { String result = StatementBuilder.create().loadLiteral("foo").invoke("toString").toJavaString(); assertEquals("Failed to generate invocation using literal parameters", "\"foo\".toString()", result); }
@Test public void testInvokeUsingStandardizedLoadLiteral() { String s = StatementBuilder.create() .load("foo") .invoke("toUpperCase").toJavaString(); assertEquals("Failed injecting literal with load()", "\"foo\".toUpperCase()", s); }
@Test public void testInvokeWithLiteralParameters() { String result = StatementBuilder.create().declareVariable("s", String.class) .loadVariable("s").invoke("replaceAll", "foo", "foo\t\n").toJavaString(); assertEquals("Failed to generate invocation using literal parameters", "s.replaceAll(\"foo\", \"foo\\t\\n\")", result); }
@Test public void testInvokeBestMatchingMethod() { String s = StatementBuilder.create() .declareVariable("n", Integer.class) .loadVariable("n") // 1 will be inferred to LiteralValue<Integer>, equals(Integer.class) // should be matched equals(Object.class) .invoke("equals", 1) .toJavaString(); assertEquals("Failed to generate invocation on matched method", "n.equals(1)", s); }
@Test public void testTypeInferenceWorksPropertyForParameterizedMethodTypes() { final String s = Stmt.loadStatic(BeanWithTypeParmedMeths.class, "INSTANCE") .invoke("setFooBarMap", Stmt.loadStatic(BeanWithTypeParmedMeths.class, "INSTANCE").invoke("getFooBarMap")) .toJavaString(); assertEquals("org.jboss.errai.codegen.test.model.BeanWithTypeParmedMeths.INSTANCE" + ".setFooBarMap(org.jboss.errai.codegen.test.model.BeanWithTypeParmedMeths.INSTANCE.getFooBarMap())", s); }
@Test public void testSingleInvocation() { String s = StatementBuilder.create() .declareVariable("obj", Object.class) .loadVariable("obj") .invoke("toString") .toJavaString(); assertEquals("Failed to generate invocation on variable", "obj.toString()", s); }
@Test public void testInvokeWithParameterTypeConversionOfIntegerToString() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("endsWith", 123) .toJavaString(); assertEquals("Failed to generate invocation with parameter type conversion", "str.endsWith(\"123\")", s); }
@Test public void testInvokeWithParameterTypeConversionOfStringToInteger() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("substring", "1", "3") .toJavaString(); assertEquals("Failed to generate invocation with parameter type conversion", "str.substring(1, 3)", s); }
@Test public void testInvokeUndefinedStaticMethod() { try { StatementBuilder.create() .invokeStatic(Integer.class, "intValue") .toJavaString(); fail("expected UndefinedMethodException"); } catch (UndefinedMethodException udme) { // expected assertEquals("Wrong exception details", udme.getMethodName(), "intValue"); } }
@Test public void testInvokeOnUndefinedVariable() { try { // injector undefined StatementBuilder.create() .loadVariable("injector") .invoke("provide", Refs.get("param"), Refs.get("param2")) .toJavaString(); fail("expected OutOfScopeException"); } catch (OutOfScopeException oose) { // expected assertTrue("Wrong exception thrown", oose.getMessage().contains("injector")); } }
@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); }
@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 testNestedCall() { final String s = StatementBuilder.create() .nestedCall( StatementBuilder.create().declareVariable("n", Integer.class).loadVariable("n").invoke("toString")) .invoke("getBytes") .toJavaString(); assertEquals("failed to generate nested call", "n.toString().getBytes()", s); }
@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 testInvokeWithParameterTypeConversionOfVariable() { Context c = Context.create().addVariable("n", Integer.class, 123); String s = StatementBuilder.create(c) .declareVariable("str", String.class) .loadVariable("str") .invoke("endsWith", c.getVariable("n").getValue()) .toJavaString(); assertEquals("Failed to generate invocation with parameter type conversion of variable", "str.endsWith(\"123\")", s); }
@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 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"); } }
/** * This reproduces a bug found in the JavaReflectionClass. * PortableIntegerParameterDefinition overrides a method with a generic return * type (getValue). Class.getDeclaredMethods returns a bridge method with a * return type of the upper type bound (in this case, Serializable). This * resulted in JavaReflectionClass.getMethods() returning a MetaMethod for * bridge method with the wrong return type, leading to seemingly mysterious * codegen errors. */ @Test public void testMethodReturnTypeIsSpecializedTypeAndNotFromBridgeMethod() throws Exception { final MetaClass mc = getMetaClassImpl(PortableIntegerParameterDefinition.class); final MetaMethod method = mc.getBestMatchingMethod("getValue", new MetaClass[0]); final ContextualStatementBuilder invokeStmt = nestedCall(newObject(mc)).invoke("getValue"); // Force statement to load return type invokeStmt.toJavaString(); final MetaClass stmtReturnType = invokeStmt.getType(); final MetaClass expected = getMetaClassImpl(java.lang.Integer.class); assertEquals(expected, method.getReturnType()); assertEquals(expected, stmtReturnType); }
@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"); } }