01: /*******************************************************************************
02: * Copyright (c) 2007 Brad Reynolds and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Brad Reynolds - initial API and implementation
10: ******************************************************************************/package org.eclipse.core.tests.internal.databinding.observable.masterdetail;
11:
12: import java.util.ArrayList;
13: import java.util.Arrays;
14:
15: import org.eclipse.core.databinding.observable.IObservable;
16: import org.eclipse.core.databinding.observable.list.WritableList;
17: import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
18: import org.eclipse.core.databinding.observable.value.WritableValue;
19: import org.eclipse.core.internal.databinding.observable.masterdetail.DetailObservableList;
20: import org.eclipse.core.runtime.AssertionFailedException;
21: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
22:
23: /**
24: * @since 3.2
25: */
26: public class DetailObservableListTest extends
27: AbstractDefaultRealmTestCase {
28: /**
29: * Asserts the use case of specifying null on construction for the detail
30: * type of the detail list.
31: *
32: * @throws Exception
33: */
34: public void testElementTypeNull() throws Exception {
35: WritableValue observableValue = new WritableValue(
36: new WritableList(new ArrayList(), Object.class), null);
37:
38: class Factory implements IObservableFactory {
39: Object type = Object.class;
40:
41: public IObservable createObservable(Object target) {
42: return new WritableList(new ArrayList(), type);
43: }
44: }
45:
46: Factory factory = new Factory();
47: DetailObservableList detailObservable = new DetailObservableList(
48: factory, observableValue, null);
49: assertNull(detailObservable.getElementType());
50:
51: //change the type returned from the factory
52: factory.type = String.class;
53: observableValue.setValue(new WritableList(new ArrayList(),
54: String.class));
55: assertNull("element type not null", detailObservable
56: .getElementType());
57: }
58:
59: /**
60: * Asserts that you can't change the type across multiple inner observables.
61: *
62: * @throws Exception
63: */
64: public void testElementTypeNotNull() throws Exception {
65: WritableValue observableValue = new WritableValue(
66: new WritableList(new ArrayList(), Object.class), null);
67:
68: class Factory implements IObservableFactory {
69: Object type = Object.class;
70:
71: public IObservable createObservable(Object target) {
72: return new WritableList(new ArrayList(), type);
73: }
74: }
75:
76: Factory factory = new Factory();
77: DetailObservableList detailObservable = new DetailObservableList(
78: factory, observableValue, Object.class);
79: assertEquals(factory.type, detailObservable.getElementType());
80:
81: try {
82: factory.type = String.class;
83: observableValue.setValue(new WritableList(Arrays
84: .asList(new Object[] { new Object() }),
85: String.class));
86: fail("if an element type is set this cannot be changed");
87: } catch (AssertionFailedException e) {
88: }
89: }
90: }
|