Repository: oozie Updated Branches: refs/heads/master 6ac783009 -> 553d5d4db
OOZIE-2914 Consolidate trim calls (Jan Hentschel via andras.piros) Project: http://git-wip-us.apache.org/repos/asf/oozie/repo Commit: http://git-wip-us.apache.org/repos/asf/oozie/commit/553d5d4d Tree: http://git-wip-us.apache.org/repos/asf/oozie/tree/553d5d4d Diff: http://git-wip-us.apache.org/repos/asf/oozie/diff/553d5d4d Branch: refs/heads/master Commit: 553d5d4dbe50d73133443b1bf4bc44f841e19dbd Parents: 6ac7830 Author: Andras Piros <[email protected]> Authored: Fri May 4 12:43:11 2018 +0200 Committer: Andras Piros <[email protected]> Committed: Fri May 4 12:43:11 2018 +0200 ---------------------------------------------------------------------- .../hadoop/CredentialsProviderFactory.java | 18 +----- .../action/hadoop/DistcpActionExecutor.java | 20 +------ .../apache/oozie/util/ELConstantsFunctions.java | 5 +- .../java/org/apache/oozie/util/StringUtils.java | 40 +++++++++++++ .../org/apache/oozie/util/TestStringUtils.java | 61 ++++++++++++++++++++ release-log.txt | 1 + 6 files changed, 108 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/oozie/blob/553d5d4d/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java b/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java index a353e15..095cdd2 100644 --- a/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java +++ b/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java @@ -27,6 +27,7 @@ import org.apache.hadoop.io.Text; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.token.Token; import org.apache.oozie.service.ConfigurationService; +import org.apache.oozie.util.StringUtils; import org.apache.oozie.util.XLog; public class CredentialsProviderFactory { @@ -53,7 +54,7 @@ public class CredentialsProviderFactory { private CredentialsProviderFactory() throws Exception { providerCache = new HashMap<>(); for (String function : ConfigurationService.getStrings(CRED_KEY)) { - function = trim(function); + function = StringUtils.trim(function); LOG.debug("Creating Credential class for : " + function); String[] str = function.split("="); if (str.length > 0) { @@ -112,19 +113,4 @@ public class CredentialsProviderFactory { UserGroupInformation.getLoginUser().checkTGTAndReloginFromKeytab(); LOG.debug("Relogin from keytab successful"); } - - /** - * To trim string - * - * @param str - * @return trim string - */ - public String trim(String str) { - if (str != null) { - str = str.replaceAll("\\n", ""); - str = str.replaceAll("\\t", ""); - str = str.trim(); - } - return str; - } } http://git-wip-us.apache.org/repos/asf/oozie/blob/553d5d4d/core/src/main/java/org/apache/oozie/action/hadoop/DistcpActionExecutor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/oozie/action/hadoop/DistcpActionExecutor.java b/core/src/main/java/org/apache/oozie/action/hadoop/DistcpActionExecutor.java index 89c8440..a64d128 100644 --- a/core/src/main/java/org/apache/oozie/action/hadoop/DistcpActionExecutor.java +++ b/core/src/main/java/org/apache/oozie/action/hadoop/DistcpActionExecutor.java @@ -23,6 +23,7 @@ import org.apache.hadoop.fs.Path; import org.apache.hadoop.mapreduce.MRJobConfig; import org.apache.oozie.action.ActionExecutorException; import org.apache.oozie.service.ConfigurationService; +import org.apache.oozie.util.StringUtils; import org.apache.oozie.util.XLog; import org.jdom.Element; @@ -63,7 +64,7 @@ public class DistcpActionExecutor extends JavaActionExecutor{ @Override public List<Class<?>> getLauncherClasses() { - List<Class<?>> classes = new ArrayList<Class<?>>(); + List<Class<?>> classes = new ArrayList<>(); try { classes.add(Class.forName(CONF_OOZIE_DISTCP_ACTION_MAIN_CLASS)); } @@ -82,7 +83,7 @@ public class DistcpActionExecutor extends JavaActionExecutor{ public static String getClassNamebyType(String type){ String classname = null; for (String function : ConfigurationService.getStrings(CLASS_NAMES)) { - function = DistcpActionExecutor.Trim(function); + function = StringUtils.trim(function); LOG.debug("class for Distcp Action: " + function); String[] str = function.split("="); if (str.length > 0) { @@ -95,21 +96,6 @@ public class DistcpActionExecutor extends JavaActionExecutor{ } /** - * To trim string - * - * @param str - * @return trim string - */ - public static String Trim(String str) { - if (str != null) { - str = str.replaceAll("\\n", ""); - str = str.replaceAll("\\t", ""); - str = str.trim(); - } - return str; - } - - /** * Return the sharelib name for the action. * * @return returns <code>distcp</code>. http://git-wip-us.apache.org/repos/asf/oozie/blob/553d5d4d/core/src/main/java/org/apache/oozie/util/ELConstantsFunctions.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/oozie/util/ELConstantsFunctions.java b/core/src/main/java/org/apache/oozie/util/ELConstantsFunctions.java index 87db66e..7039c48 100644 --- a/core/src/main/java/org/apache/oozie/util/ELConstantsFunctions.java +++ b/core/src/main/java/org/apache/oozie/util/ELConstantsFunctions.java @@ -19,13 +19,10 @@ package org.apache.oozie.util; import org.apache.hadoop.conf.Configuration; -import org.jdom.Element; import org.json.simple.JSONValue; -import java.text.SimpleDateFormat; import java.util.Map; import java.util.Properties; -import java.util.TimeZone; import java.util.Date; import java.net.URLEncoder; import java.io.UnsupportedEncodingException; @@ -161,7 +158,7 @@ public class ELConstantsFunctions { * @return the trimmed version of the given string or the empty string if the given string was <code>null</code> */ public static String trim(String input) { - return (input == null) ? "" : input.trim(); + return org.apache.commons.lang.StringUtils.trimToEmpty(input); } /** http://git-wip-us.apache.org/repos/asf/oozie/blob/553d5d4d/core/src/main/java/org/apache/oozie/util/StringUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/oozie/util/StringUtils.java b/core/src/main/java/org/apache/oozie/util/StringUtils.java new file mode 100644 index 0000000..280c83a --- /dev/null +++ b/core/src/main/java/org/apache/oozie/util/StringUtils.java @@ -0,0 +1,40 @@ +/** + * 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.oozie.util; + +/** + * Utility methods for working with {@link String} objects. + */ +public class StringUtils { + /** + * Trims leading and trailing whitespaces of a {@link String} object. Also replaces new lines characters and tab characters by + * empty Strings. + * + * @param str the {@link String} to trim + * @return the trimmed {@link String} + */ + public static String trim(String str) { + if (str != null) { + str = str.replaceAll("\\n", ""); + str = str.replaceAll("\\t", ""); + str = str.trim(); + } + return str; + } +} http://git-wip-us.apache.org/repos/asf/oozie/blob/553d5d4d/core/src/test/java/org/apache/oozie/util/TestStringUtils.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/oozie/util/TestStringUtils.java b/core/src/test/java/org/apache/oozie/util/TestStringUtils.java new file mode 100644 index 0000000..423d6be --- /dev/null +++ b/core/src/test/java/org/apache/oozie/util/TestStringUtils.java @@ -0,0 +1,61 @@ +/** + * 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.oozie.util; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; + +/** + * Test cases for the {@link StringUtils} class. + */ +public class TestStringUtils { + /** + * Tests if a new line character is replaced by an empty string. + */ + @Test + public void testTrimNewLineReplacement() { + assertEquals("A string", StringUtils.trim("A \n string")); + } + + /** + * Tests if a tab character is replaced by an empty string. + */ + @Test + public void testTrimTabReplacement() { + assertEquals("A string", StringUtils.trim("A \t string")); + } + + /** + * Tests if surrounding whitespaces are trimmed. + */ + @Test + public void testTrimWhitespaceReplacement() { + assertEquals("A string", StringUtils.trim(" A string ")); + } + + /** + * Tests if a valid string, meaning one without leading and trailing whitespaces, no new line characters and no tab character, + * is returned umodified. + */ + @Test + public void testTrimUnmodifiedString() { + assertEquals("A string", StringUtils.trim("A string")); + } +} http://git-wip-us.apache.org/repos/asf/oozie/blob/553d5d4d/release-log.txt ---------------------------------------------------------------------- diff --git a/release-log.txt b/release-log.txt index 896c7e5..9f148b8 100644 --- a/release-log.txt +++ b/release-log.txt @@ -1,5 +1,6 @@ -- Oozie 5.1.0 release (trunk - unreleased) +OOZIE-2914 Consolidate trim calls (Jan Hentschel via andras.piros) OOZIE-2934 [sharelib/spark] Fix Findbugs error (Jan Hentschel via andras.piros) OOZIE-3225 [Kerberos] Webconsole disabled message shown instead of 401 error (kmarton via andras.piros) OOZIE-2494 Cron syntax not handling DST properly (kmarton via andras.piros)
