Search in sources :

Example 6 with ExceptionDiagnostic

use of org.eclipse.xtext.diagnostics.ExceptionDiagnostic in project xtext-core by eclipse.

the class Xtext2EcoreTransformerTest method testNoException_01.

@Test
public void testNoException_01() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test with org.eclipse.xtext.common.Terminals import \'http://www.eclipse.org/emf/2002/Ecore\' as ecore generate test \'http://test\'");
    _builder.newLine();
    _builder.append("CompositeModel: (model+=Model)+;");
    _builder.newLine();
    _builder.append("Model: id=NestedModelId (\':\' value=Fraction)? (\'#\' vector=Vector)? (\'+\' dots=Dots)? \';\'");
    _builder.newLine();
    _builder.append("ModelId returns ecore::EString: ID \'.\' ID;");
    _builder.newLine();
    _builder.append("NestedModelId : ModelId \'.\' ModelId;");
    _builder.newLine();
    _builder.append("Fraction returns EBigDecimal: INT (\'/\' INT)?;");
    _builder.newLine();
    _builder.append("Vector : \'(\' INT I");
    String grammar = _builder.toString();
    final XtextResource resource = this.getResourceFromStringAndExpect(grammar, 10);
    EList<Resource.Diagnostic> _errors = resource.getErrors();
    for (final Resource.Diagnostic d : _errors) {
        Assert.assertFalse((d instanceof ExceptionDiagnostic));
    }
}
Also used : ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) TransformationDiagnostic(org.eclipse.xtext.xtext.ecoreInference.TransformationDiagnostic) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 7 with ExceptionDiagnostic

use of org.eclipse.xtext.diagnostics.ExceptionDiagnostic in project xtext-xtend by eclipse.

the class SmokeTest method checkNoErrorsInValidator.

protected void checkNoErrorsInValidator(final String model, LazyLinkingResource resource) {
    ResourceValidatorImpl validator = resourceValidatorProvider.get();
    assertNotSame(validator, resource.getResourceServiceProvider().getResourceValidator());
    validator.setDiagnosticConverter(new IDiagnosticConverter() {

        @Override
        public void convertValidatorDiagnostic(org.eclipse.emf.common.util.Diagnostic diagnostic, IAcceptor<Issue> acceptor) {
            if (diagnostic instanceof BasicDiagnostic) {
                List<?> data = diagnostic.getData();
                if (!data.isEmpty() && data.get(0) instanceof Throwable) {
                    Throwable t = (Throwable) data.get(0);
                    throwError(t);
                }
                if (EObjectValidator.DIAGNOSTIC_SOURCE.equals(diagnostic.getSource()) && diagnostic.getCode() == EObjectValidator.EOBJECT__EVERY_REFERENCE_IS_CONTAINED) {
                    throwError(new RuntimeException("Dangling reference found."));
                }
            }
        }

        private void throwError(Throwable e) {
            ComparisonFailure result = new ComparisonFailure(e.getMessage(), model, "");
            result.setStackTrace(e.getStackTrace());
            throw result;
        }

        @Override
        public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) {
            if (diagnostic instanceof ExceptionDiagnostic) {
                Exception e = ((ExceptionDiagnostic) diagnostic).getException();
                throwError(e);
            }
        }
    });
    validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
}
Also used : Issue(org.eclipse.xtext.validation.Issue) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) Severity(org.eclipse.xtext.diagnostics.Severity) IOException(java.io.IOException) ResourceValidatorImpl(org.eclipse.xtext.validation.ResourceValidatorImpl) IDiagnosticConverter(org.eclipse.xtext.validation.IDiagnosticConverter) ComparisonFailure(org.junit.ComparisonFailure) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) List(java.util.List)

Example 8 with ExceptionDiagnostic

use of org.eclipse.xtext.diagnostics.ExceptionDiagnostic in project xtext-xtend by eclipse.

the class LinkingErrorTest method validateWithoutException.

protected void validateWithoutException(XtextResource resource) {
    ResourceValidatorImpl validator = resourceValidatorProvider.get();
    assertNotSame(validator, resource.getResourceServiceProvider().getResourceValidator());
    validator.setDiagnosticConverter(new IDiagnosticConverter() {

        @Override
        public void convertValidatorDiagnostic(org.eclipse.emf.common.util.Diagnostic diagnostic, IAcceptor<Issue> acceptor) {
            if (diagnostic instanceof BasicDiagnostic) {
                List<?> data = diagnostic.getData();
                if (!data.isEmpty() && data.get(0) instanceof Throwable) {
                    Throwable t = (Throwable) data.get(0);
                    // and AssertionError does not take a throwable as argument
                    throw new Error(t);
                }
            }
        }

        @Override
        public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) {
            if (diagnostic instanceof ExceptionDiagnostic) {
                Exception e = ((ExceptionDiagnostic) diagnostic).getException();
                // and AssertionError does not take a throwable as argument
                throw new Error(new RuntimeException(e));
            }
        }
    });
    validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
}
Also used : Issue(org.eclipse.xtext.validation.Issue) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) Severity(org.eclipse.xtext.diagnostics.Severity) ResourceValidatorImpl(org.eclipse.xtext.validation.ResourceValidatorImpl) IDiagnosticConverter(org.eclipse.xtext.validation.IDiagnosticConverter) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) List(java.util.List)

