diff options
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java | 126 |
1 files changed, 76 insertions, 50 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java index 28473c79fa..2e8b30f151 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com> and others * - * 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 + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://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. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.transport; @@ -47,14 +14,19 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import org.eclipse.jgit.api.errors.AbortedByHookException; import org.eclipse.jgit.errors.NotSupportedException; import org.eclipse.jgit.errors.TransportException; +import org.eclipse.jgit.hooks.PrePushHook; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectIdRef; import org.eclipse.jgit.lib.ProgressMonitor; @@ -64,6 +36,7 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.TextProgressMonitor; import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase; import org.eclipse.jgit.transport.RemoteRefUpdate.Status; +import org.eclipse.jgit.util.io.NullOutputStream; import org.junit.Before; import org.junit.Test; @@ -83,8 +56,8 @@ public class PushProcessTest extends SampleDataRepositoryTestCase { public void setUp() throws Exception { super.setUp(); transport = new MockTransport(db, new URIish()); - refUpdates = new HashSet<RemoteRefUpdate>(); - advertisedRefs = new HashSet<Ref>(); + refUpdates = new HashSet<>(); + advertisedRefs = new HashSet<>(); connectionUpdateStatus = Status.OK; } @@ -253,7 +226,17 @@ public class PushProcessTest extends SampleDataRepositoryTestCase { .fromString("0000000000000000000000000000000000000001")); final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master", ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef")); - testOneUpdateStatus(rru, ref, Status.REJECTED_REMOTE_CHANGED, null); + try (ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bytes, true, + StandardCharsets.UTF_8); + PrintStream err = new PrintStream(NullOutputStream.INSTANCE)) { + MockPrePushHook hook = new MockPrePushHook(db, out, err); + testOneUpdateStatus(rru, ref, Status.REJECTED_REMOTE_CHANGED, null, + hook); + out.flush(); + String result = new String(bytes.toString(StandardCharsets.UTF_8)); + assertEquals("", result); + } } /** @@ -289,10 +272,22 @@ public class PushProcessTest extends SampleDataRepositoryTestCase { refUpdates.add(rruOk); refUpdates.add(rruReject); advertisedRefs.add(refToChange); - executePush(); - assertEquals(Status.OK, rruOk.getStatus()); - assertTrue(rruOk.isFastForward()); - assertEquals(Status.NON_EXISTING, rruReject.getStatus()); + try (ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bytes, true, + StandardCharsets.UTF_8); + PrintStream err = new PrintStream(NullOutputStream.INSTANCE)) { + MockPrePushHook hook = new MockPrePushHook(db, out, err); + executePush(hook); + assertEquals(Status.OK, rruOk.getStatus()); + assertTrue(rruOk.isFastForward()); + assertEquals(Status.NON_EXISTING, rruReject.getStatus()); + out.flush(); + String result = new String(bytes.toString(StandardCharsets.UTF_8)); + assertEquals( + "null 0000000000000000000000000000000000000000 " + + "refs/heads/master 2c349335b7f797072cf729c4f3bb0914ecb6dec9\n", + result); + } } /** @@ -379,10 +374,18 @@ public class PushProcessTest extends SampleDataRepositoryTestCase { final Ref advertisedRef, final Status expectedStatus, Boolean fastForward) throws NotSupportedException, TransportException { + return testOneUpdateStatus(rru, advertisedRef, expectedStatus, + fastForward, null); + } + + private PushResult testOneUpdateStatus(final RemoteRefUpdate rru, + final Ref advertisedRef, final Status expectedStatus, + Boolean fastForward, PrePushHook hook) + throws NotSupportedException, TransportException { refUpdates.add(rru); if (advertisedRef != null) advertisedRefs.add(advertisedRef); - final PushResult result = executePush(); + final PushResult result = executePush(hook); assertEquals(expectedStatus, rru.getStatus()); if (fastForward != null) assertEquals(fastForward, Boolean.valueOf(rru.isFastForward())); @@ -391,7 +394,12 @@ public class PushProcessTest extends SampleDataRepositoryTestCase { private PushResult executePush() throws NotSupportedException, TransportException { - process = new PushProcess(transport, refUpdates); + return executePush(null); + } + + private PushResult executePush(PrePushHook hook) + throws NotSupportedException, TransportException { + process = new PushProcess(transport, refUpdates, hook); return process.execute(new TextProgressMonitor()); } @@ -421,8 +429,8 @@ public class PushProcessTest extends SampleDataRepositoryTestCase { private class MockPushConnection extends BaseConnection implements PushConnection { MockPushConnection() { - final Map<String, Ref> refsMap = new HashMap<String, Ref>(); - for (final Ref r : advertisedRefs) + final Map<String, Ref> refsMap = new HashMap<>(); + for (Ref r : advertisedRefs) refsMap.put(r.getName(), r); available(refsMap); } @@ -432,19 +440,37 @@ public class PushProcessTest extends SampleDataRepositoryTestCase { // nothing here } + @Override public void push(ProgressMonitor monitor, Map<String, RemoteRefUpdate> refsToUpdate, OutputStream out) throws TransportException { push(monitor, refsToUpdate); } + @Override public void push(ProgressMonitor monitor, Map<String, RemoteRefUpdate> refsToUpdate) throws TransportException { - for (final RemoteRefUpdate rru : refsToUpdate.values()) { + for (RemoteRefUpdate rru : refsToUpdate.values()) { assertEquals(Status.NOT_ATTEMPTED, rru.getStatus()); rru.setStatus(connectionUpdateStatus); } } } + + private static class MockPrePushHook extends PrePushHook { + + private final PrintStream output; + + public MockPrePushHook(Repository repo, PrintStream out, + PrintStream err) { + super(repo, out, err); + output = out; + } + + @Override + protected void doRun() throws AbortedByHookException, IOException { + output.print(getStdinArgs()); + } + } } |