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 		try (TestRepository<? extends Repository> tr =
190 				new TestRepository<>(db)) {
191 			tr.fsck(tips);
192 		}
193 	}
194 
195 	/**
196 	 * Mirror refs
197 	 *
198 	 * @param refs
199 	 * @return set of RefSpecs
200 	 */
201 	protected static Set<RefSpec> mirror(String... refs) {
202 		HashSet<RefSpec> r = new HashSet<>();
203 		for (String name : refs) {
204 			RefSpec rs = new RefSpec(name);
205 			rs = rs.setDestination(name);
206 			rs = rs.setForceUpdate(true);
207 			r.add(rs);
208 		}
209 		return r;
210 	}
211 
212 	/**
213 	 * Push a commit
214 	 *
215 	 * @param from
216 	 * @param q
217 	 * @return collection of RefUpdates
218 	 * @throws IOException
219 	 */
220 	protected static Collection<RemoteRefUpdate> push(TestRepository from,
221 			RevCommit q) throws IOException {
222 		final Repository db = from.getRepository();
223 		final String srcExpr = q.name();
224 		final String dstName = master;
225 		final boolean forceUpdate = true;
226 		final String localName = null;
227 		final ObjectId oldId = null;
228 
229 		RemoteRefUpdate u = new RemoteRefUpdate(db, srcExpr, dstName,
230 				forceUpdate, localName, oldId);
231 		return Collections.singleton(u);
232 	}
233 
234 	/**
235 	 * Create loose object path
236 	 *
237 	 * @param base
238 	 * @param id
239 	 * @return path of the loose object
240 	 */
241 	public static String loose(URIish base, AnyObjectId id) {
242 		final String objectName = id.name();
243 		final String d = objectName.substring(0, 2);
244 		final String f = objectName.substring(2);
245 		return join(base, "objects/" + d + "/" + f);
246 	}
247 
248 	/**
249 	 * Join a base URIish and a path
250 	 *
251 	 * @param base
252 	 * @param path
253 	 *            a relative path
254 	 * @return the joined path
255 	 */
256 	public static String join(URIish base, String path) {
257 		if (path.startsWith("/"))
258 			fail("Cannot join absolute path " + path + " to URIish " + base);
259 
260 		String dir = base.getPath();
261 		if (!dir.endsWith("/"))
262 			dir += "/";
263 		return dir + path;
264 	}
265 
266 	/**
267 	 * Rewrite a url
268 	 *
269 	 * @param url
270 	 * @param newProtocol
271 	 * @param newPort
272 	 * @return the rewritten url
273 	 */
274 	protected static String rewriteUrl(String url, String newProtocol,
275 			int newPort) {
276 		String newUrl = url;
277 		if (newProtocol != null && !newProtocol.isEmpty()) {
278 			int schemeEnd = newUrl.indexOf("://");
279 			if (schemeEnd >= 0) {
280 				newUrl = newProtocol + newUrl.substring(schemeEnd);
281 			}
282 		}
283 		if (newPort > 0) {
284 			newUrl = newUrl.replaceFirst(":\\d+/", ":" + newPort + "/");
285 		} else {
286 			// Remove the port, if any
287 			newUrl = newUrl.replaceFirst(":\\d+/", "/");
288 		}
289 		return newUrl;
290 	}
291 
292 	/**
293 	 * Extend a path
294 	 *
295 	 * @param uri
296 	 * @param pathComponents
297 	 * @return the extended URIish
298 	 * @throws URISyntaxException
299 	 */
300 	protected static URIish./../../../../org/eclipse/jgit/transport/URIish.html#URIish">URIish extendPath(URIish uri, String pathComponents)
301 			throws URISyntaxException {
302 		String raw = uri.toString();
303 		String newComponents = pathComponents;
304 		if (!newComponents.startsWith("/")) {
305 			newComponents = '/' + newComponents;
306 		}
307 		if (!newComponents.endsWith("/")) {
308 			newComponents += '/';
309 		}
310 		int i = raw.lastIndexOf('/');
311 		raw = raw.substring(0, i) + newComponents + raw.substring(i + 1);
312 		return new URIish(raw);
313 	}
314 }