Example 9 with ExceptionDiagnostic

use of org.eclipse.xtext.diagnostics.ExceptionDiagnostic in project xtext-core by eclipse.

the class DerivedStateAwareResourceTest method testOperationCanceledExceptionOnInstallAndDiscard.

@Test
public void testOperationCanceledExceptionOnInstallAndDiscard() {
    TestedResource resource = new TestedResource();
    resource.setIsLoaded();
    resource.setDerivedStateComputer(new IDerivedStateComputer() {

        @Override
        public void installDerivedState(DerivedStateAwareResource resource, boolean resolve) {
            resource.getContents().add(EcoreFactory.eINSTANCE.createEObject());
            throw new OperationCanceledException();
        }

        @Override
        public void discardDerivedState(DerivedStateAwareResource resource) {
            throw new OperationCanceledException();
        }
    });
    assertFalse(resource.isFullyInitialized());
    try {
        resource.getContents();
        fail("exception expected");
    } catch (IllegalStateException e) {
    // expected
    }
    assertTrue(resource.isFullyInitialized());
    assertFalse(resource.getContents().isEmpty());
    assertEquals(1, resource.getErrors().size());
    assertTrue(((ExceptionDiagnostic) resource.getErrors().get(0)).getException() instanceof IllegalStateException);
}
Also used : OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) Test(org.junit.Test)

Example 10 with ExceptionDiagnostic

use of org.eclipse.xtext.diagnostics.ExceptionDiagnostic in project xtext-core by eclipse.

the class AbstractXtextTests method getResourceAndExpect.

public final XtextResource getResourceAndExpect(InputStream in, URI uri, int expectedErrors) throws Exception {
    XtextResource resource = doGetResource(in, uri);
    checkNodeModel(resource);
    if (expectedErrors != UNKNOWN_EXPECTATION) {
        if (expectedErrors == EXPECT_ERRORS)
            assertFalse(Joiner.on('\n').join(resource.getErrors()), resource.getErrors().isEmpty());
        else
            assertEquals(Joiner.on('\n').join(resource.getErrors()), expectedErrors, resource.getErrors().size());
    }
    for (Diagnostic d : resource.getErrors()) {
        if (d instanceof ExceptionDiagnostic)
            fail(d.getMessage());
    }
    if (expectedErrors == 0 && resource.getContents().size() > 0 && shouldTestSerializer(resource)) {
        SerializerTestHelper tester = get(SerializerTestHelper.class);
        EObject obj = resource.getContents().get(0);
        tester.assertSerializeWithNodeModel(obj);
        tester.assertSerializeWithoutNodeModel(obj);
    }
    return resource;
}
Also used : SerializerTestHelper(org.eclipse.xtext.testing.serializer.SerializerTestHelper) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) EObject(org.eclipse.emf.ecore.EObject) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) XtextResource(org.eclipse.xtext.resource.XtextResource)

Aggregations

ExceptionDiagnostic (org.eclipse.xtext.diagnostics.ExceptionDiagnostic)12 Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)6 BasicDiagnostic (org.eclipse.emf.common.util.BasicDiagnostic)5 Resource (org.eclipse.emf.ecore.resource.Resource)5 XtextResource (org.eclipse.xtext.resource.XtextResource)5 List (java.util.List)4 Severity (org.eclipse.xtext.diagnostics.Severity)4 IDiagnosticConverter (org.eclipse.xtext.validation.IDiagnosticConverter)4 Issue (org.eclipse.xtext.validation.Issue)4 ResourceValidatorImpl (org.eclipse.xtext.validation.ResourceValidatorImpl)4 Test (org.junit.Test)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 TransformationDiagnostic (org.eclipse.xtext.xtext.ecoreInference.TransformationDiagnostic)3 IOException (java.io.IOException)2 ComparisonFailure (org.junit.ComparisonFailure)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 EObject (org.eclipse.emf.ecore.EObject)1 Grammar (org.eclipse.xtext.Grammar)1 LazyLinkingResource (org.eclipse.xtext.linking.lazy.LazyLinkingResource)1 SerializerTestHelper (org.eclipse.xtext.testing.serializer.SerializerTestHelper)1