PageRenderTime 43ms CodeModel.GetById 33ms app.highlight 8ms 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/gregsfortytwo/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
 19package org.apache.hadoop.yarn.webapp.test;
 20
 21import org.apache.hadoop.yarn.webapp.Controller;
 22import org.apache.hadoop.yarn.webapp.SubView;
 23import org.apache.hadoop.yarn.webapp.View;
 24import org.apache.hadoop.yarn.webapp.WebAppException;
 25
 26import java.lang.reflect.Method;
 27import java.util.Map;
 28
 29import com.google.inject.Module;
 30import com.google.inject.Scopes;
 31import com.google.inject.servlet.RequestScoped;
 32import com.google.inject.AbstractModule;
 33import com.google.inject.Guice;
 34import com.google.inject.Injector;
 35import com.google.inject.Provides;
 36
 37import java.io.PrintWriter;
 38import javax.servlet.http.HttpServletResponse;
 39import javax.servlet.http.HttpServletRequest;
 40
 41
 42import static org.mockito.Mockito.*;
 43
 44public class WebAppTests {
 45
 46  /**
 47   * Create a mock injector for tests
 48   * @param <T> type of class/interface
 49   * @param api the interface class of the object to inject
 50   * @param impl the implementation object to inject
 51   * @param modules additional guice modules
 52   * @return an injector
 53   */
 54  public static <T> Injector createMockInjector(final Class<T> api,
 55                                                final T impl,
 56                                                final Module... modules) {
 57    return Guice.createInjector(new AbstractModule() {
 58      final PrintWriter writer = spy(new PrintWriter(System.out));
 59      final HttpServletRequest request = createRequest();
 60      final HttpServletResponse response = createResponse();
 61
 62      @Override
 63      protected void configure() {
 64        if (api != null) {
 65          bind(api).toInstance(impl);
 66        }
 67        bindScope(RequestScoped.class, Scopes.SINGLETON);
 68        if (modules != null) {
 69          for (Module module : modules) {
 70            install(module);
 71          }
 72        }
 73      }
 74
 75      @Provides HttpServletRequest request() {
 76        return request;
 77      }
 78
 79      @Provides HttpServletResponse response() {
 80        return response;
 81      }
 82
 83      @Provides PrintWriter writer() {
 84        return writer;
 85      }
 86
 87      HttpServletRequest createRequest() {
 88        // the default suffices for now
 89        return mock(HttpServletRequest.class);
 90      }
 91
 92      HttpServletResponse createResponse() {
 93        try {
 94          HttpServletResponse res = mock(HttpServletResponse.class);
 95          when(res.getWriter()).thenReturn(writer);
 96          return res;
 97        } catch (Exception e) {
 98          throw new WebAppException(e);
 99        }
100      }
101    });
102  }
103
104  // convenience
105  @SuppressWarnings("unchecked")
106  public static <T> Injector createMockInjector(T impl) {
107    return createMockInjector((Class<T>)impl.getClass(), impl);
108  }
109
110  public static void flushOutput(Injector injector) {
111    HttpServletResponse res = injector.getInstance(HttpServletResponse.class);
112    try {
113      res.getWriter().flush();
114    } catch (Exception e) {
115      throw new RuntimeException(e);
116    }
117  }
118
119  public static <T> Injector testController(Class<? extends Controller> ctrlr,
120      String methodName, Class<T> api, T impl, Module... modules) {
121    try {
122      Injector injector = createMockInjector(api, impl, modules);
123      Method method = ctrlr.getMethod(methodName, (Class<?>[])null);
124      method.invoke(injector.getInstance(ctrlr), (Object[])null);
125      return injector;
126    } catch (Exception e) {
127      throw new WebAppException(e);
128    }
129  }
130
131  public static <T> Injector testController(Class<? extends Controller> ctrlr,
132      String methodName) {
133    return testController(ctrlr, methodName, null, null);
134  }
135
136  public static <T> Injector testPage(Class<? extends View> page, Class<T> api,
137                                      T impl, Map<String,String> params, Module... modules) {
138    Injector injector = createMockInjector(api, impl, modules);
139    View view = injector.getInstance(page);
140    if(params != null) {
141      for(Map.Entry<String, String> entry: params.entrySet()) {
142        view.set(entry.getKey(), entry.getValue());
143      }
144    }
145    view.render();
146    flushOutput(injector);
147    return injector;
148  }
149  
150  public static <T> Injector testPage(Class<? extends View> page, Class<T> api,
151                                      T impl, Module... modules) {
152    return testPage(page, api, impl, null, modules);
153  }
154
155  // convenience
156  public static <T> Injector testPage(Class<? extends View> page) {
157    return testPage(page, null, null);
158  }
159
160  public static <T> Injector testBlock(Class<? extends SubView> block,
161      Class<T> api, T impl, Module... modules) {
162    Injector injector = createMockInjector(api, impl, modules);
163    injector.getInstance(block).renderPartial();
164    flushOutput(injector);
165    return injector;
166  }
167
168  // convenience
169  public static <T> Injector testBlock(Class<? extends SubView> block) {
170    return testBlock(block, null, null);
171  }
172}