Added pmd extension

Added fromProject instead of using the constructor
Improved tests
This commit is contained in:
Erik C. Thauvin 2023-04-15 20:31:13 -07:00
parent b5af26c074
commit 9dfde85473
18 changed files with 573 additions and 401 deletions

View file

@ -45,6 +45,49 @@ public final class PropertyFileUtils {
// no-op
}
/**
* Returns the new value, value or default value depending on which is specified.
*
* @param value the value
* @param newValue the new value
* @param defaultValue the default value
*/
public static Object currentValue(String value, Object defaultValue, Object newValue) {
if (newValue != null) {
return newValue;
} else if (value == null) {
return defaultValue;
} else {
return value;
}
}
/**
* Loads a {@link Properties properties} file.
*
* @param command the issuing command
* @param file the file location
* @param p the {@link Properties properties} to load into.
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static boolean loadProperties(String command, File file, Properties p) throws Exception {
boolean success = true;
if (file != null) {
if (file.exists()) {
try (var propStream = Files.newInputStream(file.toPath(), StandardOpenOption.READ)) {
p.load(propStream);
} catch (IOException ioe) {
warn(command, "Could not load properties file: " + ioe.getMessage(), ioe, true);
success = false;
}
}
} else {
warn(command, "Please specify the properties file location.");
success = false;
}
return success;
}
/**
* Processes a date {@link Properties property}.
*
@ -52,6 +95,7 @@ public final class PropertyFileUtils {
* @param p the {@link Properties property}
* @param entry the {@link Entry} containing the {@link Properties property} edits
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static boolean processDate(String command, Properties p, EntryDate entry, boolean failOnWarning)
throws Exception {
var success = true;
@ -146,23 +190,6 @@ public final class PropertyFileUtils {
return success;
}
/**
* Returns the new value, value or default value depending on which is specified.
*
* @param value the value
* @param newValue the new value
* @param defaultValue the default value
*/
public static Object currentValue(String value, Object defaultValue, Object newValue) {
if (newValue != null) {
return newValue;
} else if (value == null) {
return defaultValue;
} else {
return value;
}
}
/**
* Processes an integer {@link Properties property}.
*
@ -170,6 +197,7 @@ public final class PropertyFileUtils {
* @param p the {@link Properties property}
* @param entry the {@link Entry} containing the {@link Properties property} edits
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static boolean processInt(String command, Properties p, EntryInt entry, boolean failOnWarning)
throws Exception {
var success = true;
@ -215,6 +243,21 @@ public final class PropertyFileUtils {
return true;
}
/**
* Saves a {@link Properties properties} file.
*
* @param file the file location
* @param comment the header comment
* @param p the {@link Properties} to save into the file
*/
public static void saveProperties(File file, String comment, Properties p) throws IOException {
try (var output = Files.newOutputStream(file.toPath())) {
p.store(output, comment);
} catch (IIOException ioe) {
throw new IIOException("An IO error occurred while saving the Properties file: " + file, ioe);
}
}
/**
* Logs a warning.
*
@ -235,6 +278,7 @@ public final class PropertyFileUtils {
* @param e the related exception
* @param failOnWarning logs and throws exception if set to {@code true}
*/
@SuppressWarnings({"PMD.SignatureDeclareThrowsException"})
static void warn(String command, String message, Exception e, boolean failOnWarning) throws Exception {
if (failOnWarning) {
LOGGER.log(Level.SEVERE, '[' + command + "] " + message, e);
@ -243,44 +287,4 @@ public final class PropertyFileUtils {
warn(command, message);
}
}
/**
* Loads a {@link Properties properties} file.
*
* @param command the issuing command
* @param file the file location
* @param p the {@link Properties properties} to load into.
*/
public static boolean loadProperties(String command, File file, Properties p) throws Exception {
boolean success = true;
if (file != null) {
if (file.exists()) {
try (var propStream = Files.newInputStream(file.toPath(), StandardOpenOption.READ)) {
p.load(propStream);
} catch (IOException ioe) {
warn(command, "Could not load properties file: " + ioe.getMessage(), ioe, true);
success = false;
}
}
} else {
warn(command, "Please specify the properties file location.");
success = false;
}
return success;
}
/**
* Saves a {@link Properties properties} file.
*
* @param file the file location
* @param comment the header comment
* @param p the {@link Properties} to save into the file
*/
public static void saveProperties(File file, String comment, Properties p) throws IOException {
try (var output = Files.newOutputStream(file.toPath())) {
p.store(output, comment);
} catch (IIOException ioe) {
throw new IIOException("An IO error occurred while saving the Properties file: " + file, ioe);
}
}
}