blob: 836116c76995b663822b08d2d1c4f0c1550e6d81 [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 Borowitz01e94082014-07-30 21:05:37 -070017import com.google.common.collect.ImmutableList;
Dave Borowitz3b744b12016-08-19 16:11:10 -040018import java.net.URL;
19import java.util.Collections;
20import java.util.Enumeration;
21import javax.servlet.ServletConfig;
22import javax.servlet.ServletContext;
23import javax.servlet.ServletException;
24import javax.servlet.http.HttpServletRequest;
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
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080032/** Static utility methods for creating {@link GitilesServlet}s for testing. */
33public class TestGitilesServlet {
Dave Borowitzd91bdf72013-01-10 20:07:32 -080034 /** @see #create(TestRepository) */
35 public static GitilesServlet create(final TestRepository<DfsRepository> repo)
36 throws ServletException {
37 return create(repo, new GitwebRedirectFilter());
38 }
39
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080040 /**
41 * Create a servlet backed by a single test repository.
42 * <p>
43 * The servlet uses the same filter lists as a real servlet, but only knows
44 * about a single repo, having the name returned by
Dave Borowitz33d4fda2013-10-22 16:40:20 -070045 * {@link org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription#getRepositoryName()}.
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080046 * Pass a {@link FakeHttpServletRequest} and {@link FakeHttpServletResponse}
47 * to the servlet's {@code service} method to test.
48 *
49 * @param repo the test repo backing the servlet.
Dave Borowitzd91bdf72013-01-10 20:07:32 -080050 * @param gitwebRedirect optional redirect filter for gitweb URLs.
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080051 * @return a servlet.
52 */
Dave Borowitzcf38c032016-05-02 11:06:23 -040053 public static GitilesServlet create(
54 final TestRepository<DfsRepository> repo, GitwebRedirectFilter gitwebRedirect)
55 throws ServletException {
56 final String repoName = repo.getRepository().getDescription().getRepositoryName();
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080057 GitilesServlet servlet =
Dave Borowitzcf38c032016-05-02 11:06:23 -040058 new GitilesServlet(
59 new Config(),
60 new DefaultRenderer(
61 GitilesServlet.STATIC_PREFIX, ImmutableList.<URL>of(), repoName + " test site"),
62 TestGitilesUrls.URLS,
63 new TestGitilesAccess(repo.getRepository()),
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080064 new RepositoryResolver<HttpServletRequest>() {
65 @Override
66 public Repository open(HttpServletRequest req, String name)
67 throws RepositoryNotFoundException {
68 if (!repoName.equals(name)) {
69 throw new RepositoryNotFoundException(name);
70 }
71 return repo.getRepository();
72 }
Dave Borowitzcf38c032016-05-02 11:06:23 -040073 },
74 null,
75 null,
76 null,
77 gitwebRedirect);
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080078
Dave Borowitzcf38c032016-05-02 11:06:23 -040079 servlet.init(
80 new ServletConfig() {
81 @Override
82 public String getInitParameter(String name) {
83 return null;
84 }
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080085
Dave Borowitzcf38c032016-05-02 11:06:23 -040086 @Override
87 public Enumeration<String> getInitParameterNames() {
88 return Collections.enumeration(ImmutableList.<String>of());
89 }
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080090
Dave Borowitzcf38c032016-05-02 11:06:23 -040091 @Override
92 public ServletContext getServletContext() {
93 return null;
94 }
Dave Borowitzfda3d9d2013-01-14 16:24:10 -080095
Dave Borowitzcf38c032016-05-02 11:06:23 -040096 @Override
97 public String getServletName() {
98 return TestGitilesServlet.class.getName();
99 }
100 });
Dave Borowitzfda3d9d2013-01-14 16:24:10 -0800101 return servlet;
102 }
103
Dave Borowitzcf38c032016-05-02 11:06:23 -0400104 private TestGitilesServlet() {}
Dave Borowitzfda3d9d2013-01-14 16:24:10 -0800105}