e : parameters.entrySet()) {
+ for (String val : e.getValue()) {
+ if (!first) {
+ b.append('&');
+ }
+ first = false;
+
+ b.append(e.getKey());
+ b.append('=');
+ b.append(val);
+ }
+ }
+ }
+ b.append(' ');
+ b.append(status);
+ return b.toString();
+ }
+}
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/AppServer.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/AppServer.java
new file mode 100644
index 000000000..74df7086e
--- /dev/null
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/AppServer.java
@@ -0,0 +1,295 @@
+/*
+ * Copyright (C) 2010, Google Inc.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.http.test.util;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.eclipse.jetty.http.security.Constraint;
+import org.eclipse.jetty.http.security.Password;
+import org.eclipse.jetty.security.Authenticator;
+import org.eclipse.jetty.security.ConstraintMapping;
+import org.eclipse.jetty.security.ConstraintSecurityHandler;
+import org.eclipse.jetty.security.MappedLoginService;
+import org.eclipse.jetty.security.authentication.BasicAuthenticator;
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.UserIdentity;
+import org.eclipse.jetty.server.handler.ContextHandlerCollection;
+import org.eclipse.jetty.server.handler.RequestLogHandler;
+import org.eclipse.jetty.server.nio.SelectChannelConnector;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.util.thread.QueuedThreadPool;
+import org.eclipse.jgit.transport.URIish;
+
+/**
+ * Tiny web application server for unit testing.
+ *
+ * Tests should start the server in their {@code setUp()} method and stop the
+ * server in their {@code tearDown()} method. Only while started the server's
+ * URL and/or port number can be obtained.
+ */
+public class AppServer {
+ /** Realm name for the secure access areas. */
+ public static final String realm = "Secure Area";
+
+ /** Username for secured access areas. */
+ public static final String username = "agitter";
+
+ /** Password for {@link #username} in secured access areas. */
+ public static final String password = "letmein";
+
+ static {
+ // Install a logger that throws warning messages.
+ //
+ final String prop = "org.eclipse.jetty.util.log.class";
+ System.setProperty(prop, RecordingLogger.class.getName());
+ }
+
+ private final Server server;
+
+ private final Connector connector;
+
+ private final ContextHandlerCollection contexts;
+
+ private final TestRequestLog log;
+
+ public AppServer() {
+ connector = new SelectChannelConnector();
+ connector.setPort(0);
+ try {
+ final InetAddress me = InetAddress.getByName("localhost");
+ connector.setHost(me.getHostAddress());
+ } catch (UnknownHostException e) {
+ throw new RuntimeException("Cannot find localhost", e);
+ }
+
+ // We need a handful of threads in the thread pool, otherwise
+ // our tests will deadlock when they can't open enough requests.
+ // In theory we only need 1 concurrent connection at a time, but
+ // I suspect the JRE isn't doing request pipelining on existing
+ // connections like we want it to.
+ //
+ final QueuedThreadPool pool = new QueuedThreadPool();
+ pool.setMinThreads(1);
+ pool.setMaxThreads(4);
+ pool.setMaxQueued(8);
+
+ contexts = new ContextHandlerCollection();
+
+ log = new TestRequestLog();
+
+ final RequestLogHandler logHandler = new RequestLogHandler();
+ logHandler.setHandler(contexts);
+ logHandler.setRequestLog(log);
+
+ server = new Server();
+ server.setConnectors(new Connector[] { connector });
+ server.setThreadPool(pool);
+ server.setHandler(logHandler);
+
+ server.setStopAtShutdown(false);
+ server.setGracefulShutdown(0);
+ }
+
+ /**
+ * Create a new servlet context within the server.
+ *
+ * This method should be invoked before the server is started, once for each
+ * context the caller wants to register.
+ *
+ * @param path
+ * path of the context; use "/" for the root context if binding
+ * to the root is desired.
+ * @return the context to add servlets into.
+ */
+ public ServletContextHandler addContext(String path) {
+ assertNotYetSetUp();
+ if ("".equals(path))
+ path = "/";
+
+ ServletContextHandler ctx = new ServletContextHandler();
+ ctx.setContextPath(path);
+ contexts.addHandler(ctx);
+
+ return ctx;
+ }
+
+ public ServletContextHandler authBasic(ServletContextHandler ctx) {
+ assertNotYetSetUp();
+ auth(ctx, new BasicAuthenticator());
+ return ctx;
+ }
+
+ private void auth(ServletContextHandler ctx, Authenticator authType) {
+ final String role = "can-access";
+
+ MappedLoginService users = new MappedLoginService() {
+ @Override
+ protected UserIdentity loadUser(String who) {
+ return null;
+ }
+
+ @Override
+ protected void loadUsers() throws IOException {
+ putUser(username, new Password(password), new String[] { role });
+ }
+ };
+
+ ConstraintMapping cm = new ConstraintMapping();
+ cm.setConstraint(new Constraint());
+ cm.getConstraint().setAuthenticate(true);
+ cm.getConstraint().setDataConstraint(Constraint.DC_NONE);
+ cm.getConstraint().setRoles(new String[] { role });
+ cm.setPathSpec("/*");
+
+ ConstraintSecurityHandler sec = new ConstraintSecurityHandler();
+ sec.setStrict(false);
+ sec.setRealmName(realm);
+ sec.setAuthenticator(authType);
+ sec.setLoginService(users);
+ sec.setConstraintMappings(new ConstraintMapping[] { cm });
+ sec.setHandler(ctx);
+
+ contexts.removeHandler(ctx);
+ contexts.addHandler(sec);
+ }
+
+ /**
+ * Start the server on a random local port.
+ *
+ * @throws Exception
+ * the server cannot be started, testing is not possible.
+ */
+ public void setUp() throws Exception {
+ RecordingLogger.clear();
+ log.clear();
+ server.start();
+ }
+
+ /**
+ * Shutdown the server.
+ *
+ * @throws Exception
+ * the server refuses to halt, or wasn't running.
+ */
+ public void tearDown() throws Exception {
+ RecordingLogger.clear();
+ log.clear();
+ server.stop();
+ }
+
+ /**
+ * Get the URI to reference this server.
+ *
+ * The returned URI includes the proper host name and port number, but does
+ * not contain a path.
+ *
+ * @return URI to reference this server's root context.
+ */
+ public URI getURI() {
+ assertAlreadySetUp();
+ String host = connector.getHost();
+ if (host.contains(":") && !host.startsWith("["))
+ host = "[" + host + "]";
+ final String uri = "http://" + host + ":" + getPort();
+ try {
+ return new URI(uri);
+ } catch (URISyntaxException e) {
+ throw new RuntimeException("Unexpected URI error on " + uri, e);
+ }
+ }
+
+ /** @return the local port number the server is listening on. */
+ public int getPort() {
+ assertAlreadySetUp();
+ return ((SelectChannelConnector) connector).getLocalPort();
+ }
+
+ /** @return all requests since the server was started. */
+ public List getRequests() {
+ return new ArrayList(log.getEvents());
+ }
+
+ /**
+ * @param base
+ * base URI used to access the server.
+ * @param path
+ * the path to locate requests for, relative to {@code base}.
+ * @return all requests which match the given path.
+ */
+ public List getRequests(URIish base, String path) {
+ return getRequests(HttpTestCase.join(base, path));
+ }
+
+ /**
+ * @param path
+ * the path to locate requests for.
+ * @return all requests which match the given path.
+ */
+ public List getRequests(String path) {
+ ArrayList r = new ArrayList();
+ for (AccessEvent event : log.getEvents()) {
+ if (event.getPath().equals(path)) {
+ r.add(event);
+ }
+ }
+ return r;
+ }
+
+ private void assertNotYetSetUp() {
+ Assert.assertFalse("server is not running", server.isRunning());
+ }
+
+ private void assertAlreadySetUp() {
+ Assert.assertTrue("server is running", server.isRunning());
+ }
+}
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/HttpTestCase.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/HttpTestCase.java
new file mode 100644
index 000000000..e25975761
--- /dev/null
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/HttpTestCase.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2009-2010, Google Inc.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.http.test.util;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
+import org.eclipse.jgit.junit.TestRepository;
+import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevObject;
+import org.eclipse.jgit.transport.RefSpec;
+import org.eclipse.jgit.transport.RemoteRefUpdate;
+import org.eclipse.jgit.transport.URIish;
+
+/** Base class for HTTP related transport testing. */
+public abstract class HttpTestCase extends LocalDiskRepositoryTestCase {
+ protected static final String master = Constants.R_HEADS + Constants.MASTER;
+
+ /** In-memory application server; subclass must start. */
+ protected AppServer server;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ server = new AppServer();
+ }
+
+ protected void tearDown() throws Exception {
+ server.tearDown();
+ super.tearDown();
+ }
+
+ protected TestRepository createTestRepository() throws Exception {
+ return new TestRepository(createBareRepository());
+ }
+
+ protected URIish toURIish(String path) throws URISyntaxException {
+ URI u = server.getURI().resolve(path);
+ return new URIish(u.toString());
+ }
+
+ protected URIish toURIish(ServletContextHandler app, String name)
+ throws URISyntaxException {
+ String p = app.getContextPath();
+ if (!p.endsWith("/") && !name.startsWith("/"))
+ p += "/";
+ p += name;
+ return toURIish(p);
+ }
+
+ protected List getRequests() {
+ return server.getRequests();
+ }
+
+ protected List getRequests(URIish base, String path) {
+ return server.getRequests(base, path);
+ }
+
+ protected List getRequests(String path) {
+ return server.getRequests(path);
+ }
+
+ protected static void fsck(Repository db, RevObject... tips)
+ throws Exception {
+ new TestRepository(db).fsck(tips);
+ }
+
+ protected static Set mirror(String... refs) {
+ HashSet r = new HashSet();
+ for (String name : refs) {
+ RefSpec rs = new RefSpec(name);
+ rs = rs.setDestination(name);
+ rs = rs.setForceUpdate(true);
+ r.add(rs);
+ }
+ return r;
+ }
+
+ protected static Collection push(TestRepository from,
+ RevCommit q) throws IOException {
+ final Repository db = from.getRepository();
+ final String srcExpr = q.name();
+ final String dstName = master;
+ final boolean forceUpdate = true;
+ final String localName = null;
+ final ObjectId oldId = null;
+
+ RemoteRefUpdate u = new RemoteRefUpdate(db, srcExpr, dstName,
+ forceUpdate, localName, oldId);
+ return Collections.singleton(u);
+ }
+
+ public static String loose(URIish base, AnyObjectId id) {
+ final String objectName = id.name();
+ final String d = objectName.substring(0, 2);
+ final String f = objectName.substring(2);
+ return join(base, "objects/" + d + "/" + f);
+ }
+
+ public static String join(URIish base, String path) {
+ if (path.startsWith("/"))
+ fail("Cannot join absolute path " + path + " to URIish " + base);
+
+ String dir = base.getPath();
+ if (!dir.endsWith("/"))
+ dir += "/";
+ return dir + path;
+ }
+}
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/MockServletConfig.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/MockServletConfig.java
new file mode 100644
index 000000000..cf6f6f6a8
--- /dev/null
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/MockServletConfig.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2010, Google Inc.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.http.test.util;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+public class MockServletConfig implements ServletConfig {
+ private final Map parameters = new HashMap();
+
+ public void setInitParameter(String name, String value) {
+ parameters.put(name, value);
+ }
+
+ public String getInitParameter(String name) {
+ return parameters.get(name);
+ }
+
+ public Enumeration getInitParameterNames() {
+ final Iterator i = parameters.keySet().iterator();
+ return new Enumeration() {
+ public boolean hasMoreElements() {
+ return i.hasNext();
+ }
+
+ public String nextElement() {
+ return i.next();
+ }
+ };
+ }
+
+ public String getServletName() {
+ return "MOCK_SERVLET";
+ }
+
+ public ServletContext getServletContext() {
+ return null;
+ }
+}
\ No newline at end of file
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/RecordingLogger.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/RecordingLogger.java
new file mode 100644
index 000000000..24e125c22
--- /dev/null
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/RecordingLogger.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2010, Google Inc.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.http.test.util;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.jetty.util.log.Logger;
+
+/** Logs warnings into an array for later inspection. */
+public class RecordingLogger implements Logger {
+ private static List warnings = new ArrayList();
+
+ /** Clear the warnings, automatically done by {@link AppServer#setUp()} */
+ public static void clear() {
+ synchronized (warnings) {
+ warnings.clear();
+ }
+ }
+
+ /** @return the warnings (if any) from the last execution */
+ public static List getWarnings() {
+ synchronized (warnings) {
+ ArrayList copy = new ArrayList(warnings);
+ return Collections.unmodifiableList(copy);
+ }
+ }
+
+ @SuppressWarnings("serial")
+ public static class Warning extends Exception {
+ public Warning(String msg) {
+ super(msg);
+ }
+
+ public Warning(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+ }
+
+ private final String name;
+
+ public RecordingLogger() {
+ this("");
+ }
+
+ public RecordingLogger(final String name) {
+ this.name = name;
+ }
+
+ public Logger getLogger(@SuppressWarnings("hiding") String name) {
+ return new RecordingLogger(name);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void warn(String msg, Object arg0, Object arg1) {
+ synchronized (warnings) {
+ warnings.add(new Warning(MessageFormat.format(msg, arg0, arg1)));
+ }
+ }
+
+ public void warn(String msg, Throwable th) {
+ synchronized (warnings) {
+ warnings.add(new Warning(msg, th));
+ }
+ }
+
+ public void warn(String msg) {
+ synchronized (warnings) {
+ warnings.add(new Warning(msg));
+ }
+ }
+
+ public void debug(String msg, Object arg0, Object arg1) {
+ // Ignore (not relevant to test failures)
+ }
+
+ public void debug(String msg, Throwable th) {
+ // Ignore (not relevant to test failures)
+ }
+
+ public void debug(String msg) {
+ // Ignore (not relevant to test failures)
+ }
+
+ public void info(String msg, Object arg0, Object arg1) {
+ // Ignore (not relevant to test failures)
+ }
+
+ public void info(String msg) {
+ // Ignore (not relevant to test failures)
+ }
+
+ public boolean isDebugEnabled() {
+ return false;
+ }
+
+ public void setDebugEnabled(boolean enabled) {
+ // Ignore (not relevant to test failures)
+ }
+}
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/TestRequestLog.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/TestRequestLog.java
new file mode 100644
index 000000000..904f6aac8
--- /dev/null
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/util/TestRequestLog.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2010, Google Inc.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.http.test.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jetty.server.Request;
+import org.eclipse.jetty.server.RequestLog;
+import org.eclipse.jetty.server.Response;
+import org.eclipse.jetty.util.component.AbstractLifeCycle;
+
+/** Logs request made through {@link AppServer}. */
+class TestRequestLog extends AbstractLifeCycle implements RequestLog {
+ private final List events = new ArrayList();
+
+ /** Reset the log back to its original empty state. */
+ synchronized void clear() {
+ events.clear();
+ }
+
+ /** @return all of the events made since the last clear. */
+ synchronized List getEvents() {
+ return events;
+ }
+
+ public synchronized void log(Request request, Response response) {
+ events.add(new AccessEvent(request, response));
+ }
+}
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java
index ec9b1d7ac..ddace0df2 100644
--- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java
+++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
* Copyright (C) 2008, Robin Rosenberg
* Copyright (C) 2007, Shawn O. Pearce
* and other copyright owners as documented in the project's IP log.
@@ -57,8 +57,10 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
+import junit.framework.Assert;
import junit.framework.TestCase;
+import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileBasedConfig;
import org.eclipse.jgit.lib.PersonIdent;
@@ -404,6 +406,14 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase {
return new String(body, 0, body.length, "UTF-8");
}
+ protected static void assertEquals(AnyObjectId exp, AnyObjectId act) {
+ if (exp != null)
+ exp = exp.copy();
+ if (act != null)
+ act = act.copy();
+ Assert.assertEquals(exp, act);
+ }
+
private static String[] toEnvArray(final Map env) {
final String[] envp = new String[env.size()];
int i = 0;
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
index ce8b3e6ee..e738276bd 100644
--- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
+++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
@@ -44,11 +44,15 @@
package org.eclipse.jgit.junit;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
@@ -60,21 +64,30 @@ import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheEditor.DeletePath;
import org.eclipse.jgit.dircache.DirCacheEditor.DeleteTree;
import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
+import org.eclipse.jgit.errors.IncorrectObjectTypeException;
+import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.ObjectWritingException;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Commit;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.LockFile;
+import org.eclipse.jgit.lib.NullProgressMonitor;
+import org.eclipse.jgit.lib.ObjectChecker;
+import org.eclipse.jgit.lib.ObjectDatabase;
import org.eclipse.jgit.lib.ObjectDirectory;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectWriter;
+import org.eclipse.jgit.lib.PackFile;
+import org.eclipse.jgit.lib.PackWriter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefWriter;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.Tag;
+import org.eclipse.jgit.lib.PackIndex.MutableEntry;
+import org.eclipse.jgit.revwalk.ObjectWalk;
import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
@@ -428,26 +441,25 @@ public class TestRepository {
* @throws Exception
*/
public void updateServerInfo() throws Exception {
- if (db.getObjectDatabase() instanceof ObjectDirectory) {
+ final ObjectDatabase odb = db.getObjectDatabase();
+ if (odb instanceof ObjectDirectory) {
RefWriter rw = new RefWriter(db.getAllRefs().values()) {
@Override
protected void writeFile(final String name, final byte[] bin)
throws IOException {
- final File p = new File(db.getDirectory(), name);
- final LockFile lck = new LockFile(p);
- if (!lck.lock())
- throw new ObjectWritingException("Can't write " + p);
- try {
- lck.write(bin);
- } catch (IOException ioe) {
- throw new ObjectWritingException("Can't write " + p);
- }
- if (!lck.commit())
- throw new ObjectWritingException("Can't write " + p);
+ TestRepository.this.writeFile(name, bin);
}
};
rw.writePackedRefs();
rw.writeInfoRefs();
+
+ final StringBuilder w = new StringBuilder();
+ for (PackFile p : ((ObjectDirectory) odb).getPacks()) {
+ w.append("P ");
+ w.append(p.getPackFile().getName());
+ w.append('\n');
+ }
+ writeFile("objects/info/packs", Constants.encodeASCII(w.toString()));
}
}
@@ -484,6 +496,132 @@ public class TestRepository {
return new BranchBuilder(ref);
}
+ /**
+ * Run consistency checks against the object database.
+ *
+ * This method completes silently if the checks pass. A temporary revision
+ * pool is constructed during the checking.
+ *
+ * @param tips
+ * the tips to start checking from; if not supplied the refs of
+ * the repository are used instead.
+ * @throws MissingObjectException
+ * @throws IncorrectObjectTypeException
+ * @throws IOException
+ */
+ public void fsck(RevObject... tips) throws MissingObjectException,
+ IncorrectObjectTypeException, IOException {
+ ObjectWalk ow = new ObjectWalk(db);
+ if (tips.length != 0) {
+ for (RevObject o : tips)
+ ow.markStart(ow.parseAny(o));
+ } else {
+ for (Ref r : db.getAllRefs().values())
+ ow.markStart(ow.parseAny(r.getObjectId()));
+ }
+
+ ObjectChecker oc = new ObjectChecker();
+ for (;;) {
+ final RevCommit o = ow.next();
+ if (o == null)
+ break;
+
+ final byte[] bin = db.openObject(o).getCachedBytes();
+ oc.checkCommit(bin);
+ assertHash(o, bin);
+ }
+
+ for (;;) {
+ final RevObject o = ow.nextObject();
+ if (o == null)
+ break;
+
+ final byte[] bin = db.openObject(o).getCachedBytes();
+ oc.check(o.getType(), bin);
+ assertHash(o, bin);
+ }
+ }
+
+ private static void assertHash(RevObject id, byte[] bin) {
+ MessageDigest md = Constants.newMessageDigest();
+ md.update(Constants.encodedTypeString(id.getType()));
+ md.update((byte) ' ');
+ md.update(Constants.encodeASCII(bin.length));
+ md.update((byte) 0);
+ md.update(bin);
+ Assert.assertEquals(id.copy(), ObjectId.fromRaw(md.digest()));
+ }
+
+ /**
+ * Pack all reachable objects in the repository into a single pack file.
+ *
+ * All loose objects are automatically pruned. Existing packs however are
+ * not removed.
+ *
+ * @throws Exception
+ */
+ public void packAndPrune() throws Exception {
+ final ObjectDirectory odb = (ObjectDirectory) db.getObjectDatabase();
+ final PackWriter pw = new PackWriter(db, NullProgressMonitor.INSTANCE);
+
+ Set all = new HashSet();
+ for (Ref r : db.getAllRefs().values())
+ all.add(r.getObjectId());
+ pw.preparePack(all, Collections. emptySet());
+
+ final ObjectId name = pw.computeName();
+ FileOutputStream out;
+
+ final File pack = nameFor(odb, name, ".pack");
+ out = new FileOutputStream(pack);
+ try {
+ pw.writePack(out);
+ } finally {
+ out.close();
+ }
+ pack.setReadOnly();
+
+ final File idx = nameFor(odb, name, ".idx");
+ out = new FileOutputStream(idx);
+ try {
+ pw.writeIndex(out);
+ } finally {
+ out.close();
+ }
+ idx.setReadOnly();
+
+ odb.openPack(pack, idx);
+ updateServerInfo();
+ prunePacked(odb);
+ }
+
+ private void prunePacked(ObjectDirectory odb) {
+ for (PackFile p : odb.getPacks()) {
+ for (MutableEntry e : p)
+ odb.fileFor(e.toObjectId()).delete();
+ }
+ }
+
+ private static File nameFor(ObjectDirectory odb, ObjectId name, String t) {
+ File packdir = new File(odb.getDirectory(), "pack");
+ return new File(packdir, "pack-" + name.name() + t);
+ }
+
+ private void writeFile(final String name, final byte[] bin)
+ throws IOException, ObjectWritingException {
+ final File p = new File(db.getDirectory(), name);
+ final LockFile lck = new LockFile(p);
+ if (!lck.lock())
+ throw new ObjectWritingException("Can't write " + p);
+ try {
+ lck.write(bin);
+ } catch (IOException ioe) {
+ throw new ObjectWritingException("Can't write " + p);
+ }
+ if (!lck.commit())
+ throw new ObjectWritingException("Can't write " + p);
+ }
+
/** Helper to build a branch with one or more commits */
public class BranchBuilder {
private final String ref;
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java
index 62303de9f..c521e8051 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java
@@ -155,6 +155,8 @@ public class TransportHttp extends HttpTransport implements WalkTransport,
private final ProxySelector proxySelector;
+ private boolean useSmartHttp = true;
+
TransportHttp(final Repository local, final URIish uri)
throws NotSupportedException {
super(local, uri);
@@ -171,6 +173,20 @@ public class TransportHttp extends HttpTransport implements WalkTransport,
proxySelector = ProxySelector.getDefault();
}
+ /**
+ * Toggle whether or not smart HTTP transport should be used.
+ *
+ * This flag exists primarily to support backwards compatibility testing
+ * within a testing framework, there is no need to modify it in most
+ * applications.
+ *
+ * @param on
+ * if {@code true} (default), smart HTTP is enabled.
+ */
+ public void setUseSmartHttp(final boolean on) {
+ useSmartHttp = on;
+ }
+
@Override
public FetchConnection openFetch() throws TransportException,
NotSupportedException {
@@ -271,6 +287,10 @@ public class TransportHttp extends HttpTransport implements WalkTransport,
readSmartHeaders(in, service);
return new SmartHttpPushConnection(in);
+ } else if (!useSmartHttp) {
+ final String msg = "smart HTTP push disabled";
+ throw new NotSupportedException(msg);
+
} else {
final String msg = "remote does not support smart HTTP push";
throw new NotSupportedException(msg);
@@ -303,9 +323,11 @@ public class TransportHttp extends HttpTransport implements WalkTransport,
b.append('/');
b.append(Constants.INFO_REFS);
- b.append(b.indexOf("?") < 0 ? '?' : '&');
- b.append("service=");
- b.append(service);
+ if (useSmartHttp) {
+ b.append(b.indexOf("?") < 0 ? '?' : '&');
+ b.append("service=");
+ b.append(service);
+ }
u = new URL(b.toString());
} catch (MalformedURLException e) {
@@ -314,8 +336,12 @@ public class TransportHttp extends HttpTransport implements WalkTransport,
try {
final HttpURLConnection conn = httpOpen(u);
- String expType = "application/x-" + service + "-advertisement";
- conn.setRequestProperty(HDR_ACCEPT, expType + ", */*");
+ if (useSmartHttp) {
+ String expType = "application/x-" + service + "-advertisement";
+ conn.setRequestProperty(HDR_ACCEPT, expType + ", */*");
+ } else {
+ conn.setRequestProperty(HDR_ACCEPT, "*/*");
+ }
final int status = HttpSupport.response(conn);
switch (status) {
case HttpURLConnection.HTTP_OK:
diff --git a/pom.xml b/pom.xml
index d3467204f..d02cafbbe 100644
--- a/pom.xml
+++ b/pom.xml
@@ -138,6 +138,8 @@
CQ 3565
2.5
+
+ 7.0.1.v20091125
@@ -265,6 +267,12 @@
servlet-api
${servlet-api-version}
+
+
+ org.eclipse.jetty
+ jetty-servlet
+ ${jetty-version}
+
@@ -298,6 +306,8 @@
org.eclipse.jgit.http.server
org.eclipse.jgit.pgm
org.eclipse.jgit.junit
+
org.eclipse.jgit.test
+ org.eclipse.jgit.http.test