001: /*******************************************************************************
002: * Copyright (c) 2006, 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: * Brad Reynolds - bug 164653, 147515
011: *******************************************************************************/package org.eclipse.core.tests.databinding.observable.list;
012:
013: import java.util.ArrayList;
014: import java.util.Collections;
015:
016: import junit.framework.Test;
017: import junit.framework.TestCase;
018:
019: import org.eclipse.core.databinding.observable.IObservable;
020: import org.eclipse.core.databinding.observable.IObservableCollection;
021: import org.eclipse.core.databinding.observable.Realm;
022: import org.eclipse.core.databinding.observable.list.WritableList;
023: import org.eclipse.jface.conformance.databinding.AbstractObservableCollectionContractDelegate;
024: import org.eclipse.jface.conformance.databinding.MutableObservableListContractTest;
025: import org.eclipse.jface.conformance.databinding.ObservableListContractTest;
026: import org.eclipse.jface.conformance.databinding.SuiteBuilder;
027: import org.eclipse.jface.databinding.swt.SWTObservables;
028: import org.eclipse.jface.tests.databinding.RealmTester;
029: import org.eclipse.jface.tests.databinding.RealmTester.CurrentRealm;
030: import org.eclipse.swt.widgets.Display;
031:
032: /**
033: * @since 3.2
034: */
035: public class WritableListTest extends TestCase {
036: protected void tearDown() throws Exception {
037: RealmTester.setDefault(null);
038: }
039:
040: public void testSetRealmChecks() throws Exception {
041: RealmTester.exerciseCurrent(new Runnable() {
042: public void run() {
043: WritableList list = new WritableList();
044: list.add("");
045: list.set(0, "");
046: }
047: });
048: }
049:
050: public void testAddRealmChecks() throws Exception {
051: RealmTester.exerciseCurrent(new Runnable() {
052: public void run() {
053: WritableList list = new WritableList();
054: list.add("");
055: }
056: });
057: }
058:
059: public void testAddByIndexRealmChecks() throws Exception {
060: RealmTester.exerciseCurrent(new Runnable() {
061: public void run() {
062: WritableList list = new WritableList();
063: list.add(0, "");
064: }
065: });
066: }
067:
068: public void testAddAllRealmChecks() throws Exception {
069: RealmTester.exerciseCurrent(new Runnable() {
070: public void run() {
071: WritableList list = new WritableList();
072: list.addAll(Collections.EMPTY_LIST);
073: }
074: });
075: }
076:
077: public void testAddAllByIndexRealmChecks() throws Exception {
078: RealmTester.exerciseCurrent(new Runnable() {
079: public void run() {
080: WritableList list = new WritableList();
081: list.addAll(0, Collections.EMPTY_LIST);
082: }
083: });
084: }
085:
086: public void testRemoveRealmChecks() throws Exception {
087: RealmTester.setDefault(new CurrentRealm(true));
088: final WritableList list = new WritableList();
089: list.add("");
090: list.add("");
091:
092: RealmTester.exerciseCurrent(new Runnable() {
093: public void run() {
094: list.remove("");
095: }
096: });
097: RealmTester.setDefault(null);
098: }
099:
100: public void testRemoveByIndexRealmChecks() throws Exception {
101: RealmTester.setDefault(new CurrentRealm(true));
102: final WritableList list = new WritableList();
103: list.add("");
104: list.add("");
105:
106: RealmTester.exerciseCurrent(new Runnable() {
107: public void run() {
108: list.remove(list.size() - 1);
109: }
110: });
111:
112: RealmTester.setDefault(null);
113: }
114:
115: public void testRemoveAllRealmChecks() throws Exception {
116: RealmTester.exerciseCurrent(new Runnable() {
117: public void run() {
118: WritableList list = new WritableList();
119: list.removeAll(Collections.EMPTY_LIST);
120: }
121: });
122: }
123:
124: public void testRetainAllRealmChecks() throws Exception {
125: RealmTester.exerciseCurrent(new Runnable() {
126: public void run() {
127: WritableList list = new WritableList();
128: list.retainAll(Collections.EMPTY_LIST);
129: }
130: });
131: }
132:
133: public void testClearRealmChecks() throws Exception {
134: RealmTester.exerciseCurrent(new Runnable() {
135: public void run() {
136: WritableList list = new WritableList();
137: list.clear();
138: }
139: });
140: }
141:
142: public void testNullElementType() throws Exception {
143: RealmTester.setDefault(SWTObservables.getRealm(Display
144: .getDefault()));
145: WritableList writableList = new WritableList();
146: assertNull(writableList.getElementType());
147:
148: writableList = new WritableList(Realm.getDefault());
149: assertNull(writableList.getElementType());
150: }
151:
152: public void testWithElementType() throws Exception {
153: RealmTester.setDefault(SWTObservables.getRealm(Display
154: .getDefault()));
155:
156: Object elementType = String.class;
157: WritableList list = WritableList.withElementType(elementType);
158: assertNotNull(list);
159: assertEquals(Realm.getDefault(), list.getRealm());
160: assertEquals(elementType, list.getElementType());
161: }
162:
163: public static Test suite() {
164: Delegate delegate = new Delegate();
165: return new SuiteBuilder().addTests(WritableListTest.class)
166: .addObservableContractTest(
167: ObservableListContractTest.class, delegate)
168: .addObservableContractTest(
169: MutableObservableListContractTest.class,
170: delegate).build();
171: }
172:
173: /* package */static class Delegate extends
174: AbstractObservableCollectionContractDelegate {
175: public Object createElement(IObservableCollection collection) {
176: return String.valueOf(collection.size() + 1);
177: }
178:
179: public Object getElementType(IObservableCollection collection) {
180: return String.class;
181: }
182:
183: public IObservableCollection createObservableCollection(
184: Realm realm, final int itemCount) {
185: WritableList observable = new WritableList(realm,
186: new ArrayList(), String.class);
187:
188: for (int i = 0; i < itemCount; i++) {
189: observable.add(String.valueOf(i));
190: }
191:
192: return observable;
193: }
194:
195: public void change(IObservable observable) {
196: ((WritableList) observable).add("");
197: }
198: }
199: }
|