PageRenderTime 84ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/test/WebAppTests.java

https://github.com/bmahe/hadoop-common
Java | 172 lines | 119 code | 24 blank | 29 comment | 8 complexity | 68839860d5142b60e3dcdd3e0f2060e5 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.yarn.webapp.test;
  19. import org.apache.hadoop.yarn.webapp.Controller;
  20. import org.apache.hadoop.yarn.webapp.SubView;
  21. import org.apache.hadoop.yarn.webapp.View;
  22. import org.apache.hadoop.yarn.webapp.WebAppException;
  23. import java.lang.reflect.Method;
  24. import java.util.Map;
  25. import com.google.inject.Module;
  26. import com.google.inject.Scopes;
  27. import com.google.inject.servlet.RequestScoped;
  28. import com.google.inject.AbstractModule;
  29. import com.google.inject.Guice;
  30. import com.google.inject.Injector;
  31. import com.google.inject.Provides;
  32. import java.io.PrintWriter;
  33. import javax.servlet.http.HttpServletResponse;
  34. import javax.servlet.http.HttpServletRequest;
  35. import static org.mockito.Mockito.*;
  36. public class WebAppTests {
  37. /**
  38. * Create a mock injector for tests
  39. * @param <T> type of class/interface
  40. * @param api the interface class of the object to inject
  41. * @param impl the implementation object to inject
  42. * @param modules additional guice modules
  43. * @return an injector
  44. */
  45. public static <T> Injector createMockInjector(final Class<T> api,
  46. final T impl,
  47. final Module... modules) {
  48. return Guice.createInjector(new AbstractModule() {
  49. final PrintWriter writer = spy(new PrintWriter(System.out));
  50. final HttpServletRequest request = createRequest();
  51. final HttpServletResponse response = createResponse();
  52. @Override
  53. protected void configure() {
  54. if (api != null) {
  55. bind(api).toInstance(impl);
  56. }
  57. bindScope(RequestScoped.class, Scopes.SINGLETON);
  58. if (modules != null) {
  59. for (Module module : modules) {
  60. install(module);
  61. }
  62. }
  63. }
  64. @Provides HttpServletRequest request() {
  65. return request;
  66. }
  67. @Provides HttpServletResponse response() {
  68. return response;
  69. }
  70. @Provides PrintWriter writer() {
  71. return writer;
  72. }
  73. HttpServletRequest createRequest() {
  74. // the default suffices for now
  75. return mock(HttpServletRequest.class);
  76. }
  77. HttpServletResponse createResponse() {
  78. try {
  79. HttpServletResponse res = mock(HttpServletResponse.class);
  80. when(res.getWriter()).thenReturn(writer);
  81. return res;
  82. } catch (Exception e) {
  83. throw new WebAppException(e);
  84. }
  85. }
  86. });
  87. }
  88. // convenience
  89. @SuppressWarnings("unchecked")
  90. public static <T> Injector createMockInjector(T impl) {
  91. return createMockInjector((Class<T>)impl.getClass(), impl);
  92. }
  93. public static void flushOutput(Injector injector) {
  94. HttpServletResponse res = injector.getInstance(HttpServletResponse.class);
  95. try {
  96. res.getWriter().flush();
  97. } catch (Exception e) {
  98. throw new RuntimeException(e);
  99. }
  100. }
  101. public static <T> Injector testController(Class<? extends Controller> ctrlr,
  102. String methodName, Class<T> api, T impl, Module... modules) {
  103. try {
  104. Injector injector = createMockInjector(api, impl, modules);
  105. Method method = ctrlr.getMethod(methodName, (Class<?>[])null);
  106. method.invoke(injector.getInstance(ctrlr), (Object[])null);
  107. return injector;
  108. } catch (Exception e) {
  109. throw new WebAppException(e);
  110. }
  111. }
  112. public static <T> Injector testController(Class<? extends Controller> ctrlr,
  113. String methodName) {
  114. return testController(ctrlr, methodName, null, null);
  115. }
  116. public static <T> Injector testPage(Class<? extends View> page, Class<T> api,
  117. T impl, Map<String,String> params, Module... modules) {
  118. Injector injector = createMockInjector(api, impl, modules);
  119. View view = injector.getInstance(page);
  120. if(params != null) {
  121. for(Map.Entry<String, String> entry: params.entrySet()) {
  122. view.set(entry.getKey(), entry.getValue());
  123. }
  124. }
  125. view.render();
  126. flushOutput(injector);
  127. return injector;
  128. }
  129. public static <T> Injector testPage(Class<? extends View> page, Class<T> api,
  130. T impl, Module... modules) {
  131. return testPage(page, api, impl, null, modules);
  132. }
  133. // convenience
  134. public static <T> Injector testPage(Class<? extends View> page) {
  135. return testPage(page, null, null);
  136. }
  137. public static <T> Injector testBlock(Class<? extends SubView> block,
  138. Class<T> api, T impl, Module... modules) {
  139. Injector injector = createMockInjector(api, impl, modules);
  140. injector.getInstance(block).renderPartial();
  141. flushOutput(injector);
  142. return injector;
  143. }
  144. // convenience
  145. public static <T> Injector testBlock(Class<? extends SubView> block) {
  146. return testBlock(block, null, null);
  147. }
  148. }