public void testRemoveValidationMessageWhenRemovingRule() {
    final IValidator rule = new AlwaysWrongValidator();
    valueBindingSupport.addValidationMessage("foo", rule); // $NON-NLS-1$
    valueBindingSupport.addValidationRule(rule, ValidationTime.ON_UPDATE_TO_MODEL);

    target.setValue("value"); // $NON-NLS-1$

    assertEquals(1, markable.getMarkersOfType(ValidationMessageMarker.class).size());

    valueBindingSupport.removeValidationRule(rule);

    assertEquals(0, markable.getMarkersOfType(ValidationMessageMarker.class).size());
  }
  public void testAddAndRemoveValidationMessageWithoutRule() {
    assertEquals(0, markable.getMarkersOfType(ValidationMessageMarker.class).size());

    final MinLength rule = new MinLength(3);
    valueBindingSupport.addValidationRule(rule, ValidationTime.ON_UPDATE_TO_MODEL);
    valueBindingSupport.addValidationMessage("too short"); // $NON-NLS-1$
    target.setValue("a"); // $NON-NLS-1$

    assertEquals(1, markable.getMarkersOfType(ValidationMessageMarker.class).size());
    assertMessageMarkers("too short"); // $NON-NLS-1$

    valueBindingSupport.removeValidationMessage("too short"); // $NON-NLS-1$

    assertEquals(0, markable.getMarkersOfType(ValidationMessageMarker.class).size());
  }
  /** As per Bug 289458 */
  public void testShowErrorAfterValidationOnUpdateWithBlock() {
    final ValidRange rule = new ValidRange(18, 80);
    valueBindingSupport.addValidationRule(rule, ValidationTime.ON_UPDATE_TO_MODEL);
    final IStatus errorStatus = rule.validate("81"); // $NON-NLS-1$
    assertEquals(ValidationRuleStatus.ERROR_BLOCK_WITH_FLASH, errorStatus.getCode());

    assertEquals(0, markable.getMarkersOfType(ErrorMarker.class).size());

    valueBindingSupport.updateValidationStatus(rule, errorStatus);

    assertEquals(1, markable.getMarkersOfType(ErrorMarker.class).size());

    final IStatus okStatus = rule.validate("80"); // $NON-NLS-1$
    valueBindingSupport.updateValidationStatus(rule, okStatus);

    assertEquals(0, markable.getMarkersOfType(ErrorMarker.class).size());
  }
  public void testValidationMessagesAddAndRemoveWhileActive() throws Exception {
    valueBindingSupport.addValidationRule(
        new EvenNumberOfCharacters(), ValidationTime.ON_UPDATE_TO_MODEL);
    target.setValue("odd"); // $NON-NLS-1$

    assertEquals(1, markable.getMarkers().size());
    assertTrue(markable.getMarkers().iterator().next() instanceof ErrorMarker);

    valueBindingSupport.addValidationMessage("TestMessage1"); // $NON-NLS-1$

    assertEquals(2, markable.getMarkers().size());
    assertMessageMarkers("TestMessage1"); // $NON-NLS-1$

    final MessageMarker messageMarker = new MessageMarker("TestMessage2"); // $NON-NLS-1$
    valueBindingSupport.addValidationMessage(messageMarker);

    assertEquals(3, markable.getMarkers().size());
    assertMessageMarkers("TestMessage1", "TestMessage2"); // $NON-NLS-1$ //$NON-NLS-2$

    valueBindingSupport.removeValidationMessage("TestMessage1"); // $NON-NLS-1$

    assertEquals(2, markable.getMarkers().size());
    assertMessageMarkers("TestMessage2"); // $NON-NLS-1$

    valueBindingSupport.removeValidationMessage(messageMarker);

    assertEquals(1, markable.getMarkers().size());
    assertTrue(markable.getMarkers().iterator().next() instanceof ErrorMarker);
  }
  private void assertMessageMarkers(final String... messages) {
    final Collection<String> missingMessages = new ArrayList<String>(Arrays.asList(messages));

    for (final IMessageMarker messageMarker : markable.getMarkersOfType(IMessageMarker.class)) {
      missingMessages.remove(messageMarker.getMessage());
    }

    assertTrue(
        "missing MessageMarker for " + missingMessages, missingMessages.isEmpty()); // $NON-NLS-1$
  }
  public void testSpecialValidationMessages() throws Exception {
    final EvenNumberOfCharacters evenNumberOfCharacters = new EvenNumberOfCharacters();
    final NotEndingWithDisaster notEndingWithDisaster = new NotEndingWithDisaster();
    valueBindingSupport.addValidationRule(
        evenNumberOfCharacters, ValidationTime.ON_UPDATE_TO_MODEL);
    valueBindingSupport.addValidationRule(notEndingWithDisaster, ValidationTime.ON_UPDATE_TO_MODEL);
    valueBindingSupport.addValidationMessage(
        "TestNotEvenMessage1", evenNumberOfCharacters); // $NON-NLS-1$
    valueBindingSupport.addValidationMessage(
        new MessageMarker("TestNotEvenMessage2"), evenNumberOfCharacters); // $NON-NLS-1$
    valueBindingSupport.addValidationMessage(
        "TestDisasterMessage", notEndingWithDisaster); // $NON-NLS-1$

    assertEquals(0, markable.getMarkers().size());

    target.setValue("Disaster"); // $NON-NLS-1$

    assertEquals(2, markable.getMarkers().size());
    assertMessageMarkers("TestDisasterMessage"); // $NON-NLS-1$

    target.setValue("Disaster Area"); // $NON-NLS-1$

    assertEquals(3, markable.getMarkers().size());
    assertMessageMarkers("TestNotEvenMessage1", "TestNotEvenMessage2"); // $NON-NLS-1$ //$NON-NLS-2$

    target.setValue("We are teetering on the brink of disaster"); // $NON-NLS-1$

    assertEquals(5, markable.getMarkers().size());
    assertMessageMarkers(
        "TestNotEvenMessage1",
        "TestNotEvenMessage2",
        "TestDisasterMessage"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    target.setValue("Save again"); // $NON-NLS-1$

    assertEquals(0, markable.getMarkers().size());
  }
  /** Tests that adding the same validation several times has no effect */
  public void testAddSameValidationMessage() {
    final EvenNumberOfCharacters rule = new EvenNumberOfCharacters();
    valueBindingSupport.addValidationRule(rule, ValidationTime.ON_UPDATE_TO_MODEL);
    valueBindingSupport.addValidationMessage("TestMessage1"); // $NON-NLS-1$
    valueBindingSupport.addValidationMessage("TestMessage1"); // $NON-NLS-1$
    valueBindingSupport.addValidationMessage("TestMessage2", rule); // $NON-NLS-1$
    valueBindingSupport.addValidationMessage("TestMessage2", rule); // $NON-NLS-1$
    final MessageMarker messageMarker = new MessageMarker("TestMessage3"); // $NON-NLS-1$
    valueBindingSupport.addValidationMessage(messageMarker);
    valueBindingSupport.addValidationMessage(messageMarker);
    final MessageMarker messageMarker2 = new MessageMarker("TestMessage4"); // $NON-NLS-1$
    valueBindingSupport.addValidationMessage(messageMarker2, rule);
    valueBindingSupport.addValidationMessage(messageMarker2, rule);

    assertEquals(0, markable.getMarkers().size());

    target.setValue("odd"); // $NON-NLS-1$

    assertEquals(4, markable.getMarkersOfType(ValidationMessageMarker.class).size());

    target.setValue("even"); // $NON-NLS-1$

    assertEquals(0, markable.getMarkers().size());
  }
  public void testAddValidationMessageForUnknownRule() {
    final AlwaysWrongValidator rule = new AlwaysWrongValidator();
    valueBindingSupport.addValidationMessage("foo", rule); // $NON-NLS-1$

    assertEquals(0, markable.getMarkers().size());
  }