Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de3cb16e3857144cfc258314d689bfde9c59e16a (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*****************************************************************************
 * Copyright (c) 2016 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.emf.resource;

import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assume.assumeThat;

import org.eclipse.emf.common.util.URI;
import org.eclipse.papyrus.infra.emf.internal.resource.CrossReferenceIndex;
import org.eclipse.papyrus.junit.framework.classification.ClassificationRunner;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.SetMultimap;

/**
 * Tests for the {@link CrossReferenceIndex} class, the full indexer.
 */
@RunWith(Enclosed.class)
public abstract class CrossReferenceIndexTest extends AbstractCrossReferenceIndexTest {
	private final boolean shardsOnly;

	/**
	 * Initializes me.
	 */
	CrossReferenceIndexTest(boolean shardsOnly) {
		super(false); // Always the full indexer

		this.shardsOnly = shardsOnly;
	}

	//
	// Don't need to load any resources for these tests
	//

	@Test
	public void isShard() throws Exception {
		assertThat(index().isShard(uri("package1/packageA/foo.uml")), is(shardsOnly));
		assertThat(index().isShard(uri("package1/packageA.uml")), is(shardsOnly));
		assertThat(index().isShard(uri("package1.uml")), is(shardsOnly));
		assertThat(index().isShard(uri("root.uml")), is(false));
	}

	@Test
	public void subunits() throws Exception {
		assertThat(index().getSubunits(uri("package1/packageA/foo.uml"), shardsOnly), is(emptySet()));
		assertThat(index().getSubunits(uri("package1/packageA.uml"), shardsOnly), is(singleton(uri("package1/packageA/foo.uml"))));
		assertThat(index().getSubunits(uri("package1.uml"), shardsOnly), is(singleton(uri("package1/packageA.uml"))));
		assertThat(index().getSubunits(uri("root.uml"), shardsOnly), // This one has two sub-units
				is(ImmutableSet.of(uri("package1.uml"), uri("package2.uml"))));
	}

	@Test
	public void parents() throws Exception {
		assertThat(index().getParents(uri("package1/packageA/foo.uml"), shardsOnly), is(singleton(uri("package1/packageA.uml"))));
		assertThat(index().getParents(uri("package1/packageA.uml"), shardsOnly), is(singleton(uri("package1.uml"))));
		assertThat(index().getParents(uri("package1.uml"), shardsOnly), is(singleton(uri("root.uml"))));
		assertThat(index().getParents(uri("root.uml"), shardsOnly), is(emptySet()));
	}

	@Test
	public void roots() throws Exception {
		assertThat(index().getRoots(uri("package1/packageA/foo.uml"), shardsOnly), is(singleton(uri("root.uml"))));
		assertThat(index().getRoots(uri("package1/packageA.uml"), shardsOnly), is(singleton(uri("root.uml"))));
		assertThat(index().getRoots(uri("package1.uml"), shardsOnly), is(singleton(uri("root.uml"))));

		// A root has no parents and, therefore, no root
		assertThat(index().getRoots(uri("root.uml"), shardsOnly), is(emptySet()));

		// And this one has nothing to do with shards
		assertThat(index().getRoots(uri("referencing.uml"), shardsOnly), is(emptySet()));
	}

	@Test
	public void roots_alternate() throws Exception {
		// The alternate index is the on-demand index that only works with the
		// shard annotation, so it's not applicable in the context of sub-models
		assumeThat("Test not applicable to sub-models", shardsOnly, is(true));

		ICrossReferenceIndex index = index();
		ICrossReferenceIndex alternate = ICrossReferenceIndex.getAlternate(index, fixture);

		// Trigger re-indexing
		project.getFile(project.getURI("package1/packageA/foo.uml")).touch(null);

		assertThat(index.getRoots(uri("package1/packageA/foo.uml"), shardsOnly, alternate), is(singleton(uri("root.uml"))));
		assertThat(index.getRoots(uri("package1/packageA.uml"), shardsOnly, alternate), is(singleton(uri("root.uml"))));
		assertThat(index.getRoots(uri("package1.uml"), shardsOnly, alternate), is(singleton(uri("root.uml"))));

		// A root has no parents and, therefore, no root
		assertThat(index.getRoots(uri("root.uml"), shardsOnly, alternate), is(emptySet()));

		// And this one has nothing to do with shards
		assertThat(index.getRoots(uri("referencing.uml"), shardsOnly, alternate), is(emptySet()));
	}

	@Test
	public void outgoingReferences_givenURI() throws Exception {
		// Shard relationship (cross-resource containment) is not a cross-reference
		assertThat(index().getOutgoingCrossReferences(uri("package1.uml")), is(emptySet()));

		// We find cross-references to non-workspace resources, though those aren't indexed
		assertThat(index().getOutgoingCrossReferences(uri("root.uml")),
				is(singleton(URI.createURI(UMLResource.ECORE_PROFILE_URI))));

		// This has a cross-reference in the class generalization
		assertThat(index().getOutgoingCrossReferences(uri("referencing.uml")),
				is(singleton(uri("package2/packageB/bar.uml"))));

		// This API is generalized for the Papyrus one-file
		assertThat(index().getOutgoingCrossReferences(uri("referencing.di")),
				is(singleton(uri("package2/packageB/bar.di"))));
	}

	@Test
	public void incomingReferences_givenURI() throws Exception {
		// Parent pointer in shard annotation is not a cross-reference
		assertThat(index().getIncomingCrossReferences(uri("root.uml")), is(emptySet()));
		assertThat(index().getIncomingCrossReferences(uri("package1.uml")), is(emptySet()));

		// This has a cross-reference in the class generalization
		assertThat(index().getIncomingCrossReferences(uri("package2/packageB/bar.uml")),
				is(singleton(uri("referencing.uml"))));

		// This API is generalized for the Papyrus one-file
		assertThat(index().getIncomingCrossReferences(uri("package2/packageB/bar.di")),
				is(singleton(uri("referencing.di"))));
	}

	@Test
	public void outgoingReferences() throws Exception {
		SetMultimap<URI, URI> xrefs = index().getOutgoingCrossReferences();

		// Shard relationship (cross-resource containment) is not a cross-reference
		assertThat(xrefs.get(uri("package1.uml")), is(emptySet()));

		// We find cross-references to non-workspace resources, though those aren't indexed
		assertThat(xrefs.get(uri("root.uml")),
				is(singleton(URI.createURI(UMLResource.ECORE_PROFILE_URI))));

		// This has a cross-reference in the class generalization
		assertThat(xrefs.get(uri("referencing.uml")),
				is(singleton(uri("package2/packageB/bar.uml"))));

		// This API is *not* generalized for the Papyrus one-file
		assertThat(xrefs.get(uri("referencing.di")), is(emptySet()));
	}

	@Test
	public void incomingReferences() throws Exception {
		SetMultimap<URI, URI> xrefs = index().getIncomingCrossReferences();

		// Parent pointer in shard annotation is not a cross-reference
		assertThat(xrefs.get(uri("root.uml")), is(emptySet()));
		assertThat(xrefs.get(uri("package1.uml")), is(emptySet()));

		// This has a cross-reference in the class generalization
		assertThat(xrefs.get(uri("package2/packageB/bar.uml")),
				is(singleton(uri("referencing.uml"))));

		// This API is *not* generalized for the Papyrus one-file
		assertThat(xrefs.get(uri("package2/packageB/bar.di")), is(emptySet()));
	}

	//
	// Nested types
	//

	// Need to be explicit so that we don't inherit the Enclosed for a recursion error
	@RunWith(ClassificationRunner.class)
	public static class SubmodelsTest extends CrossReferenceIndexTest {
		public SubmodelsTest() {
			super(false);
		}

		@BeforeClass
		public static void createProjectContents() {
			createProjectContents("resources/submodels");
		}
	}

	// Need to be explicit so that we don't inherit the Enclosed for a recursion error
	@RunWith(ClassificationRunner.class)
	public static class ShardsTest extends CrossReferenceIndexTest {
		public ShardsTest() {
			super(true);
		}

		@BeforeClass
		public static void createProjectContents() {
			createProjectContents("resources/shards");
		}
	}
}

Back to the top