xref: /JGit/org.eclipse.jgit.junit.ssh/src/org/eclipse/jgit/junit/ssh/SshBasicTestBase.java (revision 0853a2410f22c8bd97a179dec14e3c083a27abbb)
1*0853a241SThomas Wolf /*
2*0853a241SThomas Wolf  * Copyright (C) 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others
3*0853a241SThomas Wolf  *
4*0853a241SThomas Wolf  * This program and the accompanying materials are made available under the
5*0853a241SThomas Wolf  * terms of the Eclipse Distribution License v. 1.0 which is available at
6*0853a241SThomas Wolf  * https://www.eclipse.org/org/documents/edl-v10.php.
7*0853a241SThomas Wolf  *
8*0853a241SThomas Wolf  * SPDX-License-Identifier: BSD-3-Clause
9*0853a241SThomas Wolf  */
10*0853a241SThomas Wolf package org.eclipse.jgit.junit.ssh;
11*0853a241SThomas Wolf 
12*0853a241SThomas Wolf import static org.junit.Assert.assertEquals;
13*0853a241SThomas Wolf import static org.junit.Assert.assertFalse;
14*0853a241SThomas Wolf import static org.junit.Assert.assertTrue;
15*0853a241SThomas Wolf 
16*0853a241SThomas Wolf import java.io.File;
17*0853a241SThomas Wolf 
18*0853a241SThomas Wolf import org.eclipse.jgit.api.Git;
19*0853a241SThomas Wolf import org.junit.Test;
20*0853a241SThomas Wolf 
21*0853a241SThomas Wolf /**
22*0853a241SThomas Wolf  * Some minimal cloning and fetching tests. Concrete subclasses can implement
23*0853a241SThomas Wolf  * the abstract operations from {@link SshTestHarness} to run with different SSH
24*0853a241SThomas Wolf  * implementations.
25*0853a241SThomas Wolf  */
26*0853a241SThomas Wolf public abstract class SshBasicTestBase extends SshTestHarness {
27*0853a241SThomas Wolf 
28*0853a241SThomas Wolf 	protected File defaultCloneDir;
29*0853a241SThomas Wolf 
30*0853a241SThomas Wolf 	@Override
setUp()31*0853a241SThomas Wolf 	public void setUp() throws Exception {
32*0853a241SThomas Wolf 		super.setUp();
33*0853a241SThomas Wolf 		defaultCloneDir = new File(getTemporaryDirectory(), "cloned");
34*0853a241SThomas Wolf 	}
35*0853a241SThomas Wolf 
36*0853a241SThomas Wolf 	@Test
testSshCloneWithConfig()37*0853a241SThomas Wolf 	public void testSshCloneWithConfig() throws Exception {
38*0853a241SThomas Wolf 		cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, null, //
39*0853a241SThomas Wolf 				"Host localhost", //
40*0853a241SThomas Wolf 				"HostName localhost", //
41*0853a241SThomas Wolf 				"Port " + testPort, //
42*0853a241SThomas Wolf 				"User " + TEST_USER, //
43*0853a241SThomas Wolf 				"IdentityFile " + privateKey1.getAbsolutePath());
44*0853a241SThomas Wolf 	}
45*0853a241SThomas Wolf 
46*0853a241SThomas Wolf 	@Test
testSshFetchWithConfig()47*0853a241SThomas Wolf 	public void testSshFetchWithConfig() throws Exception {
48*0853a241SThomas Wolf 		File localClone = cloneWith("ssh://localhost/doesntmatter",
49*0853a241SThomas Wolf 				defaultCloneDir, null, //
50*0853a241SThomas Wolf 				"Host localhost", //
51*0853a241SThomas Wolf 				"HostName localhost", //
52*0853a241SThomas Wolf 				"Port " + testPort, //
53*0853a241SThomas Wolf 				"User " + TEST_USER, //
54*0853a241SThomas Wolf 				"IdentityFile " + privateKey1.getAbsolutePath());
55*0853a241SThomas Wolf 		// Do a commit in the upstream repo
56*0853a241SThomas Wolf 		try (Git git = new Git(db)) {
57*0853a241SThomas Wolf 			writeTrashFile("SomeOtherFile.txt", "Other commit");
58*0853a241SThomas Wolf 			git.add().addFilepattern("SomeOtherFile.txt").call();
59*0853a241SThomas Wolf 			git.commit().setMessage("New commit").call();
60*0853a241SThomas Wolf 		}
61*0853a241SThomas Wolf 		// Pull in the clone
62*0853a241SThomas Wolf 		try (Git git = Git.open(localClone)) {
63*0853a241SThomas Wolf 			File f = new File(git.getRepository().getWorkTree(),
64*0853a241SThomas Wolf 					"SomeOtherFile.txt");
65*0853a241SThomas Wolf 			assertFalse(f.exists());
66*0853a241SThomas Wolf 			git.pull().setRemote("origin").call();
67*0853a241SThomas Wolf 			assertTrue(f.exists());
68*0853a241SThomas Wolf 			assertEquals("Other commit", read(f));
69*0853a241SThomas Wolf 		}
70*0853a241SThomas Wolf 	}
71*0853a241SThomas Wolf }
72