org.eclipse.xtext.validation.AbstractValidationDiagnostic Java Examples

The following examples show how to use org.eclipse.xtext.validation.AbstractValidationDiagnostic. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractValidationTest.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Persist list diagnostics into string to display the list of issue codes.
 *
 * @param diagnostics
 *          list of diagnostics
 * @param displayPathToTargetObject
 *          if true, the path through the object hierarchy is printed out up to the root node
 * @return
 *         string with list of issue codes, separated with a line break
 */
// TODO (ACF-4153) generalize for all kinds of errors and move to AbstractXtextTest
private String diagnosticsToString(final Diagnostic diagnostics, final boolean displayPathToTargetObject) {
  StringBuilder sb = new StringBuilder();
  for (Diagnostic diagnostic : diagnostics.getChildren()) {
    if (diagnostic instanceof AbstractValidationDiagnostic) {
      AbstractValidationDiagnostic avd = (AbstractValidationDiagnostic) diagnostic;
      sb.append("    ");
      sb.append(avd.getIssueCode());
      if (displayPathToTargetObject) {
        sb.append(" at line: ");
        sb.append(NodeModelUtils.findActualNodeFor(avd.getSourceEObject()).getStartLine());
        sb.append(" on \n");
        sb.append(pathFromRootAsString(avd.getSourceEObject(), "      "));
      }
      sb.append(LINE_BREAK);
    }
  }
  return sb.toString();
}
 
Example #2
Source File: AbstractXtextTestUtil.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets the offset and the error message for a given {@link Diagnostic}.
 *
 * @param diagnostic
 *          instance of {@link Diagnostic}
 * @return
 *         offset and error message
 */
private Pair<Integer, String> processDiagnostic(final Diagnostic diagnostic) {
  StringBuilder errorMessage = new StringBuilder();
  if (diagnostic instanceof AbstractValidationDiagnostic) {
    AbstractValidationDiagnostic avd = (AbstractValidationDiagnostic) diagnostic;
    errorMessage.append("Unexpected issue found. Code '");
    errorMessage.append(avd.getIssueCode()).append("'\n");
    errorMessage.append(avd.getMessage());
    if (avd instanceof FeatureBasedDiagnostic && ((FeatureBasedDiagnostic) avd).getFeature() != null) {
      List<INode> nodes = NodeModelUtils.findNodesForFeature(avd.getSourceEObject(), ((FeatureBasedDiagnostic) avd).getFeature());
      if (nodes != null && !nodes.isEmpty()) {
        return new Pair<Integer, String>(findFirstNonHiddenLeafNode(nodes.get(0)).getTotalOffset(), errorMessage.toString());
      }
    } else if (avd instanceof RangeBasedDiagnostic) {
      return new Pair<Integer, String>(((RangeBasedDiagnostic) avd).getOffset(), errorMessage.toString());
    } else {
      return new Pair<Integer, String>(NodeModelUtils.getNode(avd.getSourceEObject()).getTotalOffset(), errorMessage.toString());
    }
  }
  return null;
}
 
Example #3
Source File: N4JSDiagnosticConverter.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** Overwritten to catch stacktrace. */
@Override
protected String[] getIssueData(org.eclipse.emf.common.util.Diagnostic diagnostic) {
	if (diagnostic instanceof AbstractValidationDiagnostic) {
		AbstractValidationDiagnostic diagnosticImpl = (AbstractValidationDiagnostic) diagnostic;
		return diagnosticImpl.getIssueData();
	} else {
		// replace any EObjects by their URIs
		EObject causer = getCauser(diagnostic);
		List<String> issueData = newArrayList();
		for (Object object : diagnostic.getData()) {
			if (object != causer && object instanceof EObject) {
				EObject eObject = (EObject) object;
				issueData.add(EcoreUtil.getURI(eObject).toString());
			} else if (object instanceof String) {
				issueData.add((String) object);
			} else if (object instanceof Throwable) {
				/** Catch this throwable and put it into the message */
				Throwable thr = ((Throwable) object);
				StringWriter sw = new StringWriter();
				thr.printStackTrace(new PrintWriter(sw));
				String stacktraceAsString = sw.toString();
				issueData.add(stacktraceAsString);
			}
		}
		return issueData.toArray(new String[issueData.size()]);
	}
}
 
Example #4
Source File: AssertableDiagnostics.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean apply(Diagnostic d) {
	if (severity != null && d.getSeverity() != severity)
		return false;
	if (code != null && !code.equals(d.getCode()))
		return false;
	if (issueCode != null && d instanceof AbstractValidationDiagnostic
			&& !((AbstractValidationDiagnostic) d).getIssueCode().equals(issueCode))
		return false;
	if (msg != null && d.getMessage() != null && !d.getMessage().contains(msg))
		return false;
	return true;
}
 
