Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Validator Testing with JUnit 5
Validator Testing with JUnit 5 [message #1838277] Sat, 20 February 2021 08:21 Go to next message
Oli Ana is currently offline Oli AnaFriend
Messages: 3
Registered: February 2021
Junior Member
Hi all,

I can't figure out how to test the custom validator developed for my grammar using Junit 5. At runtime the error is triggered, but when testing everything seems ok, no error is fired.

I've read the documentation (web and book), looked carefully at every example in eclipse, but I didn't have any success.

This is a simplified version of the code.
The grammar:
grammar org.xtext.example.mydsl.Mydsl with org.eclipse.xtext.common.Terminals

generate mydsl ...

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID '!';


The custom validator:
package org.xtext.example.mydsl.validation;

import org.eclipse.xtext.validation.Check;
import org.xtext.example.mydsl.mydsl.MydslPackage;
import org.xtext.example.mydsl.mydsl.Greeting;

public class MydslValidator extends AbstractMydslValidator {
	
	public static final String INVALID_NAME = "invalidName";

	@Check
	public void checkGreetingStartsWithCapital(Greeting greeting) {
		if (!Character.isUpperCase(greeting.getName().charAt(0))) {
			warning("Name should start with a capital",
					MydslPackage.Literals.GREETING__NAME,
					INVALID_NAME);
		}
	}
	
}


and the tests
package org.xtext.example.mydsl.tests

import com.google.inject.Inject
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.extensions.InjectionExtension
import org.eclipse.xtext.testing.util.ParseHelper
import org.eclipse.xtext.testing.validation.ValidationTestHelper
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.^extension.ExtendWith
import org.xtext.example.mydsl.mydsl.Model
import org.xtext.example.mydsl.mydsl.MydslPackage
import org.xtext.example.mydsl.validation.MydslValidator

@ExtendWith(InjectionExtension)
@InjectWith(MydslInjectorProvider)
class MydslValidationTest {
	@Inject extension
	ParseHelper<Model> 
	@Inject extension ValidationTestHelper
	
	@Test
	def void greetingStartsWithCapitalTest(){
		val result = '''
			Hello xtext!
		'''.parse
		result.assertWarning(MydslPackage.Literals.GREETING, "Name should start with a capital", MydslValidator.INVALID_NAME)
	}
}


The result:
Expected WARNING 'Name should start with a capital' on Greeting at [-1:-1] but got


if I replace
result.assertWarning(MydslPackage.Literals.GREETING, "Name should start with a capital", MydslValidator.INVALID_NAME)


with
result.assertNoErrors()


The test succeeds.

If I introduce any syntax error in the model to be tested, an error is fired.

I guess my validator isn't taken into account while testing. What am I missing?

Any help would be appreciated.
Thanks in advance.
Re: Validator Testing with JUnit 5 [message #1838292 is a reply to message #1838277] Sat, 20 February 2021 17:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13888
Registered: July 2009
Senior Member
this is a regression in Xtext 2.24.
if you use 2.25 M1 or M2 it should be fixed
https://github.com/eclipse/xtext-core/issues/1648


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: Validator Testing with JUnit 5 [message #1838313 is a reply to message #1838292] Mon, 22 February 2021 08:35 Go to previous messageGo to next message
Oli Ana is currently offline Oli AnaFriend
Messages: 3
Registered: February 2021
Junior Member
Thanks for the quick response.

However, I can't manage to install either of the milestones...
An error occurred while collecting items to be installed
session context was:(profile=C__Users_xxx_eclipse_dsl-2020-12_eclipse, phase=org.eclipse.equinox.internal.p2.engine.phases.Collect, operand=, action=).
Artifact not found: osgi.bundle,org.eclipse.xtend.core,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.doc,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.examples,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.ide,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.ide.common,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.lib,2.25.0.v20210203-0750.
Artifact not found: osgi.bundle,org.eclipse.xtend.lib.macro,2.25.0.v20210203-0750.
Artifact not found: osgi.bundle,org.eclipse.xtend.m2e,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.standalone,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtext,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.activities,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.builder,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.builder.standalone,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.buildship,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types.edit,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types.shared,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types.shared.jdt38,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.doc,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ecore,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.generator,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.ide,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.java,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.junit4,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.logging,1.2.15.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.m2e,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.purexbase,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.purexbase.ide,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.purexbase.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.smap,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.testing,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.codemining,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.codetemplates,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.codetemplates.ide,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.codetemplates.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.ecore,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.shared,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.testing,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.util,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.ide,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.junit,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.lib,2.25.0.v20210203-0750.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.testing,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.ui.testing,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.generator,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.ide,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.ui.examples,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.ui.graph,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.wizard,2.25.0.v20210203-0753.


[Updated on: Mon, 22 February 2021 10:02]

Report message to a moderator

Re: Validator Testing with JUnit 5 [message #1838317 is a reply to message #1838313] Mon, 22 February 2021 10:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13888
Registered: July 2009
Senior Member
please note: we are publishing a new milestone right now so the webservers might not deliver complete data right now.
can you try this repo: http://www.eclipse.org/modeling/download.php?file=/modeling/tmf/xtext/downloads/drops/2.25.0/S202102220934/tmf-xtext-Update-2.25.0.M3.zip


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: Validator Testing with JUnit 5 [message #1838318 is a reply to message #1838317] Mon, 22 February 2021 10:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13888
Registered: July 2009
Senior Member
you may also comment
https://bugs.eclipse.org/bugs/show_bug.cgi?id=571058


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: Validator Testing with JUnit 5 [message #1838320 is a reply to message #1838318] Mon, 22 February 2021 10:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13888
Registered: July 2009
Senior Member
@Oli Ana do you have any malware detection (e.g. traps) on your machine? which os do you use?

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: Validator Testing with JUnit 5 [message #1838321 is a reply to message #1838320] Mon, 22 February 2021 11:05 Go to previous message
Oli Ana is currently offline Oli AnaFriend
Messages: 3
Registered: February 2021
Junior Member
I succeeded installing M2 by changing the eclipse.ini file to use C:\Program Files\AdoptOpenJDK\jdk-11.0.10.9-hotspot\bin instead of
C:/Users/xxx/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.1.v20201027-0507/jre/bin as stated here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=571058

My validator is loaded for testing now.

Thank you very much for the responses.





Previous Topic:Rename fails if cursor is behind identifier
Next Topic:Scoping / Linking
Goto Forum:
  


Current Time: Thu Apr 22 04:08:58 GMT 2021

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

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

Back to the top