blob: e7079d32780abfff898215ba535d5f0362e6c0f7 [file] [log] [blame]
Dave Borowitzfda3d9d2013-01-14 16:24:10 -08001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080017import java.util.Collections;
18import java.util.Enumeration;
19
20import javax.servlet.ServletConfig;
21import javax.servlet.ServletContext;
22import javax.servlet.ServletException;
23import javax.servlet.http.HttpServletRequest;
24
Shawn Pearceb43b2d52013-03-18 10:55:15 -070025import org.eclipse.jgit.errors.RepositoryNotFoundException;
26import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
27import org.eclipse.jgit.junit.TestRepository;
28import org.eclipse.jgit.lib.Config;
29import org.eclipse.jgit.lib.Repository;
30import org.eclipse.jgit.transport.resolver.RepositoryResolver;
31
32import com.google.common.collect.ImmutableList;
33
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080034/** Static utility methods for creating {@link GitilesServlet}s for testing. */
35public class TestGitilesServlet {
Dave Borowitzd91bdf72013-01-10 20:07:32 -080036 /** @see #create(TestRepository) */
37 public static GitilesServlet create(final TestRepository<DfsRepository> repo)
38 throws ServletException {
39 return create(repo, new GitwebRedirectFilter());
40 }
41
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080042 /**
43 * Create a servlet backed by a single test repository.
44 * <p>
45 * The servlet uses the same filter lists as a real servlet, but only knows
46 * about a single repo, having the name returned by
Dave Borowitz33d4fda2013-10-22 16:40:20 -070047 * {@link org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription#getRepositoryName()}.
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080048 * Pass a {@link FakeHttpServletRequest} and {@link FakeHttpServletResponse}
49 * to the servlet's {@code service} method to test.
50 *
51 * @param repo the test repo backing the servlet.
Dave Borowitzd91bdf72013-01-10 20:07:32 -080052 * @param gitwebRedirect optional redirect filter for gitweb URLs.
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080053 * @return a servlet.
54 */
Dave Borowitzd91bdf72013-01-10 20:07:32 -080055 public static GitilesServlet create(final TestRepository<DfsRepository> repo,
56 GitwebRedirectFilter gitwebRedirect) throws ServletException {
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080057 final String repoName =
58 repo.getRepository().getDescription().getRepositoryName();
59 GitilesServlet servlet =
60 new GitilesServlet(new Config(), new DebugRenderer(
61 GitilesServlet.STATIC_PREFIX, null, null, repoName + " test site"),
62 TestGitilesUrls.URLS, new TestGitilesAccess(repo.getRepository()),
63 new RepositoryResolver<HttpServletRequest>() {
64 @Override
65 public Repository open(HttpServletRequest req, String name)
66 throws RepositoryNotFoundException {
67 if (!repoName.equals(name)) {
68 throw new RepositoryNotFoundException(name);
69 }
70 return repo.getRepository();
71 }
Dave Borowitzd91bdf72013-01-10 20:07:32 -080072 }, null, null, gitwebRedirect);
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080073
74 servlet.init(new ServletConfig() {
75 @Override
76 public String getInitParameter(String name) {
77 return null;
78 }
79
80 @Override
81 public Enumeration<String> getInitParameterNames() {
82 return Collections.enumeration(ImmutableList.<String> of());
83 }
84
85 @Override
86 public ServletContext getServletContext() {
87 return null;
88 }
89
90 @Override
91 public String getServletName() {
92 return TestGitilesServlet.class.getName();
93 }
94 });
95 return servlet;
96 }
97
98 private TestGitilesServlet() {
99 }
100}