下面列出了怎么用org.eclipse.emf.ecore.util.Diagnostician的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Validates a object instance and its children using the EMF core validator
* API.
*
* @param element
* the element to validate
* @param label
* the type label of the element
* @throws IllegalStateException
* if validation errors are encountered
*/
protected static void validateDevice ( final EObject object, final String label )
{
final Diagnostic diag = Diagnostician.INSTANCE.validate ( object );
if ( diag.getSeverity () == Diagnostic.ERROR )
{
final StringBuilder sb = new StringBuilder ( "Invalid " + label );
for ( final Diagnostic child : diag.getChildren () )
{
if ( child.getSeverity () == Diagnostic.ERROR )
{
sb.append ( System.lineSeparator () );
sb.append ( child.getMessage () );
}
}
throw new IllegalStateException ( sb.toString () );
}
}
@Inject
public ValidatorTester(T validator, EValidatorRegistrar registrar, @Named(Constants.LANGUAGE_NAME) final String languageName) {
this.validator = validator;
EValidator.Registry originalRegistry = registrar.getRegistry();
EValidatorRegistryImpl newRegistry = new EValidatorRegistryImpl();
registrar.setRegistry(newRegistry);
this.validator.register(registrar);
diagnostician = new Diagnostician(newRegistry) {
@Override
public java.util.Map<Object,Object> createDefaultContext() {
java.util.Map<Object,Object> map = super.createDefaultContext();
map.put(AbstractInjectableValidator.CURRENT_LANGUAGE_NAME, languageName);
return map;
}
};
registrar.setRegistry(originalRegistry);
validatorCalled = false;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#validateValue(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
*
*/
public Diagnostic validateValue(IPropertiesEditionEvent event) {
Diagnostic ret = Diagnostic.OK_INSTANCE;
if (event.getNewValue() != null) {
try {
if (EipViewsRepository.Gateway.Properties.name == event.getAffectedEditor()) {
Object newValue = event.getNewValue();
if (newValue instanceof String) {
newValue = EEFConverterUtil.createFromString(EipPackage.eINSTANCE.getEndpoint_Name().getEAttributeType(), (String)newValue);
}
ret = Diagnostician.INSTANCE.validate(EipPackage.eINSTANCE.getEndpoint_Name().getEAttributeType(), newValue);
}
} catch (IllegalArgumentException iae) {
ret = BasicDiagnostic.toDiagnostic(iae);
} catch (WrappedException we) {
ret = BasicDiagnostic.toDiagnostic(we);
}
}
return ret;
}
@Test public void testOverrideFinal() throws Exception {
XtextResourceSet rs = get(XtextResourceSet.class);
getResourceFromString("grammar org.xtext.Supergrammar with org.eclipse.xtext.common.Terminals\n" +
"generate supergrammar \"http://org.xtext.supergrammar\"\n" +
"@Final\n" +
"RuleFinal:name=ID;\n" +
"Rule: name=ID;","superGrammar.xtext", rs);
XtextResource resource = getResourceFromString(
"grammar org.foo.Bar with org.xtext.Supergrammar\n" +
"generate bar \"http://org.xtext.Bar\"\n" +
"RuleFinal: name=ID;", "foo.xtext", rs);
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("This rule illegally overrides RuleFinal in org.xtext.Supergrammar which is final.", issues.get(0).getMessage());
assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
}
@Test public void testOverrideDeprecated_1() throws Exception {
XtextResourceSet rs = get(XtextResourceSet.class);
getResourceFromString("grammar org.xtext.Supergrammar with org.eclipse.xtext.common.Terminals\n" +
"generate supergrammar \"http://org.xtext.supergrammar\"\n" +
"@Deprecated\n" +
"RuleDeprecated: name=ID;\n" +
"Rule: name=ID;","superGrammar.xtext", rs);
XtextResource resource = getResourceFromString(
"grammar org.foo.Bar with org.xtext.Supergrammar\n" +
"generate bar \"http://org.xtext.Bar\"\n" +
"@Override RuleDeprecated: name=ID;", "foo.xtext", rs);
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("This rule overrides RuleDeprecated in org.xtext.Supergrammar which is deprecated.", issues.get(0).getMessage());
assertEquals("diag.isWarning", diag.getSeverity(), Diagnostic.WARNING);
}
/**
* {@inheritDoc}
*
* @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#validateValue(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
*
*/
public Diagnostic validateValue(IPropertiesEditionEvent event) {
Diagnostic ret = Diagnostic.OK_INSTANCE;
if (event.getNewValue() != null) {
try {
if (EipViewsRepository.CompositeProcessor.Properties.name == event.getAffectedEditor()) {
Object newValue = event.getNewValue();
if (newValue instanceof String) {
newValue = EEFConverterUtil.createFromString(EipPackage.eINSTANCE.getEndpoint_Name().getEAttributeType(), (String)newValue);
}
ret = Diagnostician.INSTANCE.validate(EipPackage.eINSTANCE.getEndpoint_Name().getEAttributeType(), newValue);
}
} catch (IllegalArgumentException iae) {
ret = BasicDiagnostic.toDiagnostic(iae);
} catch (WrappedException we) {
ret = BasicDiagnostic.toDiagnostic(we);
}
}
return ret;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#validateValue(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
*
*/
public Diagnostic validateValue(IPropertiesEditionEvent event) {
Diagnostic ret = Diagnostic.OK_INSTANCE;
if (event.getNewValue() != null) {
try {
if (EipViewsRepository.ServiceActivator.Properties.name == event.getAffectedEditor()) {
Object newValue = event.getNewValue();
if (newValue instanceof String) {
newValue = EEFConverterUtil.createFromString(EipPackage.eINSTANCE.getEndpoint_Name().getEAttributeType(), (String)newValue);
}
ret = Diagnostician.INSTANCE.validate(EipPackage.eINSTANCE.getEndpoint_Name().getEAttributeType(), newValue);
}
} catch (IllegalArgumentException iae) {
ret = BasicDiagnostic.toDiagnostic(iae);
} catch (WrappedException we) {
ret = BasicDiagnostic.toDiagnostic(we);
}
}
return ret;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#validateValue(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
*
*/
public Diagnostic validateValue(IPropertiesEditionEvent event) {
Diagnostic ret = Diagnostic.OK_INSTANCE;
if (event.getNewValue() != null) {
try {
if (EipViewsRepository.Splitter.Properties.name == event.getAffectedEditor()) {
Object newValue = event.getNewValue();
if (newValue instanceof String) {
newValue = EEFConverterUtil.createFromString(EipPackage.eINSTANCE.getEndpoint_Name().getEAttributeType(), (String)newValue);
}
ret = Diagnostician.INSTANCE.validate(EipPackage.eINSTANCE.getEndpoint_Name().getEAttributeType(), newValue);
}
} catch (IllegalArgumentException iae) {
ret = BasicDiagnostic.toDiagnostic(iae);
} catch (WrappedException we) {
ret = BasicDiagnostic.toDiagnostic(we);
}
}
return ret;
}
@Test public void testCycleInTypeHierarchy() throws Exception {
String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals" +
" generate test 'http://test'";
grammarAsText += " RuleA: RuleB;";
grammarAsText += " RuleB: RuleC;";
grammarAsText += " RuleC: RuleA;";
grammarAsText += " RuleD: RuleA;";
Grammar grammar = (Grammar) getModel(grammarAsText);
AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
XtextValidator validator = get(XtextValidator.class);
ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(grammar.getRules().get(0).getType(), true, false);
messageAcceptor.expectedContext(
grammar.getRules().get(1).getType(),
grammar.getRules().get(2).getType()
);
validator.setMessageAcceptor(messageAcceptor);
validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
messageAcceptor.validate();
}
@Test public void testNameClash_02() throws Exception {
String grammarAsText =
"grammar test with org.eclipse.xtext.common.Terminals\n" +
"generate test 'http://test'\n" +
"Class returns Class: {Class_} name=ID;\n";
Grammar grammar = (Grammar) getModel(grammarAsText);
AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
XtextValidator validator = get(XtextValidator.class);
ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
CompoundElement element = (CompoundElement) grammar.getRules().get(0).getAlternatives();
messageAcceptor.expectedContext(
grammar.getRules().get(0).getType(),
((Action) element.getElements().get(0)).getType()
);
validator.setMessageAcceptor(messageAcceptor);
validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
messageAcceptor.validate();
}
@Test public void testNameClash_03() throws Exception {
String grammarAsText =
"grammar test with org.eclipse.xtext.common.Terminals\n" +
"generate test 'http://test'\n" +
"Foo: myVars=ID my_vars=ID;\n";
Grammar grammar = (Grammar) getModel(grammarAsText);
AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
XtextValidator validator = get(XtextValidator.class);
ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
CompoundElement element = (CompoundElement) grammar.getRules().get(0).getAlternatives();
messageAcceptor.expectedContext(
grammar.getRules().get(0).getType(),
element.getElements().get(0),
element.getElements().get(1)
);
validator.setMessageAcceptor(messageAcceptor);
validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
messageAcceptor.validate();
}
@Test
public void checkExitTransitionExistsNoSync() {
statechart = AbstractTestModelsUtil.loadStatechart(VALIDATION_TESTMODEL_DIR + "NoExitTransitionToSync.sct");
Diagnostic diagnostics = Diagnostician.INSTANCE.validate(statechart);
assertIssueCount(diagnostics, 1);
assertError(diagnostics, EXIT_UNUSED);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated NOT
*/
@Override
public void validate() throws MigrationException {
final BasicDiagnostic diagnostic = new BasicDiagnostic();
final Diagnostician diagnostician = new Diagnostician();
for (final EPackage p : getEPackages()) {
diagnostician.validate(p, diagnostic);
}
if (diagnostic.getSeverity() != Diagnostic.OK) {
throw new MigrationException(new DiagnosticException("Metamodel not valid", diagnostic)); //$NON-NLS-1$
}
}
public Diagnostic runEMFValidator(final View target) {
if (target.isSetElement() && target.getElement() != null) {
return new Diagnostician() {
@Override
public String getObjectLabel(final EObject eObject) {
return EMFCoreUtil.getQualifiedName(eObject, true);
}
}.validate(target.getElement());
}
return Diagnostic.OK_INSTANCE;
}
@Check
public void checkGeneratedPackage(GeneratedMetamodel metamodel) {
Map<Object, Object> context = getContext();
if (context != null) {
Diagnostician diagnostician = (Diagnostician) context.get(EValidator.class);
if (diagnostician != null)
checkGeneratedPackage(metamodel, diagnostician, context);
}
}
/**
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=385620
*/
@Test public void emptyImportURIStringShouldBeInvalid() throws Exception {
String model = "import ''";
Resource resource = getResourceFromString(model);
Diagnostic diagnostics = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
assertEquals(Diagnostic.ERROR, diagnostics.getSeverity());
}
@Test public void testExplicitOverride02() throws Exception {
XtextResource resource = getResourceFromString(
"grammar org.foo.Bar with org.eclipse.xtext.common.Terminals\n" +
"terminal ID: ('a'..'z'|'A'..'Z'|'_');");
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("This rule overrides ID in org.eclipse.xtext.common.Terminals and thus should be annotated with @Override.", issues.get(0).getMessage());
assertEquals("diag.isWarning", diag.getSeverity(), Diagnostic.WARNING);
}
@Test public void testExplicitOverride03() throws Exception {
XtextResource resource = getResourceFromString(
"grammar org.foo.Bar with org.eclipse.xtext.common.Terminals\n" +
"@Override\n" +
"terminal ID_2: ('a'..'z'|'A'..'Z'|'_');");
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("The rule ID_2 does not override a rule from a super grammar.", issues.get(0).getMessage());
assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
}
@Test public void testExplicitOverride05() throws Exception {
XtextResource resource = getResourceFromString(
"grammar org.foo.Bar\n" +
"import \"http://www.eclipse.org/emf/2002/Ecore\" as ecore" +
"@Override\n" +
"terminal ID: ('a'..'z'|'A'..'Z'|'_');");
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("This grammar has no super grammar and therefore cannot override any rules.", issues.get(0).getMessage());
assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
}
@Test public void testOverrideFinal_1() throws Exception {
XtextResourceSet rs = get(XtextResourceSet.class);
getResourceFromString("grammar org.xtext.Supergrammar with org.eclipse.xtext.common.Terminals\n" +
"generate supergrammar \"http://org.xtext.supergrammar\"\n" +
"@Final\n" +
"RuleFinal:name=ID;\n" +
"Rule: name=ID;","superGrammar.xtext", rs);
XtextResource resource = getResourceFromString(
"grammar org.foo.Bar with org.xtext.Supergrammar\n" +
"generate bar \"http://org.xtext.Bar\"\n" +
"@Override Rule: name=ID;", "foo.xtext", rs);
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 0, issues.size());
}
@Test public void testCallDeprecatedRule() throws Exception {
XtextResource resource = getResourceFromString(
"grammar org.foo.Bar with org.eclipse.xtext.common.Terminals\n" +
"generate bar \"http://org.xtext.Bar\"\n" +
"Model : rules+=RuleDeprecated*;\n" +
"@Deprecated\n" +
"RuleDeprecated: name=ID;");
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("The called rule is marked as deprecated.", issues.get(0).getMessage());
assertEquals("diag.isWarning", diag.getSeverity(), Diagnostic.WARNING);
}
/**
* @generated
*/
private static Diagnostic runEMFValidator(View target) {
if (target.isSetElement() && target.getElement() != null) {
return new Diagnostician() {
public String getObjectLabel(EObject eObject) {
return EMFCoreUtil.getQualifiedName(eObject, true);
}
}.validate(target.getElement());
}
return Diagnostic.OK_INSTANCE;
}
@Test public void testTerminalAnnotation_1() throws Exception {
Resource resource = getResourceFromString("grammar org.xtext.Supergrammar with org.eclipse.xtext.common.Terminals\n" +
"generate supergrammar \"http://org.xtext.supergrammar\"\n" +
"Rule: name=ID;\n"+
"@Exported\n"+
"terminal TERMINAL: ID;");
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("Rule cannot be exported!", issues.get(0).getMessage());
assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
}
@Test public void testEnumAnnotation() throws Exception {
Resource resource = getResourceFromString("grammar org.xtext.Supergrammar with org.eclipse.xtext.common.Terminals\n" +
"generate supergrammar \"http://org.xtext.supergrammar\"\n" +
"Rule: name=ID;\n"+
"@Deprecated\n"+
"enum MyEnum: FOO | BAR;");
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("Rule cannot be deprecated!", issues.get(0).getMessage());
assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
}
@Test public void testEnumAnnotation_1() throws Exception {
Resource resource = getResourceFromString("grammar org.xtext.Supergrammar with org.eclipse.xtext.common.Terminals\n" +
"generate supergrammar \"http://org.xtext.supergrammar\"\n" +
"Rule: name=ID;\n"+
"@Exported\n"+
"enum MyEnum: FOO | BAR;");
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
List<Diagnostic> issues = diag.getChildren();
assertEquals(issues.toString(), 1, issues.size());
assertEquals("Rule cannot be exported!", issues.get(0).getMessage());
assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
}
/**
* see https://bugs.eclipse.org/bugs/show_bug.cgi?id=348052
*/
@Test public void testGrammarHasNoNamespace() throws Exception {
XtextResource resource = getResourceFromString(
"grammar Bar with org.eclipse.xtext.common.Terminals\n" +
"generate metamodel 'myURI'\n" +
"Model: name=ID;\n");
assertTrue(resource.getErrors().toString(), resource.getErrors().isEmpty());
assertTrue(resource.getWarnings().toString(), resource.getWarnings().isEmpty());
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
assertNotNull("diag", diag);
assertEquals(diag.getChildren().toString(), 1, diag.getChildren().size());
assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
}
@Test
public void testBug322875_02() throws Exception {
URIConverter.URI_MAP.put(URI.createURI("platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore"), URI.createURI(getClass().getResource("/model/Ecore.ecore").toExternalForm()));
String testGrammar = "grammar foo.Bar with org.eclipse.xtext.common.Terminals\n " +
" import 'platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore' " +
"Model returns EClass: name=ID;";
XtextResource resource = getResourceFromString(testGrammar);
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
assertNotNull("diag", diag);
assertEquals(diag.toString(), 0, diag.getChildren().size());
assertEquals("diag.isOk", Diagnostic.OK, diag.getSeverity());
}
@Test public void testBug322875_03() throws Exception {
String testGrammar = "grammar foo.Bar with org.eclipse.xtext.common.Terminals\n " +
" import 'http://www.eclipse.org/emf/2002/Ecore' " +
"Model returns EClass: name=ID;";
XtextResource resource = getResourceFromString(testGrammar);
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
assertNotNull("diag", diag);
assertEquals(diag.toString(), 0, diag.getChildren().size());
}
@Test
public void checkSyncNoTriggersOnOutgoingTransition() {
Statechart statechart = AbstractTestModelsUtil
.loadStatechart(VALIDATION_TESTMODEL_DIR + "SynchronizationExitTransition.sct");
Diagnostic diagnostics = Diagnostician.INSTANCE.validate(statechart);
assertIssueCount(diagnostics, 1);
assertWarning(diagnostics, SYNC_OUTGOING_TRIGGER);
}
@Test public void testBug_282852_01() throws Exception {
XtextResource resource = getResourceFromString(
"grammar org.foo.Bar with org.eclipse.xtext.common.Terminals\n" +
"generate metamodel 'myURI'\n" +
"Model: name=ID ref='bar';\n" +
"Model: name=ID ref='foo';");
assertTrue(resource.getErrors().toString(), resource.getErrors().isEmpty());
assertTrue(resource.getWarnings().toString(), resource.getWarnings().isEmpty());
Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
assertNotNull("diag", diag);
assertEquals(diag.getChildren().toString(), 2, diag.getChildren().size());
assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
}