1 /*******************************************************************************
2  * Copyright (c) 2006, 2014 Brad Reynolds and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Brad Reynolds - initial API and implementation
13  *     Simon Scholz <simon.scholz@vogella.com> - Bug 444829
14  ******************************************************************************/
15 
16 package org.eclipse.jface.databinding.conformance.util;
17 
18 import org.eclipse.core.databinding.observable.Realm;
19 import org.eclipse.core.runtime.AssertionFailedException;
20 import org.junit.Assert;
21 
22 /**
23  * Aids in the testing of Realms.
24  *
25  * @since 3.2
26  */
27 public class RealmTester {
28 
29 	/**
30 	 * Sets the default realm without using Realm.runWithDefault() for testing
31 	 * purposes.
32 	 *
33 	 * @param realm
34 	 */
setDefault(Realm realm)35 	public static void setDefault(Realm realm) {
36 		CurrentRealm.setDefault(realm);
37 	}
38 
39 	/**
40 	 * Runs the provided <code>runnable</code> when the realm is both current
41 	 * and not current. It checks for AssertionFailedExceptions and if an
42 	 * exception occurs or doesn't occur as expected the test fails. The realm
43 	 * of an observable created before the method was invoked must be of type
44 	 * {@link CurrentRealm}. The default realm during the runnable invocation is
45 	 * set to an instance of {@link CurrentRealm} when the runnable is invoked.
46 	 *
47 	 * @param runnable
48 	 */
exerciseCurrent(Runnable runnable)49 	public static void exerciseCurrent(Runnable runnable) {
50 		CurrentRealm previousRealm = (CurrentRealm) Realm.getDefault();
51 		CurrentRealm realm = new CurrentRealm();
52 		setDefault(realm);
53 
54 		try {
55 			realm.setCurrent(true);
56 			if (previousRealm != null) {
57 				previousRealm.setCurrent(true);
58 			}
59 
60 			try {
61 				runnable.run();
62 			} catch (AssertionFailedException e) {
63 				Assert.fail("Correct realm, exception should not have been thrown");
64 			}
65 
66 			realm.setCurrent(false);
67 			if (previousRealm != null) {
68 				previousRealm.setCurrent(false);
69 			}
70 
71 			try {
72 				runnable.run();
73 				Assert.fail("Incorrect realm, exception should have been thrown");
74 			} catch (AssertionFailedException e) {
75 			}
76 		} finally {
77 			setDefault(previousRealm);
78 		}
79 	}
80 
81 	/**
82 	 * Runs the provided <code>runnable</code> when the realm is both current
83 	 * and not current. It checks for AssertionFailedExceptions and if an
84 	 * exception occurs or doesn't occur as expected the test fails.
85 	 *
86 	 * @param runnable
87 	 * @param realm
88 	 */
exerciseCurrent(Runnable runnable, CurrentRealm realm)89 	public static void exerciseCurrent(Runnable runnable, CurrentRealm realm) {
90 		realm.setCurrent(true);
91 
92 		try {
93 			runnable.run();
94 		} catch (AssertionFailedException e) {
95 			Assert.fail("Correct realm, exception should not have been thrown");
96 		}
97 
98 		realm.setCurrent(false);
99 
100 		try {
101 			runnable.run();
102 			Assert.fail("Incorrect realm, exception should have been thrown");
103 		} catch (AssertionFailedException e) {
104 		}
105 	}
106 }
107