Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Complete example Custom Validator Test?
Complete example Custom Validator Test? [message #758749] Thu, 24 November 2011 13:20 Go to next message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
Trying to write some custom validations test first. I looked at example code in the docs - which is useful but doesn't include imports or really show how to create sample models to test against. I tried the sample projects but none seems to include tests for custom validation. I'm not familiar with the ecore API which doesn't help.

Would appreciate if someone could point me to a complete example of a test file. So far I have the basics in place I think (imports, setup):

package org.domainmodels.Ooa.unittest;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.domainmodels.OoaInjectorProvider;
import org.domainmodels.OoaStandaloneSetup;
import org.domainmodels.validation.OoaJavaValidator;

import org.eclipse.xtext.junit4.InjectWith;
import org.eclipselabs.xtext.utils.unittesting.XtextRunner2;
import org.eclipse.xtext.junit.AbstractXtextTests;
import org.eclipse.xtext.junit.validation.ValidatorTester;

@RunWith(XtextRunner2.class)
@InjectWith(OoaInjectorProvider.class)
public class TestOoaCustomValidation extends AbstractXtextTests {

	private ValidatorTester<OoaJavaValidator> tester;

	  @Override
	  public void setUp() {
		  try {
		    with(OoaStandaloneSetup.class);
		    OoaJavaValidator validator = get(OoaJavaValidator.class);
		    tester = new ValidatorTester<OoaJavaValidator>(validator, getInjector());
		  } catch(Exception e) {
			  System.out.println(e);
		  }
	  }
	  
	@Test
	public void testClassMustHaveAtLeastOneIdentifier(){
		
		fail();
	}

}


Thanks.

[Updated on: Thu, 24 November 2011 13:21]

Report message to a moderator

Re: Complete example Custom Validator Test? [message #758761 is a reply to message #758749] Thu, 24 November 2011 13:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13769
Registered: July 2009
Senior Member
MyDslJavaValidator validator = get(MyDslJavaValidator.class);
ValidatorTester<MyDslJavaValidator> tester = new ValidatorTester<MyDslJavaValidator>(validator, getInjector());
tester.validate(undertest);
tester.diagnose().assertOK();

alternatively you can call tester.validator().checkGreetingStartsWithCapital(undertest);


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

[Updated on: Thu, 24 November 2011 13:48]

Report message to a moderator

Re: Complete example Custom Validator Test? [message #758763 is a reply to message #758761] Thu, 24 November 2011 13:59 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Or alternatively, you can use org.eclipse.emf.ecore.util.Diagnostician which also takes care of composited validators.

Re: Complete example Custom Validator Test? [message #758833 is a reply to message #758763] Thu, 24 November 2011 16:35 Go to previous message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
Christian, Meinte,

Thanks for the quick response.

My question was more specifically around creating an ecore model in memory to test. However I found another example in the docs and got the rest from there.
Previous Topic:Property Generator.pathRtProject used in two views of the model
Next Topic:URI serialization
Goto Forum:
  


Current Time: Sat Mar 06 17:28:40 GMT 2021

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

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

Back to the top