Repository: sqoop Updated Branches: refs/heads/trunk 916cae19f -> 0e26d92a3
SQOOP-3053: Create a cmd line argument for sqoop.throwOnError and use it through SqoopOptions (Boglarka Egyed via Attila Szabo) Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/0e26d92a Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/0e26d92a Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/0e26d92a Branch: refs/heads/trunk Commit: 0e26d92a34029b21a2dff7c42839714913960513 Parents: 916cae1 Author: Attila Szabo <[email protected]> Authored: Fri Dec 2 15:34:50 2016 +0100 Committer: Attila Szabo <[email protected]> Committed: Fri Dec 2 15:34:50 2016 +0100 ---------------------------------------------------------------------- src/java/com/cloudera/sqoop/Sqoop.java | 3 - src/java/com/cloudera/sqoop/SqoopOptions.java | 4 + src/java/org/apache/sqoop/Sqoop.java | 24 +++++- src/java/org/apache/sqoop/SqoopOptions.java | 28 +++++++ .../org/apache/sqoop/tool/BaseSqoopTool.java | 31 +++++++- src/java/org/apache/sqoop/tool/CodeGenTool.java | 8 +- .../apache/sqoop/tool/CreateHiveTableTool.java | 8 +- src/java/org/apache/sqoop/tool/ExportTool.java | 15 +--- .../apache/sqoop/tool/ImportAllTablesTool.java | 15 +--- src/java/org/apache/sqoop/tool/ImportTool.java | 21 ++--- src/java/org/apache/sqoop/tool/MergeTool.java | 8 +- .../com/cloudera/sqoop/TestSqoopOptions.java | 76 +++++++++++++++++++ .../apache/sqoop/tool/TestBaseSqoopTool.java | 80 ++++++++++++++++++++ 13 files changed, 255 insertions(+), 66 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/com/cloudera/sqoop/Sqoop.java ---------------------------------------------------------------------- diff --git a/src/java/com/cloudera/sqoop/Sqoop.java b/src/java/com/cloudera/sqoop/Sqoop.java index c3d9725..8ec9f8f 100644 --- a/src/java/com/cloudera/sqoop/Sqoop.java +++ b/src/java/com/cloudera/sqoop/Sqoop.java @@ -31,9 +31,6 @@ public class Sqoop public static final Log SQOOP_LOG = org.apache.sqoop.Sqoop.SQOOP_LOG; - public static final String SQOOP_RETHROW_PROPERTY = - org.apache.sqoop.Sqoop.SQOOP_RETHROW_PROPERTY; - public static final String SQOOP_OPTIONS_FILE_SPECIFIER = org.apache.sqoop.Sqoop.SQOOP_OPTIONS_FILE_SPECIFIER; http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/com/cloudera/sqoop/SqoopOptions.java ---------------------------------------------------------------------- diff --git a/src/java/com/cloudera/sqoop/SqoopOptions.java b/src/java/com/cloudera/sqoop/SqoopOptions.java index f4ababe..0863ef6 100644 --- a/src/java/com/cloudera/sqoop/SqoopOptions.java +++ b/src/java/com/cloudera/sqoop/SqoopOptions.java @@ -93,6 +93,10 @@ public class SqoopOptions return org.apache.sqoop.SqoopOptions.getHiveHomeDefault(); } + public static boolean isSqoopRethrowSystemPropertySet() { + return org.apache.sqoop.SqoopOptions.isSqoopRethrowSystemPropertySet(); + } + /** * {@inheritDoc}. * @deprecated http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/Sqoop.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/Sqoop.java b/src/java/org/apache/sqoop/Sqoop.java index 93736a6..8764aff 100644 --- a/src/java/org/apache/sqoop/Sqoop.java +++ b/src/java/org/apache/sqoop/Sqoop.java @@ -20,6 +20,7 @@ package org.apache.sqoop; import java.util.Arrays; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; @@ -32,6 +33,9 @@ import com.cloudera.sqoop.cli.ToolOptions; import com.cloudera.sqoop.tool.SqoopTool; import com.cloudera.sqoop.util.OptionsFileUtil; +import static com.cloudera.sqoop.SqoopOptions.isSqoopRethrowSystemPropertySet; +import static org.apache.sqoop.tool.BaseSqoopTool.THROW_ON_ERROR_ARG; + /** * Main entry-point for Sqoop * Usage: hadoop jar (this_jar_name) com.cloudera.sqoop.Sqoop (options) @@ -174,18 +178,30 @@ public class Sqoop extends Configured implements Tool { * GenericOptionsParser would remove them. */ public static int runSqoop(Sqoop sqoop, String [] args) { + String[] toolArgs = sqoop.stashChildPrgmArgs(args); try { - String [] toolArgs = sqoop.stashChildPrgmArgs(args); return ToolRunner.run(sqoop.getConf(), sqoop, toolArgs); } catch (Exception e) { LOG.error("Got exception running Sqoop: " + e.toString()); e.printStackTrace(); - if (System.getProperty(SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(e); - } + rethrowIfRequired(toolArgs, e); return 1; } + } + + public static void rethrowIfRequired(String[] toolArgs, Exception ex) { + if (!isSqoopRethrowSystemPropertySet() && !ArrayUtils.contains(toolArgs, THROW_ON_ERROR_ARG)) { + return; + } + + final RuntimeException exceptionToThrow; + if (ex instanceof RuntimeException) { + exceptionToThrow = (RuntimeException) ex; + } else { + exceptionToThrow = new RuntimeException(ex); + } + throw exceptionToThrow; } /** http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/SqoopOptions.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/SqoopOptions.java b/src/java/org/apache/sqoop/SqoopOptions.java index ef26f16..2350c7a 100644 --- a/src/java/org/apache/sqoop/SqoopOptions.java +++ b/src/java/org/apache/sqoop/SqoopOptions.java @@ -52,6 +52,8 @@ import com.cloudera.sqoop.tool.SqoopTool; import com.cloudera.sqoop.util.RandomHash; import com.cloudera.sqoop.util.StoredAsProperty; +import static org.apache.sqoop.Sqoop.SQOOP_RETHROW_PROPERTY; + /** * Configurable state used by Sqoop tools. */ @@ -112,6 +114,10 @@ public class SqoopOptions implements Cloneable { @StoredAsProperty("temporary.dirRoot") private String tempRootDir; + // If this property is set, always throw an exception during a job, do not just + // exit with status 1. + @StoredAsProperty("sqoop.throwOnError") private boolean throwOnError; + @StoredAsProperty("mapreduce.job.name") private String mapreduceJobName; @StoredAsProperty("db.connect.string") private String connectString; @@ -1028,6 +1034,11 @@ public class SqoopOptions implements Cloneable { //to support backward compatibility. Do not exchange it with //org.apache.sqoop.tool.BaseSqoopTool#TEMP_ROOTDIR_ARG this.tempRootDir = System.getProperty(OLD_SQOOP_TEST_IMPORT_ROOT_DIR, "_sqoop"); + + //This default value is set intentionally according to SQOOP_RETHROW_PROPERTY system property + //to support backward compatibility. Do not exchange it. + this.throwOnError = isSqoopRethrowSystemPropertySet(); + this.isValidationEnabled = false; // validation is disabled by default this.validatorClass = RowCountValidator.class; this.validationThresholdClass = AbsoluteValidationThreshold.class; @@ -1045,6 +1056,15 @@ public class SqoopOptions implements Cloneable { } /** + * The SQOOP_RETHROW_PROPERTY system property is considered to be set if it is set to + * any kind of String value, i.e. it is not null. + */ + // Type of SQOOP_RETHROW_PROPERTY is String only to provide backward compatibility. + public static boolean isSqoopRethrowSystemPropertySet() { + return (System.getProperty(SQOOP_RETHROW_PROPERTY) != null); + } + + /** * Given a string containing a single character or an escape sequence * representing a char, return that char itself. * @@ -1145,6 +1165,14 @@ public class SqoopOptions implements Cloneable { this.tempRootDir = tempRootDir; } + public boolean isThrowOnError() { + return throwOnError; + } + + public void setThrowOnError(boolean throwOnError) { + this.throwOnError = throwOnError; + } + /** * Get the temporary directory; guaranteed to end in File.separator * (e.g., '/'). http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/tool/BaseSqoopTool.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/tool/BaseSqoopTool.java b/src/java/org/apache/sqoop/tool/BaseSqoopTool.java index 3ed0f77..6082296 100644 --- a/src/java/org/apache/sqoop/tool/BaseSqoopTool.java +++ b/src/java/org/apache/sqoop/tool/BaseSqoopTool.java @@ -39,7 +39,6 @@ import org.apache.sqoop.util.LoggingUtils; import org.apache.sqoop.util.password.CredentialProviderHelper; import com.cloudera.sqoop.ConnFactory; -import com.cloudera.sqoop.Sqoop; import com.cloudera.sqoop.SqoopOptions; import com.cloudera.sqoop.SqoopOptions.IncrementalMode; import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException; @@ -171,6 +170,7 @@ public abstract class BaseSqoopTool extends com.cloudera.sqoop.tool.SqoopTool { public static final String CALL_ARG = "call"; public static final String SKIP_DISTCACHE_ARG = "skip-dist-cache"; public static final String RELAXED_ISOLATION = "relaxed-isolation"; + public static final String THROW_ON_ERROR_ARG = "throw-on-error"; // Arguments for validation. public static final String VALIDATE_ARG = "validate"; @@ -271,14 +271,28 @@ public abstract class BaseSqoopTool extends com.cloudera.sqoop.tool.SqoopTool { } catch (Exception e) { LOG.error("Got error creating database manager: " + StringUtils.stringifyException(e)); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(e); - } + rethrowIfRequired(sqoopOpts, e); } return false; } + protected void rethrowIfRequired(SqoopOptions options, Exception ex) { + if (!options.isThrowOnError()) { + return; + } + + final RuntimeException exceptionToThrow; + if (ex instanceof RuntimeException) { + exceptionToThrow = (RuntimeException) ex; + } else { + exceptionToThrow = new RuntimeException(ex); + } + + throw exceptionToThrow; + } + + /** * Should be called in a 'finally' block at the end of the run() method. */ @@ -487,6 +501,10 @@ public abstract class BaseSqoopTool extends com.cloudera.sqoop.tool.SqoopTool { .hasArg() .withArgName("isolationlevel") .create()); + commonOpts.addOption(OptionBuilder + .withDescription("Rethrow a RuntimeException on error occurred during the job") + .withLongOpt(THROW_ON_ERROR_ARG) + .create()); // relax isolation requirements commonOpts.addOption(OptionBuilder .withDescription("Use read-uncommitted isolation for imports") @@ -963,6 +981,11 @@ public abstract class BaseSqoopTool extends com.cloudera.sqoop.tool.SqoopTool { out.setTempRootDir(in.getOptionValue(TEMP_ROOTDIR_ARG)); } + if (in.hasOption(THROW_ON_ERROR_ARG)) { + LOG.debug("Throw exception on error during job is enabled."); + out.setThrowOnError(true); + } + if (in.hasOption(CONNECT_STRING_ARG)) { out.setConnectString(in.getOptionValue(CONNECT_STRING_ARG)); } http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/tool/CodeGenTool.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/tool/CodeGenTool.java b/src/java/org/apache/sqoop/tool/CodeGenTool.java index b3107a2..443cbf1 100644 --- a/src/java/org/apache/sqoop/tool/CodeGenTool.java +++ b/src/java/org/apache/sqoop/tool/CodeGenTool.java @@ -28,7 +28,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.util.StringUtils; -import com.cloudera.sqoop.Sqoop; import com.cloudera.sqoop.SqoopOptions; import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException; import com.cloudera.sqoop.cli.RelatedOptions; @@ -140,11 +139,8 @@ public class CodeGenTool extends com.cloudera.sqoop.tool.BaseSqoopTool { } catch (IOException ioe) { LOG.error("Encountered IOException running codegen job: " + StringUtils.stringifyException(ioe)); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ioe); - } else { - return 1; - } + rethrowIfRequired(options, ioe); + return 1; } finally { destroy(options); } http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/tool/CreateHiveTableTool.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/tool/CreateHiveTableTool.java b/src/java/org/apache/sqoop/tool/CreateHiveTableTool.java index 427376d..ec35491 100644 --- a/src/java/org/apache/sqoop/tool/CreateHiveTableTool.java +++ b/src/java/org/apache/sqoop/tool/CreateHiveTableTool.java @@ -26,7 +26,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.util.StringUtils; -import com.cloudera.sqoop.Sqoop; import com.cloudera.sqoop.SqoopOptions; import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException; import com.cloudera.sqoop.cli.RelatedOptions; @@ -60,11 +59,8 @@ public class CreateHiveTableTool extends com.cloudera.sqoop.tool.BaseSqoopTool { } catch (IOException ioe) { LOG.error("Encountered IOException running create table job: " + StringUtils.stringifyException(ioe)); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ioe); - } else { - return 1; - } + rethrowIfRequired(options, ioe); + return 1; } finally { destroy(options); } http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/tool/ExportTool.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/tool/ExportTool.java b/src/java/org/apache/sqoop/tool/ExportTool.java index 89f8590..5512fa7 100644 --- a/src/java/org/apache/sqoop/tool/ExportTool.java +++ b/src/java/org/apache/sqoop/tool/ExportTool.java @@ -26,7 +26,6 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import com.cloudera.sqoop.Sqoop; import com.cloudera.sqoop.SqoopOptions; import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException; import com.cloudera.sqoop.SqoopOptions.UpdateMode; @@ -100,18 +99,12 @@ public class ExportTool extends com.cloudera.sqoop.tool.BaseSqoopTool { exportTable(options, options.getTableName()); } catch (IOException ioe) { LOG.error("Encountered IOException running export job: ", ioe); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ioe); - } else { - return 1; - } + rethrowIfRequired(options, ioe); + return 1; } catch (ExportException ee) { LOG.error("Error during export: ", ee); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ee); - } else { - return 1; - } + rethrowIfRequired(options, ee); + return 1; } finally { destroy(options); } http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/tool/ImportAllTablesTool.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/tool/ImportAllTablesTool.java b/src/java/org/apache/sqoop/tool/ImportAllTablesTool.java index 0952c11..d6d9f60 100644 --- a/src/java/org/apache/sqoop/tool/ImportAllTablesTool.java +++ b/src/java/org/apache/sqoop/tool/ImportAllTablesTool.java @@ -28,7 +28,6 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import com.cloudera.sqoop.Sqoop; import com.cloudera.sqoop.SqoopOptions; import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException; import com.cloudera.sqoop.cli.RelatedOptions; @@ -115,18 +114,12 @@ public class ImportAllTablesTool extends com.cloudera.sqoop.tool.ImportTool { } catch (IOException ioe) { LOG.error("Encountered IOException running import job: " + ioe.toString()); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ioe); - } else { - return 1; - } + rethrowIfRequired(options, ioe); + return 1; } catch (ImportException ie) { LOG.error("Error during import: " + ie.toString()); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ie); - } else { - return 1; - } + rethrowIfRequired(options, ie); + return 1; } finally { destroy(options); } http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/tool/ImportTool.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/tool/ImportTool.java b/src/java/org/apache/sqoop/tool/ImportTool.java index d3f8b93..ed951ea 100644 --- a/src/java/org/apache/sqoop/tool/ImportTool.java +++ b/src/java/org/apache/sqoop/tool/ImportTool.java @@ -41,7 +41,6 @@ import org.apache.hadoop.fs.Path; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.util.StringUtils; -import com.cloudera.sqoop.Sqoop; import com.cloudera.sqoop.SqoopOptions; import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException; import com.cloudera.sqoop.cli.RelatedOptions; @@ -618,25 +617,17 @@ public class ImportTool extends com.cloudera.sqoop.tool.BaseSqoopTool { importTable(options, options.getTableName(), hiveImport); } catch (IllegalArgumentException iea) { LOG.error("Imported Failed: " + iea.getMessage()); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw iea; - } - return 1; + rethrowIfRequired(options, iea); + return 1; } catch (IOException ioe) { LOG.error("Encountered IOException running import job: " + StringUtils.stringifyException(ioe)); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ioe); - } else { - return 1; - } + rethrowIfRequired(options, ioe); + return 1; } catch (ImportException ie) { LOG.error("Error during import: " + ie.toString()); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ie); - } else { - return 1; - } + rethrowIfRequired(options, ie); + return 1; } finally { destroy(options); } http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/java/org/apache/sqoop/tool/MergeTool.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/sqoop/tool/MergeTool.java b/src/java/org/apache/sqoop/tool/MergeTool.java index 09589d8..a710740 100644 --- a/src/java/org/apache/sqoop/tool/MergeTool.java +++ b/src/java/org/apache/sqoop/tool/MergeTool.java @@ -25,7 +25,6 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.util.StringUtils; -import com.cloudera.sqoop.Sqoop; import com.cloudera.sqoop.SqoopOptions; import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException; import com.cloudera.sqoop.cli.RelatedOptions; @@ -61,11 +60,8 @@ public class MergeTool extends com.cloudera.sqoop.tool.BaseSqoopTool { } catch (IOException ioe) { LOG.error("Encountered IOException running import job: " + StringUtils.stringifyException(ioe)); - if (System.getProperty(Sqoop.SQOOP_RETHROW_PROPERTY) != null) { - throw new RuntimeException(ioe); - } else { - return 1; - } + rethrowIfRequired(options, ioe); + return 1; } return 0; http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/test/com/cloudera/sqoop/TestSqoopOptions.java ---------------------------------------------------------------------- diff --git a/src/test/com/cloudera/sqoop/TestSqoopOptions.java b/src/test/com/cloudera/sqoop/TestSqoopOptions.java index f9d1d54..d5d09b6 100644 --- a/src/test/com/cloudera/sqoop/TestSqoopOptions.java +++ b/src/test/com/cloudera/sqoop/TestSqoopOptions.java @@ -27,12 +27,29 @@ import org.apache.commons.lang.ArrayUtils; import com.cloudera.sqoop.lib.DelimiterSet; import com.cloudera.sqoop.tool.ImportTool; import com.cloudera.sqoop.testutil.HsqldbTestServer; +import org.junit.Before; +import org.junit.After; +import org.junit.Test; + +import static org.apache.sqoop.Sqoop.SQOOP_RETHROW_PROPERTY; /** * Test aspects of the SqoopOptions class. */ public class TestSqoopOptions extends TestCase { + private Properties originalSystemProperties; + + @Before + public void setup() { + originalSystemProperties = System.getProperties(); + } + + @After + public void tearDown() { + System.setProperties(originalSystemProperties); + } + // tests for the toChar() parser public void testNormalChar() throws Exception { assertEquals('a', SqoopOptions.toChar("a")); @@ -426,6 +443,64 @@ public class TestSqoopOptions extends TestCase { assertEquals("_sqoop", opts.getTempRootDir()); } + @Test + public void testDefaultThrowOnErrorWithNotSetSystemProperty() { + System.clearProperty(SQOOP_RETHROW_PROPERTY); + SqoopOptions opts = new SqoopOptions(); + assertFalse(opts.isThrowOnError()); + } + + @Test + public void testDefaultThrowOnErrorWithSetSystemProperty() { + String testSqoopRethrowProperty = ""; + System.setProperty(SQOOP_RETHROW_PROPERTY, testSqoopRethrowProperty); + SqoopOptions opts = new SqoopOptions(); + + assertTrue(opts.isThrowOnError()); + } + + @Test + public void testDefaultLoadedThrowOnErrorWithNotSetSystemProperty() { + System.clearProperty(SQOOP_RETHROW_PROPERTY); + SqoopOptions out = new SqoopOptions(); + Properties props = out.writeProperties(); + SqoopOptions opts = new SqoopOptions(); + opts.loadProperties(props); + + assertFalse(opts.isThrowOnError()); + } + + @Test + public void testDefaultLoadedThrowOnErrorWithSetSystemProperty() { + String testSqoopRethrowProperty = ""; + System.setProperty(SQOOP_RETHROW_PROPERTY, testSqoopRethrowProperty); + SqoopOptions out = new SqoopOptions(); + Properties props = out.writeProperties(); + SqoopOptions opts = new SqoopOptions(); + opts.loadProperties(props); + + assertTrue(opts.isThrowOnError()); + } + + @Test + public void testThrowOnErrorWithNotSetSystemProperty() throws Exception { + System.clearProperty(SQOOP_RETHROW_PROPERTY); + String[] args = {"--throw-on-error"}; + SqoopOptions opts = parse(args); + + assertTrue(opts.isThrowOnError()); + } + + @Test + public void testThrowOnErrorWithSetSystemProperty() throws Exception { + String testSqoopRethrowProperty = ""; + System.setProperty(SQOOP_RETHROW_PROPERTY, testSqoopRethrowProperty); + String[] args = {"--throw-on-error"}; + SqoopOptions opts = parse(args); + + assertTrue(opts.isThrowOnError()); + } + // test that hadoop-home is accepted as an option public void testHadoopHome() throws Exception { String [] args = { @@ -556,4 +631,5 @@ public class TestSqoopOptions extends TestCase { // Expected } } + } http://git-wip-us.apache.org/repos/asf/sqoop/blob/0e26d92a/src/test/org/apache/sqoop/tool/TestBaseSqoopTool.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/sqoop/tool/TestBaseSqoopTool.java b/src/test/org/apache/sqoop/tool/TestBaseSqoopTool.java new file mode 100644 index 0000000..fbbffe9 --- /dev/null +++ b/src/test/org/apache/sqoop/tool/TestBaseSqoopTool.java @@ -0,0 +1,80 @@ +/** + * 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.tool; + +import com.cloudera.sqoop.SqoopOptions; +import junit.framework.JUnit4TestAdapter; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mockito; + +import static org.hamcrest.CoreMatchers.sameInstance; +import static org.mockito.Mockito.mock; + +@RunWith(JUnit4.class) +public class TestBaseSqoopTool { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + private BaseSqoopTool testBaseSqoopTool; + private SqoopOptions testSqoopOptions; + + @Before + public void setup() { + testBaseSqoopTool = mock(BaseSqoopTool.class, Mockito.CALLS_REAL_METHODS); + testSqoopOptions = new SqoopOptions(); + } + + @Test + public void testRethrowIfRequiredWithoutRethrowPropertySetOrThrowOnErrorOption() { + testSqoopOptions.setThrowOnError(false); + + testBaseSqoopTool.rethrowIfRequired(testSqoopOptions, new Exception()); + } + + @Test + public void testRethrowIfRequiredWithRethrowPropertySetAndRuntimeException() { + RuntimeException expectedException = new RuntimeException(); + testSqoopOptions.setThrowOnError(true); + + + exception.expect(sameInstance(expectedException)); + testBaseSqoopTool.rethrowIfRequired(testSqoopOptions, expectedException); + } + + @Test + public void testRethrowIfRequiredWithRethrowPropertySetAndException() { + Exception expectedCauseException = new Exception(); + testSqoopOptions.setThrowOnError(true); + + exception.expect(RuntimeException.class); + exception.expectCause(sameInstance(expectedCauseException)); + testBaseSqoopTool.rethrowIfRequired(testSqoopOptions, expectedCauseException); + } + + public static junit.framework.Test suite() { + return new JUnit4TestAdapter(TestBaseSqoopTool.class); + } + +}
