Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » CompilationTestHelper problem with generated non-java code
CompilationTestHelper problem with generated non-java code [message #1050667] Sat, 27 April 2013 15:00 Go to next message
Thomas Fritsch is currently offline Thomas FritschFriend
Messages: 20
Registered: April 2013
Location: Germany
Junior Member
Hi all,

I'm using Xtext 2.4.1.
For generating both Java code and XML code from my DSL I have a subclass of JvmModelGenerator.
The generator works fine. From my DSL it produces *.java files and an XML file.

But now I want JUnit-testing my generator and run into trouble.
My Junit test looks like this
package org.xtext.example.mydsl

import com.google.inject.Inject
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.xbase.compiler.CompilationTestHelper
import org.junit.Test
import org.junit.runner.RunWith

import static org.junit.Assert.*

@RunWith(typeof(XtextRunner))
@InjectWith(typeof(MyDslInjectorProvider))
class CompilerTest {
	
	@Inject extension CompilationTestHelper

	@Test
	def testEntity() {
		// The DSL below produces these files:
		//   com/oce/example/Person.java
		//   com/oce/example/Address.java
		//   data/example-ds.xml
		'''
			package com.oce.example {
				entity Person {
				}
				entity Address {
				}
			}
			database "data/example-ds.xml" {
			}
		'''.compile [
			
			val person = getCompiledClass("com.oce.example.Person").newInstance
			assertNotNull(person)
		]
	}	
}

I got the following error:
java.lang.RuntimeException: java.lang.IllegalArgumentException: Couldn't compile : ----------
1. ERROR in /tmp/otfjc26240014/data/example-d.java (at line 1)
	<?xml version="1.0" encoding="UTF-8"?>
	^
Syntax error on tokens, delete these tokens
[...]
Caused by: java.lang.IllegalArgumentException: Couldn't compile : ----------
1. ERROR in/tmp/otfjc26240014/data/otfjc26240014\data\example-d.java (at line 1)
	<?xml version="1.0" encoding="UTF-8"?>
	^
Syntax error on tokens, delete these tokens
[...]
	at org.eclipse.xtext.xbase.compiler.OnTheFlyJavaCompiler.compileToClasses(OnTheFlyJavaCompiler.java:315)
	at org.eclipse.xtext.xbase.compiler.CompilationTestHelper$2.getCompiledClasses(CompilationTestHelper.java:171)
	at org.eclipse.xtext.xbase.compiler.CompilationTestHelper$2.getCompiledClass(CompilationTestHelper.java:223)
	at org.xtext.example.mydsl.CompilerTest$1.accept(CompilerTest.java:52)
	at org.xtext.example.mydsl.CompilerTest$1.accept(CompilerTest.java:1)
	at org.eclipse.xtext.xbase.compiler.CompilationTestHelper.compile(CompilationTestHelper.java:164)
	... 26 more

It seems the generated XML file "data/example-ds.xml" has been treated as Java source and fed into the Java compiler.

I don't know if my approach is wrong or if it is a bug of Xtext.

Regards,
Thomas

Re: CompilationTestHelper problem with generated non-java code [message #1051800 is a reply to message #1050667] Mon, 29 April 2013 08:27 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
The CompilationTestHelper belongs to Xbase and assumes Java code to be
generated. This is not a bug but works as designed.

If you want to inspect the generated XML code you have to implement
your own helper class, e.g. by stripping the Java stuff from
org.eclipse.xtext.xbase.compiler.CompilationTestHelper.compile(ResourceSet,
IAcceptor<Result>) or using the ParseHelper and the IGenerator of your
language.



Am 28.04.13 18:50, schrieb Thomas Fritsch:
> Hi all,
>
> I'm using Xtext 2.4.1.
> For generating both Java code and XML code from my DSL I have a subclass
> of JvmModelGenerator.
> The generator works fine. From my DSL it produces *.java files and an
> XML file.
>
> But now I want JUnit-testing my generator and run into trouble.
> My Junit test looks like this
> package org.xtext.example.mydsl
>
> import com.google.inject.Inject
> import org.eclipse.xtext.junit4.InjectWith
> import org.eclipse.xtext.junit4.XtextRunner
> import org.eclipse.xtext.xbase.compiler.CompilationTestHelper
> import org.junit.Test
> import org.junit.runner.RunWith
>
> import static org.junit.Assert.*
>
> @RunWith(typeof(XtextRunner))
> @InjectWith(typeof(MyDslInjectorProvider))
> class CompilerTest {
>
> @Inject extension CompilationTestHelper
>
> @Test
> def testEntity() {
> // The DSL below produces these files:
> // com/oce/example/Person.java
> // com/oce/example/Address.java
> // data/example-ds.xml
> '''
> package com.oce.example {
> entity Person {
> }
> entity Address {
> }
> }
> database "data/example-ds.xml" {
> }
> '''.compile [
>
> val person =
> getCompiledClass("com.oce.example.Person").newInstance
> assertNotNull(person)
> ]
> }
> }
> I got the following error:
> java.lang.RuntimeException: java.lang.IllegalArgumentException: Couldn't
> compile : ----------
> 1. ERROR in /tmp/otfjc26240014/data/example-d.java (at line 1)
> <?xml version="1.0" encoding="UTF-8"?>
> ^
> Syntax error on tokens, delete these tokens
> [...]
> Caused by: java.lang.IllegalArgumentException: Couldn't compile :
> ----------
> 1. ERROR in/tmp/otfjc26240014/data/otfjc26240014\data\example-d.java (at
> line 1)
> <?xml version="1.0" encoding="UTF-8"?>
> ^
> Syntax error on tokens, delete these tokens
> [...]
> at
> org.eclipse.xtext.xbase.compiler.OnTheFlyJavaCompiler.compileToClasses(OnTheFlyJavaCompiler.java:315)
>
> at
> org.eclipse.xtext.xbase.compiler.CompilationTestHelper$2.getCompiledClasses(CompilationTestHelper.java:171)
>
> at
> org.eclipse.xtext.xbase.compiler.CompilationTestHelper$2.getCompiledClass(CompilationTestHelper.java:223)
>
> at org.xtext.example.mydsl.CompilerTest$1.accept(CompilerTest.java:52)
> at org.xtext.example.mydsl.CompilerTest$1.accept(CompilerTest.java:1)
> at
> org.eclipse.xtext.xbase.compiler.CompilationTestHelper.compile(CompilationTestHelper.java:164)
>
> ... 26 more
> It seems the generated XML file "data/example-ds.xml" has been treated
> as Java source and fed into the Java compiler.
>
> I don't know if my approach is wrong or if it is a bug of Xtext.
>
> Regards,
> Thomas
>
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: CompilationTestHelper problem with generated non-java code [message #1051919 is a reply to message #1051800] Mon, 29 April 2013 11:24 Go to previous messageGo to next message
Thomas Fritsch is currently offline Thomas FritschFriend
Messages: 20
Registered: April 2013
Location: Germany
Junior Member
Jan Kohnlein wrote on Mon, 29 April 2013 10:27
The CompilationTestHelper belongs to Xbase and assumes Java code to be
generated. This is not a bug but works as designed.

If you want to inspect the generated XML code you have to implement
your own helper class, e.g. by stripping the Java stuff from
org.eclipse.xtext.xbase.compiler.CompilationTestHelper.compile(ResourceSet,
IAcceptor<Result>) or using the ParseHelper and the IGenerator of your
language.


Thanks for your reply.

Ok, I agree. For inspecting the generated XML code I need to implement a helper class of my own. I will begin that soon.

But how about inspecting/testing the generated Java code?
For doing so it would be nice if the CompilationTestHelper just would gracefully ignore all non-Java code. May be like in attachment.

~Thomas
  • Attachment: x.patch
    (Size: 1.15KB, Downloaded 54 times)
Re: CompilationTestHelper problem with generated non-java code [message #1051925 is a reply to message #1051919] Mon, 29 April 2013 11:31 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1418
Registered: July 2009
Senior Member
On 04/29/2013 01:24 PM, Thomas Fritsch wrote:
> Jan Kohnlein wrote on Mon, 29 April 2013 10:27
>> The CompilationTestHelper belongs to Xbase and assumes Java code to be
>> generated. This is not a bug but works as designed.
>>
>> If you want to inspect the generated XML code you have to implement
>> your own helper class, e.g. by stripping the Java stuff from
>> org.eclipse.xtext.xbase.compiler.CompilationTestHelper.compile(ResourceSet,
>> IAcceptor<Result>) or using the ParseHelper and the IGenerator of your
>> language.
>
>
> Thanks for your reply.
>
> Ok, I agree. For inspecting the generated XML code I need to implement a helper class of my own. I will begin that soon.
>
> But how about inspecting/testing the generated Java code?
> For doing so it would be nice if the CompilationTestHelper just would gracefully ignore all non-Java code. May be like in attachment.
>
> ~Thomas
>

I was using CompilationTestHelper in Xtext 2.3.1 and I was able to check
also generated Xml files, besides Java files, by inspecting, manually,
all the generated files. I think the proposed modification really
makes sense :)

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Re: CompilationTestHelper problem with generated non-java code [message #1052137 is a reply to message #1051919] Mon, 29 April 2013 17:27 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
Please file bugzillas for such enhancements.

Regards
Jan

Am 29.04.13 13:24, schrieb Thomas Fritsch:
> Jan Kohnlein wrote on Mon, 29 April 2013 10:27
>> The CompilationTestHelper belongs to Xbase and assumes Java code to be
>> generated. This is not a bug but works as designed.
>>
>> If you want to inspect the generated XML code you have to implement
>> your own helper class, e.g. by stripping the Java stuff from
>> org.eclipse.xtext.xbase.compiler.CompilationTestHelper.compile(ResourceSet,
>> IAcceptor<Result>) or using the ParseHelper and the IGenerator of your
>> language.
>
>
> Thanks for your reply.
>
> Ok, I agree. For inspecting the generated XML code I need to implement a helper class of my own. I will begin that soon.
>
> But how about inspecting/testing the generated Java code?
> For doing so it would be nice if the CompilationTestHelper just would gracefully ignore all non-Java code. May be like in attachment.
>
> ~Thomas
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: CompilationTestHelper problem with generated non-java code [message #1052169 is a reply to message #1052137] Mon, 29 April 2013 18:38 Go to previous message
Thomas Fritsch is currently offline Thomas FritschFriend
Messages: 20
Registered: April 2013
Location: Germany
Junior Member
Jan Kohnlein wrote on Mon, 29 April 2013 19:27
Please file bugzillas for such enhancements.

Regards
Jan


Done. https://bugs.eclipse.org/bugs/show_bug.cgi?id=406835

Regards
Thomas
Previous Topic:Following the tutorial, confused on how I actually generate any Java code
Next Topic:XAnnotation / ImportManager
Goto Forum:
  


Current Time: Mon Dec 22 04:30:29 GMT 2014

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

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