diff --git a/source/java/org/alfresco/repo/content/ContentMinimalContextTestSuite.java b/source/java/org/alfresco/repo/content/ContentMinimalContextTestSuite.java index b6daf8160f..fbef82d337 100644 --- a/source/java/org/alfresco/repo/content/ContentMinimalContextTestSuite.java +++ b/source/java/org/alfresco/repo/content/ContentMinimalContextTestSuite.java @@ -52,6 +52,7 @@ import org.alfresco.repo.content.transform.TextMiningContentTransformerTest; import org.alfresco.repo.content.transform.TextToPdfContentTransformerTest; import org.alfresco.repo.content.transform.TikaAutoContentTransformerTest; import org.alfresco.repo.content.transform.magick.ImageMagickContentTransformerTest; +import org.alfresco.service.cmr.repository.TemporalSourceOptionsTest; import org.alfresco.service.cmr.repository.TransformationOptionLimitsTest; import org.alfresco.service.cmr.repository.TransformationOptionPairTest; import org.alfresco.util.ApplicationContextHelper; @@ -95,6 +96,9 @@ public class ContentMinimalContextTestSuite extends TestSuite suite.addTest(new JUnit4TestAdapter(TransformationOptionLimitsTest.class)); suite.addTest(new JUnit4TestAdapter(TransformationOptionPairTest.class)); + // Source options + suite.addTest(new JUnit4TestAdapter(TemporalSourceOptionsTest.class)); + // Metadata tests suite.addTestSuite( DWGMetadataExtracterTest.class ); suite.addTestSuite( HtmlMetadataExtracterTest.class ); diff --git a/source/java/org/alfresco/service/cmr/repository/TemporalSourceOptions.java b/source/java/org/alfresco/service/cmr/repository/TemporalSourceOptions.java index d54b574a56..25441b0f27 100644 --- a/source/java/org/alfresco/service/cmr/repository/TemporalSourceOptions.java +++ b/source/java/org/alfresco/service/cmr/repository/TemporalSourceOptions.java @@ -21,6 +21,7 @@ package org.alfresco.service.cmr.repository; import java.io.Serializable; import java.util.Map; +import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.service.cmr.repository.AbstractTransformationSourceOptions; /** @@ -38,6 +39,9 @@ import org.alfresco.service.cmr.repository.AbstractTransformationSourceOptions; public class TemporalSourceOptions extends AbstractTransformationSourceOptions { + /** Validation regex for hh:mm:ss[.xxx], ignoring leap seconds and allowing up to 99 hours */ + private static final String VALID_TIME_STRING_REGEX = "\\d{2}:[0-5][0-9]:[0-5][0-9](\\.\\d{1,3})?"; + /** The offset time code from which to start the transformation */ private String offset; @@ -72,6 +76,7 @@ public class TemporalSourceOptions extends AbstractTransformationSourceOptions */ public void setOffset(String offset) { + TemporalSourceOptions.validateTimeString(offset); this.offset = offset; } @@ -94,8 +99,22 @@ public class TemporalSourceOptions extends AbstractTransformationSourceOptions */ public void setDuration(String duration) { + TemporalSourceOptions.validateTimeString(duration); this.duration = duration; } + + /** + * Validates that the given value is of the form hh:mm:ss[.xxx] + * + * @param value + */ + public static void validateTimeString(String value) + { + if (value != null && !value.matches(VALID_TIME_STRING_REGEX)) + { + throw new AlfrescoRuntimeException("'" + value + "' is not a valid time specification of the form hh:mm:ss[.xxx]"); + } + } @Override public TransformationSourceOptions mergedOptions(TransformationSourceOptions overridingOptions) diff --git a/source/java/org/alfresco/service/cmr/repository/TemporalSourceOptionsTest.java b/source/java/org/alfresco/service/cmr/repository/TemporalSourceOptionsTest.java new file mode 100644 index 0000000000..8d2fbbc11c --- /dev/null +++ b/source/java/org/alfresco/service/cmr/repository/TemporalSourceOptionsTest.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2005-2013 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.service.cmr.repository; + +import static org.junit.Assert.*; + +import org.alfresco.error.AlfrescoRuntimeException; +import org.junit.Before; +import org.junit.Test; + +/** + * Test {@link TemporalSourceOptions}. + * + * @author Ray Gauss II + */ +public class TemporalSourceOptionsTest +{ + private TemporalSourceOptions temporalSourceOptions; + + private String[] expectedFailures = new String[] { + "not even close", + "2 hours", + "01:00", // Incomplete: hours, minutes, and seconds required + "0.01", // Incomplete: hours, minutes, and seconds required + "00.00.01", // Delimiter is incorrect + "1:30:15", // Hours, minutes, and seconds must have leading zeros + "00:99:99" // Minutes and seconds can not be greater than 60 + }; + + private String[] expectedSuccesses = new String[] { + "00:00:00.05", + "00:01:30", + "01:01:01", + "01:01:01.1", + "99:59:59.999", + "99:00:00" + }; + + @Before + public void setUp() throws Exception + { + temporalSourceOptions = new TemporalSourceOptions(); + } + + protected void setDurationWithExpectedFailure(String value) + { + try + { + temporalSourceOptions.setDuration(value); + fail("'" + value + "' should be invalid"); + } + catch (AlfrescoRuntimeException e) + { + // expected + } + } + + protected void setDurationWithExpectedSuccess(String value) + { + try + { + temporalSourceOptions.setDuration(value); + } + catch (AlfrescoRuntimeException e) + { + fail(e.getMessage()); + } + } + + protected void setOffsetWithExpectedFailure(String value) + { + try + { + temporalSourceOptions.setOffset(value); + fail("'" + value + "' should be invalid"); + } + catch (AlfrescoRuntimeException e) + { + // expected + } + } + + protected void setOffsetWithExpectedSuccess(String value) + { + try + { + temporalSourceOptions.setOffset(value); + } + catch (AlfrescoRuntimeException e) + { + fail(e.getMessage()); + } + } + + @Test + public void testDurationValidation() throws Exception + { + for (String expectedFailure : expectedFailures) + { + setDurationWithExpectedFailure(expectedFailure); + } + for (String expectedSuccess : expectedSuccesses) + { + setDurationWithExpectedSuccess(expectedSuccess); + } + } + + @Test + public void testOffsetValidation() throws Exception + { + for (String expectedFailure : expectedFailures) + { + setOffsetWithExpectedFailure(expectedFailure); + } + for (String expectedSuccess : expectedSuccesses) + { + setOffsetWithExpectedSuccess(expectedSuccess); + } + } + +} +