View Javadoc
1   /*
2    * Copyright (C) 2016, 2017 Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.http.test;
11  
12  import javax.servlet.http.HttpServletRequest;
13  
14  import org.eclipse.jgit.errors.RepositoryNotFoundException;
15  import org.eclipse.jgit.junit.TestRepository;
16  import org.eclipse.jgit.lib.Repository;
17  import org.eclipse.jgit.transport.resolver.RepositoryResolver;
18  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
19  
20  /**
21   * A simple repository resolver for tests.
22   */
23  public final class TestRepositoryResolver
24  		implements RepositoryResolver<HttpServletRequest> {
25  
26  	private final TestRepository<Repository> repo;
27  
28  	private final String repoName;
29  
30  	/**
31  	 * Create a new {@link org.eclipse.jgit.http.test.TestRepositoryResolver}
32  	 * that resolves the given name to the given repository.
33  	 *
34  	 * @param repo
35  	 *            to resolve to
36  	 * @param repoName
37  	 *            to match
38  	 */
39  	public TestRepositoryResolver(TestRepository<Repository> repo, String repoName) {
40  		this.repo = repo;
41  		this.repoName = repoName;
42  	}
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	public Repository open(HttpServletRequest req, String name)
47  			throws RepositoryNotFoundException, ServiceNotEnabledException {
48  		if (!name.equals(repoName)) {
49  			throw new RepositoryNotFoundException(name);
50  		}
51  		Repository db = repo.getRepository();
52  		db.incrementOpen();
53  		return db;
54  	}
55  }