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 6.0KB

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