View Javadoc
1   /*
2    * Copyright (c) 2022 Darius Jokilehto <darius.jokilehto+os@gmail.com>
3    * Copyright (c) 2022 Antonio Barone <syntonyze@gmail.com>
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.transport;
13  
14  import org.eclipse.jgit.errors.NoRemoteRepositoryException;
15  import org.eclipse.jgit.errors.TransportException;
16  import org.eclipse.jgit.internal.JGitText;
17  import org.junit.Test;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.EOFException;
21  import java.io.IOException;
22  import java.util.Arrays;
23  
24  import static org.hamcrest.MatcherAssert.assertThat;
25  import static org.hamcrest.Matchers.endsWith;
26  import static org.hamcrest.Matchers.hasItem;
27  import static org.hamcrest.Matchers.instanceOf;
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertThrows;
30  
31  public class BasePackPushConnectionTest {
32  	@Test
33  	public void testNoRemoteRepository() {
34  		NoRemoteRepositoryException openFetchException =
35                  new NoRemoteRepositoryException( new URIish(), "not found");
36  		IOException ioException = new IOException("not read");
37  
38  		try (FailingBasePackPushConnection fbppc =
39  				new FailingBasePackPushConnection(openFetchException)) {
40  			TransportException result = fbppc.noRepository(ioException);
41  
42  			assertEquals(openFetchException, result);
43  			assertThat(Arrays.asList(result.getSuppressed()),
44  					hasItem(ioException));
45  		}
46  	}
47  
48  	@Test
49  	public void testPushNotPermitted() {
50  		URIish uri = new URIish();
51  		TransportException openFetchException = new TransportException(uri,
52  				"a transport exception");
53  		IOException ioException = new IOException("not read");
54  
55  		try (FailingBasePackPushConnection fbppc =
56  				new FailingBasePackPushConnection(openFetchException)) {
57  			TransportException result = fbppc.noRepository(ioException);
58  
59  			assertEquals(TransportException.class, result.getClass());
60  			assertThat(result.getMessage(),
61  					endsWith(JGitText.get().pushNotPermitted));
62  			assertEquals(openFetchException, result.getCause());
63  			assertThat(Arrays.asList(result.getSuppressed()),
64  					hasItem(ioException));
65  		}
66  	}
67  
68  	@Test
69  	public void testReadAdvertisedRefPropagatesCauseAndSuppressedExceptions() {
70  		IOException ioException = new IOException("not read");
71  		try (FailingBasePackPushConnection basePackConnection =
72  				new FailingBasePackPushConnection(
73  						new NoRemoteRepositoryException(
74  								new URIish(), "not found", ioException))) {
75  			Exception result = assertThrows(NoRemoteRepositoryException.class,
76  					basePackConnection::readAdvertisedRefs);
77  			assertEquals(ioException, result.getCause());
78  			assertThat(Arrays.asList(result.getSuppressed()),
79  					hasItem(instanceOf(EOFException.class)));
80  		}
81  	}
82  
83  	private static class FailingBasePackPushConnection
84  			extends BasePackPushConnection {
85  		FailingBasePackPushConnection(TransportException openFetchException) {
86  			super(new TransportLocal(new URIish(),
87  					new java.io.File("")) {
88  				@Override public FetchConnection openFetch()
89  						throws TransportException {
90  					throw openFetchException;
91  				}
92  			});
93  			pckIn = new PacketLineIn(new ByteArrayInputStream(new byte[0]));
94  		}
95  	}
96  }