Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 60d283aaf5c13e80f760f1402f90a33f6590fb42 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * 
 * 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.uml.search.ui.tests;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Arrays;
import java.util.regex.Pattern;

import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.cdo.core.tests.AbstractPapyrusCDOTest;
import org.eclipse.papyrus.views.search.results.AbstractResultEntry;
import org.eclipse.papyrus.views.search.results.AttributeMatch;
import org.eclipse.papyrus.views.search.results.ModelElementMatch;
import org.eclipse.search.ui.ISearchResult;
import org.eclipse.search.ui.text.AbstractTextSearchResult;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.NamedElement;
import org.junit.Before;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

/**
 * This is the AbstractPapyrusCDOSearchTest type. Enjoy.
 */
public abstract class AbstractPapyrusCDOSearchTest extends AbstractPapyrusCDOTest {

	public static final String PLUGIN_ID = "org.eclipse.papyrus.cdo.uml.search.ui.tests";

	private IWorkbenchPage page;

	public AbstractPapyrusCDOSearchTest() {
		super();
	}

	@Before
	public void initWorkbench() throws Exception {
		page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

		// ensure the Papyrus perspective
		IPerspectiveDescriptor perspective = page.getWorkbenchWindow().getWorkbench().getPerspectiveRegistry().findPerspectiveWithId("org.eclipse.papyrus.infra.core.perspective");
		if(!perspective.getId().equals(page.getPerspective().getId())) {
			page.setPerspective(perspective);
		}

		// minimize the Welcome view
		for(IViewReference next : page.getViewReferences()) {
			if("org.eclipse.ui.internal.introview".equals(next.getId())) {
				page.setPartState(next, IWorkbenchPage.STATE_MINIMIZED);
				break;
			}
		}

		// bring the Repository Explorer forward
		@SuppressWarnings("restriction")
		IViewPart reposView = page.showView(org.eclipse.papyrus.cdo.internal.ui.views.ModelRepositoriesView.ID);
		page.activate(reposView);

		page.setEditorAreaVisible(true);

		flushDisplayEvents();
	}

	@Before
	public void importTestModel() throws Exception {

		CDOTransaction transaction = createTransaction();
		ResourceSet rset = transaction.getResourceSet();

		importModel(transaction, "garage", "GarageModel/garage");
		importModel(transaction, "j2ee.profile", "BugTracker/j2ee.profile");
		importModel(transaction, "tracker", "BugTracker/tracker");
		importModel(transaction, "admin", "AdminConsole/admin");

		getPapyrusRepository().commit(rset);
		getPapyrusRepository().close(rset);
	}

	private void importModel(CDOTransaction transaction, String name, String dstPath) {
		importResource(transaction, name + ".uml", dstPath + ".uml");
		importResource(transaction, name + ".notation", dstPath + ".notation");
		importResource(transaction, name + ".di", dstPath + ".di");
	}

	private Resource importResource(CDOTransaction transaction, String srcPath, String dstPath) {
		ResourceSet rset = transaction.getResourceSet();

		Resource xml = rset.getResource(URI.createPlatformPluginURI(PLUGIN_ID + "/resources/" + srcPath, true), true);

		EcoreUtil.resolveAll(xml);

		Resource result = transaction.getOrCreateResource(getResourcePath(dstPath));
		result.getContents().clear();
		result.getContents().addAll(xml.getContents());

		xml.unload();
		rset.getResources().remove(xml);

		return result;
	}

	protected IWorkbenchPage getWorkbenchPage() {
		return page;
	}

	protected void flushDisplayEvents() {
		while(Display.getCurrent().readAndDispatch()) {
			// pass
		}
	}

	protected void sleep(int seconds) {
		for(int i = 0; i < seconds; i++) {
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				// fine. So, we don't sleep so long
			}

			flushDisplayEvents();
		}
	}

	protected void assertMatched(ISearchResult searchResult, Predicate<? super EObject> predicate) {
		boolean found = false;

		AbstractTextSearchResult textResult = (AbstractTextSearchResult)searchResult;
		for(Object element : textResult.getElements()) {
			for(AbstractResultEntry next : Iterables.filter(Arrays.asList(textResult.getMatches(element)), AbstractResultEntry.class)) {
				if((next instanceof ModelElementMatch) || (next instanceof AttributeMatch)) {
					assertThat("Search result match incorrect.", predicate.apply((EObject)next.getSource()), is(true));
					found = true;
				}
			}
		}

		assertThat("No elements in search result matched.", found, is(true));
	}

	protected void assertMatchedAny(ISearchResult searchResult, Predicate<? super EObject> predicate) {
		boolean found = false;

		AbstractTextSearchResult textResult = (AbstractTextSearchResult)searchResult;
		for(Object element : textResult.getElements()) {
			for(AbstractResultEntry next : Iterables.filter(Arrays.asList(textResult.getMatches(element)), AbstractResultEntry.class)) {
				if((next instanceof ModelElementMatch) || (next instanceof AttributeMatch)) {
					if(predicate.apply((EObject)next.getSource())) {
						found = true;
						break;
					}
				}
			}
		}

		assertThat("No elements in search result matched.", found, is(true));
	}

	protected void assertNotMatched(ISearchResult searchResult, Predicate<? super EObject> predicate) {
		AbstractTextSearchResult textResult = (AbstractTextSearchResult)searchResult;
		for(Object element : textResult.getElements()) {
			for(AbstractResultEntry next : Iterables.filter(Arrays.asList(textResult.getMatches(element)), AbstractResultEntry.class)) {
				if((next instanceof ModelElementMatch) || (next instanceof AttributeMatch)) {
					assertThat("Search result match incorrect.", predicate.apply((EObject)next.getSource()), is(false));
				}
			}
		}
	}

	protected void assertNotMatchedAny(ISearchResult searchResult, Predicate<? super EObject> predicate) {
		boolean found = false;

		AbstractTextSearchResult textResult = (AbstractTextSearchResult)searchResult;
		for(Object element : textResult.getElements()) {
			for(AbstractResultEntry next : Iterables.filter(Arrays.asList(textResult.getMatches(element)), AbstractResultEntry.class)) {
				if((next instanceof ModelElementMatch) || (next instanceof AttributeMatch)) {
					if(!predicate.apply((EObject)next.getSource())) {
						found = true;
						break;
					}
				}
			}
		}

		assertThat("All elements in search result matched.", found, is(true));
	}

	protected Predicate<Object> type(final EClassifier type) {
		return new Predicate<Object>() {

			@Override
			public boolean apply(Object input) {
				return type.isInstance(input);
			}
		};
	}

	protected Predicate<EObject> name(final Pattern regex) {
		return new Predicate<EObject>() {

			@Override
			public boolean apply(EObject input) {
				boolean result = input instanceof NamedElement;

				if(result) {
					NamedElement element = (NamedElement)input;
					result = element.getName() != null && regex.matcher(element.getName()).matches();
				}

				return result;
			}
		};
	}

	protected Predicate<EObject> anyString(final Pattern regex) {
		return new Predicate<EObject>() {

			@Override
			public boolean apply(EObject input) {
				boolean result = false;

				for(EAttribute next : input.eClass().getEAllAttributes()) {
					if(next.getEType().getInstanceClass() == String.class) {
						Object value = input.eGet(next);
						String string = next.isMany() ? String.valueOf(value) : (String)value;

						if((string != null) && regex.matcher(string).matches()) {
							result = true;
							break;
						}
					}
				}

				return result;
			}
		};
	}
}

Back to the top