Skip to main content
summaryrefslogtreecommitdiffstats
blob: c22f82de4e19b96b58801ddd65982ab7584e2c14 (plain) (blame)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2016 Obeo and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *     Philip Langer - support more flexible parameters of test methods
 *******************************************************************************/
package org.eclipse.emf.compare.ide.ui.tests.git.framework.internal.statements;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.GitCorePreferences;
import org.eclipse.emf.compare.ide.ui.tests.framework.EMFCompareTestConfiguration;
import org.eclipse.emf.compare.ide.ui.tests.framework.ResolutionStrategyID;
import org.eclipse.emf.compare.ide.ui.tests.git.framework.GitMergeStrategyID;
import org.eclipse.emf.compare.ide.ui.tests.git.framework.GitTestSupport;
import org.junit.runners.model.FrameworkMethod;

/**
 * This class handle all test related to git operations (merge, rebase, cherry-pick).
 * 
 * @author <a href="mailto:mathieu.cartaud@obeo.fr">Mathieu Cartaud</a>
 */
@SuppressWarnings("restriction")
public abstract class AbstractGitOperationStatement extends AbstractGitStatement {

	/** The path of the archive containing the repository. */
	protected final String path;

	/** The merge strategy used for the test. */
	private final GitMergeStrategyID mergeStrategy;

	/** The EGit preferences. */
	private final IEclipsePreferences eGitPreferences;

	/** The default merge strategy to use if none is provided by user. */
	private String defaultMergeStrategy = GitMergeStrategyID.MODEL_RECURSIVE.getValue();

	/**
	 * Constructor for Git comparison statements.
	 * 
	 * @param testObject
	 *            The test class
	 * @param test
	 *            The test method
	 * @param resolutionStrategy
	 *            The resolution strategy used for this test
	 * @param configuration
	 *            EMFCompare configuration for this test
	 * @param mergeStrategy
	 *            The merge strategy used for the test
	 * @param path
	 *            The path of the archive containing the repository
	 */
	public AbstractGitOperationStatement(Object testObject, FrameworkMethod test,
			ResolutionStrategyID resolutionStrategy, EMFCompareTestConfiguration configuration,
			GitMergeStrategyID mergeStrategy, String path) {
		super(testObject, test, resolutionStrategy, configuration);
		this.mergeStrategy = mergeStrategy;
		this.eGitPreferences = InstanceScope.INSTANCE.getNode(Activator.getPluginId());
		this.path = normalizePath(path);
	}

	@Override
	public void evaluate() throws Throwable {
		setEMFComparePreferences();
		String from = InternalGitTestSupport.normalizeBranch(getCheckoutedBranch());
		String to = InternalGitTestSupport.normalizeBranch(getOtherBranch());

		GitTestSupport gitTestsSupport = new GitTestSupport();
		try {
			gitTestsSupport.setup();
			gitTestsSupport.createRepositoryFromPath(test.getMethod().getDeclaringClass(), path);
			callGitOperation(gitTestsSupport, from, to);
			Object[] parameters = createParameters(test.getMethod(), gitTestsSupport);
			test.invokeExplosively(testObject, parameters);
		} finally {
			restoreEMFComparePreferences();
			gitTestsSupport.tearDown();
		}
	}

	/**
	 * Get the branch that will be used as local branch for the test. This method have to be sub-classed by
	 * clients to properly get the branch.
	 * 
	 * @return the local branch
	 */
	protected abstract String getCheckoutedBranch();

	/**
	 * Get the branch that will be used as remote branch for the test. This method have to be sub-classed by
	 * clients to properly get the branch..
	 * 
	 * @return the remote branch
	 */
	protected abstract String getOtherBranch();

	/**
	 * Call the Git operation used in for the test. This method have to be sub-classed by clients to call the
	 * correct operation.
	 * 
	 * @param gitTestsSupport
	 *            The support class that allow to perform Git operations
	 * @param from
	 *            The local branch
	 * @param to
	 *            The remote branch
	 * @throws Exception
	 *             Thrown if a Git operation have an issue during execution
	 */
	protected abstract void callGitOperation(GitTestSupport gitTestsSupport, String from, String to)
			throws Exception;

	@Override
	protected void setEMFComparePreferences() {
		super.setEMFComparePreferences();
		setMergeStrategyPreference();
	}

	@Override
	protected void restoreEMFComparePreferences() {
		super.restoreEMFComparePreferences();
		eGitPreferences.put(GitCorePreferences.core_preferredMergeStrategy, defaultMergeStrategy);
	}

	/**
	 * Set the merge strategy preference to run the test in the correct conditions.
	 */
	private void setMergeStrategyPreference() {
		defaultMergeStrategy = eGitPreferences.get(GitCorePreferences.core_preferredMergeStrategy,
				defaultMergeStrategy);
		eGitPreferences.put(GitCorePreferences.core_preferredMergeStrategy, mergeStrategy.getValue());
	}

}

Back to the top