Skip to content

Commit

Permalink
test: restructure and enhance validation unit tests (#4350)
Browse files Browse the repository at this point in the history
* test: add basic validation unit tests

* remove unused imports

* add license headers

* Update vaadin-text-field-flow-parent/vaadin-text-field-flow/src/test/java/com/vaadin/flow/component/textfield/validation/AbstractBasicValidationTest.java

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>

* Update vaadin-text-field-flow-parent/vaadin-text-field-flow/src/test/java/com/vaadin/flow/component/textfield/validation/AbstractBasicValidationTest.java

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>

* Update vaadin-text-field-flow-parent/vaadin-text-field-flow/src/test/java/com/vaadin/flow/component/textfield/validation/AbstractBasicValidationTest.java

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>

* Update vaadin-text-field-flow-parent/vaadin-text-field-flow/src/test/java/com/vaadin/flow/component/textfield/validation/AbstractBasicValidationTest.java

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
  • Loading branch information
vursen and sissbruecker committed Dec 14, 2022
1 parent fc149fb commit c913d04
Show file tree
Hide file tree
Showing 21 changed files with 461 additions and 230 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.textfield.validation;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasValidation;

public abstract class AbstractBasicValidationTest<T extends Component & HasValidation> {
protected T testField;

@Before
public void setup() {
testField = createTestField();
}

@Test
public void setErrorMessage_getErrorMessage() {
Assert.assertNull(testField.getErrorMessage());
Assert.assertNull(testField.getElement().getProperty("errorMessage"));

testField.setErrorMessage("Error");

Assert.assertEquals("Error", testField.getErrorMessage());
Assert.assertEquals("Error",
testField.getElement().getProperty("errorMessage"));
}

@Test
public void setInvalid_isInvalid() {
Assert.assertFalse(testField.isInvalid());
Assert.assertEquals("false",
testField.getElement().getProperty("invalid"));

testField.setInvalid(true);

Assert.assertTrue(testField.isInvalid());
Assert.assertEquals("true",
testField.getElement().getProperty("invalid"));
}

abstract protected T createTestField();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package com.vaadin.flow.component.textfield.binder;
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.textfield.validation;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasValue;
Expand All @@ -11,7 +26,7 @@
import org.junit.Test;
import org.mockito.*;

public abstract class AbstractTextFieldValidationTest<T, K extends Component & HasValue<?, T>> {
public abstract class AbstractBinderValidationTest<T, K extends Component & HasValue<?, T>> {

private static final String BINDER_FAIL_MESSAGE = "BINDER_VALIDATION_FAIL";
private static final String BINDER_REQUIRED_MESSAGE = "REQUIRED";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.textfield.tests;
package com.vaadin.flow.component.textfield.validation;

import com.vaadin.flow.component.HasValidation;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.testutil.ValidationTestView;
import com.vaadin.flow.component.textfield.BigDecimalField;

/**
* View for testing validation with {@link TextField}.
*/
@Route("vaadin-text-field/text-field-validation")
public class TextFieldValidationPage extends ValidationTestView {

@Override
protected HasValidation getValidationComponent() {
return new TextField();
public class BigDecimalFieldBasicValidationTest
extends AbstractBasicValidationTest<BigDecimalField> {
protected BigDecimalField createTestField() {
return new BigDecimalField();
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package com.vaadin.flow.component.textfield.binder;
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.textfield.validation;

import com.vaadin.flow.component.textfield.BigDecimalField;
import com.vaadin.flow.function.SerializablePredicate;
import org.junit.Ignore;

import java.math.BigDecimal;

public class BigDecimalFieldValidationTest
extends AbstractTextFieldValidationTest<BigDecimal, BigDecimalField> {
public class BigDecimalFieldBinderValidationTest
extends AbstractBinderValidationTest<BigDecimal, BigDecimalField> {

@Override
protected void initField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.textfield.tests;
package com.vaadin.flow.component.textfield.validation;

import com.vaadin.flow.component.HasValidation;
import com.vaadin.flow.component.textfield.PasswordField;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.testutil.ValidationTestView;
import com.vaadin.flow.component.textfield.EmailField;

/**
* View for testing validation with {@link PasswordField}.
*/
@Route("vaadin-text-field/passwork-field-validation")
public class PasswordFieldValidationPage extends ValidationTestView {

@Override
protected HasValidation getValidationComponent() {
return new PasswordField();
public class EmailFieldBasicValidationTest
extends AbstractBasicValidationTest<EmailField> {
protected EmailField createTestField() {
return new EmailField();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package com.vaadin.flow.component.textfield.binder;
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.textfield.validation;

import com.vaadin.flow.component.textfield.EmailField;
import com.vaadin.flow.function.SerializablePredicate;

import java.util.Objects;

public class EmailFieldValidationTest
extends AbstractTextFieldValidationTest<String, EmailField> {
public class EmailFieldBinderValidationTest
extends AbstractBinderValidationTest<String, EmailField> {

@Override
protected void initField() {
Expand Down
Loading

0 comments on commit c913d04

Please sign in to comment.