Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d332faeb09f5943af230f9b6d50e8f1e2fe9b36e (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
/*****************************************************************************
 * Copyright (c) 2015, 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.core.language;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Collection;

import org.eclipse.papyrus.infra.core.internal.language.LanguageModelRegistry;
import org.eclipse.papyrus.infra.core.resource.IModel;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.junit.utils.resources.EcoreModel;
import org.eclipse.papyrus.junit.utils.rules.ModelSetFixture;
import org.eclipse.papyrus.junit.utils.rules.PluginResource;
import org.eclipse.papyrus.junit.utils.rules.ServiceRegistryModelSetFixture;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

/**
 * Tests the Papyrus Language Service.
 */
@PluginResource({ "resources/My.ecore", "resources/My.genmodel" })
public class LanguageServiceTest {
	private static final String ECORE_LANGUAGE_ID = "org.eclipse.papyrus.infra.core.tests.language.ecore"; //$NON-NLS-1$
	private static final String ECORE_LANGUAGE_VERSION = "2.11"; //$NON-NLS-1$
	private static final String ECORE_LANGUAGE_NAME = "Ecore"; //$NON-NLS-1$

	private static final String GENMODEL_LANGUAGE_ID = "org.eclipse.papyrus.infra.core.tests.language.genmodel"; //$NON-NLS-1$
	private static final String GENMODEL_LANGUAGE_VERSION = "2.11.0.v20150518-0831"; //$NON-NLS-1$
	private static final String GENMODEL_LANGUAGE_NAME = "Genmodel"; //$NON-NLS-1$

	@ClassRule
	public static final ModelSetFixture modelSet = new ServiceRegistryModelSetFixture();

	private static IModel ecoreModel;

	public LanguageServiceTest() {
		super();
	}

	@Test
	public void contentTypeBasedLanguages() throws ServiceException {
		ILanguageService service = modelSet.requireService(ILanguageService.class);

		ILanguage ecore = null;
		ILanguage genmodel = null;
		for (ILanguage next : service.getLanguages(modelSet.getProject().getURI("My.ecore"), true)) {
			if (ECORE_LANGUAGE_ID.equals(next.getID())) {
				ecore = next;
			} else if (GENMODEL_LANGUAGE_ID.equals(next.getID())) {
				genmodel = next;
			}
		}

		assertThat(genmodel, nullValue()); // Asked only about the *.ecore resource

		assertThat(ecore, notNullValue());
		assertThat(ecore.getVersion(), is(new Version(ECORE_LANGUAGE_VERSION)));
		assertThat(ecore.getName(), is(ECORE_LANGUAGE_NAME));
	}

	@Test
	public void contentTypeBasedLanguages_uriWithoutExtension() throws ServiceException {
		ILanguageService service = modelSet.requireService(ILanguageService.class);

		ILanguage ecore = null;
		ILanguage genmodel = null;
		for (ILanguage next : service.getLanguages(modelSet.getProject().getURI("My"), false)) {
			if (ECORE_LANGUAGE_ID.equals(next.getID())) {
				ecore = next;
			} else if (GENMODEL_LANGUAGE_ID.equals(next.getID())) {
				genmodel = next;
			}
		}

		assertThat(ecore, notNullValue());
		assertThat(ecore.getVersion(), is(new Version(ECORE_LANGUAGE_VERSION)));
		assertThat(ecore.getName(), is(ECORE_LANGUAGE_NAME));

		assertThat(genmodel, notNullValue());
		assertThat(genmodel.getVersion(), is(new Version(GENMODEL_LANGUAGE_VERSION)));
		assertThat(genmodel.getName(), is(GENMODEL_LANGUAGE_NAME));
	}

	@Test
	public void languageBindings() {
		Collection<IModel> models = ILanguageService.getLanguageModels(modelSet.getResourceSet());

		assertThat(models, hasItem(ecoreModel));
	}

	//
	// Test framework
	//

	@BeforeClass
	public static void registerEcoreModel() {
		ecoreModel = new EcoreModel();
		LanguageModelRegistry.INSTANCE.bind(ECORE_LANGUAGE_ID, ecoreModel.getIdentifier());

		modelSet.getResourceSet().getInternal().registerModel(ecoreModel, false);
	}

	@AfterClass
	public static void unregisterEcoreModel() {
		LanguageModelRegistry.INSTANCE.bind(ECORE_LANGUAGE_ID, ecoreModel.getIdentifier());
		ecoreModel = null;
	}
}

Back to the top