Skip to content

Commit 2d566ac

Browse files
sbabcocScott Babcock
andauthored
Fix Macintosh path acquisition; clean up stuff (#29)
Co-authored-by: Scott Babcock <[email protected]>
1 parent 63d84a9 commit 2d566ac

File tree

11 files changed

+85
-63
lines changed

11 files changed

+85
-63
lines changed

src/main/java/com/nordstrom/common/file/OSInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
*/
1515
public class OSInfo<T extends Enum<T> & OSInfo.OSProps> {
1616

17-
private static String osName = System.getProperty("os.name");
18-
private static String version = System.getProperty("os.version");
19-
private static String arch = System.getProperty("os.arch");
17+
private static final String osName = System.getProperty("os.name");
18+
private static final String version = System.getProperty("os.version");
19+
private static final String arch = System.getProperty("os.arch");
2020

2121
private final Map<T, String> typeMap = new LinkedHashMap<>();
2222

src/main/java/com/nordstrom/common/file/PathUtils.java

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public enum ReportsDirectory {
7676
FAILSAFE_3("(.*)(ITCase)", FAILSAFE_PATH),
7777
ARTIFACT(".*", "artifact-capture");
7878

79-
private String regex;
80-
private String folder;
79+
private final String regex;
80+
private final String folder;
8181

8282
ReportsDirectory(String regex, String folder) {
8383
this.regex = regex;
@@ -196,11 +196,12 @@ public static String getBaseDir() {
196196

197197
private static class Visitor implements FileVisitor<Path> {
198198

199-
private String baseName;
200-
private String extension;
201-
private int base, ext;
202-
private PathMatcher pathMatcher;
203-
private List<Integer> intList = new ArrayList<>();
199+
private final String baseName;
200+
private final String extension;
201+
private final int base;
202+
private final int ext;
203+
private final PathMatcher pathMatcher;
204+
private final List<Integer> intList = new ArrayList<>();
204205

205206
Visitor(String baseName, String extension) {
206207
this.baseName = baseName;
@@ -221,7 +222,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
221222
String name = file.getFileName().toString();
222223
String iStr = "0" + name.substring(base, name.length() - ext);
223224
iStr = iStr.replace("0-", "");
224-
intList.add(Integer.valueOf(iStr) + 1);
225+
intList.add(Integer.parseInt(iStr) + 1);
225226
}
226227
return FileVisitResult.CONTINUE;
227228
}
@@ -242,7 +243,7 @@ public String getNewName() {
242243
if (intList.isEmpty()) {
243244
newName = baseName + "." + extension;
244245
} else {
245-
Collections.sort(intList, Collections.reverseOrder());
246+
intList.sort(Collections.reverseOrder());
246247
newName = baseName + "-" + intList.get(0) + "." + extension;
247248
}
248249

@@ -286,9 +287,9 @@ public static boolean addSystemPathList(List<String> pathList) {
286287
}
287288
}
288289
String path = env.get(name);
289-
return (path != null) ? pathList.addAll(Arrays.asList(path.split(File.pathSeparator))) : false;
290+
return (path != null) && addNewPaths(pathList, Arrays.asList(path.split(File.pathSeparator)));
290291
}
291-
292+
292293
/**
293294
* Append Macintosh path entries to the specified list.
294295
* <p>
@@ -300,27 +301,44 @@ public static boolean addSystemPathList(List<String> pathList) {
300301
*/
301302
public static boolean addMacintoshPathList(List<String> pathList) {
302303
boolean didChange = false;
303-
if (System.getProperty("os.name").startsWith("Mac")) {
304-
List<String> pathFileList = new ArrayList<>();
305-
pathFileList.add("/etc/paths");
306-
String[] paths = new File("/etc/paths.d").list();
307-
if (paths != null) {
308-
pathFileList.addAll(Arrays.asList(paths));
304+
if (OSInfo.getDefault().getType() == OSInfo.OSType.MACINTOSH) {
305+
List<File> fileList = new ArrayList<>();
306+
File pathsFile = new File("/etc/paths");
307+
if (pathsFile.exists()) fileList.add(pathsFile);
308+
File[] pathsList = new File("/etc/paths.d").listFiles();
309+
if (pathsList != null) {
310+
fileList.addAll(Arrays.asList(pathsList));
309311
}
310-
for (String thisPathFile : pathFileList) {
311-
File pathFile = new File(thisPathFile);
312-
if (pathFile.exists()) {
313-
try {
314-
didChange |= pathList.addAll(Files.readAllLines(pathFile.toPath()));
315-
} catch (IOException eaten) {
316-
// nothing to do here
317-
}
312+
for (File thisFile : fileList) {
313+
try {
314+
didChange |= addNewPaths(pathList, Files.readAllLines(thisFile.toPath()));
315+
} catch (IOException eaten) {
316+
// nothing to do here
318317
}
319318
}
320319
}
321320
return didChange;
322321
}
323322

323+
/**
324+
* Append new path entries to the specified list.
325+
* <p>
326+
* <b>NOTE</b>: Entries from [newPaths] that already exist in [pathList] are ignored.
327+
*
328+
* @param pathList existing list to receive new path entries
329+
* @param newPaths path entries to be evaluated for novelty
330+
* @return {@code true} if entries were appended; otherwise {@code false}
331+
*/
332+
private static boolean addNewPaths(List<String> pathList, List<String> newPaths) {
333+
boolean didChange = false;
334+
for (String thisPath : newPaths) {
335+
if (!pathList.contains(thisPath)) {
336+
didChange |= pathList.add(thisPath);
337+
}
338+
}
339+
return didChange;
340+
}
341+
324342
/**
325343
* Prepend the specified string to the indicated array.
326344
*

src/main/java/com/nordstrom/common/file/VolumeInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public class VolumeInfo {
2020

2121
static final boolean IS_WINDOWS = (OSInfo.getDefault().getType() == OSType.WINDOWS);
22-
22+
2323
private VolumeInfo() {
2424
throw new AssertionError("VolumeInfo is a static utility class that cannot be instantiated");
2525
}
@@ -80,7 +80,7 @@ public static class VolumeProps {
8080
String type;
8181
String[] opts;
8282

83-
private long size;
83+
private final long size;
8484
private long free;
8585

8686
VolumeProps(String spec, String file, String type, String... opts) {

src/main/java/com/nordstrom/common/jdbc/DatabaseUtils.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
*/
7272
public class DatabaseUtils {
7373

74-
private static Pattern SPROC_PATTERN =
74+
private static final Pattern SPROC_PATTERN =
7575
Pattern.compile("([\\p{Alpha}_][\\p{Alpha}\\p{Digit}@$#_]*)(?:\\(([<>=](?:,\\s*[<>=])*)?(:)?\\))?");
7676

7777
private DatabaseUtils() {
@@ -94,7 +94,7 @@ private DatabaseUtils() {
9494
*/
9595
public static int update(QueryAPI query, Object... queryArgs) {
9696
Integer result = executeQuery(null, query, queryArgs);
97-
return (result != null) ? result.intValue() : -1;
97+
return (result != null) ? result : -1;
9898
}
9999

100100
/**
@@ -106,7 +106,7 @@ public static int update(QueryAPI query, Object... queryArgs) {
106106
* @throws IllegalStateException if no rows were returned
107107
*/
108108
public static int getInt(QueryAPI query, Object... queryArgs) {
109-
return requireResult(Integer.class, query, queryArgs).intValue();
109+
return requireResult(Integer.class, query, queryArgs);
110110
}
111111

112112
/**
@@ -240,7 +240,7 @@ public static <T> T executeQuery(Class<T> resultType, String connectionStr, Stri
240240
* @throws IllegalStateException if no rows were returned
241241
*/
242242
public static int getInt(SProcAPI sproc, Object... params) {
243-
return requireResult(Integer.class, sproc, params).intValue();
243+
return requireResult(Integer.class, sproc, params);
244244
}
245245

246246
/**
@@ -465,7 +465,7 @@ public static <T> T executeStoredProcedure(Class<T> resultType, String connectio
465465
*
466466
* @param <T> desired result type
467467
* @param resultType desired result type (see TYPES above)
468-
* @param connectionStr database connection string
468+
* @param connection database connection string
469469
* @param statement prepared statement to be executed (query or store procedure)
470470
* @return for update operations, the number of rows affected; for query operations, an object of the indicated type
471471
* <p>
@@ -481,7 +481,7 @@ private static <T> T executeStatement(Class<T> resultType, Connection connection
481481

482482
try {
483483
if (resultType == null) {
484-
result = Integer.valueOf(statement.executeUpdate());
484+
result = statement.executeUpdate();
485485
} else {
486486
if (statement instanceof CallableStatement) {
487487
if (statement.execute()) {
@@ -503,7 +503,7 @@ private static <T> T executeStatement(Class<T> resultType, Connection connection
503503
if (resultType == ResultPackage.class) {
504504
result = new ResultPackage(connection, statement, resultSet); //NOSONAR
505505
} else if (resultType == Integer.class) {
506-
result = Integer.valueOf((resultSet.next()) ? resultSet.getInt(1) : -1);
506+
result = (resultSet.next()) ? resultSet.getInt(1) : -1;
507507
} else if (resultType == String.class) {
508508
result = (resultSet.next()) ? resultSet.getString(1) : null;
509509
} else {
@@ -894,20 +894,20 @@ public void close() {
894894
try {
895895
resultSet.close();
896896
resultSet = null;
897-
} catch (SQLException e) { }
897+
} catch (SQLException ignored) { }
898898
}
899899
if (statement != null) {
900900
try {
901901
statement.close();
902902
statement = null;
903-
} catch (SQLException e) { }
903+
} catch (SQLException ignored) { }
904904
}
905905
if (connection != null) {
906906
try {
907907
connection.commit();
908908
connection.close();
909909
connection = null;
910-
} catch (SQLException e) { }
910+
} catch (SQLException ignored) { }
911911
}
912912
}
913913
}

src/main/java/com/nordstrom/common/jdbc/Param.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ public enum Mode {
465465
private static final int INPUT = 1;
466466
private static final int OUTPUT = 2;
467467

468-
private char chr;
469-
private int val;
468+
private final char chr;
469+
private final int val;
470470

471471
/**
472472
* Constructor

src/test/java/com/nordstrom/common/file/OSInfoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class OSInfoTest {
1212

13-
private static String osName = System.getProperty("os.name").toLowerCase();
13+
private static final String osName = System.getProperty("os.name").toLowerCase();
1414

1515
@Test
1616
public void testDefaultMapping() {

src/test/java/com/nordstrom/common/file/PathUtilsTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.nordstrom.common.file;
22

33
import static org.testng.Assert.assertEquals;
4+
import static org.testng.Assert.assertFalse;
45

56
import java.io.File;
67
import java.io.IOException;
@@ -15,6 +16,7 @@
1516
import org.testng.ITestResult;
1617
import org.testng.Reporter;
1718
import org.testng.annotations.Test;
19+
import org.testng.util.Strings;
1820

1921
public class PathUtilsTest {
2022

@@ -81,8 +83,7 @@ private Path getOutputPath() {
8183
ITestResult testResult = Reporter.getCurrentTestResult();
8284
ITestContext testContext = testResult.getTestContext();
8385
String outputDirectory = testContext.getOutputDirectory();
84-
Path outputDir = Paths.get(outputDirectory);
85-
return outputDir;
86+
return Paths.get(outputDirectory);
8687
}
8788

8889
private Path getBasePath() {
@@ -137,4 +138,11 @@ public void testNullExtenstion() throws IOException {
137138
public void testEmptyExtension() throws IOException {
138139
PathUtils.getNextPath(getOutputPath(), "test", "");
139140
}
141+
142+
@Test
143+
public void testGetSystemPath() {
144+
String systemPath = PathUtils.getSystemPath();
145+
assertFalse(Strings.isNullOrEmpty(systemPath));
146+
}
147+
140148
}

src/test/java/com/nordstrom/common/jar/JarUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class JarUtilsTest {
1010

11-
private static String[] CONTEXTS = { "org.testng.annotations.Test", "com.beust.jcommander.JCommander",
11+
private static final String[] CONTEXTS = { "org.testng.annotations.Test", "com.beust.jcommander.JCommander",
1212
"org.apache.derby.jdbc.EmbeddedDriver", "com.google.common.base.Charsets" };
1313

1414
@Test

src/test/java/com/nordstrom/common/jdbc/DatabaseUtilsTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void updateRows() {
8484
public void showAddresses() {
8585
try {
8686
DatabaseUtils.update(TestQuery.SHOW_ADDRESSES);
87-
} catch (Exception e) {
87+
} catch (Exception ignored) {
8888
}
8989

9090
ResultPackage pkg = DatabaseUtils.getResultPackage(TestSProc.SHOW_ADDRESSES);
@@ -97,7 +97,7 @@ public void showAddresses() {
9797
String addr = pkg.getResultSet().getString("addr");
9898
System.out.println("addr" + rowCount + ": " + num + " " + addr);
9999
}
100-
} catch (SQLException e) {
100+
} catch (SQLException ignored) {
101101
}
102102
pkg.close();
103103

@@ -130,7 +130,7 @@ public void dropTable() {
130130
public void testInVarargs() {
131131
try {
132132
DatabaseUtils.update(TestQuery.IN_VARARGS);
133-
} catch (Exception e) {
133+
} catch (Exception ignored) {
134134
}
135135

136136
String result = DatabaseUtils.getString(TestSProc.IN_VARARGS, "", 5, 4, 3);
@@ -142,7 +142,7 @@ public void testInVarargs() {
142142
public void testOutVarargs() throws SQLException {
143143
try {
144144
DatabaseUtils.update(TestQuery.OUT_VARARGS);
145-
} catch (Exception e) {
145+
} catch (Exception ignored) {
146146
}
147147

148148
ResultPackage pkg = DatabaseUtils.getResultPackage(TestSProc.OUT_VARARGS, 5, 0, 0, 0);
@@ -164,7 +164,7 @@ public void testOutVarargs() throws SQLException {
164164
public void testInOutVarargs() throws SQLException {
165165
try {
166166
DatabaseUtils.update(TestQuery.INOUT_VARARGS);
167-
} catch (Exception e) {
167+
} catch (Exception ignored) {
168168
}
169169

170170
ResultPackage pkg = DatabaseUtils.getResultPackage(TestSProc.INOUT_VARARGS, 5, 3, 10, 100);
@@ -207,8 +207,8 @@ enum TestQuery implements QueryAPI {
207207
+ "external name 'com.nordstrom.common.jdbc.StoredProcedure.inoutVarargs'"),
208208
DROP_PROC_INOUT("drop procedure INOUT_VARARGS");
209209

210-
private String query;
211-
private String[] args;
210+
private final String query;
211+
private final String[] args;
212212

213213
TestQuery(String query, String... args) {
214214
this.query = query;
@@ -246,8 +246,8 @@ enum TestSProc implements SProcAPI {
246246
OUT_VARARGS("OUT_VARARGS(>, <:)", Types.INTEGER, Types.INTEGER),
247247
INOUT_VARARGS("INOUT_VARARGS(>, =:)", Types.INTEGER, Types.INTEGER);
248248

249-
private int[] argTypes;
250-
private String signature;
249+
private final int[] argTypes;
250+
private final String signature;
251251

252252
TestSProc(String signature, int... argTypes) {
253253
this.signature = signature;

0 commit comments

Comments
 (0)