aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/diffmergetool/ExternalToolTestCase.java
blob: 7a6ff46578d5d2a88bd9f19935f2fe8d4c86aa4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2020-2021, Simeon Andreev <simeon.danailov.andreev@gmail.com> and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.internal.diffmergetool;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FS_POSIX;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;

/**
 * Base test case for external merge and diff tool tests.
 */
public abstract class ExternalToolTestCase extends RepositoryTestCase {

	protected static final String DEFAULT_CONTENT = "line1";

	protected File localFile;

	protected File remoteFile;

	protected File mergedFile;

	protected File baseFile;

	protected File commandResult;

	protected FileElement local;

	protected FileElement remote;

	protected FileElement merged;

	protected FileElement base;

	@Before
	@Override
	public void setUp() throws Exception {
		super.setUp();

		localFile = writeTrashFile("localFile.txt", DEFAULT_CONTENT + "\n");
		localFile.deleteOnExit();
		remoteFile = writeTrashFile("remoteFile.txt", DEFAULT_CONTENT + "\n");
		remoteFile.deleteOnExit();
		mergedFile = writeTrashFile("mergedFile.txt", "");
		mergedFile.deleteOnExit();
		baseFile = writeTrashFile("baseFile.txt", "");
		baseFile.deleteOnExit();
		commandResult = writeTrashFile("commandResult.txt", "");
		commandResult.deleteOnExit();

		local = new FileElement(localFile.getAbsolutePath(),
				FileElement.Type.LOCAL);
		remote = new FileElement(remoteFile.getAbsolutePath(),
				FileElement.Type.REMOTE);
		merged = new FileElement(mergedFile.getAbsolutePath(),
				FileElement.Type.MERGED);
		base = new FileElement(baseFile.getAbsolutePath(),
				FileElement.Type.BASE);
	}

	@After
	@Override
	public void tearDown() throws Exception {
		Files.delete(localFile.toPath());
		Files.delete(remoteFile.toPath());
		Files.delete(mergedFile.toPath());
		Files.delete(baseFile.toPath());
		Files.delete(commandResult.toPath());

		super.tearDown();
	}


	protected static void assumePosixPlatform() {
		Assume.assumeTrue(
				"This test can run only in Linux tests",
				FS.DETECTED instanceof FS_POSIX);
	}

	protected static class PromptHandler implements PromptContinueHandler {

		private final boolean promptResult;

		final List<String> toolPrompts = new ArrayList<>();

		private PromptHandler(boolean promptResult) {
			this.promptResult = promptResult;
		}

		static PromptHandler acceptPrompt() {
			return new PromptHandler(true);
		}

		static PromptHandler cancelPrompt() {
			return new PromptHandler(false);
		}

		@Override
		public boolean prompt(String toolName) {
			toolPrompts.add(toolName);
			return promptResult;
		}
	}

	protected static class MissingToolHandler implements InformNoToolHandler {

		final List<String> missingTools = new ArrayList<>();

		@Override
		public void inform(List<String> toolNames) {
			missingTools.addAll(toolNames);
		}
	}
}