Repository: sqoop Updated Branches: refs/heads/sqoop2 1440159de -> 1d4d70ac2
SQOOP-2615: Sqoop2: We should have a validator to test if an Integer input is within a range (Abraham Fine via Jarek Jarcec Cecho) Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/1d4d70ac Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/1d4d70ac Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/1d4d70ac Branch: refs/heads/sqoop2 Commit: 1d4d70ac237f22bbc11205a03c476679afe47a76 Parents: 1440159 Author: Jarek Jarcec Cecho <[email protected]> Authored: Tue Oct 13 16:50:26 2015 -0700 Committer: Jarek Jarcec Cecho <[email protected]> Committed: Tue Oct 13 16:50:26 2015 -0700 ---------------------------------------------------------------------- .../org/apache/sqoop/error/code/CoreError.java | 6 ++ .../sqoop/validation/validators/InRange.java | 54 ++++++++++++++ .../validation/validators/TestInRange.java | 78 ++++++++++++++++++++ 3 files changed, 138 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/sqoop/blob/1d4d70ac/common/src/main/java/org/apache/sqoop/error/code/CoreError.java ---------------------------------------------------------------------- diff --git a/common/src/main/java/org/apache/sqoop/error/code/CoreError.java b/common/src/main/java/org/apache/sqoop/error/code/CoreError.java index bc3076c..5eae162 100644 --- a/common/src/main/java/org/apache/sqoop/error/code/CoreError.java +++ b/common/src/main/java/org/apache/sqoop/error/code/CoreError.java @@ -57,6 +57,12 @@ public enum CoreError implements ErrorCode { /** Sqoop classpath error */ CORE_0009("Sqoop classpath error"), + /** Sqoop validator argument error error */ + CORE_0010("Validator needs defined argument"), + + /** Sqoop validator argument error error */ + CORE_0011("Validator has malformed argument"), + ; private final String message; http://git-wip-us.apache.org/repos/asf/sqoop/blob/1d4d70ac/common/src/main/java/org/apache/sqoop/validation/validators/InRange.java ---------------------------------------------------------------------- diff --git a/common/src/main/java/org/apache/sqoop/validation/validators/InRange.java b/common/src/main/java/org/apache/sqoop/validation/validators/InRange.java new file mode 100644 index 0000000..7ca9196 --- /dev/null +++ b/common/src/main/java/org/apache/sqoop/validation/validators/InRange.java @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.sqoop.validation.validators; + +import org.apache.sqoop.classification.InterfaceAudience; +import org.apache.sqoop.classification.InterfaceStability; +import org.apache.sqoop.common.SqoopException; +import org.apache.sqoop.error.code.CoreError; +import org.apache.sqoop.validation.Status; + +/** + * Validator to test whether an Interger is included in an + * inclusive interval defined by a string argument with format + * X,Y + */ [email protected] [email protected] +public class InRange extends AbstractValidator<Integer> { + @Override + public void validate(Integer instance) { + if (instance != null) { + if (getStringArgument().equals(AbstractValidator.DEFAULT_STRING_ARGUMENT)) + throw new SqoopException(CoreError.CORE_0010, "InRange validator needs argument"); + + int min; + int max; + try { + min = Integer.parseInt(getStringArgument().split(",")[0]); + max = Integer.parseInt(getStringArgument().split(",")[1]); + } catch (NumberFormatException exception) { + throw new SqoopException(CoreError.CORE_0011, "InRange argument needs to have form MIN,MAX", exception); + } + + if (instance < min || instance > max) { + addMessage(Status.ERROR, "Integer must be in interval: [" + getStringArgument() + "]"); + } + } + } +} http://git-wip-us.apache.org/repos/asf/sqoop/blob/1d4d70ac/common/src/test/java/org/apache/sqoop/validation/validators/TestInRange.java ---------------------------------------------------------------------- diff --git a/common/src/test/java/org/apache/sqoop/validation/validators/TestInRange.java b/common/src/test/java/org/apache/sqoop/validation/validators/TestInRange.java new file mode 100644 index 0000000..efcc143 --- /dev/null +++ b/common/src/test/java/org/apache/sqoop/validation/validators/TestInRange.java @@ -0,0 +1,78 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.sqoop.validation.validators; + +import org.apache.sqoop.common.SqoopException; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; + +public class TestInRange { + + AbstractValidator validator = new InRange(); + + @BeforeMethod + public void setValidArgumentForValidator() { + validator.setStringArgument("0,100"); + } + + @AfterMethod + public void resetValidator() { + validator.reset(); + } + + @Test + public void testEmpty() { + assertEquals(0, validator.getMessages().size()); + } + + @Test + public void testNoMessagesWhenInRange() { + validator.validate(0); + assertEquals(0, validator.getMessages().size()); + + validator.validate(100); + assertEquals(0, validator.getMessages().size()); + + validator.validate(50); + assertEquals(0, validator.getMessages().size()); + } + + @Test + public void testMessagesWhenOutsideRange() { + validator.validate(-1); + assertEquals(1, validator.getMessages().size()); + + validator.validate(101); + assertEquals(2, validator.getMessages().size()); + } + + @Test(expectedExceptions = {SqoopException.class}) + public void testNoArgumentSet() { + validator.setStringArgument(AbstractValidator.DEFAULT_STRING_ARGUMENT); + validator.validate(0); + } + + @Test(expectedExceptions = {SqoopException.class}) + public void testMalformedArgument() { + validator.setStringArgument("9,hello"); + validator.validate(0); + } +}
