From fa7574c93b218b612fb01a8982cfc1981ba7f2ff Mon Sep 17 00:00:00 2001 From: Scott Babcock Date: Mon, 8 Aug 2022 23:17:46 -0700 Subject: [PATCH] Fix Macintosh path acquisition; clean up stuff --- .../com/nordstrom/common/file/OSInfo.java | 6 +- .../com/nordstrom/common/file/PathUtils.java | 68 ++++++++++++------- .../com/nordstrom/common/file/VolumeInfo.java | 4 +- .../nordstrom/common/jdbc/DatabaseUtils.java | 20 +++--- .../java/com/nordstrom/common/jdbc/Param.java | 4 +- .../com/nordstrom/common/file/OSInfoTest.java | 2 +- .../nordstrom/common/file/PathUtilsTest.java | 12 +++- .../nordstrom/common/jar/JarUtilsTest.java | 2 +- .../common/jdbc/DatabaseUtilsTest.java | 18 ++--- .../common/jdbc/StoredProcedure.java | 10 +-- .../nordstrom/common/params/ParamTest.java | 2 +- 11 files changed, 85 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/nordstrom/common/file/OSInfo.java b/src/main/java/com/nordstrom/common/file/OSInfo.java index bd51e97..0bee130 100644 --- a/src/main/java/com/nordstrom/common/file/OSInfo.java +++ b/src/main/java/com/nordstrom/common/file/OSInfo.java @@ -14,9 +14,9 @@ */ public class OSInfo & OSInfo.OSProps> { - private static String osName = System.getProperty("os.name"); - private static String version = System.getProperty("os.version"); - private static String arch = System.getProperty("os.arch"); + private static final String osName = System.getProperty("os.name"); + private static final String version = System.getProperty("os.version"); + private static final String arch = System.getProperty("os.arch"); private final Map typeMap = new LinkedHashMap<>(); diff --git a/src/main/java/com/nordstrom/common/file/PathUtils.java b/src/main/java/com/nordstrom/common/file/PathUtils.java index 66db045..dfa0772 100644 --- a/src/main/java/com/nordstrom/common/file/PathUtils.java +++ b/src/main/java/com/nordstrom/common/file/PathUtils.java @@ -76,8 +76,8 @@ public enum ReportsDirectory { FAILSAFE_3("(.*)(ITCase)", FAILSAFE_PATH), ARTIFACT(".*", "artifact-capture"); - private String regex; - private String folder; + private final String regex; + private final String folder; ReportsDirectory(String regex, String folder) { this.regex = regex; @@ -196,11 +196,12 @@ public static String getBaseDir() { private static class Visitor implements FileVisitor { - private String baseName; - private String extension; - private int base, ext; - private PathMatcher pathMatcher; - private List intList = new ArrayList<>(); + private final String baseName; + private final String extension; + private final int base; + private final int ext; + private final PathMatcher pathMatcher; + private final List intList = new ArrayList<>(); Visitor(String baseName, String extension) { this.baseName = baseName; @@ -221,7 +222,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO String name = file.getFileName().toString(); String iStr = "0" + name.substring(base, name.length() - ext); iStr = iStr.replace("0-", ""); - intList.add(Integer.valueOf(iStr) + 1); + intList.add(Integer.parseInt(iStr) + 1); } return FileVisitResult.CONTINUE; } @@ -242,7 +243,7 @@ public String getNewName() { if (intList.isEmpty()) { newName = baseName + "." + extension; } else { - Collections.sort(intList, Collections.reverseOrder()); + intList.sort(Collections.reverseOrder()); newName = baseName + "-" + intList.get(0) + "." + extension; } @@ -286,9 +287,9 @@ public static boolean addSystemPathList(List pathList) { } } String path = env.get(name); - return (path != null) ? pathList.addAll(Arrays.asList(path.split(File.pathSeparator))) : false; + return (path != null) && addNewPaths(pathList, Arrays.asList(path.split(File.pathSeparator))); } - + /** * Append Macintosh path entries to the specified list. *

@@ -300,27 +301,44 @@ public static boolean addSystemPathList(List pathList) { */ public static boolean addMacintoshPathList(List pathList) { boolean didChange = false; - if (System.getProperty("os.name").startsWith("Mac")) { - List pathFileList = new ArrayList<>(); - pathFileList.add("/etc/paths"); - String[] paths = new File("/etc/paths.d").list(); - if (paths != null) { - pathFileList.addAll(Arrays.asList(paths)); + if (OSInfo.getDefault().getType() == OSInfo.OSType.MACINTOSH) { + List fileList = new ArrayList<>(); + File pathsFile = new File("/etc/paths"); + if (pathsFile.exists()) fileList.add(pathsFile); + File[] pathsList = new File("/etc/paths.d").listFiles(); + if (pathsList != null) { + fileList.addAll(Arrays.asList(pathsList)); } - for (String thisPathFile : pathFileList) { - File pathFile = new File(thisPathFile); - if (pathFile.exists()) { - try { - didChange |= pathList.addAll(Files.readAllLines(pathFile.toPath())); - } catch (IOException eaten) { - // nothing to do here - } + for (File thisFile : fileList) { + try { + didChange |= addNewPaths(pathList, Files.readAllLines(thisFile.toPath())); + } catch (IOException eaten) { + // nothing to do here } } } return didChange; } + /** + * Append new path entries to the specified list. + *

+ * NOTE: Entries from [newPaths] that already exist in [pathList] are ignored. + * + * @param pathList existing list to receive new path entries + * @param newPaths path entries to be evaluated for novelty + * @return {@code true} if entries were appended; otherwise {@code false} + */ + private static boolean addNewPaths(List pathList, List newPaths) { + boolean didChange = false; + for (String thisPath : newPaths) { + if (!pathList.contains(thisPath)) { + didChange |= pathList.add(thisPath); + } + } + return didChange; + } + /** * Prepend the specified string to the indicated array. * diff --git a/src/main/java/com/nordstrom/common/file/VolumeInfo.java b/src/main/java/com/nordstrom/common/file/VolumeInfo.java index b5dbc5a..181dc37 100644 --- a/src/main/java/com/nordstrom/common/file/VolumeInfo.java +++ b/src/main/java/com/nordstrom/common/file/VolumeInfo.java @@ -19,7 +19,7 @@ public class VolumeInfo { static final boolean IS_WINDOWS = (OSInfo.getDefault().getType() == OSType.WINDOWS); - + private VolumeInfo() { throw new AssertionError("VolumeInfo is a static utility class that cannot be instantiated"); } @@ -80,7 +80,7 @@ public static class VolumeProps { String type; String[] opts; - private long size; + private final long size; private long free; VolumeProps(String spec, String file, String type, String... opts) { diff --git a/src/main/java/com/nordstrom/common/jdbc/DatabaseUtils.java b/src/main/java/com/nordstrom/common/jdbc/DatabaseUtils.java index e13a47b..bf7dc31 100644 --- a/src/main/java/com/nordstrom/common/jdbc/DatabaseUtils.java +++ b/src/main/java/com/nordstrom/common/jdbc/DatabaseUtils.java @@ -71,7 +71,7 @@ */ public class DatabaseUtils { - private static Pattern SPROC_PATTERN = + private static final Pattern SPROC_PATTERN = Pattern.compile("([\\p{Alpha}_][\\p{Alpha}\\p{Digit}@$#_]*)(?:\\(([<>=](?:,\\s*[<>=])*)?(:)?\\))?"); private DatabaseUtils() { @@ -94,7 +94,7 @@ private DatabaseUtils() { */ public static int update(QueryAPI query, Object... queryArgs) { Integer result = executeQuery(null, query, queryArgs); - return (result != null) ? result.intValue() : -1; + return (result != null) ? result : -1; } /** @@ -106,7 +106,7 @@ public static int update(QueryAPI query, Object... queryArgs) { * @throws IllegalStateException if no rows were returned */ public static int getInt(QueryAPI query, Object... queryArgs) { - return requireResult(Integer.class, query, queryArgs).intValue(); + return requireResult(Integer.class, query, queryArgs); } /** @@ -240,7 +240,7 @@ public static T executeQuery(Class resultType, String connectionStr, Stri * @throws IllegalStateException if no rows were returned */ public static int getInt(SProcAPI sproc, Object... params) { - return requireResult(Integer.class, sproc, params).intValue(); + return requireResult(Integer.class, sproc, params); } /** @@ -465,7 +465,7 @@ public static T executeStoredProcedure(Class resultType, String connectio * * @param desired result type * @param resultType desired result type (see TYPES above) - * @param connectionStr database connection string + * @param connection database connection string * @param statement prepared statement to be executed (query or store procedure) * @return for update operations, the number of rows affected; for query operations, an object of the indicated type *

@@ -481,7 +481,7 @@ private static T executeStatement(Class resultType, Connection connection try { if (resultType == null) { - result = Integer.valueOf(statement.executeUpdate()); + result = statement.executeUpdate(); } else { if (statement instanceof CallableStatement) { if (statement.execute()) { @@ -503,7 +503,7 @@ private static T executeStatement(Class resultType, Connection connection if (resultType == ResultPackage.class) { result = new ResultPackage(connection, statement, resultSet); //NOSONAR } else if (resultType == Integer.class) { - result = Integer.valueOf((resultSet.next()) ? resultSet.getInt(1) : -1); + result = (resultSet.next()) ? resultSet.getInt(1) : -1; } else if (resultType == String.class) { result = (resultSet.next()) ? resultSet.getString(1) : null; } else { @@ -894,20 +894,20 @@ public void close() { try { resultSet.close(); resultSet = null; - } catch (SQLException e) { } + } catch (SQLException ignored) { } } if (statement != null) { try { statement.close(); statement = null; - } catch (SQLException e) { } + } catch (SQLException ignored) { } } if (connection != null) { try { connection.commit(); connection.close(); connection = null; - } catch (SQLException e) { } + } catch (SQLException ignored) { } } } } diff --git a/src/main/java/com/nordstrom/common/jdbc/Param.java b/src/main/java/com/nordstrom/common/jdbc/Param.java index 7e7b62c..4b98325 100644 --- a/src/main/java/com/nordstrom/common/jdbc/Param.java +++ b/src/main/java/com/nordstrom/common/jdbc/Param.java @@ -465,8 +465,8 @@ public enum Mode { private static final int INPUT = 1; private static final int OUTPUT = 2; - private char chr; - private int val; + private final char chr; + private final int val; /** * Constructor diff --git a/src/test/java/com/nordstrom/common/file/OSInfoTest.java b/src/test/java/com/nordstrom/common/file/OSInfoTest.java index df1fc77..9e250a9 100644 --- a/src/test/java/com/nordstrom/common/file/OSInfoTest.java +++ b/src/test/java/com/nordstrom/common/file/OSInfoTest.java @@ -10,7 +10,7 @@ public class OSInfoTest { - private static String osName = System.getProperty("os.name").toLowerCase(); + private static final String osName = System.getProperty("os.name").toLowerCase(); @Test public void testDefaultMapping() { diff --git a/src/test/java/com/nordstrom/common/file/PathUtilsTest.java b/src/test/java/com/nordstrom/common/file/PathUtilsTest.java index 5791dcb..585bc8d 100644 --- a/src/test/java/com/nordstrom/common/file/PathUtilsTest.java +++ b/src/test/java/com/nordstrom/common/file/PathUtilsTest.java @@ -1,6 +1,7 @@ package com.nordstrom.common.file; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import java.io.File; import java.io.IOException; @@ -15,6 +16,7 @@ import org.testng.ITestResult; import org.testng.Reporter; import org.testng.annotations.Test; +import org.testng.util.Strings; public class PathUtilsTest { @@ -81,8 +83,7 @@ private Path getOutputPath() { ITestResult testResult = Reporter.getCurrentTestResult(); ITestContext testContext = testResult.getTestContext(); String outputDirectory = testContext.getOutputDirectory(); - Path outputDir = Paths.get(outputDirectory); - return outputDir; + return Paths.get(outputDirectory); } private Path getBasePath() { @@ -137,4 +138,11 @@ public void testNullExtenstion() throws IOException { public void testEmptyExtension() throws IOException { PathUtils.getNextPath(getOutputPath(), "test", ""); } + + @Test + public void testGetSystemPath() { + String systemPath = PathUtils.getSystemPath(); + assertFalse(Strings.isNullOrEmpty(systemPath)); + } + } diff --git a/src/test/java/com/nordstrom/common/jar/JarUtilsTest.java b/src/test/java/com/nordstrom/common/jar/JarUtilsTest.java index 4620106..aed4c2c 100644 --- a/src/test/java/com/nordstrom/common/jar/JarUtilsTest.java +++ b/src/test/java/com/nordstrom/common/jar/JarUtilsTest.java @@ -8,7 +8,7 @@ public class JarUtilsTest { - private static String[] CONTEXTS = { "org.testng.annotations.Test", "com.beust.jcommander.JCommander", + private static final String[] CONTEXTS = { "org.testng.annotations.Test", "com.beust.jcommander.JCommander", "org.apache.derby.jdbc.EmbeddedDriver", "com.google.common.base.Charsets" }; @Test diff --git a/src/test/java/com/nordstrom/common/jdbc/DatabaseUtilsTest.java b/src/test/java/com/nordstrom/common/jdbc/DatabaseUtilsTest.java index cda95a8..49b5ebd 100644 --- a/src/test/java/com/nordstrom/common/jdbc/DatabaseUtilsTest.java +++ b/src/test/java/com/nordstrom/common/jdbc/DatabaseUtilsTest.java @@ -84,7 +84,7 @@ public void updateRows() { public void showAddresses() { try { DatabaseUtils.update(TestQuery.SHOW_ADDRESSES); - } catch (Exception e) { + } catch (Exception ignored) { } ResultPackage pkg = DatabaseUtils.getResultPackage(TestSProc.SHOW_ADDRESSES); @@ -97,7 +97,7 @@ public void showAddresses() { String addr = pkg.getResultSet().getString("addr"); System.out.println("addr" + rowCount + ": " + num + " " + addr); } - } catch (SQLException e) { + } catch (SQLException ignored) { } pkg.close(); @@ -130,7 +130,7 @@ public void dropTable() { public void testInVarargs() { try { DatabaseUtils.update(TestQuery.IN_VARARGS); - } catch (Exception e) { + } catch (Exception ignored) { } String result = DatabaseUtils.getString(TestSProc.IN_VARARGS, "", 5, 4, 3); @@ -142,7 +142,7 @@ public void testInVarargs() { public void testOutVarargs() throws SQLException { try { DatabaseUtils.update(TestQuery.OUT_VARARGS); - } catch (Exception e) { + } catch (Exception ignored) { } ResultPackage pkg = DatabaseUtils.getResultPackage(TestSProc.OUT_VARARGS, 5, 0, 0, 0); @@ -164,7 +164,7 @@ public void testOutVarargs() throws SQLException { public void testInOutVarargs() throws SQLException { try { DatabaseUtils.update(TestQuery.INOUT_VARARGS); - } catch (Exception e) { + } catch (Exception ignored) { } ResultPackage pkg = DatabaseUtils.getResultPackage(TestSProc.INOUT_VARARGS, 5, 3, 10, 100); @@ -207,8 +207,8 @@ enum TestQuery implements QueryAPI { + "external name 'com.nordstrom.common.jdbc.StoredProcedure.inoutVarargs'"), DROP_PROC_INOUT("drop procedure INOUT_VARARGS"); - private String query; - private String[] args; + private final String query; + private final String[] args; TestQuery(String query, String... args) { this.query = query; @@ -246,8 +246,8 @@ enum TestSProc implements SProcAPI { OUT_VARARGS("OUT_VARARGS(>, <:)", Types.INTEGER, Types.INTEGER), INOUT_VARARGS("INOUT_VARARGS(>, =:)", Types.INTEGER, Types.INTEGER); - private int[] argTypes; - private String signature; + private final int[] argTypes; + private final String signature; TestSProc(String signature, int... argTypes) { this.signature = signature; diff --git a/src/test/java/com/nordstrom/common/jdbc/StoredProcedure.java b/src/test/java/com/nordstrom/common/jdbc/StoredProcedure.java index 358b0a2..8f7596e 100644 --- a/src/test/java/com/nordstrom/common/jdbc/StoredProcedure.java +++ b/src/test/java/com/nordstrom/common/jdbc/StoredProcedure.java @@ -33,7 +33,7 @@ public static void inVarargs(String[] result, int... values) { buffer.append("RESULT: "); for (int value : values) { - buffer.append(" " + Integer.toString(value)); + buffer.append(" ").append(value); } retval = buffer.toString(); @@ -43,9 +43,7 @@ public static void inVarargs(String[] result, int... values) { } public static void outVarargs(int seed, int[]... values) throws Exception { - if (values == null) { - return; - } else { + if (values != null) { for (int i = 0; i < values.length; i++) { values[i][0] = seed + i; } @@ -53,9 +51,7 @@ public static void outVarargs(int seed, int[]... values) throws Exception { } public static void inoutVarargs(int seed, int[]... values) throws Exception { - if (values == null) { - return; - } else { + if (values != null) { for (int i = 0; i < values.length; i++) { values[i][0] += seed; } diff --git a/src/test/java/com/nordstrom/common/params/ParamTest.java b/src/test/java/com/nordstrom/common/params/ParamTest.java index c6dcf6d..a5d4603 100644 --- a/src/test/java/com/nordstrom/common/params/ParamTest.java +++ b/src/test/java/com/nordstrom/common/params/ParamTest.java @@ -58,7 +58,7 @@ private void verifyBoolean(Object value) { private void verifyInt(Object value) { assertTrue(value instanceof Integer); - assertEquals(value, Integer.valueOf(1)); + assertEquals(value, 1); } private void verifyString(Object value) {