Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 742d8a7f0d8fc88351341ca8400222eb9e95d508 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*****************************************************************************
 * Copyright (c) 2012, 2014 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 422257
 *  Christian W. Damus (CEA) - bug 434133
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.tests.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.ui.emf.providers.EMFContentProvider;
import org.eclipse.papyrus.infra.ui.emf.providers.strategy.SemanticEMFContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IAdaptableContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IHierarchicContentProvider;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
import org.eclipse.papyrus.uml.tools.tests.Activator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

//TODO : Test TreeToFlatContentProvider (Number of elements displayed, isValid...)
public class ContentProviderTest extends AbstractPapyrusTest {

	@Rule
	public final HouseKeeper houseKeeper = new HouseKeeper();

	private EObject testModel;

	private EClass Class1, Class2, Class3, Class4;

	private final int maxLoops = 20; //Avoid infinite recursion on infinite trees

	private EStructuralFeature singleValuedProperty, multiValuedProperty, enumProperty;

	@Before
	public void init() {
		try {
			testModel = EMFHelper.loadEMFModel(houseKeeper.createResourceSet(), URI.createPlatformPluginURI(Activator.PLUGIN_ID + "/resources/emf/TestModel.xmi", true));
		} catch (IOException ex) {
			Activator.log.error(ex);
		}

		assertNotNull("Cannot load the EMF Tests model", testModel);

		Class1 = getEClass("Class1");
		Class2 = getEClass("Class2");
		Class3 = getEClass("Class3");
		Class4 = getEClass("Class4");

		singleValuedProperty = getFeature("Class1", "singleValuedProperty");
		multiValuedProperty = getFeature("Class1", "multiValuedProperty");
		enumProperty = getFeature("Class2", "enum");

		assertNotNull(Class1);
		assertNotNull(Class2);
		assertNotNull(Class3);
		assertNotNull(Class4);

		assertNotNull(singleValuedProperty);
		assertNotNull(multiValuedProperty);
		assertNotNull(enumProperty);
	}

	@Test
	public void singleValuePropertyTest() {
		//The semantic content provider should return the whole model as a Tree
		SemanticEMFContentProvider provider = new SemanticEMFContentProvider(testModel, singleValuedProperty, new EObject[]{ testModel });
		Object[] roots = provider.getElements();
		assertNotNull(roots);
		assertEquals(1, roots.length);

		testTree(roots[0], provider, provider, maxLoops);
	}

	@Test
	public void singleValuePropertyGraphicalTest() {
		//The graphical content provider should only return the valid values for the selected feature
		EMFContentProvider provider = new EMFContentProvider(testModel, singleValuedProperty);

		//The semantic roots contain both the model and the metamodel, but the metamodel doesn't contain any valid value. It should not be displayed.
		Object[] roots = provider.getElements();
		assertNotNull(roots);
		assertEquals(1, roots.length);

		testTree(roots[0], provider, provider, maxLoops);

		//TODO : Test filters
	}

	@Test
	public void enumPropertyTest() {
		EMFContentProvider provider = new EMFContentProvider(testModel, enumProperty);
		Object[] elements = provider.getElements();
		assertNotNull(elements);
		assertEquals(3, elements.length); //It should only return the 3 valid enum literals

		//TODO Improve this test. Currently, the only implementation provides a flat provider, but a tree
		//provider might also be valid
	}

	private void testTree(Object current, IHierarchicContentProvider hierarchicProvider, IAdaptableContentProvider adaptableProvider, int maxLoops) {
		if(maxLoops <= 0) {
			//This is not necessarily an error. ContentProviders can be infinite
			//assertTrue("Infinite recursion", false);
			return;
		}
		maxLoops--;

		testElement(current, hierarchicProvider, adaptableProvider);

		for(Object containerElement : hierarchicProvider.getChildren(current)) {
			testTree(containerElement, hierarchicProvider, adaptableProvider, maxLoops);
		}
	}

	private boolean testElement(Object current, IHierarchicContentProvider hierarchicProvider, IAdaptableContentProvider adaptableProvider) {
		//current is an EMFFacet wrapper. Test the adapted value

		Object adaptedValue = adaptableProvider.getAdaptedValue(current);
		assertNotNull(adaptedValue);
		assertTrue(adaptedValue instanceof EObject);

		//Bug 434133: EMFFacet Wrapper should still be adaptable to EObject/EReference
		Assert.assertTrue(current instanceof IAdaptable);

		if(adaptedValue instanceof EReference) {
			EStructuralFeature.Setting setting = (EStructuralFeature.Setting)((IAdaptable)current).getAdapter(EStructuralFeature.Setting.class);
			Assert.assertNotNull(setting);
			Assert.assertEquals(adaptedValue, setting.getEStructuralFeature());
		} else if(adaptedValue instanceof EObject) {
			Assert.assertEquals(adaptedValue, ((IAdaptable)current).getAdapter(EObject.class));
		}
		//End of test for Bug 434133

		EObject eObject = (EObject)adaptedValue;

		boolean isValid = hierarchicProvider.isValidValue(current);

		//Only Class2 and Class4 elements are valid for this property
		assertEquals(isValid, eObject.eClass() == Class2 || eObject.eClass() == Class4);

		return isValid;
	}

	@Ignore("Not implemented yet")
	@Test
	public void multipleValuePropertyTest() {
		throw new UnsupportedOperationException("Not implemtend yet");
	}

	private EStructuralFeature getFeature(String className, String featureName) {
		EClass eClass = getEClass(className);
		return eClass.getEStructuralFeature(featureName);
	}

	private EPackage getEPackage() {
		return testModel.eClass().getEPackage();
	}

	private EClass getEClass(String className) {
		return (EClass)getEPackage().getEClassifier(className);
	}
}

Back to the top