View Javadoc
1   /*
2    * Copyright (C) 2019, Google LLC. 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.transport;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertTrue;
14  
15  import java.util.Arrays;
16  import java.util.Collection;
17  
18  import org.eclipse.jgit.lib.ObjectId;
19  import org.junit.Test;
20  import org.junit.runner.RunWith;
21  import org.junit.runners.JUnit4;
22  
23  @RunWith(JUnit4.class)
24  public class PreUploadHookChainTest {
25  
26  	@Test
27  	public void testDefaultIfEmpty() {
28  		PreUploadHook[] noHooks = {};
29  		PreUploadHook newChain = PreUploadHookChain
30  				.newChain(Arrays.asList(noHooks));
31  		assertEquals(newChain, PreUploadHook.NULL);
32  	}
33  
34  	@Test
35  	public void testFlattenChainIfOnlyOne() {
36  		FakePreUploadHook hook1 = new FakePreUploadHook();
37  		PreUploadHook newChain = PreUploadHookChain
38  				.newChain(Arrays.asList(PreUploadHook.NULL, hook1));
39  		assertEquals(newChain, hook1);
40  	}
41  
42  	@Test
43  	public void testMultipleHooks() throws ServiceMayNotContinueException {
44  		FakePreUploadHook hook1 = new FakePreUploadHook();
45  		FakePreUploadHook hook2 = new FakePreUploadHook();
46  
47  		PreUploadHook chained = PreUploadHookChain
48  				.newChain(Arrays.asList(hook1, hook2));
49  		chained.onBeginNegotiateRound(null, null, 0);
50  
51  		assertTrue(hook1.wasInvoked());
52  		assertTrue(hook2.wasInvoked());
53  	}
54  
55  	private static final class FakePreUploadHook implements PreUploadHook {
56  		boolean invoked;
57  
58  		@Override
59  		public void onBeginNegotiateRound(UploadPack up,
60  				Collection<? extends ObjectId> wants, int cntOffered)
61  				throws ServiceMayNotContinueException {
62  			invoked = true;
63  		}
64  
65  		@Override
66  		public void onEndNegotiateRound(UploadPack up,
67  				Collection<? extends ObjectId> wants, int cntCommon,
68  				int cntNotFound, boolean ready)
69  				throws ServiceMayNotContinueException {
70  			throw new UnsupportedOperationException();
71  		}
72  
73  		@Override
74  		public void onSendPack(UploadPack up,
75  				Collection<? extends ObjectId> wants,
76  				Collection<? extends ObjectId> haves)
77  				throws ServiceMayNotContinueException {
78  			throw new UnsupportedOperationException();
79  		}
80  
81  		public boolean wasInvoked() {
82  			return invoked;
83  		}
84  	}
85  }