001: /*******************************************************************************
002: * Copyright (c) 2006 Brad Reynolds and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Brad Reynolds - initial API and implementation
010: ******************************************************************************/package org.eclipse.jface.tests.databinding;
011:
012: import junit.framework.Assert;
013:
014: import org.eclipse.core.databinding.observable.Realm;
015: import org.eclipse.core.runtime.AssertionFailedException;
016:
017: /**
018: * Aids in the testing of Realms.
019: *
020: * @since 3.2
021: */
022: public class RealmTester {
023:
024: /**
025: * Sets the default realm without using Realm.runWithDefault() for testing
026: * purposes.
027: *
028: * @param realm
029: */
030: public static void setDefault(Realm realm) {
031: CurrentRealm.setDefault(realm);
032: }
033:
034: /**
035: * Runs the provided <code>runnable</code> when the realm is both current
036: * and not current. It checks for AssertionFailedExceptions and if an
037: * exception occurs or doesn't occur as expected the test fails. The realm
038: * of an observable created before the method was invoked must be of type
039: * {@link CurrentRealm}. The default realm during the runnable invocation
040: * is set to an instance of {@link CurrentRealm} when the runnable is
041: * invoked.
042: *
043: * @param runnable
044: */
045: public static void exerciseCurrent(Runnable runnable) {
046: CurrentRealm previousRealm = (CurrentRealm) Realm.getDefault();
047: CurrentRealm realm = new CurrentRealm();
048: setDefault(realm);
049:
050: try {
051: realm.setCurrent(true);
052: if (previousRealm != null) {
053: previousRealm.setCurrent(true);
054: }
055:
056: try {
057: runnable.run();
058: } catch (AssertionFailedException e) {
059: Assert
060: .fail("Correct realm, exception should not have been thrown");
061: }
062:
063: realm.setCurrent(false);
064: if (previousRealm != null) {
065: previousRealm.setCurrent(false);
066: }
067:
068: try {
069: runnable.run();
070: Assert
071: .fail("Incorrect realm, exception should have been thrown");
072: } catch (AssertionFailedException e) {
073: }
074: } finally {
075: setDefault(previousRealm);
076: }
077: }
078:
079: /**
080: * Runs the provided <code>runnable</code> when the realm is both current
081: * and not current. It checks for AssertionFailedExceptions and if an
082: * exception occurs or doesn't occur as expected the test fails.
083: *
084: * @param runnable
085: * @param realm
086: */
087: public static void exerciseCurrent(Runnable runnable,
088: CurrentRealm realm) {
089: realm.setCurrent(true);
090:
091: try {
092: runnable.run();
093: } catch (AssertionFailedException e) {
094: Assert
095: .fail("Correct realm, exception should not have been thrown");
096: }
097:
098: realm.setCurrent(false);
099:
100: try {
101: runnable.run();
102: Assert
103: .fail("Incorrect realm, exception should have been thrown");
104: } catch (AssertionFailedException e) {
105: }
106: }
107:
108: /**
109: * Realm that will delegate to another for all operations except calls to
110: * {@link #isCurrent()}. The current status can be set by the consumer to
111: * enable testing of realm checks.
112: *
113: * @since 3.2
114: */
115: public static class DelegatingRealm extends CurrentRealm {
116: private Realm realm;
117:
118: public DelegatingRealm(Realm realm) {
119: this .realm = realm;
120: }
121:
122: protected void syncExec(Runnable runnable) {
123: realm.exec(runnable);
124: }
125:
126: public void asyncExec(Runnable runnable) {
127: realm.asyncExec(runnable);
128: }
129: }
130:
131: /**
132: * Allows for the toggling of the current status of the realm. The
133: * asyncExec(...) implementations do nothing.
134: *
135: * @since 3.2
136: */
137: public static class CurrentRealm extends Realm {
138: private boolean current;
139:
140: public CurrentRealm() {
141: this (false);
142: }
143:
144: public CurrentRealm(boolean current) {
145: this .current = current;
146: }
147:
148: public boolean isCurrent() {
149: return current;
150: }
151:
152: public void setCurrent(boolean current) {
153: this .current = current;
154: }
155:
156: protected void syncExec(Runnable runnable) {
157: super .syncExec(runnable);
158: }
159:
160: public void asyncExec(Runnable runnable) {
161: throw new UnsupportedOperationException(
162: "CurrentRealm does not support asyncExec(Runnable).");
163: }
164:
165: protected static Realm setDefault(Realm realm) {
166: return Realm.setDefault(realm);
167: }
168: }
169: }
|