Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unit Test for a Language which cross-references an Ecore model
Unit Test for a Language which cross-references an Ecore model [message #1745193] Thu, 06 October 2016 08:58 Go to next message
Desmond Hume is currently offline Desmond HumeFriend
Messages: 9
Registered: October 2016
Junior Member
Dear all,

given a xtext grammar which references an ecore model such as the following works just fine when running Eclipse.

Model:
	'MyModel' myModel=[ecore::EPackage] '{' ... '}'
;


However, how do I unit test such a language? I came across some previous threads which discussed the testing of languages with cross-references to other xtext languages.

There you had to create a ResourceSet and pass it to the ParseHelper<>.parse method. Unfortunately, that does not work for Ecore resources because they cannot be casted to XtextResources and therefore "resource.contents.head.assertNoIssues" results in a ClassCastException: "org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1 cannot be cast to org.eclipse.xtext.resource.XtextResource".

Is there another way to create a ResourceSet with an Ecore model?

class LanguageParsingTest {

	@Inject extension ParseHelper<MyModel> 
	@Inject extension ValidationTestHelper

	@Inject
	Provider<XtextResourceSet> resourceSetProvider;
	
	@Test
	def void testParsing() {
		val fooPackage = EcoreFactory::eINSTANCE.createEPackage
		fooPackage.name = "foo"
		fooPackage.nsPrefix ="foo"
		fooPackage.nsURI = "http://foo"

		val resourceSet = resourceSetProvider.get
		val resource = resourceSet.createResource(URI.createURI("foo.ecore"))
		resource.contents.add(fooPackage)
		resource.save(null)

		resourceSet.getPackageRegistry().put(fooPackage.getNsURI(), fooPackage);
		resource.load(null);

		resource.contents.head.assertNoIssues

		val model = '''MyModel foo { }'''.parse(resourceSet)
		model.assertNotNull
		model.assertNoErrors	
	}
}


Thank you very much for your help!

[Updated on: Thu, 06 October 2016 17:59]

Report message to a moderator

Re: Unit Test for a Language which cross-references an Ecore model [message #1745229 is a reply to message #1745193] Thu, 06 October 2016 16:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont know of a "super duper easy one method make me happy way" that you might be looking for,
although one could surely optimize / restructure etc what you are doing.
(e.g. by having a parse method that takes both the ecore and the text)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unit Test for a Language which cross-references an Ecore model [message #1745230 is a reply to message #1745229] Thu, 06 October 2016 16:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
of regarding your error in the first place.
of course you could try to make places like org.eclipse.xtext.testing.validation.ValidationTestHelper.validate(Resource) and maybe others more robust.
can you elaborate where the classcast exception actually happens?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unit Test for a Language which cross-references an Ecore model [message #1745235 is a reply to message #1745230] Thu, 06 October 2016 18:48 Go to previous messageGo to next message
Desmond Hume is currently offline Desmond HumeFriend
Messages: 9
Registered: October 2016
Junior Member
Thanks for your reply Christian!

The exception is indeed thrown by ValidationTestHelper.validate(Resource) as the EMF resource is casted to a XtextResource.

However, I have to admit that I made a small mistake when explaining my problem. Please ignore the 'head.assertNoIssues line, .parse(resourceSet)' line for a second. The 'assertNoErrors' in the last line also invokes the ValidationTestHelper.validate method. The result is "[ERROR:Couldn't resolve reference to EPackage 'foo'." When debugging that error, it seems that it fails to lazy load the ecore resource even though I didn't figure out the reason why it fails.

That's why I was wondering if there is another way to create a ResourceSet with an Ecore model?
Re: Unit Test for a Language which cross-references an Ecore model [message #1745239 is a reply to message #1745235] Thu, 06 October 2016 19:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
do you have org.eclipse.xtext.ecore added to the test project. do you call ecoreSupport somewhere? eg.


@RunWith(XtextRunner)
@InjectWith(ExtendedMyDslInjectorProvider)
class DemoTest {

@Inject extension ParseHelper<Model>
@Inject extension ValidationTestHelper

@Inject
Provider<XtextResourceSet> resourceSetProvider;

@Test
def void testParsing() {
val fooPackage = EcoreFactory::eINSTANCE.createEPackage
fooPackage.name = "foo"
fooPackage.nsPrefix ="foo"
fooPackage.nsURI = "http://foo"
val resourceSet = resourceSetProvider.get
val resource = resourceSet.createResource(URI.createURI("foo.ecore"))
resource.contents.add(fooPackage)
val model = '''MyModel foo { }'''.parse(resourceSet)
model.assertNotNull
model.assertNoErrors
}

}

class ExtendedMyDslInjectorProvider extends MyDslInjectorProvider {

override protected internalCreateInjector() {
return new MyDslStandaloneSetup() {

override register(Injector injector) {
EcoreSupportStandaloneSetup.setup
super.register(injector)
}

}.createInjectorAndDoEMFRegistration();
}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unit Test for a Language which cross-references an Ecore model [message #1745343 is a reply to message #1745239] Fri, 07 October 2016 14:53 Go to previous message
Desmond Hume is currently offline Desmond HumeFriend
Messages: 9
Registered: October 2016
Junior Member
The EcoreSupport was missing. Thank you very much!
Previous Topic:error EXPAND
Next Topic:error expand
Goto Forum:
  


Current Time: Thu Apr 25 02:23:01 GMT 2024

Powered by FUDForum. Page generated in 0.03219 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top