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.

AjaxTestJre.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2013, The gwtquery team.
  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.google.gwt.query.client.ajax;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.Random;
  20. import javax.servlet.Servlet;
  21. import org.mortbay.jetty.Server;
  22. import org.mortbay.jetty.handler.HandlerWrapper;
  23. import org.mortbay.jetty.servlet.DefaultServlet;
  24. import org.mortbay.jetty.webapp.WebAppClassLoader;
  25. import org.mortbay.jetty.webapp.WebAppContext;
  26. import com.google.gwt.query.servlet.GQAjaxTestServlet;
  27. import com.google.gwt.query.vm.AjaxTransportJre;
  28. /**
  29. * Tests for Data Binders and Ajax run in the JVM
  30. */
  31. public class AjaxTestJre extends AjaxTests {
  32. static Server server;
  33. static int port = new Random().nextInt(1000) + 2000;
  34. public String getModuleName() {
  35. return null;
  36. }
  37. public AjaxTestJre() throws Exception {
  38. String localDomain = "http://127.0.0.1:" + port;
  39. AjaxTransportJre.enableCORS(localDomain);
  40. String corsDomain = "http://localhost:" + port;
  41. echoUrl = localDomain + "/" + servletPath;
  42. echoUrlCORS = corsDomain + "/" + servletPath + "?cors=true";
  43. startWebServer(port);
  44. }
  45. protected void startWebServer(int port) throws Exception {
  46. if (server == null) {
  47. final Map<String, Class<? extends Servlet>> servlets = new HashMap<String, Class<? extends Servlet>>();
  48. servlets.put("/" + servletPath, GQAjaxTestServlet.class);
  49. server = createWebServer(port, ".", null, servlets, null);
  50. }
  51. }
  52. public static Server createWebServer(final int port, final String resourceBase, final String[] classpath,
  53. final Map<String, Class<? extends Servlet>> servlets, final HandlerWrapper handler) throws Exception {
  54. final Server server = new Server(port);
  55. final WebAppContext context = new WebAppContext();
  56. context.setContextPath("/");
  57. context.setResourceBase(resourceBase);
  58. if (servlets != null) {
  59. for (final Map.Entry<String, Class<? extends Servlet>> entry : servlets.entrySet()) {
  60. final String pathSpec = entry.getKey();
  61. final Class<? extends Servlet> servlet = entry.getValue();
  62. context.addServlet(servlet, pathSpec);
  63. // disable defaults if someone likes to register his own root servlet
  64. if ("/".equals(pathSpec)) {
  65. context.setDefaultsDescriptor(null);
  66. context.addServlet(DefaultServlet.class, "/favicon.ico");
  67. }
  68. }
  69. }
  70. final WebAppClassLoader loader = new WebAppClassLoader(context);
  71. if (classpath != null) {
  72. for (final String path : classpath) {
  73. loader.addClassPath(path);
  74. }
  75. }
  76. context.setClassLoader(loader);
  77. if (handler != null) {
  78. handler.setHandler(context);
  79. server.setHandler(handler);
  80. } else {
  81. server.setHandler(context);
  82. }
  83. server.start();
  84. return server;
  85. }
  86. }