001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation 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: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.jface.conformance.databinding;
011:
012: import java.util.Arrays;
013:
014: import org.eclipse.core.databinding.observable.IObservableCollection;
015: import org.eclipse.jface.tests.databinding.RealmTester;
016: import org.eclipse.jface.tests.databinding.RealmTester.CurrentRealm;
017:
018: /**
019: * Tests for IObservableCollection that don't mutate the collection.
020: * <p>
021: * This class is experimental and can change at any time. It is recommended to
022: * not subclass or assume the test names will not change. The only API that is
023: * guaranteed to not change are the constructors. The tests will remain public
024: * and not final in order to allow for consumers to turn off a test if needed by
025: * subclassing.
026: * </p>
027: *
028: * @since 3.2
029: */
030: public class ObservableCollectionContractTest extends
031: ObservableContractTest {
032: private IObservableCollectionContractDelegate delegate;
033:
034: private IObservableCollection collection;
035:
036: public ObservableCollectionContractTest(
037: IObservableCollectionContractDelegate delegate) {
038: super (delegate);
039: this .delegate = delegate;
040: }
041:
042: public ObservableCollectionContractTest(String testName,
043: IObservableCollectionContractDelegate delegate) {
044: super (testName, delegate);
045: this .delegate = delegate;
046: }
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050:
051: collection = (IObservableCollection) getObservable();
052: }
053:
054: public void testIterator_GetterCalled() throws Exception {
055: assertGetterCalled(new Runnable() {
056: public void run() {
057: collection.iterator();
058: }
059: }, "Collection.iterator()", collection);
060: }
061:
062: public void testIterator_RealmCheck() throws Exception {
063: RealmTester.exerciseCurrent(new Runnable() {
064: public void run() {
065: collection.iterator();
066: }
067: }, (CurrentRealm) collection.getRealm());
068: }
069:
070: public void testSize_GetterCalled() throws Exception {
071: assertGetterCalled(new Runnable() {
072: public void run() {
073: collection.size();
074: }
075: }, "Collection.size()", collection);
076: }
077:
078: public void testSize_RealmCheck() throws Exception {
079: RealmTester.exerciseCurrent(new Runnable() {
080: public void run() {
081: collection.size();
082: }
083: }, (CurrentRealm) collection.getRealm());
084: }
085:
086: public void testIsEmpty_GetterCalled() throws Exception {
087: assertGetterCalled(new Runnable() {
088: public void run() {
089: collection.isEmpty();
090: }
091: }, "Collection.isEmpty()", collection);
092: }
093:
094: public void testIsEmpty_RealmCheck() throws Exception {
095: RealmTester.exerciseCurrent(new Runnable() {
096: public void run() {
097: collection.isEmpty();
098: }
099: }, (CurrentRealm) collection.getRealm());
100: }
101:
102: public void testContains_GetterCalled() throws Exception {
103: assertGetterCalled(new Runnable() {
104: public void run() {
105: collection.contains(delegate.createElement(collection));
106: }
107: }, "Collection.contains(...)", collection);
108: }
109:
110: public void testContains_RealmCheck() throws Exception {
111: RealmTester.exerciseCurrent(new Runnable() {
112: public void run() {
113: collection.contains(delegate.createElement(collection));
114: }
115: }, (CurrentRealm) collection.getRealm());
116: }
117:
118: public void testContainsAll_GetterCalled() throws Exception {
119: assertGetterCalled(new Runnable() {
120: public void run() {
121: collection.containsAll(Arrays
122: .asList(new Object[] { delegate
123: .createElement(collection) }));
124: }
125: }, "Collection.containsAll(Collection)", collection);
126: }
127:
128: public void testContainsAll_RealmCheck() throws Exception {
129: RealmTester.exerciseCurrent(new Runnable() {
130: public void run() {
131: collection.containsAll(Arrays
132: .asList(new Object[] { delegate
133: .createElement(collection) }));
134: }
135: }, (CurrentRealm) collection.getRealm());
136: }
137:
138: public void testToArray_GetterCalled() throws Exception {
139: assertGetterCalled(new Runnable() {
140: public void run() {
141: collection.toArray();
142: }
143: }, "Collection.toArray()", collection);
144: }
145:
146: public void testToArray_RealmCheck() throws Exception {
147: RealmTester.exerciseCurrent(new Runnable() {
148: public void run() {
149: collection.toArray();
150: }
151: }, (CurrentRealm) collection.getRealm());
152: }
153:
154: public void testToArrayWithObjectArray_GetterCalled()
155: throws Exception {
156: assertGetterCalled(new Runnable() {
157: public void run() {
158: collection.toArray(new Object[collection.size()]);
159: }
160: }, "Collection.toArray(Object[])", collection);
161: }
162:
163: public void testToArrayWithObjectArray_RealmCheck()
164: throws Exception {
165: RealmTester.exerciseCurrent(new Runnable() {
166: public void run() {
167: collection.toArray(new Object[collection.size()]);
168: }
169: }, (CurrentRealm) collection.getRealm());
170: }
171:
172: public void testEquals_GetterCalled() throws Exception {
173: assertGetterCalled(new Runnable() {
174: public void run() {
175: collection.equals(collection);
176: }
177: }, "Collection.equals(Object)", collection);
178: }
179:
180: public void testEquals_RealmCheck() throws Exception {
181: RealmTester.exerciseCurrent(new Runnable() {
182: public void run() {
183: collection.equals(collection);
184: }
185: }, (CurrentRealm) collection.getRealm());
186: }
187:
188: public void testHashCode_GetterCalled() throws Exception {
189: assertGetterCalled(new Runnable() {
190: public void run() {
191: collection.hashCode();
192: }
193: }, "Collection.hashCode()", collection);
194: }
195:
196: public void testHashCode_RealmCheck() throws Exception {
197: RealmTester.exerciseCurrent(new Runnable() {
198: public void run() {
199: collection.hashCode();
200: }
201: }, (CurrentRealm) collection.getRealm());
202: }
203:
204: public void testGetElementType_ReturnsType() throws Exception {
205: assertEquals(
206: "Element type of the collection should be returned from IObservableCollection.getElementType()",
207: delegate.getElementType(collection), collection
208: .getElementType());
209: }
210: }
|