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  
17  import org.junit.Test;
18  import org.junit.runner.RunWith;
19  import org.junit.runners.JUnit4;
20  
21  @RunWith(JUnit4.class)
22  public class ProtocolV2HookChainTest {
23  
24  	@Test
25  	public void testDefaultIfEmpty() {
26  		ProtocolV2Hook[] noHooks = {};
27  		ProtocolV2Hook newChain = ProtocolV2HookChain
28  				.newChain(Arrays.asList(noHooks));
29  		assertEquals(newChain, ProtocolV2Hook.DEFAULT);
30  	}
31  
32  	@Test
33  	public void testFlattenChainIfOnlyOne() {
34  		FakeProtocolV2Hook hook1 = new FakeProtocolV2Hook();
35  		ProtocolV2Hook newChain = ProtocolV2HookChain
36  				.newChain(Arrays.asList(ProtocolV2Hook.DEFAULT, hook1));
37  		assertEquals(newChain, hook1);
38  	}
39  
40  	@Test
41  	public void testMultipleHooks() throws ServiceMayNotContinueException {
42  		FakeProtocolV2Hook hook1 = new FakeProtocolV2Hook();
43  		FakeProtocolV2Hook hook2 = new FakeProtocolV2Hook();
44  
45  		ProtocolV2Hook chained = ProtocolV2HookChain
46  				.newChain(Arrays.asList(hook1, hook2));
47  		chained.onLsRefs(LsRefsV2Request.builder().build());
48  
49  		assertTrue(hook1.wasInvoked());
50  		assertTrue(hook2.wasInvoked());
51  	}
52  
53  	private static final class FakeProtocolV2Hook implements ProtocolV2Hook {
54  		boolean invoked;
55  
56  		@Override
57  		public void onLsRefs(LsRefsV2Request req)
58  				throws ServiceMayNotContinueException {
59  			invoked = true;
60  		}
61  
62  		@Override
63  		public void onCapabilities(CapabilitiesV2Request req)
64  				throws ServiceMayNotContinueException {
65  			throw new UnsupportedOperationException();
66  		}
67  
68  		@Override
69  		public void onFetch(FetchV2Request req)
70  				throws ServiceMayNotContinueException {
71  			throw new UnsupportedOperationException();
72  		}
73  
74  		public boolean wasInvoked() {
75  			return invoked;
76  		}
77  	}
78  }