Example #5
Source File: AssertableDiagnostics.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String toString() {
	List<String> r = new ArrayList<String>();
	if (severity != null)
		r.add(AbstractValidationDiagnostic.severityToStr(severity));
	if (issueCode != null)
		r.add("issueCode=" + issueCode);
	if (code != null)
		r.add("code=" + code);
	if (msg != null)
		r.add("msgFragment='" + msg + "'");
	return "(" + Joiner.on(" ").join(r) + ")";
}
 
Example #6
Source File: AbstractValidationTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check if the given issue code is found among issue codes for the object, located at the given position.
 *
 * @param root
 *          root object of the document
 * @param pos
 *          position to locate the target object
 */
@Override
public void apply(final EObject root, final Integer pos) {
  final Diagnostic diagnostics = validate(root);
  final BasicDiagnostic diagnosticsOnTargetPosition = new BasicDiagnostic();
  boolean issueFound = false;
  int actualSeverity = SEVERITY_UNDEFINED;
  boolean expectedSeverityMatches = false;
  boolean expectedMessageMatches = false;
  String actualMessage = "";

  for (AbstractValidationDiagnostic avd : Iterables.filter(diagnostics.getChildren(), AbstractValidationDiagnostic.class)) {
    if (diagnosticPositionEquals(pos, avd)) {
      // Add issue to the list of issues at the given position
      diagnosticsOnTargetPosition.add(avd);
      if (avd.getIssueCode().equals(issueCode)) {
        issueFound = true;
        actualSeverity = avd.getSeverity();
        // True if the expected severity is not set, or if matches with the actual one
        expectedSeverityMatches = expectedSeverity == SEVERITY_UNDEFINED || expectedSeverity == actualSeverity;
        actualMessage = avd.getMessage();
        // True if message matches with actual message or message is null
        expectedMessageMatches = message == null || actualMessage.equals(message);
        if (issueMustBeFound) {
          // Remove the diagnostic from the list of non-expected diagnostics
          getUnexpectedDiagnostics().remove(avd);
          // Don't need to display error messages
          if (expectedSeverityMatches && expectedMessageMatches) {
            return;
          }
        }
      }
    }
  }

  // Create error message
  createErrorMessage(pos, diagnosticsOnTargetPosition, issueFound, expectedSeverityMatches, actualSeverity, expectedMessageMatches, actualMessage);
}
 
Example #7
Source File: AbstractValidationTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Assert that diagnosticList contains a diagnostic of the given issueCode.
 *
 * @param diagnostics
 *          the diagnostic to check for issues
 * @param issueCode
 *          the code of the issue to look for
 */
private void assertDiagnostic(final Diagnostic diagnostics, final String issueCode) {
  for (Diagnostic diagnostic : diagnostics.getChildren()) {
    if (diagnostic instanceof AbstractValidationDiagnostic && ((AbstractValidationDiagnostic) diagnostic).getIssueCode().equals(issueCode)) {
      return;
    }
  }
  fail("Issue with code '" + issueCode + "' not found");
}
 
Example #8
Source File: AbstractValidationTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Assert that diagnosticList does not contain a diagnostic of the given issueCode.
 *
 * @param diagnostics
 *          the diagnostic to check for issues
 * @param issueCode
 *          the code of the issue to look for
 */
private void assertNoDiagnostic(final Diagnostic diagnostics, final String issueCode) {
  for (Diagnostic diagnostic : diagnostics.getChildren()) {
    if (((AbstractValidationDiagnostic) diagnostic).getIssueCode().equals(issueCode)) {
      fail("Issue with code '" + issueCode + "' found");
      return;
    }
  }
}
 
Example #9
Source File: AbstractValidationTest.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Assert that diagnosticList contains a diagnostic of the given issueCode on a given EObject.
 * For performance reasons one can validate the root object and afterwards use this method
 * to check that a particular diagnostic exists on one of the child objects of the validated model.
 *
 * @param diagnostics
 *          the diagnostic to check for issues
 * @param targetObject
 *          the object that should have a diagnostic with the given issueCode
 * @param issueCode
 *          the code of the issue to look for
 */
protected void assertDiagnosticOnObject(final Diagnostic diagnostics, final EObject targetObject, final String issueCode) {
  for (Diagnostic diagnostic : diagnostics.getChildren()) {
    if (diagnostic instanceof AbstractValidationDiagnostic) {
      AbstractValidationDiagnostic avd = (AbstractValidationDiagnostic) diagnostic;
      if (avd.getSourceEObject() == targetObject && avd.getIssueCode().equals(issueCode)) {
        return;
      }
    }
  }
  fail("Issue with code '" + issueCode + "' not found");
}
 
Example #10
Source File: AbstractValidValidationTest.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Checks that there is no occurrence of a diagnostic predicate in a diagnostic list.
 *
 * @param validate
 *          the diagnostic list in which to look for a diagnostic predicate
 * @param predicate
 *          the predicate diagnostic to look for
 */
private void assertNot(final AssertableDiagnostics validate, final DiagnosticPredicate predicate) {
  if (Iterables.any(Iterables.filter(validate.getAllDiagnostics(), AbstractValidationDiagnostic.class), predicate)) {
    fail("Predicate " + predicate.toString() + " found.");
  }
}