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.eclipse.jgit.storage.pack.PackStatistics;
18  import org.junit.Test;
19  import org.junit.runner.RunWith;
20  import org.junit.runners.JUnit4;
21  
22  @RunWith(JUnit4.class)
23  public class PostUploadHookChainTest {
24  
25  	@Test
26  	public void testDefaultIfEmpty() {
27  		PostUploadHook[] noHooks = {};
28  		PostUploadHook newChain = PostUploadHookChain
29  				.newChain(Arrays.asList(noHooks));
30  		assertEquals(newChain, PostUploadHook.NULL);
31  	}
32  
33  	@Test
34  	public void testFlattenChainIfOnlyOne() {
35  		FakePostUploadHook hook1 = new FakePostUploadHook();
36  		PostUploadHook newChain = PostUploadHookChain
37  				.newChain(Arrays.asList(PostUploadHook.NULL, hook1));
38  		assertEquals(newChain, hook1);
39  	}
40  
41  	@Test
42  	public void testMultipleHooks() {
43  		FakePostUploadHook hook1 = new FakePostUploadHook();
44  		FakePostUploadHook hook2 = new FakePostUploadHook();
45  
46  		PostUploadHook chained = PostUploadHookChain
47  				.newChain(Arrays.asList(hook1, hook2));
48  		chained.onPostUpload(null);
49  
50  		assertTrue(hook1.wasInvoked());
51  		assertTrue(hook2.wasInvoked());
52  	}
53  
54  	private static final class FakePostUploadHook implements PostUploadHook {
55  		boolean invoked;
56  
57  		public boolean wasInvoked() {
58  			return invoked;
59  		}
60  
61  		@Override
62  		public void onPostUpload(PackStatistics stats) {
63  			invoked = true;
64  		}
65  	}
66  }