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.

TrackMessageSizeUI.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.vaadin.tests.push;
  2. import java.io.IOException;
  3. import java.net.MalformedURLException;
  4. import java.net.URL;
  5. import javax.servlet.ServletContext;
  6. import org.apache.commons.io.IOUtils;
  7. import com.vaadin.annotations.JavaScript;
  8. import com.vaadin.server.VaadinRequest;
  9. import com.vaadin.server.VaadinService;
  10. import com.vaadin.server.VaadinServletService;
  11. import com.vaadin.tests.components.AbstractTestUIWithLog;
  12. import com.vaadin.ui.JavaScriptFunction;
  13. import elemental.json.JsonArray;
  14. // Load vaadinPush.js so that jQueryVaadin is defined
  15. @JavaScript("vaadin://vaadinPush.debug.js")
  16. public class TrackMessageSizeUI extends AbstractTestUIWithLog {
  17. private String testMethod = "function testSequence(expected, data) {\n"
  18. + " var request = {trackMessageLength: true, messageDelimiter: '|'};\n"
  19. + " _request = {trackMessageLength: true, messageDelimiter: '|'};\n"
  20. + " _handleProtocol = function(a,message) {return message;};"
  21. + " var response = {partialMessage: ''};\n"
  22. + " var messages = [];\n"
  23. + " for (var i = 0; i < data.length; i++) {\n"
  24. + " if (!_trackMessageSize(data[i], request, response))\n"
  25. + " messages = messages.concat(response.messages);\n"
  26. + " }\n"
  27. + " if (JSON.stringify(expected) != JSON.stringify(messages)) {\n"
  28. + " if (console && typeof console.error == 'function') console.error('Expected', expected, 'but got', messages, 'for', data);\n"
  29. + " logToServer('Test failed, see javascript console for details.');\n"
  30. + " }" + "}\n";
  31. @Override
  32. protected void setup(VaadinRequest request) {
  33. String methodImplementation = findMethodImplementation();
  34. getPage().getJavaScript().addFunction("logToServer",
  35. new JavaScriptFunction() {
  36. @Override
  37. public void call(JsonArray arguments) {
  38. String message = arguments.getString(0);
  39. log(message);
  40. }
  41. });
  42. getPage().getJavaScript()
  43. .execute(methodImplementation + testMethod + buildTestCase());
  44. }
  45. private String buildTestCase() {
  46. // Could maybe express the cases in java and generate JS?
  47. return "testSequence(['a', 'b'], ['1|a1|b', '']);\n"
  48. + "testSequence(['a', 'b'], ['1|a1|', 'b']);\n"
  49. + "testSequence(['a', 'b'], ['1|a1', '|b']);\n"
  50. + "testSequence(['a', 'b'], ['1|a', '1|b']);\n"
  51. + "testSequence(['a', 'b'], ['1|a', '', '1|b']);\n"
  52. + "testSequence(['a|', '|b'], ['2|a|2||b']);\n"
  53. + "testSequence(['a|', 'b'], ['2|a|', '', '1|b']);\n"
  54. + "testSequence(['a|', 'b'], ['2|a|', '1|b']);\n"
  55. + "testSequence(['a|', 'b'], ['2|a|1', '|b']);\n"
  56. + "testSequence(['a|', 'b'], ['2|a|1|', 'b']);\n"
  57. + "testSequence([' ', 'b'], ['1| 1|b']);\n"
  58. + "testSequence([' ', 'b'], ['1| ','1|b']);\n"
  59. + "testSequence([' ', 'b'], ['1|',' 1|b']);\n"
  60. + "logToServer('All tests run')\n";
  61. }
  62. private String findMethodImplementation() {
  63. String filename = "/VAADIN/vaadinPush.debug.js";
  64. URL resourceURL = findResourceURL(filename,
  65. (VaadinServletService) VaadinService.getCurrent());
  66. if (resourceURL == null) {
  67. log("Can't find " + filename);
  68. return null;
  69. }
  70. try {
  71. String string = IOUtils.toString(resourceURL);
  72. // Find the function inside the script content
  73. int startIndex = string.indexOf("function _trackMessageSize");
  74. if (startIndex == -1) {
  75. log("function not found");
  76. return null;
  77. }
  78. // Assumes there's a /** comment before the next function
  79. int endIndex = string.indexOf("/**", startIndex);
  80. if (endIndex == -1) {
  81. log("End of function not found");
  82. return null;
  83. }
  84. string = string.substring(startIndex, endIndex);
  85. string = string.replaceAll("jQuery", "jQueryVaadin");
  86. return string;
  87. } catch (IOException e) {
  88. throw new RuntimeException(e);
  89. }
  90. }
  91. private URL findResourceURL(String filename, VaadinServletService service) {
  92. ServletContext sc = service.getServlet().getServletContext();
  93. URL resourceUrl;
  94. try {
  95. resourceUrl = sc.getResource(filename);
  96. } catch (MalformedURLException e) {
  97. throw new RuntimeException(e);
  98. }
  99. if (resourceUrl == null) {
  100. // try if requested file is found from classloader
  101. // strip leading "/" otherwise stream from JAR wont work
  102. if (filename.startsWith("/")) {
  103. filename = filename.substring(1);
  104. }
  105. resourceUrl = service.getClassLoader().getResource(filename);
  106. }
  107. return resourceUrl;
  108. }
  109. @Override
  110. protected String getTestDescription() {
  111. return "Unit tests for _trackMessageSize in vaadinPush.debug.js. Implemented with testbench and a full Vaadin server side since the testing requires some file mangling.";
  112. }
  113. @Override
  114. protected Integer getTicketNumber() {
  115. return Integer.valueOf(12468);
  116. }
  117. }