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.Arrays;
13: import java.util.HashSet;
14:
15: import org.eclipse.core.databinding.observable.IObservable;
16: import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
17: import org.eclipse.core.databinding.observable.set.WritableSet;
18: import org.eclipse.core.databinding.observable.value.WritableValue;
19: import org.eclipse.core.internal.databinding.observable.masterdetail.DetailObservableSet;
20: import org.eclipse.core.runtime.AssertionFailedException;
21: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
22:
23: /**
24: * @since 3.2
25: *
26: */
27: public class DetailObservableSetTest extends
28: AbstractDefaultRealmTestCase {
29: /**
30: * Asserts the use case of specifying null on construction for the detail
31: * type of the detail list.
32: *
33: * @throws Exception
34: */
35: public void testElementTypeNull() throws Exception {
36: WritableValue observableValue = new WritableValue(
37: new WritableSet(new HashSet(), Object.class), null);
38:
39: class Factory implements IObservableFactory {
40: Object type;
41:
42: public IObservable createObservable(Object target) {
43: return new WritableSet(new HashSet(), type);
44: }
45: }
46:
47: Factory factory = new Factory();
48: DetailObservableSet detailObservable = new DetailObservableSet(
49: factory, observableValue, null);
50: assertNull(detailObservable.getElementType());
51:
52: factory.type = Object.class;
53: observableValue.setValue(new WritableSet(Arrays
54: .asList(new Object[] { new Object() }), String.class));
55: assertNull("element type not null", detailObservable
56: .getElementType());
57:
58: factory.type = String.class;
59: // set the value again to ensure that the observable doesn't update the
60: // element type with that of the new element type
61: observableValue.setValue(new WritableSet(Arrays
62: .asList(new String[] { "1" }), Object.class));
63: assertNull("element type not null", detailObservable
64: .getElementType());
65: }
66:
67: /**
68: * Asserts that you can't change the type across multiple inner observables.
69: *
70: * @throws Exception
71: */
72: public void testElementTypeNotNull() throws Exception {
73: WritableValue observableValue = new WritableValue(
74: new WritableSet(new HashSet(), Object.class), null);
75:
76: class Factory implements IObservableFactory {
77: Object type = Object.class;
78:
79: public IObservable createObservable(Object target) {
80: return new WritableSet(new HashSet(), type);
81: }
82: }
83:
84: Factory factory = new Factory();
85: DetailObservableSet detailObservable = new DetailObservableSet(
86: factory, observableValue, Object.class);
87: assertEquals(factory.type, detailObservable.getElementType());
88:
89: try {
90: factory.type = String.class;
91: observableValue.setValue(new WritableSet(Arrays
92: .asList(new Object[] { new Object() }),
93: String.class));
94: fail("if an element type is set this cannot be changed");
95: } catch (AssertionFailedException e) {
96: }
97: }
98: }
|