Skip to main content
summaryrefslogtreecommitdiffstats
blob: a905457db98aa0dde135bae3c5aee2bda31d7e23 (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
/*******************************************************************************
 * Copyright (c) 2015, 2017 EclipseSource Muenchen GmbH 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:
 *     Michael Borkowski - initial tests
 *     Philip Langer - bug 516524
 *******************************************************************************/
package org.eclipse.emf.compare.tests.utils;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.utils.MatchUtil;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.junit.Before;
import org.junit.Test;

@SuppressWarnings({"boxing", "nls" })
public class MatchUtilFeatureContainsTest {

	private static final int MOCK_FEATURE_ID = 1;

	List<EObject> featureList;

	InternalEObject value;

	InternalEObject container;

	EClass eClass;

	EStructuralFeature feature;

	@Before
	public void setUp() {
		EPackage ePackage = mock(EPackage.class);

		container = mock(InternalEObject.class);

		feature = mock(EStructuralFeature.class);
		when(feature.isMany()).thenReturn(true);

		eClass = mock(EClass.class);
		when(eClass.getEPackage()).thenReturn(ePackage);
		when(eClass.getFeatureID(feature)).thenReturn(MOCK_FEATURE_ID);

		when(container.eClass()).thenReturn(eClass);
		when(feature.getEContainingClass()).thenReturn(eClass);

		featureList = new LinkedList<EObject>();
		when(container.eGet(MOCK_FEATURE_ID, false, true)).thenReturn(featureList);

		value = mockInternalObject();
		featureList.add(value);
	}

	@Test
	public void test_SameObject() {
		assertTrue(MatchUtil.featureContains(container, feature, value));
	}

	@Test
	public void test_Proxy() {
		URI uri = URI.createFileURI("/my/path");

		InternalEObject proxyValue = mockInternalObject();
		when(proxyValue.eIsProxy()).thenReturn(true);
		when(proxyValue.eProxyURI()).thenReturn(uri);

		when(value.eIsProxy()).thenReturn(true);
		when(value.eProxyURI()).thenReturn(uri);

		assertTrue(MatchUtil.featureContains(container, feature, proxyValue));
	}

	@Test
	public void test_DifferentObject() {
		InternalEObject otherValue = mockInternalObject();

		assertFalse(MatchUtil.featureContains(container, feature, otherValue));
	}

	private InternalEObject mockInternalObject() {
		InternalEObject internalObject = mock(InternalEObject.class);
		when(internalObject.eClass()).thenReturn(eClass);
		return internalObject;
	}
}

Back to the top