View Javadoc
1   /*
2    * Copyright (C) 2009-2017, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.junit.http;
45  
46  import static org.junit.Assert.fail;
47  
48  import java.io.IOException;
49  import java.net.URI;
50  import java.net.URISyntaxException;
51  import java.util.Collection;
52  import java.util.Collections;
53  import java.util.HashSet;
54  import java.util.List;
55  import java.util.Set;
56  
57  import org.eclipse.jetty.servlet.ServletContextHandler;
58  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
59  import org.eclipse.jgit.junit.TestRepository;
60  import org.eclipse.jgit.lib.AnyObjectId;
61  import org.eclipse.jgit.lib.Constants;
62  import org.eclipse.jgit.lib.ObjectId;
63  import org.eclipse.jgit.lib.Repository;
64  import org.eclipse.jgit.revwalk.RevCommit;
65  import org.eclipse.jgit.revwalk.RevObject;
66  import org.eclipse.jgit.transport.RefSpec;
67  import org.eclipse.jgit.transport.RemoteRefUpdate;
68  import org.eclipse.jgit.transport.URIish;
69  
70  /**
71   * Base class for HTTP related transport testing.
72   */
73  public abstract class HttpTestCase extends LocalDiskRepositoryTestCase {
74  	/** Constant <code>master="Constants.R_HEADS + Constants.MASTER"</code> */
75  	protected static final String master = Constants.R_HEADS + Constants.MASTER;
76  
77  	/** In-memory application server; subclass must start. */
78  	protected AppServer server;
79  
80  	/** {@inheritDoc} */
81  	@Override
82  	public void setUp() throws Exception {
83  		super.setUp();
84  		server = createServer();
85  	}
86  
87  	/** {@inheritDoc} */
88  	@Override
89  	public void tearDown() throws Exception {
90  		server.tearDown();
91  		super.tearDown();
92  	}
93  
94  	/**
95  	 * Create the {@link AppServer}.This default implementation creates a server
96  	 * without SSLsupport listening for HTTP connections on a dynamically chosen
97  	 * port, which can be gotten once the server has been started via its
98  	 * {@link org.eclipse.jgit.junit.http.AppServer#getPort()} method.
99  	 * Subclasses may override if they need a more specialized server.
100 	 *
101 	 * @return the {@link org.eclipse.jgit.junit.http.AppServer}.
102 	 * @since 4.9
103 	 */
104 	protected AppServer createServer() {
105 		return new AppServer();
106 	}
107 
108 	/**
109 	 * Create TestRepository
110 	 *
111 	 * @return the TestRepository
112 	 * @throws IOException
113 	 */
114 	protected TestRepository<Repository> createTestRepository()
115 			throws IOException {
116 		return new TestRepository<>(createBareRepository());
117 	}
118 
119 	/**
120 	 * Convert path to URIish
121 	 *
122 	 * @param path
123 	 * @return the URIish
124 	 * @throws URISyntaxException
125 	 */
126 	protected URIish toURIish(String path) throws URISyntaxException {
127 		URI u = server.getURI().resolve(path);
128 		return new URIish(u.toString());
129 	}
130 
131 	/**
132 	 * Convert a path relative to the app's context path to a URIish
133 	 *
134 	 * @param app
135 	 * @param name
136 	 * @return the warnings (if any) from the last execution
137 	 * @throws URISyntaxException
138 	 */
139 	protected URIish toURIish(ServletContextHandler app, String name)
140 			throws URISyntaxException {
141 		String p = app.getContextPath();
142 		if (!p.endsWith("/") && !name.startsWith("/"))
143 			p += "/";
144 		p += name;
145 		return toURIish(p);
146 	}
147 
148 	/**
149 	 * Get requests.
150 	 *
151 	 * @return list of events
152 	 */
153 	protected List<AccessEvent> getRequests() {
154 		return server.getRequests();
155 	}
156 
157 	/**
158 	 * Get requests.
159 	 *
160 	 * @param base
161 	 * @param path
162 	 *
163 	 * @return list of events
164 	 */
165 	protected List<AccessEvent> getRequests(URIish base, String path) {
166 		return server.getRequests(base, path);
167 	}
168 
169 	/**
170 	 * Get requests.
171 	 *
172 	 * @param path
173 	 *
174 	 * @return list of events
175 	 */
176 	protected List<AccessEvent> getRequests(String path) {
177 		return server.getRequests(path);
178 	}
179 
180 	/**
181 	 * Run fsck
182 	 *
183 	 * @param db
184 	 * @param tips
185 	 * @throws Exception
186 	 */
187 	protected static void fsck(Repository db, RevObject... tips)
188 			throws Exception {
189 		TestRepository<? extends Repository> tr =
190 				new TestRepository<>(db);
191 		tr.fsck(tips);
192 	}
193 
194 	/**
195 	 * Mirror refs
196 	 *
197 	 * @param refs
198 	 * @return set of RefSpecs
199 	 */
200 	protected static Set<RefSpec> mirror(String... refs) {
201 		HashSet<RefSpec> r = new HashSet<>();
202 		for (String name : refs) {
203 			RefSpec rs = new RefSpec(name);
204 			rs = rs.setDestination(name);
205 			rs = rs.setForceUpdate(true);
206 			r.add(rs);
207 		}
208 		return r;
209 	}
210 
211 	/**
212 	 * Push a commit
213 	 *
214 	 * @param from
215 	 * @param q
216 	 * @return collection of RefUpdates
217 	 * @throws IOException
218 	 */
219 	protected static Collection<RemoteRefUpdate> push(TestRepository from,
220 			RevCommit q) throws IOException {
221 		final Repository db = from.getRepository();
222 		final String srcExpr = q.name();
223 		final String dstName = master;
224 		final boolean forceUpdate = true;
225 		final String localName = null;
226 		final ObjectId oldId = null;
227 
228 		RemoteRefUpdate u = new RemoteRefUpdate(db, srcExpr, dstName,
229 				forceUpdate, localName, oldId);
230 		return Collections.singleton(u);
231 	}
232 
233 	/**
234 	 * Create loose object path
235 	 *
236 	 * @param base
237 	 * @param id
238 	 * @return path of the loose object
239 	 */
240 	public static String loose(URIish base, AnyObjectId id) {
241 		final String objectName = id.name();
242 		final String d = objectName.substring(0, 2);
243 		final String f = objectName.substring(2);
244 		return join(base, "objects/" + d + "/" + f);
245 	}
246 
247 	/**
248 	 * Join a base URIish and a path
249 	 *
250 	 * @param base
251 	 * @param path
252 	 *            a relative path
253 	 * @return the joined path
254 	 */
255 	public static String join(URIish base, String path) {
256 		if (path.startsWith("/"))
257 			fail("Cannot join absolute path " + path + " to URIish " + base);
258 
259 		String dir = base.getPath();
260 		if (!dir.endsWith("/"))
261 			dir += "/";
262 		return dir + path;
263 	}
264 
265 	/**
266 	 * Rewrite a url
267 	 *
268 	 * @param url
269 	 * @param newProtocol
270 	 * @param newPort
271 	 * @return the rewritten url
272 	 */
273 	protected static String rewriteUrl(String url, String newProtocol,
274 			int newPort) {
275 		String newUrl = url;
276 		if (newProtocol != null && !newProtocol.isEmpty()) {
277 			int schemeEnd = newUrl.indexOf("://");
278 			if (schemeEnd >= 0) {
279 				newUrl = newProtocol + newUrl.substring(schemeEnd);
280 			}
281 		}
282 		if (newPort > 0) {
283 			newUrl = newUrl.replaceFirst(":\\d+/", ":" + newPort + "/");
284 		} else {
285 			// Remove the port, if any
286 			newUrl = newUrl.replaceFirst(":\\d+/", "/");
287 		}
288 		return newUrl;
289 	}
290 
291 	/**
292 	 * Extend a path
293 	 *
294 	 * @param uri
295 	 * @param pathComponents
296 	 * @return the extended URIish
297 	 * @throws URISyntaxException
298 	 */
299 	protected static URIish extendPath(URIish uri, String pathComponents)
300 			throws URISyntaxException {
301 		String raw = uri.toString();
302 		String newComponents = pathComponents;
303 		if (!newComponents.startsWith("/")) {
304 			newComponents = '/' + newComponents;
305 		}
306 		if (!newComponents.endsWith("/")) {
307 			newComponents += '/';
308 		}
309 		int i = raw.lastIndexOf('/');
310 		raw = raw.substring(0, i) + newComponents + raw.substring(i + 1);
311 		return new URIish(raw);
312 	}
313 }