PageRenderTime 1332ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/JsTestDriver/src/com/google/jstestdriver/IDEPluginActionBuilder.java

http://js-test-driver.googlecode.com/
Java | 193 lines | 144 code | 24 blank | 25 comment | 3 complexity | 5f90a0b4fbb8123ec397d2d7a3136b15 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, BSD-2-Clause
  1. /*
  2. * Copyright 2009 Google Inc.
  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.jstestdriver;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Injector;
  20. import com.google.inject.Module;
  21. import com.google.inject.Singleton;
  22. import com.google.inject.TypeLiteral;
  23. import com.google.inject.assistedinject.FactoryProvider;
  24. import com.google.inject.multibindings.Multibinder;
  25. import com.google.inject.name.Names;
  26. import com.google.jstestdriver.config.DefaultConfiguration;
  27. import com.google.jstestdriver.guice.BrowserActionProvider;
  28. import com.google.jstestdriver.guice.FlagsModule;
  29. import com.google.jstestdriver.hooks.ServerListener;
  30. import com.google.jstestdriver.html.HtmlDocModule;
  31. import java.io.File;
  32. import java.util.Collections;
  33. import java.util.LinkedList;
  34. import java.util.List;
  35. import java.util.Set;
  36. import java.util.concurrent.ExecutorService;
  37. import java.util.concurrent.Executors;
  38. /**
  39. * A builder for IDE's to use. Minimizes the surface area of the API which needs
  40. * to be maintained on the IDE plugin side. TODO(jeremiele) We should rename
  41. * this for other API uses. Refactor the crap out of this.
  42. *
  43. * @author alexeagle@google.com (Alex Eagle)
  44. */
  45. public class IDEPluginActionBuilder {
  46. private final ConfigurationParser configParser;
  47. private final String serverAddress;
  48. private final String captureAddress;
  49. private final ResponseStreamFactory responseStreamFactory;
  50. private List<String> tests = new LinkedList<String>();
  51. private final LinkedList<Module> modules = new LinkedList<Module>();
  52. private boolean reset = false;
  53. private List<String> dryRunFor = new LinkedList<String>();
  54. private File basePath;
  55. public IDEPluginActionBuilder(ConfigurationParser configParser,
  56. String serverAddress, String captureAddress,
  57. ResponseStreamFactory responseStreamFactory, File basePath) {
  58. this.configParser = configParser;
  59. this.serverAddress = serverAddress;
  60. this.captureAddress = captureAddress;
  61. this.responseStreamFactory = responseStreamFactory;
  62. this.basePath = basePath;
  63. }
  64. public IDEPluginActionBuilder addAllTests() {
  65. tests.add("all");
  66. return this;
  67. }
  68. public IDEPluginActionBuilder addTests(List<String> testCases) {
  69. tests.addAll(testCases);
  70. return this;
  71. }
  72. public IDEPluginActionBuilder dryRunFor(List<String> dryRunFor) {
  73. this.dryRunFor.addAll(dryRunFor);
  74. return this;
  75. }
  76. public IDEPluginActionBuilder resetBrowsers() {
  77. reset = true;
  78. return this;
  79. }
  80. public IDEPluginActionBuilder install(Module module) {
  81. modules.add(module);
  82. return this;
  83. }
  84. public ActionRunner build() {
  85. configParser.parse();
  86. modules.add(new HtmlDocModule());
  87. Injector injector = Guice.createInjector(new ActionFactoryModule(),
  88. new ConfigurationModule(modules, tests, reset, dryRunFor,
  89. serverAddress != null ? serverAddress : configParser.getServer(),
  90. captureAddress != null ? captureAddress : configParser.getCaptureAddress(),
  91. basePath, configParser.getFilesList(), responseStreamFactory));
  92. return injector.getInstance(ActionRunner.class);
  93. }
  94. // TODO(corysmith): Combine this class with the JsTestDriverModule
  95. private static class ConfigurationModule extends AbstractModule {
  96. private final List<String> tests;
  97. private final boolean reset;
  98. private final List<String> dryRunFor;
  99. private final String serverAddress;
  100. private final String captureAddress;
  101. private final File basePath;
  102. private final Set<FileInfo> fileSet;
  103. private final ResponseStreamFactory responseStreamFactory;
  104. private final LinkedList<Module> modules;
  105. public ConfigurationModule(LinkedList<Module> modules, List<String> tests,
  106. boolean reset, List<String> dryRunFor, String serverAddress,
  107. String captureAddress, File basePath, Set<FileInfo> fileSet,
  108. ResponseStreamFactory responseStreamFactory) {
  109. this.modules = modules;
  110. this.tests = tests;
  111. this.reset = reset;
  112. this.dryRunFor = dryRunFor;
  113. this.serverAddress = serverAddress;
  114. this.captureAddress = captureAddress;
  115. this.basePath = basePath;
  116. this.fileSet = fileSet;
  117. this.responseStreamFactory = responseStreamFactory;
  118. }
  119. @Override
  120. protected void configure() {
  121. FlagsImpl flags = new FlagsImpl();
  122. flags.setTests(tests);
  123. flags.setReset(reset);
  124. flags.setDryRunFor(dryRunFor);
  125. install(new FlagsModule(flags));
  126. bind(new TypeLiteral<Set<FileInfo>>() {})
  127. .annotatedWith(Names.named("originalFileSet"))
  128. .toInstance(fileSet);
  129. bind(String.class)
  130. .annotatedWith(Names.named("server"))
  131. .toInstance(serverAddress);
  132. bind(String.class)
  133. .annotatedWith(Names.named("captureAddress"))
  134. .toInstance(captureAddress);
  135. bind(Boolean.class)
  136. .annotatedWith(Names.named("debug"))
  137. .toInstance(Boolean.FALSE);
  138. bind(new TypeLiteral<List<Action>>() {
  139. }).toProvider(ActionListProvider.class);
  140. // TODO(corysmith): Change this to an actual class, so that we can JITI
  141. // it.
  142. bind(ResponseStreamFactory.class).toInstance(responseStreamFactory);
  143. bind(File.class).annotatedWith(Names.named("basePath")).toInstance(
  144. basePath);
  145. bind(new TypeLiteral<List<BrowserAction>>() {
  146. }).toProvider(BrowserActionProvider.class);
  147. bind(ExecutorService.class).toInstance(Executors.newCachedThreadPool());
  148. bind(Long.class).annotatedWith(Names.named("testSuiteTimeout"))
  149. .toInstance(DefaultConfiguration.DEFAULT_TEST_TIMEOUT);
  150. bind(Time.class).to(TimeImpl.class);
  151. Multibinder.newSetBinder(binder(), ServerListener.class);
  152. bind(JsTestDriverServer.Factory.class).toProvider(
  153. FactoryProvider.newFactory(JsTestDriverServer.Factory.class,
  154. JsTestDriverServerImpl.class));
  155. for (Module module : modules) {
  156. install(module);
  157. }
  158. bind(new TypeLiteral<Set<FileInfo>>() {
  159. }).annotatedWith(Names.named("fileSet"))
  160. .toProvider(FileSetProvider.class).in(Singleton.class);
  161. bind(new TypeLiteral<List<FileInfo>>() {
  162. }).annotatedWith(Names.named("tests")).toInstance(
  163. Collections.<FileInfo> emptyList());
  164. bind(new TypeLiteral<List<FileInfo>>() {
  165. }).annotatedWith(Names.named("plugins")).toInstance(
  166. Collections.<FileInfo> emptyList());
  167. }
  168. }
  169. }