| 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 | |
| 17 | import com.google.common.collect.ImmutableList; |
| 18 | |
| 19 | import org.eclipse.jgit.errors.RepositoryNotFoundException; |
| 20 | import org.eclipse.jgit.junit.TestRepository; |
| 21 | import org.eclipse.jgit.lib.Config; |
| 22 | import org.eclipse.jgit.lib.Repository; |
| 23 | import org.eclipse.jgit.storage.dfs.DfsRepository; |
| 24 | import org.eclipse.jgit.transport.resolver.RepositoryResolver; |
| 25 | |
| 26 | import java.util.Collections; |
| 27 | import java.util.Enumeration; |
| 28 | |
| 29 | import javax.servlet.ServletConfig; |
| 30 | import javax.servlet.ServletContext; |
| 31 | import javax.servlet.ServletException; |
| 32 | import javax.servlet.http.HttpServletRequest; |
| 33 | |
| 34 | /** Static utility methods for creating {@link GitilesServlet}s for testing. */ |
| 35 | public class TestGitilesServlet { |
| 36 | /** |
| 37 | * Create a servlet backed by a single test repository. |
| 38 | * <p> |
| 39 | * The servlet uses the same filter lists as a real servlet, but only knows |
| 40 | * about a single repo, having the name returned by |
| 41 | * {@link org.eclipse.jgit.storage.dfs.DfsRepositoryDescription#getRepositoryName()}. |
| 42 | * Pass a {@link FakeHttpServletRequest} and {@link FakeHttpServletResponse} |
| 43 | * to the servlet's {@code service} method to test. |
| 44 | * |
| 45 | * @param repo the test repo backing the servlet. |
| 46 | * @return a servlet. |
| 47 | */ |
| 48 | public static GitilesServlet create(final TestRepository<DfsRepository> repo) |
| 49 | throws ServletException { |
| 50 | final String repoName = |
| 51 | repo.getRepository().getDescription().getRepositoryName(); |
| 52 | GitilesServlet servlet = |
| 53 | new GitilesServlet(new Config(), new DebugRenderer( |
| 54 | GitilesServlet.STATIC_PREFIX, null, null, repoName + " test site"), |
| 55 | TestGitilesUrls.URLS, new TestGitilesAccess(repo.getRepository()), |
| 56 | new RepositoryResolver<HttpServletRequest>() { |
| 57 | @Override |
| 58 | public Repository open(HttpServletRequest req, String name) |
| 59 | throws RepositoryNotFoundException { |
| 60 | if (!repoName.equals(name)) { |
| 61 | throw new RepositoryNotFoundException(name); |
| 62 | } |
| 63 | return repo.getRepository(); |
| 64 | } |
| 65 | }, null, null); |
| 66 | |
| 67 | servlet.init(new ServletConfig() { |
| 68 | @Override |
| 69 | public String getInitParameter(String name) { |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Enumeration<String> getInitParameterNames() { |
| 75 | return Collections.enumeration(ImmutableList.<String> of()); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public ServletContext getServletContext() { |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String getServletName() { |
| 85 | return TestGitilesServlet.class.getName(); |
| 86 | } |
| 87 | }); |
| 88 | return servlet; |
| 89 | } |
| 90 | |
| 91 | private TestGitilesServlet() { |
| 92 | } |
| 93 | } |