View Javadoc
1   /*
2    * Copyright (C) 2020-2021, Simeon Andreev <simeon.danailov.andreev@gmail.com> 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.internal.diffmergetool;
11  
12  import java.io.File;
13  import java.nio.file.Files;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import org.eclipse.jgit.junit.RepositoryTestCase;
18  import org.eclipse.jgit.util.FS;
19  import org.eclipse.jgit.util.FS_POSIX;
20  import org.junit.After;
21  import org.junit.Assume;
22  import org.junit.Before;
23  
24  /**
25   * Base test case for external merge and diff tool tests.
26   */
27  public abstract class ExternalToolTestCase extends RepositoryTestCase {
28  
29  	protected static final String DEFAULT_CONTENT = "line1";
30  
31  	protected File localFile;
32  
33  	protected File remoteFile;
34  
35  	protected File mergedFile;
36  
37  	protected File baseFile;
38  
39  	protected File commandResult;
40  
41  	protected FileElement local;
42  
43  	protected FileElement remote;
44  
45  	protected FileElement merged;
46  
47  	protected FileElement base;
48  
49  	@Before
50  	@Override
51  	public void setUp() throws Exception {
52  		super.setUp();
53  
54  		localFile = writeTrashFile("localFile.txt", DEFAULT_CONTENT + "\n");
55  		localFile.deleteOnExit();
56  		remoteFile = writeTrashFile("remoteFile.txt", DEFAULT_CONTENT + "\n");
57  		remoteFile.deleteOnExit();
58  		mergedFile = writeTrashFile("mergedFile.txt", "");
59  		mergedFile.deleteOnExit();
60  		baseFile = writeTrashFile("baseFile.txt", "");
61  		baseFile.deleteOnExit();
62  		commandResult = writeTrashFile("commandResult.txt", "");
63  		commandResult.deleteOnExit();
64  
65  		local = new FileElement(localFile.getAbsolutePath(),
66  				FileElement.Type.LOCAL);
67  		remote = new FileElement(remoteFile.getAbsolutePath(),
68  				FileElement.Type.REMOTE);
69  		merged = new FileElement(mergedFile.getAbsolutePath(),
70  				FileElement.Type.MERGED);
71  		base = new FileElement(baseFile.getAbsolutePath(),
72  				FileElement.Type.BASE);
73  	}
74  
75  	@After
76  	@Override
77  	public void tearDown() throws Exception {
78  		Files.delete(localFile.toPath());
79  		Files.delete(remoteFile.toPath());
80  		Files.delete(mergedFile.toPath());
81  		Files.delete(baseFile.toPath());
82  		Files.delete(commandResult.toPath());
83  
84  		super.tearDown();
85  	}
86  
87  
88  	protected static void assumePosixPlatform() {
89  		Assume.assumeTrue(
90  				"This test can run only in Linux tests",
91  				FS.DETECTED instanceof FS_POSIX);
92  	}
93  
94  	protected static class PromptHandler implements PromptContinueHandler {
95  
96  		private final boolean promptResult;
97  
98  		final List<String> toolPrompts = new ArrayList<>();
99  
100 		private PromptHandler(boolean promptResult) {
101 			this.promptResult = promptResult;
102 		}
103 
104 		static PromptHandler acceptPrompt() {
105 			return new PromptHandler(true);
106 		}
107 
108 		static PromptHandler cancelPrompt() {
109 			return new PromptHandler(false);
110 		}
111 
112 		@Override
113 		public boolean prompt(String toolName) {
114 			toolPrompts.add(toolName);
115 			return promptResult;
116 		}
117 	}
118 
119 	protected static class MissingToolHandler implements InformNoToolHandler {
120 
121 		final List<String> missingTools = new ArrayList<>();
122 
123 		@Override
124 		public void inform(List<String> toolNames) {
125 			missingTools.addAll(toolNames);
126 		}
127 	}
128 }