| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | 01e9408 | 2014-07-30 21:05:37 -0700 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 18 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 19 | import org.eclipse.jgit.errors.RepositoryNotFoundException; |
| 20 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 21 | import org.eclipse.jgit.junit.TestRepository; |
| 22 | import org.eclipse.jgit.lib.Config; |
| 23 | import org.eclipse.jgit.lib.Repository; |
| 24 | import org.eclipse.jgit.transport.resolver.RepositoryResolver; |
| 25 | |
| Dave Borowitz | 01e9408 | 2014-07-30 21:05:37 -0700 | [diff] [blame] | 26 | import java.net.URL; |
| 27 | import java.util.Collections; |
| 28 | import java.util.Enumeration; |
| 29 | |
| 30 | import javax.servlet.ServletConfig; |
| 31 | import javax.servlet.ServletContext; |
| 32 | import javax.servlet.ServletException; |
| 33 | import javax.servlet.http.HttpServletRequest; |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 34 | |
| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 35 | /** Static utility methods for creating {@link GitilesServlet}s for testing. */ |
| 36 | public class TestGitilesServlet { |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 37 | /** @see #create(TestRepository) */ |
| 38 | public static GitilesServlet create(final TestRepository<DfsRepository> repo) |
| 39 | throws ServletException { |
| 40 | return create(repo, new GitwebRedirectFilter()); |
| 41 | } |
| 42 | |
| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Create a servlet backed by a single test repository. |
| 45 | * <p> |
| 46 | * The servlet uses the same filter lists as a real servlet, but only knows |
| 47 | * about a single repo, having the name returned by |
| Dave Borowitz | 33d4fda | 2013-10-22 16:40:20 -0700 | [diff] [blame] | 48 | * {@link org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription#getRepositoryName()}. |
| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 49 | * Pass a {@link FakeHttpServletRequest} and {@link FakeHttpServletResponse} |
| 50 | * to the servlet's {@code service} method to test. |
| 51 | * |
| 52 | * @param repo the test repo backing the servlet. |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 53 | * @param gitwebRedirect optional redirect filter for gitweb URLs. |
| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 54 | * @return a servlet. |
| 55 | */ |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 56 | public static GitilesServlet create(final TestRepository<DfsRepository> repo, |
| 57 | GitwebRedirectFilter gitwebRedirect) throws ServletException { |
| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 58 | final String repoName = |
| 59 | repo.getRepository().getDescription().getRepositoryName(); |
| 60 | GitilesServlet servlet = |
| Dave Borowitz | 01e9408 | 2014-07-30 21:05:37 -0700 | [diff] [blame] | 61 | new GitilesServlet(new Config(), new DefaultRenderer(GitilesServlet.STATIC_PREFIX, |
| 62 | ImmutableList.<URL> of(), repoName + " test site"), |
| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 63 | TestGitilesUrls.URLS, new TestGitilesAccess(repo.getRepository()), |
| 64 | 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 Borowitz | 86462df | 2014-02-03 14:23:57 -0800 | [diff] [blame] | 73 | }, null, null, null, gitwebRedirect); |
| Dave Borowitz | fda3d9d | 2013-01-14 16:24:10 -0800 | [diff] [blame] | 74 | |
| 75 | servlet.init(new ServletConfig() { |
| 76 | @Override |
| 77 | public String getInitParameter(String name) { |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Enumeration<String> getInitParameterNames() { |
| 83 | return Collections.enumeration(ImmutableList.<String> of()); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public ServletContext getServletContext() { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public String getServletName() { |
| 93 | return TestGitilesServlet.class.getName(); |
| 94 | } |
| 95 | }); |
| 96 | return servlet; |
| 97 | } |
| 98 | |
| 99 | private TestGitilesServlet() { |
| 100 | } |
| 101 | } |