Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » JUnit tests for content assist needing classpath context
JUnit tests for content assist needing classpath context [message #1343688] Fri, 09 May 2014 12:09 Go to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1753
Registered: July 2009
Senior Member
I'm happily using some infrastructure from o.e.x.junit4, delighted to find advanced stuff like AbstractContentAssistProcessorTest.

This works nicely for simple examples, but some of my content assists need to retrieve information from secondary models on the class path.

I couldn't find a way to configure these tests with two or more resources so that one could resolve references to the other.

Am I looking in the wrong place? Are there other test classes better suitable for testing multi-model situations?

thanks
Stephan
Re: JUnit tests for content assist needing classpath context [message #1345716 is a reply to message #1343688] Sat, 10 May 2014 08:48 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1762
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 09/05/2014 14:09, Stephan Herrmann wrote:
> I'm happily using some infrastructure from o.e.x.junit4, delighted to
> find advanced stuff like AbstractContentAssistProcessorTest.
>
> This works nicely for simple examples, but some of my content assists
> need to retrieve information from secondary models on the class path.
>
> I couldn't find a way to configure these tests with two or more
> resources so that one could resolve references to the other.
>
> Am I looking in the wrong place? Are there other test classes better
> suitable for testing multi-model situations?

Hi

you may want to look in that base class and see how things are setup; in
particular, it uses org.eclipse.xtext.junit4.ui.util.JavaProjectSetupUtil.*
if you dig into org.eclipse.xtext.junit4 package you see that there are
many utility functions for setting up a project programmatically,
creating files, etc.; you may have to perform additional setup steps
yourself.

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: JUnit tests for content assist needing classpath context [message #1385171 is a reply to message #1345716] Wed, 04 June 2014 18:05 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1753
Registered: July 2009
Senior Member
Hi Lorenzo,

thanks for your encouragement (I was "detained" by https://bugs.eclipse.org/430302 , but I seem to be back in the game ...).

Unfortunately, I can't see the connection from AbstractContentAssistProcessorTest to JavaProjectSetupUtil, which you mentioned, and all attempts to combine them two let me think that those are not meant to be working together. Even single-resource tests that have been working with AbstractContentAssistProcessorTest completely break down when I try to run them with an IJavaProject.

Hence my question: are there any tests out there that successfully use AbstractContentAssistProcessorTest with an IJavaProject?

FYI, these are some of the hooks that I override so far:
- a @Before method to call createJavaProject and addNature
- override get(Class<T>) to create a XtextResourceSet for the given IJavaProject
- write my own createFile() method (for secondary files), because everything that's routed through doGetResource() doesn't really create the resource in the file system, which means JDT cannot see it.
- override doGetResource to avoid calling rs.setClasspathURIContext() (we already have the IJavaProject as the context, using getClass() is wrong in this situation).

The "best" I could achieve with all this is: no completion proposals.
In the debugger I see that org.eclipse.xtext.ui.editor.contentassist.antlr.ParserBasedContentAssistContextFactory.StatefulFactory.doCreateContexts(int) fails to create any contextBuilders. I have a valid root model element, but it doesn't seem to have and followElements. Without an IJavaProject all was well for this test.

anyone have any successful examples?
thanks,
Stephan
Re: JUnit tests for content assist needing classpath context [message #1385175 is a reply to message #1385171] Wed, 04 June 2014 18:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13317
Registered: July 2009
Senior Member
HI,

what do you mean "by classpath"? on a second project? in a jar?


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: JUnit tests for content assist needing classpath context [message #1385178 is a reply to message #1385175] Wed, 04 June 2014 19:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13317
Registered: July 2009
Senior Member
btw the following works for me:

Model:
	elements+=Element*;
	
Element:
	'element' name=ID ('extends' extends=[Element])?;


package org.xtext.example.mydsl.tests;

import java.io.InputStream;

import javax.inject.Inject;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.xtext.ISetup;
import org.eclipse.xtext.junit4.InjectWith;
import org.eclipse.xtext.junit4.XtextRunner;
import org.eclipse.xtext.junit4.ui.AbstractContentAssistProcessorTest;
import org.eclipse.xtext.junit4.ui.util.IResourcesSetupUtil;
import org.eclipse.xtext.junit4.ui.util.JavaProjectSetupUtil;
import org.eclipse.xtext.linking.lazy.LazyLinkingResource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.ui.XtextProjectHelper;
import org.eclipse.xtext.util.CancelIndicator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xtext.example.mydsl.MyDslUiInjectorProvider;

import com.google.inject.Injector;

@SuppressWarnings("restriction")
@RunWith(XtextRunner.class)
@InjectWith(MyDslUiInjectorProvider.class)
public class MyDslContentAssistProcessorTest extends AbstractContentAssistProcessorTest {

	@Inject
	private Injector injector;
	
	private IJavaProject project;
	
	@Override
	public void setUp() throws Exception {
		super.setUp();
		
		IJavaProject lib = JavaProjectSetupUtil.createJavaProject("lib");
		JavaProjectSetupUtil.addSourceFolder(lib, "src");
		IResourcesSetupUtil.addNature(lib.getProject(), XtextProjectHelper.NATURE_ID);
		IResourcesSetupUtil.addBuilder(lib.getProject(), XtextProjectHelper.BUILDER_ID);
		IResourcesSetupUtil.createFile("lib/src/dummy.mydsl", "element D");
		
		project = JavaProjectSetupUtil.createJavaProject("dummy");
		JavaProjectSetupUtil.addSourceFolder(project, "src");
		IResourcesSetupUtil.addNature(project.getProject(), XtextProjectHelper.NATURE_ID);
		IResourcesSetupUtil.addBuilder(project.getProject(), XtextProjectHelper.BUILDER_ID);
		IResourcesSetupUtil.createFile("dummy/src/dummy.mydsl", "element C");
		JavaProjectSetupUtil.addProjectReference(project, lib);
		IResourcesSetupUtil.waitForAutoBuild();
	}
	
	@Override
	protected ISetup doGetSetup() {
		return new ISetup() {
			
			public Injector createInjectorAndDoEMFRegistration() {
				return injector;
			}
		};
	}
	
	@Test
	public void testSomething() throws Exception {
		newBuilder().append("element A element B extends ").assertText("A","B","C","D");
	}
	
	protected XtextResource doGetResource(InputStream in, URI uri) throws Exception {
		uri = URI.createURI("platform:/resource/dummy/test.mydsl");
		XtextResourceSet rs = get(XtextResourceSet.class);
		rs.setClasspathURIContext(project);
		XtextResource resource = (XtextResource) getResourceFactory().createResource(uri);
		rs.getResources().add(resource);
		resource.load(in, null);
		if (resource instanceof LazyLinkingResource) {
			((LazyLinkingResource) resource).resolveLazyCrossReferences(CancelIndicator.NullImpl);
		} else {
			EcoreUtil.resolveAll(resource);
		}
		return resource;
	}
	
	

}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: JUnit tests for content assist needing classpath context [message #1385181 is a reply to message #1385178] Wed, 04 June 2014 19:49 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1753
Registered: July 2009
Senior Member
Hi Christian,

Thanks a lot for the example. At a first glance it looks like I was lacking the addBuilder() and waitForAutoBuild() calls.
I'll try that right now.

thanks,
Stephan
Re: JUnit tests for content assist needing classpath context [message #1385184 is a reply to message #1385181] Wed, 04 June 2014 20:01 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1753
Registered: July 2009
Senior Member
Yep, that did the trick.

Thanks again, Christian!
Previous Topic:JvmModelInferrer and debug: skip steps?
Next Topic:Xtext Enums
Goto Forum:
  


Current Time: Fri Jan 24 11:46:36 GMT 2020

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

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

Back to the top