Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problem: Validator/Generator cannot navigate through AST/Index/ResourceSet from Unit-Tests(Xtext 2.9.1 UnitTest with ResourceSet)
Problem: Validator/Generator cannot navigate through AST/Index/ResourceSet from Unit-Tests [message #1725978] Tue, 08 March 2016 19:11 Go to next message
Tobias L. is currently offline Tobias L.Friend
Messages: 24
Registered: July 2010
Junior Member
Hello *,

I would like to implement unit tests for mydsl. Specifically for the validator and the generators.

Both the Validator, and the generators query the index (ResourceSet) to objects in the AST - and find nothing. Why not? (Please see attached file).

In unit test I set up a ResourceSet of three models and can navigate through the AST also. However, the validator and the generators can not access the ResourceSet (or find anything).

Why is that?

The most important code snippets (for example ValidatorTest):

Unit-Test:
@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class MyDslValidatorTest {

	static val DATATYPES = '''
		datatypes {
			type Integer
			type String
			type Date
			type Timestamp
		}
	'''

//	static val CONFIGURATION = '''
//		configuration ConfigX {
//		}
//	'''

	@Inject extension ParseHelper<Model>
	@Inject extension ValidationTestHelper
	@Inject extension Provider<XtextResourceSet>

	var resourceSet = null as XtextResourceSet

	@Before
	def void setup() {
		resourceSet = get
		resourceSet.classpathURIContext = this
		
		DATATYPES.parse(URI.createURI("datatypes.mydsl"), resourceSet)

		// TESTCASE: no config should raise an error in MyDslValidator
		// val config = resourceSet.createResource(URI.createURI("config.mydsl"))
		// config.load(new StringInputStream(CONFIGURATION), emptyMap)
	}

	@Test
	def testCheckConfiguration() {
		val model = '''
			package foo.bar
			
			entity MyEntity
			{
				Id : Integer
				Name : String
				ValidFrom : Timestamp
			}
		'''.parse(URI.createURI("model.mydsl"), resourceSet)

		model.assertError(MyDslPackage.Literals.MODEL, 'ERR-CODE-0815')
	}
}


Validator:
	@Inject extension ResourceExtension

	@Check
	def checkConfiguration(Model it) {
		if (eResource.allConfigurations.nullOrEmpty) {
			error("no config? no cookies!", MyDslPackage.Literals.MODEL__PACKAGE, 'ERR-CODE-0815')
		}
	}


What's wrong in my example? *headscratch*
Please - help ;-)

Tobias
Re: Problem: Validator/Generator cannot navigate through AST/Index/ResourceSet from Unit-Tests [message #1725981 is a reply to message #1725978] Tue, 08 March 2016 19:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
Hi,

the problem is that validators silently catch errors.
if you catch for exceptions you will get

Caused by: java.lang.NullPointerException
	at org.eclipse.xtext.resource.impl.ResourceSetBasedResourceDescriptions.getResourceDescription(ResourceSetBasedResourceDescriptions.java:111)
	at org.xtext.example.mydsl.extensions.ResourceExtension.getAllResourceDescriptions(ResourceExtension.java:41)
	at org.xtext.example.mydsl.extensions.ResourceExtension.getAllObjectDescriptions(ResourceExtension.java:54)
	at org.xtext.example.mydsl.extensions.ResourceExtension.getAllConfigurations(ResourceExtension.java:63)
	at org.xtext.example.mydsl.validation.MyDslValidator.checkConfiguration(MyDslValidator.java:36)


so the problem is your impl of the resourceextensions

class ResourceExtension {

	@Inject ResourceDescriptionsProvider indexProvider
	@Inject IResourceServiceProvider.Registry registery

	private def IContainer.Manager getManager(Resource it) {
		registery.getResourceServiceProvider(URI).containerManager;
	}

	private def getAllResourceDescriptions(Resource it) {
		val index = indexProvider.getResourceDescriptions(it)
		val resourceDescByURI =  index.getResourceDescription(URI)
		manager.getVisibleContainers(resourceDescByURI, index).map[resourceDescriptions].flatten
	}


(alternatively call iresourcedescriptions.setContext manually)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Problem: Validator/Generator cannot navigate through AST/Index/ResourceSet from Unit-Tests [message #1725983 is a reply to message #1725981] Tue, 08 March 2016 20:10 Go to previous message
Tobias L. is currently offline Tobias L.Friend
Messages: 24
Registered: July 2010
Junior Member
Hello Christian,

thank you for your fast reply!

Your solution works fine!
...and Exception Handling in Validator is a good point.

Tobias
Previous Topic:[SOLVED] Resolve cross references from a different DSL during Maven build
Next Topic:Standalone command-line compiler
Goto Forum:
  


Current Time: Sat Sep 18 04:58:32 GMT 2021

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

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

Back to the top