package com.vaadin.tests.server.component.textfield;

import junit.framework.TestCase;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.validator.RangeValidator;
import com.vaadin.tests.data.converter.ConverterFactory.ConvertTo42;
import com.vaadin.ui.TextField;

public class TextFieldWithConverterAndValidator extends TestCase {

    private TextField field;
    private ObjectProperty<Integer> property;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        field = new TextField();
        field.setInvalidAllowed(false);
    }

    public void testConvert42AndValidator() {
        property = new ObjectProperty<Integer>(123);
        field.setConverter(new ConvertTo42());
        field.setPropertyDataSource(property);

        field.addValidator(new RangeValidator<Integer>("Incorrect value",
                Integer.class, 42, 42));

        // succeeds
        field.setValue("a");
        // succeeds
        field.setValue("42");
        // succeeds - no validation
        property.setValue(42);

        // nulls

        // fails
        try {
            property.setValue(null);
            fail();
        } catch (InvalidValueException e) {
            // should fail
        }
        // succeeds
        field.setValue(null);
    }

    // TODO test converter changing value to null with validator
}

@@ -85,64 +85,8 @@
 e;%0A%0A
-import com.vaadin.data.Validator.InvalidValueException;%0A
 impo
@@ -1094,32 +1094,102 @@
  // 
-fails%0A        try %7B%0A    
+succeeds - validate() converts field value back to property type%0A        // before validation%0A
     
@@ -1229,100 +1229,25 @@
     
- 
-   fail();%0A        %7D catch (InvalidValueException e) %7B%0A            // should fail%0A        %7D
+field.validate();
 %0A   

Fix validator test (#10713)