Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f5570d83c1d358397116309ad8c6baf8d1e72fd6 (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
142
/*****************************************************************************
 * Copyright (c) 2018 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.junit.utils.rules;

import java.util.function.BiFunction;

import org.eclipse.jface.preference.IPreferenceStore;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
 * A JUnit test rule for ensuring some Eclipse Preference value for the duration
 * of a test.
 */
public final class PreferenceRule<T> implements TestRule {

	private final IPreferenceStore store;
	private final String key;
	private final T value;
	private T oldValue;

	private final BiFunction<IPreferenceStore, String, ? extends T> accessor;
	private final TriConsumer<IPreferenceStore, String, ? super T> mutator;

	/**
	 * Initializes me.
	 * 
	 * @param store
	 *            the preference store on which to operate
	 * @param key
	 *            the preference key on which to operate
	 * @param the
	 *            value to set in the preference
	 */
	private PreferenceRule(IPreferenceStore store, String key, T value,
			BiFunction<IPreferenceStore, String, ? extends T> accessor,
			TriConsumer<IPreferenceStore, String, ? super T> mutator) {

		super();

		this.store = store;
		this.key = key;
		this.value = value;
		this.accessor = accessor;
		this.mutator = mutator;
	}

	@Override
	public Statement apply(Statement base, Description description) {
		return new Statement() {

			@Override
			public void evaluate() throws Throwable {
				setPreference(store, key);

				try {
					base.evaluate();
				} finally {
					restorePreference(store, key);
				}
			}
		};
	}

	protected void setPreference(IPreferenceStore store, String key) {
		oldValue = accessor.apply(store, key);
		mutator.accept(store, key, value);
	}

	protected void restorePreference(IPreferenceStore store, String key) {
		mutator.accept(store, key, oldValue);
	}

	/**
	 * Create a boolean-valued preference rule.
	 * 
	 * @param store
	 *            the preference store
	 * @param key
	 *            the preference key
	 * @param value
	 *            the value to set for the duration of the test
	 * @return the preference rule
	 */
	public static final PreferenceRule<Boolean> create(IPreferenceStore store, String key, boolean value) {
		return new PreferenceRule<Boolean>(store, key, value,
				IPreferenceStore::getBoolean, IPreferenceStore::setValue);
	}

	/**
	 * Create a string-valued preference rule.
	 * 
	 * @param store
	 *            the preference store
	 * @param key
	 *            the preference key
	 * @param value
	 *            the value to set for the duration of the test
	 * @return the preference rule
	 */
	public static final PreferenceRule<String> create(IPreferenceStore store, String key, String value) {
		return new PreferenceRule<String>(store, key, value,
				IPreferenceStore::getString, IPreferenceStore::setValue);
	}

	/**
	 * Create an integer-valued preference rule.
	 * 
	 * @param store
	 *            the preference store
	 * @param key
	 *            the preference key
	 * @param value
	 *            the value to set for the duration of the test
	 * @return the preference rule
	 */
	public static final PreferenceRule<Integer> create(IPreferenceStore store, String key, int value) {
		return new PreferenceRule<Integer>(store, key, value,
				IPreferenceStore::getInt, IPreferenceStore::setValue);
	}

	//
	// Nested types
	//

	@FunctionalInterface
	private interface TriConsumer<T, U, V> {
		void accept(T t, U u, V v);
	}
}

Back to the top