diff options
author | Henri Sara <hesara@vaadin.com> | 2013-01-17 09:07:09 +0000 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-01-17 09:07:09 +0000 |
commit | e27fa2111f73828afcb513afb77b957d69bc8f58 (patch) | |
tree | 3685259296de9c7e0c80710fa124e73eb902da99 /server/tests/src/com | |
parent | 75a09fb9df67afbc9042b90f41b199f17387a53d (diff) | |
parent | 662ce8bdfb90c3afb07efd1f5a9f3d95877897e9 (diff) | |
download | vaadin-framework-e27fa2111f73828afcb513afb77b957d69bc8f58.tar.gz vaadin-framework-e27fa2111f73828afcb513afb77b957d69bc8f58.zip |
Merge "(#10716 ) Created tests for validators and converters. Fixed StringToBoolean so that it accepts empty string."
Diffstat (limited to 'server/tests/src/com')
13 files changed, 573 insertions, 0 deletions
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestDateToLongConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestDateToLongConverter.java new file mode 100644 index 0000000000..aab4ae18f0 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestDateToLongConverter.java @@ -0,0 +1,21 @@ +package com.vaadin.tests.data.converter; + +import java.util.Date; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.DateToLongConverter; + +public class TestDateToLongConverter extends TestCase { + + DateToLongConverter converter = new DateToLongConverter(); + + public void testNullConversion() { + assertEquals(null, converter.convertToModel(null, null)); + } + + public void testValueConversion() { + assertEquals(Long.valueOf(946677600000l), + converter.convertToModel(new Date(100, 0, 1), null)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToBooleanConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBooleanConverter.java new file mode 100644 index 0000000000..1c2b919db0 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBooleanConverter.java @@ -0,0 +1,23 @@ +package com.vaadin.tests.data.converter; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.StringToBooleanConverter; + +public class TestStringToBooleanConverter extends TestCase { + + StringToBooleanConverter converter = new StringToBooleanConverter(); + + public void testNullConversion() { + assertEquals(null, converter.convertToModel(null, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", null)); + } + + public void testValueConversion() { + assertTrue(converter.convertToModel("true", null)); + assertFalse(converter.convertToModel("false", null)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToDateConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToDateConverter.java new file mode 100644 index 0000000000..2961b93c89 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToDateConverter.java @@ -0,0 +1,26 @@ +package com.vaadin.tests.data.converter; + +import java.util.Date; +import java.util.Locale; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.StringToDateConverter; + +public class TestStringToDateConverter extends TestCase { + + StringToDateConverter converter = new StringToDateConverter(); + + public void testNullConversion() { + assertEquals(null, converter.convertToModel(null, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", null)); + } + + public void testValueConversion() { + assertEquals(new Date(100, 0, 1), converter.convertToModel( + "Jan 1, 2000 12:00:00 AM", Locale.ENGLISH)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToDoubleConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToDoubleConverter.java new file mode 100644 index 0000000000..9903a8deaa --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToDoubleConverter.java @@ -0,0 +1,22 @@ +package com.vaadin.tests.data.converter; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.StringToDoubleConverter; + +public class TestStringToDoubleConverter extends TestCase { + + StringToDoubleConverter converter = new StringToDoubleConverter(); + + public void testNullConversion() { + assertEquals(null, converter.convertToModel(null, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", null)); + } + + public void testValueConversion() { + assertEquals(10.0, converter.convertToModel("10", null)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToFloatConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToFloatConverter.java new file mode 100644 index 0000000000..670daec202 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToFloatConverter.java @@ -0,0 +1,22 @@ +package com.vaadin.tests.data.converter; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.StringToFloatConverter; + +public class TestStringToFloatConverter extends TestCase { + + StringToFloatConverter converter = new StringToFloatConverter(); + + public void testNullConversion() { + assertEquals(null, converter.convertToModel(null, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", null)); + } + + public void testValueConversion() { + assertEquals(Float.valueOf(10), converter.convertToModel("10", null)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToIntegerConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToIntegerConverter.java new file mode 100644 index 0000000000..80414cccaf --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToIntegerConverter.java @@ -0,0 +1,22 @@ +package com.vaadin.tests.data.converter; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.StringToIntegerConverter; + +public class TestStringToIntegerConverter extends TestCase { + + StringToIntegerConverter converter = new StringToIntegerConverter(); + + public void testNullConversion() { + assertEquals(null, converter.convertToModel(null, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", null)); + } + + public void testValueConversion() { + assertEquals(Integer.valueOf(10), converter.convertToModel("10", null)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java new file mode 100644 index 0000000000..d3f87d628a --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java @@ -0,0 +1,23 @@ +package com.vaadin.tests.data.converter; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.StringToNumberConverter; + +public class TestStringToNumberConverter extends TestCase { + + StringToNumberConverter converter = new StringToNumberConverter(); + + public void testNullConversion() { + assertEquals(null, converter.convertToModel(null, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", null)); + } + + public void testValueConversion() { + assertEquals(Long.valueOf(10), converter.convertToModel("10", null)); + assertEquals(10.5, converter.convertToModel("10.5", null)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/validator/TestCompositeValidator.java b/server/tests/src/com/vaadin/tests/data/validator/TestCompositeValidator.java new file mode 100644 index 0000000000..1d69c77a3d --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/validator/TestCompositeValidator.java @@ -0,0 +1,116 @@ +package com.vaadin.tests.data.validator; + +import junit.framework.TestCase; + +import com.vaadin.data.Validator; +import com.vaadin.data.validator.CompositeValidator; +import com.vaadin.data.validator.CompositeValidator.CombinationMode; +import com.vaadin.data.validator.EmailValidator; +import com.vaadin.data.validator.RegexpValidator; + +public class TestCompositeValidator extends TestCase { + + CompositeValidator and = new CompositeValidator(CombinationMode.AND, + "One validator not valid"); + CompositeValidator or = new CompositeValidator(CombinationMode.OR, + "No validators are valid"); + EmailValidator email = new EmailValidator("Faulty email"); + RegexpValidator regex = new RegexpValidator("@mail.com", false, + "Partial match validator error"); + + @Override + protected void setUp() throws Exception { + super.setUp(); + + and.addValidator(email); + and.addValidator(regex); + + or.addValidator(email); + or.addValidator(regex); + } + + public void testCorrectValue() { + String testString = "user@mail.com"; + assertTrue(email.isValid(testString)); + assertTrue(regex.isValid(testString)); + try { + // notNull.validate(null); + // fail("expected null to fail with an exception"); + and.validate(testString); + } catch (Validator.InvalidValueException ex) { + // assertEquals("Null not accepted", ex.getMessage()); + fail("And validator should be valid"); + } + try { + or.validate(testString); + } catch (Validator.InvalidValueException ex) { + // assertEquals("Null not accepted", ex.getMessage()); + fail("And validator should be valid"); + } + } + + public void testCorrectRegex() { + + String testString = "@mail.com"; + assertFalse(testString + " should not validate", + email.isValid(testString)); + assertTrue(testString + "should validate", regex.isValid(testString)); + try { + // notNull.validate(null); + and.validate(testString); + fail("expected and to fail with an exception"); + } catch (Validator.InvalidValueException ex) { + assertEquals("Faulty email", ex.getMessage()); + // fail("And validator should be valid"); + } + try { + or.validate(testString); + } catch (Validator.InvalidValueException ex) { + // assertEquals("Null not accepted", ex.getMessage()); + fail("Or validator should be valid"); + } + } + + public void testCorrectEmail() { + + String testString = "user@gmail.com"; + + assertTrue(testString + " should validate", email.isValid(testString)); + assertFalse(testString + " should not validate", + regex.isValid(testString)); + try { + and.validate(testString); + fail("expected and to fail with an exception"); + } catch (Validator.InvalidValueException ex) { + assertEquals("Partial match validator error", ex.getMessage()); + } + try { + or.validate(testString); + } catch (Validator.InvalidValueException ex) { + fail("Or validator should be valid"); + } + } + + public void testBothFaulty() { + + String testString = "gmail.com"; + + assertFalse(testString + " should not validate", + email.isValid(testString)); + assertFalse(testString + " should not validate", + regex.isValid(testString)); + try { + and.validate(testString); + fail("expected and to fail with an exception"); + } catch (Validator.InvalidValueException ex) { + assertEquals("Faulty email", ex.getMessage()); + } + try { + or.validate(testString); + fail("expected or to fail with an exception"); + } catch (Validator.InvalidValueException ex) { + assertEquals("No validators are valid", ex.getMessage()); + } + } + +} diff --git a/server/tests/src/com/vaadin/tests/data/validator/TestDateRangeValidator.java b/server/tests/src/com/vaadin/tests/data/validator/TestDateRangeValidator.java new file mode 100644 index 0000000000..cdb1e12545 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/validator/TestDateRangeValidator.java @@ -0,0 +1,97 @@ +package com.vaadin.tests.data.validator; + +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.TimeZone; + +import junit.framework.TestCase; + +import com.vaadin.data.validator.DateRangeValidator; +import com.vaadin.shared.ui.datefield.Resolution; + +public class TestDateRangeValidator extends TestCase { + Calendar startDate = new GregorianCalendar(TimeZone.getTimeZone("GMT"), + Locale.ENGLISH); + Calendar endDate = new GregorianCalendar(TimeZone.getTimeZone("GMT"), + Locale.ENGLISH); + + private DateRangeValidator cleanValidator; + private DateRangeValidator minValidator; + private DateRangeValidator maxValidator; + private DateRangeValidator minMaxValidator; + + @Override + protected void setUp() throws Exception { + super.setUp(); + startDate.set(2000, Calendar.JANUARY, 1, 12, 0, 0); + endDate.set(2000, Calendar.FEBRUARY, 20, 12, 0, 0); + + cleanValidator = new DateRangeValidator("Given date outside range", + null, null, Resolution.DAY); + minValidator = new DateRangeValidator("Given date before startDate", + startDate.getTime(), null, Resolution.DAY); + maxValidator = new DateRangeValidator("Given date after endDate", null, + endDate.getTime(), Resolution.DAY); + minMaxValidator = new DateRangeValidator("Given date outside range", + startDate.getTime(), endDate.getTime(), Resolution.DAY); + } + + public void testNullValue() { + assertTrue("Didn't accept null", cleanValidator.isValid(null)); + assertTrue("Didn't accept null", minValidator.isValid(null)); + assertTrue("Didn't accept null", maxValidator.isValid(null)); + assertTrue("Didn't accept null", minMaxValidator.isValid(null)); + } + + public void testMinValue() { + Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), + Locale.ENGLISH); + cal.setTime(startDate.getTime()); + cal.add(Calendar.SECOND, 1); + + assertTrue("Validator without ranges didn't accept value", + cleanValidator.isValid(cal.getTime())); + assertTrue("Didn't accept valid value", + minValidator.isValid(cal.getTime())); + + cal.add(Calendar.SECOND, -3); + + assertFalse("Accepted too small value", + minValidator.isValid(cal.getTime())); + } + + public void testMaxValue() { + Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), + Locale.ENGLISH); + cal.setTime(endDate.getTime()); + cal.add(Calendar.SECOND, -1); + + assertTrue("Validator without ranges didn't accept value", + cleanValidator.isValid(cal.getTime())); + assertTrue("Didn't accept valid value", + maxValidator.isValid(cal.getTime())); + + cal.add(Calendar.SECOND, 2); + assertFalse("Accepted too large value", + maxValidator.isValid(cal.getTime())); + } + + public void testMinMaxValue() { + Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), + Locale.ENGLISH); + cal.setTime(endDate.getTime()); + + assertTrue("Didn't accept valid value", + minMaxValidator.isValid(cal.getTime())); + cal.add(Calendar.SECOND, 1); + assertFalse("Accepted too large value", + minMaxValidator.isValid(cal.getTime())); + cal.setTime(startDate.getTime()); + assertTrue("Didn't accept valid value", + minMaxValidator.isValid(cal.getTime())); + cal.add(Calendar.SECOND, -1); + assertFalse("Accepted too small value", + minMaxValidator.isValid(cal.getTime())); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/validator/TestDoubleRangeValidator.java b/server/tests/src/com/vaadin/tests/data/validator/TestDoubleRangeValidator.java new file mode 100644 index 0000000000..3fe61e13f4 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/validator/TestDoubleRangeValidator.java @@ -0,0 +1,45 @@ +package com.vaadin.tests.data.validator; + +import junit.framework.TestCase; + +import com.vaadin.data.validator.DoubleRangeValidator; + +public class TestDoubleRangeValidator extends TestCase { + + private DoubleRangeValidator cleanValidator = new DoubleRangeValidator( + "no values", null, null); + private DoubleRangeValidator minValidator = new DoubleRangeValidator( + "no values", 10.1, null); + private DoubleRangeValidator maxValidator = new DoubleRangeValidator( + "no values", null, 100.1); + private DoubleRangeValidator minMaxValidator = new DoubleRangeValidator( + "no values", 10.5, 100.5); + + public void testNullValue() { + assertTrue("Didn't accept null", cleanValidator.isValid(null)); + assertTrue("Didn't accept null", minValidator.isValid(null)); + assertTrue("Didn't accept null", maxValidator.isValid(null)); + assertTrue("Didn't accept null", minMaxValidator.isValid(null)); + } + + public void testMinValue() { + assertTrue("Validator without ranges didn't accept value", + cleanValidator.isValid(-15.0)); + assertTrue("Didn't accept valid value", minValidator.isValid(10.1)); + assertFalse("Accepted too small value", minValidator.isValid(10.0)); + } + + public void testMaxValue() { + assertTrue("Validator without ranges didn't accept value", + cleanValidator.isValid(1120.0)); + assertTrue("Didn't accept valid value", maxValidator.isValid(15.0)); + assertFalse("Accepted too large value", maxValidator.isValid(100.6)); + } + + public void testMinMaxValue() { + assertTrue("Didn't accept valid value", minMaxValidator.isValid(10.5)); + assertTrue("Didn't accept valid value", minMaxValidator.isValid(100.5)); + assertFalse("Accepted too small value", minMaxValidator.isValid(10.4)); + assertFalse("Accepted too large value", minMaxValidator.isValid(100.6)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/validator/TestIntegerRangeValidator.java b/server/tests/src/com/vaadin/tests/data/validator/TestIntegerRangeValidator.java new file mode 100644 index 0000000000..864fc61ab6 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/validator/TestIntegerRangeValidator.java @@ -0,0 +1,45 @@ +package com.vaadin.tests.data.validator; + +import junit.framework.TestCase; + +import com.vaadin.data.validator.IntegerRangeValidator; + +public class TestIntegerRangeValidator extends TestCase { + + private IntegerRangeValidator cleanValidator = new IntegerRangeValidator( + "no values", null, null); + private IntegerRangeValidator minValidator = new IntegerRangeValidator( + "no values", 10, null); + private IntegerRangeValidator maxValidator = new IntegerRangeValidator( + "no values", null, 100); + private IntegerRangeValidator minMaxValidator = new IntegerRangeValidator( + "no values", 10, 100); + + public void testNullValue() { + assertTrue("Didn't accept null", cleanValidator.isValid(null)); + assertTrue("Didn't accept null", minValidator.isValid(null)); + assertTrue("Didn't accept null", maxValidator.isValid(null)); + assertTrue("Didn't accept null", minMaxValidator.isValid(null)); + } + + public void testMinValue() { + assertTrue("Validator without ranges didn't accept value", + cleanValidator.isValid(-15)); + assertTrue("Didn't accept valid value", minValidator.isValid(15)); + assertFalse("Accepted too small value", minValidator.isValid(9)); + } + + public void testMaxValue() { + assertTrue("Validator without ranges didn't accept value", + cleanValidator.isValid(1120)); + assertTrue("Didn't accept valid value", maxValidator.isValid(15)); + assertFalse("Accepted too large value", maxValidator.isValid(120)); + } + + public void testMinMaxValue() { + assertTrue("Didn't accept valid value", minMaxValidator.isValid(15)); + assertTrue("Didn't accept valid value", minMaxValidator.isValid(99)); + assertFalse("Accepted too small value", minMaxValidator.isValid(9)); + assertFalse("Accepted too large value", minMaxValidator.isValid(110)); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/validator/TestNullValidator.java b/server/tests/src/com/vaadin/tests/data/validator/TestNullValidator.java new file mode 100644 index 0000000000..1c838db3ec --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/validator/TestNullValidator.java @@ -0,0 +1,46 @@ +package com.vaadin.tests.data.validator; + +import junit.framework.TestCase; + +import org.junit.Rule; +import org.junit.rules.ExpectedException; + +import com.vaadin.data.Validator; +import com.vaadin.data.validator.NullValidator; + +public class TestNullValidator extends TestCase { + + NullValidator notNull = new NullValidator("Null not accepted", false); + NullValidator onlyNull = new NullValidator("Only null accepted", true); + + @Rule + public ExpectedException exception = ExpectedException.none(); + + public void testNullValue() { + try { + notNull.validate(null); + fail("expected null to fail with an exception"); + } catch (Validator.InvalidValueException ex) { + assertEquals("Null not accepted", ex.getMessage()); + } + try { + onlyNull.validate(null); + } catch (Validator.InvalidValueException ex) { + fail("onlyNull should not throw exception for null"); + } + } + + public void testNonNullValue() { + try { + onlyNull.validate("Not a null value"); + fail("expected onlyNull validator to fail with an exception"); + } catch (Validator.InvalidValueException ex) { + assertEquals("Only null accepted", ex.getMessage()); + } + try { + notNull.validate("Not a null value"); + } catch (Validator.InvalidValueException ex) { + fail("notNull should not throw exception for \"Not a null value\""); + } + } +} diff --git a/server/tests/src/com/vaadin/tests/data/validator/TestStringLengthValidator.java b/server/tests/src/com/vaadin/tests/data/validator/TestStringLengthValidator.java new file mode 100644 index 0000000000..032b8b6d14 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/validator/TestStringLengthValidator.java @@ -0,0 +1,65 @@ +package com.vaadin.tests.data.validator; + +import junit.framework.TestCase; + +import com.vaadin.data.validator.StringLengthValidator; + +public class TestStringLengthValidator extends TestCase { + + private StringLengthValidator validator = new StringLengthValidator("Error"); + private StringLengthValidator validatorNoNull = new StringLengthValidator( + "Error", 1, 5, false); + private StringLengthValidator validatorMinValue = new StringLengthValidator( + "Error", 5, null, true); + private StringLengthValidator validatorMaxValue = new StringLengthValidator( + "Error", null, 15, true); + + public void testValidatorWithNull() { + assertTrue("Didn't accept null", validator.isValid(null)); + assertTrue("Didn't accept null", validatorMinValue.isValid(null)); + } + + public void testValidatorNotAcceptingNull() { + assertFalse("Accepted null", validatorNoNull.isValid(null)); + } + + public void testEmptyString() { + assertTrue("Didn't accept empty String", validator.isValid("")); + assertTrue("Didn't accept empty String", validatorMaxValue.isValid("")); + assertFalse("Accepted empty string even though has lower bound of 1", + validatorNoNull.isValid("")); + assertFalse("Accepted empty string even though has lower bound of 5", + validatorMinValue.isValid("")); + } + + public void testTooLongString() { + assertFalse("Too long string was accepted", + validatorNoNull.isValid("This string is too long")); + assertFalse("Too long string was accepted", + validatorMaxValue.isValid("This string is too long")); + } + + public void testNoUpperBound() { + assertTrue( + "String not accepted even though no upper bound", + validatorMinValue + .isValid("This is a really long string to test that no upper bound exists")); + } + + public void testNoLowerBound() { + assertTrue("Didn't accept short string", validatorMaxValue.isValid("")); + assertTrue("Didn't accept short string", validatorMaxValue.isValid("1")); + } + + public void testStringLengthValidatorWithOkStringLength() { + assertTrue("Didn't accept string of correct length", + validatorNoNull.isValid("OK!")); + assertTrue("Didn't accept string of correct length", + validatorMaxValue.isValid("OK!")); + } + + public void testTooShortStringLength() { + assertFalse("Accepted a string that was too short.", + validatorMinValue.isValid("shot")); + } +} |