Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unit Test for XAnnotation validation shows unresolved reference
Unit Test for XAnnotation validation shows unresolved reference [message #1734213] Mon, 06 June 2016 09:25 Go to next message
Marek Stankiewicz is currently offline Marek StankiewiczFriend
Messages: 11
Registered: June 2016
Junior Member
Hi All,

I am using XAnnotations in my small DSL. Here is relevant fragment:

JGActionBlock:
	annotations+=XAnnotation? 'action' name=ValidID '{' 
	imports=JGImports?
	locals=JGLocals?
	exports=JGExports?
	entityActions=JGEntityActions?
	mainpart=JGMainDeclaration
	events+=JGEventDeclaration*
	'}';


Sample usage:

@JYActionBlock (bussys1="SomeSystem")
action SelectObject {
   main {}
}


Java Annotation class JYActionBlock is delivered in the runtime library. All works fine in the Eclipse environment and annotation.annotationType gives:

JvmAnnotationType: eu.jgen.gen.open.mdl.annotations.JYActionBlock (visibility: PUBLIC, simpleName: JYActionBlock, identifier: eu.jgen.gen.open.mdl.annotations.JYActionBlock, deprecated: false) (abstract: true, static: false, final: false, packageName: eu.jgen.gen.open.mdl.annotations)


And it is as should be, but in unit test it does not and I am getting compilation problem

JvmVoid:  (eProxyURI: __synthetic0.mdl#|0)


I need to write unit test for the validation checking that @JYActionBlock can only be used for 'action' type of declarations. How to do it?

Thanks
Regards
Marek
Re: Unit Test for XAnnotation validation shows unresolved reference [message #1734243 is a reply to message #1734213] Mon, 06 June 2016 14:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
is the annotatio n on the classapth of the unit test?
what do you exactly do inside the unit test?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unit Test for XAnnotation validation shows unresolved reference [message #1734276 is a reply to message #1734243] Mon, 06 June 2016 17:03 Go to previous messageGo to next message
Marek Stankiewicz is currently offline Marek StankiewiczFriend
Messages: 11
Registered: June 2016
Junior Member
Hi Christian,

Thanks for looking into my problem.

Here is validation:

	/*
	 *  Checking action block annotation
	 */	
	@Check
	def checkActionBlockAnnotation(JGActionBlock actionBlock) {
		actionBlock.annotations.forEach[annotation |

 			println("checkActionBlockAnnotation=" + annotation.annotationType) 

			if(annotation.annotationType instanceof JYActionBlock) {
				val pair = annotation.elementValuePairs
				println(pair) 
			}
		]
	}



package eu.jgen.gen.open.mdl.tests

import com.google.inject.Inject
import eu.jgen.gen.open.mdl.jGModel.JGModel
import org.eclipse.xtext.generator.InMemoryFileSystemAccess
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.util.ParseHelper
import org.eclipse.xtext.junit4.validation.ValidationTestHelper
import org.eclipse.xtext.util.JavaVersion
import org.eclipse.xtext.xbase.compiler.InMemoryJavaCompiler
import org.eclipse.xtext.xbase.compiler.InMemoryJavaCompiler.Result
import org.eclipse.xtext.xbase.compiler.JavaSource
import org.eclipse.xtext.xbase.compiler.JvmModelGenerator
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(XtextRunner)
@InjectWith(JGModelInjectorProvider)
class JGModelAnnotation //extends AbstractXbaseEvaluationTest 
{

	@Inject extension ParseHelper<JGModel>
	@Inject extension ValidationTestHelper
//	@Inject InMemoryJavaCompiler javaCompiler
	@Inject JvmModelGenerator generator

	@Test
	def void testActionBlockWithAnnotation() {

//		val setup = new StandaloneSetup ()
//		  setup.setPlatformUri("/Users/Marek/git/stage-two/eu.jgen.gen.open.mdl.tests");
//		 
//		JGModelStandaloneSetup.doSetup()
		
		var InMemoryJavaCompiler javaCompiler = new InMemoryJavaCompiler(this.class.classLoader, JavaVersion.JAVA7)
		 

		val model = '''
			package example.one
			system myx 
			commands {
				cmd is EXIT
			}
			@JYActionBlock (bussys1="My")
			action SelectPerson {
				main {
					}
			}
		'''.parse
		model.assertNoError("")

		val fsa = new InMemoryFileSystemAccess()
		generator.doGenerate(model.eResource(), fsa)
		val JavaSource[] s = newArrayOfSize(4)
		val map = fsa.allFiles
		s.set(0,
			new JavaSource("DEFAULT_OUTPUTexample/one/Command.java",
				map.get("DEFAULT_OUTPUTexample/one/Command.java").toString()))
		s.set(1,
			new JavaSource("DEFAULT_OUTPUTexample/one/Global.java",
				map.get("DEFAULT_OUTPUTexample/one/Global.java").toString()))
		s.set(2,
			new JavaSource("DEFAULT_OUTPUTexample/one/ActionBlock.java",
				map.get("DEFAULT_OUTPUTexample/one/ActionBlock.java").toString()))
		s.set(3,
			new JavaSource("DEFAULT_OUTPUTexample/one/SelectPerson.java",
				map.get("DEFAULT_OUTPUTexample/one/SelectPerson.java").toString()))			
		val Result result = javaCompiler.compile(s)
		result.compilationProblems.forEach [ problem |
			println(problem)  
		]
	}

}


and it prints when running inside unit test:

checkActionBlockAnnotation=JvmVoid:  (eProxyURI: __synthetic0.mdl#|0)


Generated Java code is correct and compiles OK. It does not have annotation replicated and this is as designed. Annotations are just a device to influence code generation process. It looks like validation cannot resolve to Jvm when in unit test.

Regards
Marek
Re: Unit Test for XAnnotation validation shows unresolved reference [message #1734281 is a reply to message #1734276] Mon, 06 June 2016 17:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
i cannot reproduce is. are you sure the Anntation is on the classpath of the unit test.
please not seems your model element and the annotation seems to have the same name
JGActionBlock


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unit Test for XAnnotation validation shows unresolved reference [message #1734286 is a reply to message #1734281] Mon, 06 June 2016 18:53 Go to previous message
Marek Stankiewicz is currently offline Marek StankiewiczFriend
Messages: 11
Registered: June 2016
Junior Member
Hi Christian,

I found the reason why it could not find class during unit test.

It should be

val result = parseHelper.parse('''
@org.xtext.example.mydsl.jvmmodel.MyAnnotation
Hello Xtext!
''')

instead of just

@MyAnnotation

Thanks for helping
Regards
Marek
Previous Topic:Xtext grammar for custom hash comment
Next Topic:org.eclipse.xtext.validation.CheckMode definition
Goto Forum:
  


Current Time: Fri Mar 29 10:55:46 GMT 2024

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

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

Back to the top