From 61cdb4877a4aaf70fae3d2f444bc737443de00e8 Mon Sep 17 00:00:00 2001 From: felmer <franz-josef.elmer@id.ethz.ch> Date: Mon, 20 May 2019 13:04:58 +0200 Subject: [PATCH] SSDM-8101: fixing bug in SimplePropertyValidator.TimestampValidator. SimplePropertyValidatorTest introduced. --- .../shared/util/SimplePropertyValidator.java | 25 ++---- .../util/SimplePropertyValidatorTest.java | 90 +++++++++++++++++++ 2 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/util/SimplePropertyValidatorTest.java diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/SimplePropertyValidator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/SimplePropertyValidator.java index b51495b33de..cf9a047d67d 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/SimplePropertyValidator.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/SimplePropertyValidator.java @@ -209,7 +209,7 @@ public class SimplePropertyValidator /** * Manually validates the date value on cases which are omitted by DateUtils. * - * @param value the date-time value to validate. + * @param value the date-time value to validate. * @throws UserFailureException thrown if the value is not considered as a well formatted date. */ private void validateHyphens(final String value) throws UserFailureException @@ -218,18 +218,15 @@ public class SimplePropertyValidator { return; } - - final String dateValue = extractDate(value); - final boolean hyphenFormat = dateValue.matches("\\d*-\\d*-\\d*"); - final boolean desiredHyphenFormat = dateValue.matches("\\d{4,}-\\d+-\\d+"); - if (hyphenFormat && !desiredHyphenFormat) + int indexOfHyphen = value.indexOf('-'); + if (indexOfHyphen >= 0 && indexOfHyphen != 4) { - // When the date value uses hyphens as separators but does not have 4 digits for the year value + // When the date value uses hyphens as separators but does not have 4 digits for the year value // throw an exception. throwUserFailureException(value); } } - + /** * Throws UserFailureException. * @@ -242,18 +239,6 @@ public class SimplePropertyValidator "Date value '%s' has improper format. It must be one of '%s'.", value, Arrays.toString(DATE_PATTERNS)); } - - /** - * Extracts date part from the string representation of a date-time. - * - * @param value the value considered as string representation of date-time. - * @return the date portion of the date-time string. - */ - private final static String extractDate(final String value) - { - final int dateSeparator = Math.min(value.indexOf(' '), value.indexOf('T')); - return dateSeparator >= 0 ? value.substring(0, dateSeparator) : value; - } } public final static class IntegerValidator implements IDataTypeValidator diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/util/SimplePropertyValidatorTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/util/SimplePropertyValidatorTest.java new file mode 100644 index 00000000000..a0f487a45cf --- /dev/null +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/util/SimplePropertyValidatorTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2019 ETH Zuerich, SIS + * + * 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 ch.systemsx.cisd.openbis.generic.shared.util; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.openbis.generic.shared.util.SimplePropertyValidator.TimestampValidator; + +/** + * @author Franz-Josef Elmer + */ +public class SimplePropertyValidatorTest +{ + + @DataProvider + public Object[][] validTimestamps() + { + return new Object[][] { + { "2020-05-16", "2020-05-16 00:00:00 +0200" }, + { "2020-5-16", "2020-05-16 00:00:00 +0200" }, + { "2019-01-16", "2019-01-16 00:00:00 +0100" }, + { "2019-01-16 3:4", "2019-01-16 03:04:00 +0100" }, + { "2019-01-16 18:23:56", "2019-01-16 18:23:56 +0100" }, + { "2019-01-16 18:23:56 +0700", "2019-01-16 12:23:56 +0100" }, + { "2019-01-16 18:23:56 GMT", "2019-01-16 19:23:56 +0100" }, + { "1/16/19", "2019-01-16 00:00:00 +0100" }, + { "1/16/19 8:9", "2019-01-16 08:09:00 +0100" }, + { "1/16/19 8:9 p", "2019-01-16 20:09:00 +0100" }, + { "1/16/19 18:19", "2019-01-16 18:19:00 +0100" }, + }; + } + + @DataProvider + public Object[][] invalidTimestamps() + { + return new Object[][] { + { "2010-05-06T17:13:39" }, + { "10-05-06" }, + { "10-05-06 7:23" }, + { "10-05-06 17:13:39" }, + { "2010-05-36 17:13:39" }, + { "2010-05-06 27:13:39" }, + { "13/12/11 7:39" }, + { "3/12/11 7:39:22" }, + }; + } + + @Test(dataProvider = "validTimestamps") + public void testTimestampValidatorWithValidExamples(String stringToParse, String canonicalTimestamp) + { + TimestampValidator validator = new SimplePropertyValidator.TimestampValidator(); + + assertEquals(validator.validate(stringToParse), canonicalTimestamp); + } + + @Test(dataProvider = "invalidTimestamps") + public void testTimestampValidatorWithInvalidExamples(String stringToParse) + { + TimestampValidator validator = new SimplePropertyValidator.TimestampValidator(); + + try + { + validator.validate(stringToParse); + } catch (UserFailureException e) + { + assertEquals(e.getMessage(), "Date value '" + stringToParse + "' has improper format. It must be one of " + + "'[yyyy-MM-dd, yyyy-MM-dd HH:mm, yyyy-MM-dd HH:mm:ss, M/d/yy, M/d/yy h:mm a, M/d/yy HH:mm, " + + "yyyy-MM-dd HH:mm:ss Z, yyyy-MM-dd HH:mm:ss ZZZZ]'."); + } + } + +} -- GitLab