PublishedServerEventHandlerRpcHandler extends AbstractRpcInvocationHandler { static void invokeMethod(Component instance, Class<?> clazz, String methodName, JsonArray args, int promiseId) { assert instance != null; Optional<Method> method = findMethod(instance, clazz, methodName); if (method.isPresent()) { invokeMethod(instance, method.get(), args, promiseId); } else if (instance instanceof Composite) { Component compositeContent = ((Composite<?>) instance).getContent(); invokeMethod(compositeContent, compositeContent.getClass(), methodName, args, promiseId); } else { String msg = String.format("Neither class '%s' " + "nor its super classes declare event handler method '%s'", instance.getClass().getName(), methodName); throw new IllegalStateException(msg); } }  @Override String getRpcType(); @Override Optional<Runnable> handleNode(StateNode node,
            JsonObject invocationJson);  }
@Test public void methodIsInvoked() { ComponentWithCompute component = new ComponentWithCompute(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "method", Json.createArray(), -1); Assert.assertTrue(component.isInvoked); }
@Test public void methodIsInvokedOnCompositeContent() { CompositeOfComponentWithCompute composite = new CompositeOfComponentWithCompute(); ComponentWithCompute component = composite.getContent(); PublishedServerEventHandlerRpcHandler.invokeMethod(composite, composite.getClass(), "method", Json.createArray(), -1); Assert.assertTrue(component.isInvoked); }
@Test public void methodIsInvokectOnCompositeOfComposite() { CompositeOfComposite composite = new CompositeOfComposite(); ComponentWithCompute component = composite.getContent().getContent(); PublishedServerEventHandlerRpcHandler.invokeMethod(composite, composite.getClass(), "method", Json.createArray(), -1); Assert.assertTrue(component.isInvoked); }
@Test public void methodWithDecoderParameters_convertableValues_methodIsInvoked() { JsonArray params = Json.createArray(); params.set(0, "264"); params.set(1, "MRS"); DecoderParameters component = new DecoderParameters(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "method", params, -1); Assert.assertTrue(component.isInvoked); }
@Test(expected = IllegalArgumentException.class) public void methodWithDecoderParameters_nonConvertableValues_methodIsInvoked() { JsonArray params = Json.createArray(); params.set(0, "264.1"); params.set(1, "MR"); DecoderParameters component = new DecoderParameters(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "method", params, -1); }
@Test(expected = IllegalArgumentException.class) public void methodWithoutArgs_argsProvided() { JsonArray args = Json.createArray(); args.set(0, true); ComponentWithCompute component = new ComponentWithCompute(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "method", args, -1); }
@Test public void promiseSuccess() { int promiseId = 4; JsonArray args = Json.createArray(); args.set(0, 36); ComponentWithCompute component = new ComponentWithCompute(); UI ui = new UI(); ui.getInternals().setSession(session); ui.add(component); ui.getInternals().getStateTree().runExecutionsBeforeClientResponse(); ui.getInternals().dumpPendingJavaScriptInvocations(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "compute", args, promiseId); ui.getInternals().getStateTree().runExecutionsBeforeClientResponse(); List<PendingJavaScriptInvocation> pendingJavaScriptInvocations = ui .getInternals().dumpPendingJavaScriptInvocations(); Assert.assertEquals(1, pendingJavaScriptInvocations.size()); JavaScriptInvocation invocation = pendingJavaScriptInvocations.get(0) .getInvocation(); Assert.assertTrue("Invocation does not look like a promise callback", invocation.getExpression() .contains(JsonConstants.RPC_PROMISE_CALLBACK_NAME)); List<Object> parameters = invocation.getParameters(); Assert.assertEquals( "Expected three paramters: promiseId, value, target", 3, parameters.size()); Assert.assertEquals( "Promise id should match the value passed to invokeMethod", Integer.valueOf(promiseId), parameters.get(0)); Assert.assertEquals("Promise value should be sqrt(36) = 6", Integer.valueOf(6), parameters.get(1)); Assert.assertEquals("Target should be the component's element", component.getElement(), parameters.get(2)); }
@Test public void promiseFailure() { int promiseId = 4; JsonArray args = Json.createArray(); args.set(0, -36); ComponentWithCompute component = new ComponentWithCompute(); UI ui = new UI(); ui.getInternals().setSession(session); ui.add(component); ui.getInternals().getStateTree().runExecutionsBeforeClientResponse(); ui.getInternals().dumpPendingJavaScriptInvocations(); try { PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "compute", args, promiseId); Assert.fail("Exception should be thrown"); } catch (RuntimeException e) { Assert.assertTrue(e.getCause() instanceof ArithmeticException); } ui.getInternals().getStateTree().runExecutionsBeforeClientResponse(); List<PendingJavaScriptInvocation> pendingJavaScriptInvocations = ui .getInternals().dumpPendingJavaScriptInvocations(); Assert.assertEquals(1, pendingJavaScriptInvocations.size()); JavaScriptInvocation invocation = pendingJavaScriptInvocations.get(0) .getInvocation(); Assert.assertTrue("Invocation does not look like a promise callback", invocation.getExpression() .contains(JsonConstants.RPC_PROMISE_CALLBACK_NAME)); List<Object> parameters = invocation.getParameters(); Assert.assertEquals("Expected two paramters: promiseId, target", 2, parameters.size()); Assert.assertEquals( "Promise id should match the value passed to invokeMethod", Integer.valueOf(promiseId), parameters.get(0)); Assert.assertEquals("Target should be the component's element", component.getElement(), parameters.get(1)); }
@Test public void methodWithVarArg_acceptNoValues() { JsonArray array = Json.createArray(); MethodWithVarArgParameter component = new MethodWithVarArgParameter(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "varArgMethod", array, -1); Assert.assertEquals(0, component.varArg.length); }
@Test public void methodWithSeveralArgsAndVarArg_acceptNoValues() { JsonArray array = Json.createArray(); JsonArray firstArg = Json.createArray(); firstArg.set(0, 5.6d); firstArg.set(1, 78.36d); array.set(0, firstArg); MethodWithParameters component = new MethodWithParameters(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "method2", array, -1); Assert.assertArrayEquals( new Double[] { firstArg.getNumber(0), firstArg.getNumber(1) }, component.doubleArg); Assert.assertNotNull(component.varArg); Assert.assertEquals(0, component.varArg.length); }
@Test public void methodWithVarArg_acceptOneValue() { JsonArray array = Json.createArray(); array.set(0, "foo"); MethodWithVarArgParameter component = new MethodWithVarArgParameter(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "varArgMethod", array, -1); Assert.assertEquals(1, component.varArg.length); Assert.assertEquals("foo", component.varArg[0]); }
@Test public void methodWithVarArg_arrayIsCorrectlyHandled() { JsonArray array = Json.createArray(); JsonArray value = Json.createArray(); value.set(0, "foo"); array.set(0, value); MethodWithVarArgParameter component = new MethodWithVarArgParameter(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "varArgMethod", array, -1); Assert.assertArrayEquals(new String[] { value.getString(0) }, component.varArg); }
@Test(expected = IllegalStateException.class) public void noClientCallableMethodException() { ComponentWithNoClientCallableMethod component = new ComponentWithNoClientCallableMethod(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "operation", Json.createArray(), -1); }
@Test(expected = IllegalStateException.class) public void noMethodException() { ComponentWithNoClientCallableMethod component = new ComponentWithNoClientCallableMethod(); PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "operation1", Json.createArray(), -1); }