You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestClientMethodSerialization.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.server;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.lang.reflect.Method;
  23. import junit.framework.TestCase;
  24. import com.vaadin.server.ClientMethodInvocation;
  25. import com.vaadin.server.JavaScriptCallbackHelper;
  26. import com.vaadin.ui.JavaScript.JavaScriptCallbackRpc;
  27. import com.vaadin.util.ReflectTools;
  28. import elemental.json.Json;
  29. import elemental.json.JsonArray;
  30. import elemental.json.impl.JsonUtil;
  31. public class TestClientMethodSerialization extends TestCase {
  32. private static final Method JAVASCRIPT_CALLBACK_METHOD = ReflectTools
  33. .findMethod(JavaScriptCallbackRpc.class, "call", String.class,
  34. JsonArray.class);
  35. private static final Method BASIC_PARAMS_CALL_METHOD = ReflectTools
  36. .findMethod(TestClientMethodSerialization.class,
  37. "basicParamsMethodForTesting", String.class, Integer.class);
  38. private static final Method NO_PARAMS_CALL_METHOD = ReflectTools
  39. .findMethod(TestClientMethodSerialization.class,
  40. "noParamsMethodForTesting");
  41. public void basicParamsMethodForTesting(String stringParam,
  42. Integer integerParam) {
  43. }
  44. public void noParamsMethodForTesting() {
  45. }
  46. /**
  47. * Tests the {@link ClientMethodInvocation} serialization when using
  48. * {@link JavaScriptCallbackHelper#invokeCallback(String, Object...)}.
  49. * #12532
  50. */
  51. public void testClientMethodSerialization_WithJSONArray_ContentStaysSame()
  52. throws Exception {
  53. JsonArray originalArray = Json.createArray();
  54. originalArray.set(0, "callbackParameter1");
  55. originalArray.set(1, "callBackParameter2");
  56. originalArray.set(2, "12345");
  57. ClientMethodInvocation original = new ClientMethodInvocation(null,
  58. "interfaceName", JAVASCRIPT_CALLBACK_METHOD, new Object[] {
  59. "callBackMethodName", originalArray });
  60. ClientMethodInvocation copy = (ClientMethodInvocation) serializeAndDeserialize(original);
  61. JsonArray copyArray = (JsonArray) copy.getParameters()[1];
  62. assertEquals(JsonUtil.stringify(originalArray), JsonUtil.stringify(copyArray));
  63. }
  64. public void testClientMethodSerialization_WithBasicParams_NoChanges()
  65. throws Exception {
  66. String stringParam = "a string 123";
  67. Integer integerParam = 1234567890;
  68. ClientMethodInvocation original = new ClientMethodInvocation(null,
  69. "interfaceName", BASIC_PARAMS_CALL_METHOD, new Serializable[] {
  70. stringParam, integerParam });
  71. ClientMethodInvocation copy = (ClientMethodInvocation) serializeAndDeserialize(original);
  72. String copyString = (String) copy.getParameters()[0];
  73. Integer copyInteger = (Integer) copy.getParameters()[1];
  74. assertEquals(copyString, stringParam);
  75. assertEquals(copyInteger, integerParam);
  76. }
  77. public void testClientMethodSerialization_NoParams_NoExceptions() {
  78. ClientMethodInvocation original = new ClientMethodInvocation(null,
  79. "interfaceName", NO_PARAMS_CALL_METHOD, null);
  80. ClientMethodInvocation copy = (ClientMethodInvocation) serializeAndDeserialize(original);
  81. }
  82. private static Serializable serializeAndDeserialize(Serializable input) {
  83. Serializable output = null;
  84. try {
  85. ByteArrayOutputStream bs = new ByteArrayOutputStream();
  86. ObjectOutputStream out = new ObjectOutputStream(bs);
  87. out.writeObject(input);
  88. byte[] data = bs.toByteArray();
  89. ObjectInputStream in = new ObjectInputStream(
  90. new ByteArrayInputStream(data));
  91. output = (Serializable) in.readObject();
  92. } catch (Exception e) {
  93. fail("Exception during serialization/deserialization: "
  94. + e.getMessage());
  95. }
  96. return output;
  97. }
  98. }