From 9dfde8547339c920d38fbcfa64813735680166a3 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 15 Apr 2023 20:31:13 -0700 Subject: [PATCH] Added pmd extension Added fromProject instead of using the constructor Improved tests --- .idea/codeStyles/codeStyleConfig.xml | 5 + README.md | 3 +- config/pmd.xml | 105 +++++++ examples/.idea/libraries/bld.xml | 4 +- examples/.vscode/settings.json | 2 +- examples/lib/bld/bld-wrapper.jar | Bin 25100 -> 25100 bytes examples/lib/bld/bld-wrapper.properties | 6 +- .../com/example/PropertyFileExampleBuild.java | 15 +- lib/bld/bld-wrapper.properties | 4 +- .../propertyfile/PropertyFileBuild.java | 30 +- .../rife/bld/extension/propertyfile/Calc.java | 5 + .../bld/extension/propertyfile/Entry.java | 44 +-- .../bld/extension/propertyfile/EntryBase.java | 156 +++++----- .../bld/extension/propertyfile/EntryDate.java | 74 ++--- .../bld/extension/propertyfile/EntryInt.java | 28 +- .../propertyfile/PropertyFileOperation.java | 109 +++---- .../propertyfile/PropertyFileUtils.java | 118 ++++---- .../propertyfile/PropertyFileUtilsTest.java | 266 ++++++++++-------- 18 files changed, 573 insertions(+), 401 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 config/pmd.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..d91f848 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/README.md b/README.md index ad25797..b124109 100755 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ An extension for creating or modifying [property files](https://docs.oracle.com/ ```java @BuildCommand public void updateMajor() throws Exception { - new PropertyFileOperation(this) + new PropertyFileOperation() + .fromProject(this) .file("version.properties") .entry(new EntryInt("version.major").defaultValue(0).calc(ADD)) .entry(new EntryInt("version.minor").set(0)) diff --git a/config/pmd.xml b/config/pmd.xml new file mode 100644 index 0000000..3dc68dc --- /dev/null +++ b/config/pmd.xml @@ -0,0 +1,105 @@ + + + Erik's Ruleset + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/.idea/libraries/bld.xml b/examples/.idea/libraries/bld.xml index 970a359..081d064 100644 --- a/examples/.idea/libraries/bld.xml +++ b/examples/.idea/libraries/bld.xml @@ -2,12 +2,12 @@ - + - + diff --git a/examples/.vscode/settings.json b/examples/.vscode/settings.json index ba59365..122fc60 100644 --- a/examples/.vscode/settings.json +++ b/examples/.vscode/settings.json @@ -7,7 +7,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}/.rife2/dist/rife2-1.5.19.jar", + "${HOME}/.rife2/dist/rife2-1.5.20.jar", "lib/compile/*.jar", "lib/runtime/*.jar", "lib/test/*.jar" diff --git a/examples/lib/bld/bld-wrapper.jar b/examples/lib/bld/bld-wrapper.jar index 7cc21a1f38b5c955ee41462d7c6fc744ae5a2c2b..2780742b191e8d3c2aa75f9cf17b6cc60ea39c9c 100644 GIT binary patch delta 151 zcmeAJzuf%P{z!2cg&XM(amfhqF@gg7vlb^>sgK6CaPcU7X5CWzzB)EcUiNrXF Pc%nZCP<j4=Lj>kVpJxQi zoM;08Nh2=L diff --git a/examples/lib/bld/bld-wrapper.properties b/examples/lib/bld/bld-wrapper.properties index 5cd8e32..5ebf190 100644 --- a/examples/lib/bld/bld-wrapper.properties +++ b/examples/lib/bld/bld-wrapper.properties @@ -1,6 +1,6 @@ #Sun Apr 02 10:32:44 PDT 2023 -bld.extension=com.uwyn.rife2:bld-property-file:0.9.1 -bld.repositories=MAVEN_LOCAL,RIFE2_RELEASES +bld.extension=com.uwyn.rife2:bld-property-file:0.9.2-SNAPSHOT +bld.repositories=MAVEN_LOCAL,MAVEN_LOCAL,RIFE2_RELEASES,RIFE2_SNAPSHOTS bld.downloadExtensionSources=true rife2.downloadLocation= -rife2.version=1.5.19 \ No newline at end of file +rife2.version=1.5.20 \ No newline at end of file diff --git a/examples/src/bld/java/com/example/PropertyFileExampleBuild.java b/examples/src/bld/java/com/example/PropertyFileExampleBuild.java index 615caa2..5aa8900 100644 --- a/examples/src/bld/java/com/example/PropertyFileExampleBuild.java +++ b/examples/src/bld/java/com/example/PropertyFileExampleBuild.java @@ -39,7 +39,8 @@ public class PropertyFileExampleBuild extends Project { @BuildCommand(summary = "Updates major version") public void updateMajor() throws Exception { - new PropertyFileOperation(this) + new PropertyFileOperation() + .fromProject(this) .file("version.properties") // set the major version to 1 if it doesn't exist, increase by 1 .entry(new EntryInt("version.major").defaultValue(0).calc(ADD)) @@ -54,7 +55,8 @@ public class PropertyFileExampleBuild extends Project { @BuildCommand(summary = "Updates minor version") public void updateMinor() throws Exception { - new PropertyFileOperation(this) + new PropertyFileOperation() + .fromProject(this) .file("version.properties") // set the major version to 1 if it doesn't exist .entry(new EntryInt("version.major").defaultValue(1)) @@ -69,7 +71,8 @@ public class PropertyFileExampleBuild extends Project { @BuildCommand(summary = "Updates patch version") public void updatePatch() throws Exception { - new PropertyFileOperation(this) + new PropertyFileOperation() + .fromProject(this) .file("version.properties") // set the major version to 1 if it doesn't exist .entry(new EntryInt("version.major").defaultValue(1)) @@ -84,7 +87,8 @@ public class PropertyFileExampleBuild extends Project { @BuildCommand(summary = "Updates the release") public void updateRelease() throws Exception { - new PropertyFileOperation(this) + new PropertyFileOperation() + .fromProject(this) .file("version.properties") // set the release to current date/time .entry(new EntryDate("release").now().pattern("yyyyMMddHHmmss")) @@ -95,7 +99,8 @@ public class PropertyFileExampleBuild extends Project { @BuildCommand(summary = "Delete version properties") public void deleteVersion() throws Exception { - new PropertyFileOperation(this) + new PropertyFileOperation() + .fromProject(this) .file("version.properties") .entry(new Entry("version.major").delete()) .entry(new Entry("version.minor").delete()) diff --git a/lib/bld/bld-wrapper.properties b/lib/bld/bld-wrapper.properties index 9eaef71..f5e3e99 100644 --- a/lib/bld/bld-wrapper.properties +++ b/lib/bld/bld-wrapper.properties @@ -1,6 +1,6 @@ bld.downloadExtensionJavadoc=false bld.downloadExtensionSources=true -bld.extensions= -bld.repositories=MAVEN_CENTRAL,RIFE2_RELEASES +bld.extension-pmd=com.uwyn.rife2:bld-pmd:0.9.1-SNAPSHOT +bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES rife2.downloadLocation= rife2.version=1.5.20 diff --git a/src/bld/java/rife/bld/extension/propertyfile/PropertyFileBuild.java b/src/bld/java/rife/bld/extension/propertyfile/PropertyFileBuild.java index 59b238f..1556910 100644 --- a/src/bld/java/rife/bld/extension/propertyfile/PropertyFileBuild.java +++ b/src/bld/java/rife/bld/extension/propertyfile/PropertyFileBuild.java @@ -1,8 +1,9 @@ package rife.bld.extension.propertyfile; +import rife.bld.BuildCommand; import rife.bld.Project; +import rife.bld.extension.PmdOperation; import rife.bld.publish.PublishDeveloper; -import rife.bld.publish.PublishInfo; import rife.bld.publish.PublishLicense; import rife.bld.publish.PublishScm; @@ -33,27 +34,27 @@ public class PropertyFileBuild extends Project { .include(dependency("org.assertj:assertj-joda-time:2.2.0")); javadocOperation() - .javadocOptions() + .javadocOptions() .docLint(NO_MISSING) .link("https://rife2.github.io/rife2/"); publishOperation() - .repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2")) -// .repository(MAVEN_LOCAL) - .info() + .repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2")) +// .repository(MAVEN_LOCAL) + .info() .groupId("com.uwyn.rife2") .artifactId("bld-property-file") .description("bld Extension to Create or Modify Properties Files") .url("https://github.com/rife2/bld-property-file") .developer(new PublishDeveloper().id("ethauvin").name("Erik C. Thauvin").email("erik@thauvin.net") - .url("https://erik.thauvin.net/")) + .url("https://erik.thauvin.net/")) .developer(new PublishDeveloper().id("gbevin").name("Geert Bevin").email("gbevin@uwyn.com") - .url("https://github.com/gbevin")) + .url("https://github.com/gbevin")) .license(new PublishLicense().name("The Apache License, Version 2.0") - .url("http://www.apache.org/licenses/LICENSE-2.0.txt")) + .url("http://www.apache.org/licenses/LICENSE-2.0.txt")) .scm(new PublishScm().connection("scm:git:https://github.com/rife2/bld-property-file.git") - .developerConnection("scm:git:git@github.com:rife2/bld-property-file.git") - .url("https://github.com/rife2/bld-property-file")) + .developerConnection("scm:git:git@github.com:rife2/bld-property-file.git") + .url("https://github.com/rife2/bld-property-file")) .signKey(property("sign.key")) .signPassphrase(property("sign.passphrase")); } @@ -61,4 +62,13 @@ public class PropertyFileBuild extends Project { public static void main(String[] args) { new PropertyFileBuild().start(args); } + + @BuildCommand(summary = "Runs PMD analysis") + public void pmd() throws Exception { + new PmdOperation() + .fromProject(this) + .failOnViolation(true) + .ruleSets("config/pmd.xml") + .execute(); + } } diff --git a/src/main/java/rife/bld/extension/propertyfile/Calc.java b/src/main/java/rife/bld/extension/propertyfile/Calc.java index 9e90642..d6b2f70 100644 --- a/src/main/java/rife/bld/extension/propertyfile/Calc.java +++ b/src/main/java/rife/bld/extension/propertyfile/Calc.java @@ -29,6 +29,11 @@ public final class Calc { public static final IntFunction ADD = Calc::add; public static final IntFunction SUB = Calc::sub; + + private Calc() { + // no-op + } + /** * Adds {@code 1} to the value. * diff --git a/src/main/java/rife/bld/extension/propertyfile/Entry.java b/src/main/java/rife/bld/extension/propertyfile/Entry.java index 89a7e3e..bd8087b 100644 --- a/src/main/java/rife/bld/extension/propertyfile/Entry.java +++ b/src/main/java/rife/bld/extension/propertyfile/Entry.java @@ -30,16 +30,6 @@ public class Entry extends EntryBase { super(key); } - /** - * Sets the new {@link java.util.Properties property} value. - * - * @param s The new value - */ - public Entry set(Object s) { - setNewValue(s); - return this; - } - /** *

Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.

* @@ -51,6 +41,24 @@ public class Entry extends EntryBase { return this; } + /** + * Sets the {@link Entry entry} up for deletion. + */ + public Entry delete() { + setDelete(true); + return this; + } + + /** + * Creates a new {@link Entry entry}. + * + * @param modify the modification function + */ + public Entry modify(BiFunction modify) { + setModify(modify); + return this; + } + /** * Creates a new {@link Entry entry}. * @@ -64,20 +72,12 @@ public class Entry extends EntryBase { } /** - * Creates a new {@link Entry entry}. + * Sets the new {@link java.util.Properties property} value. * - * @param modify the modification function + * @param s The new value */ - public Entry modify(BiFunction modify) { - setModify(modify); - return this; - } - - /** - * Sets the {@link Entry entry} up for deletion. - */ - public Entry delete() { - setDelete(true); + public Entry set(Object s) { + setNewValue(s); return this; } } diff --git a/src/main/java/rife/bld/extension/propertyfile/EntryBase.java b/src/main/java/rife/bld/extension/propertyfile/EntryBase.java index ba9edc7..1cadf1c 100644 --- a/src/main/java/rife/bld/extension/propertyfile/EntryBase.java +++ b/src/main/java/rife/bld/extension/propertyfile/EntryBase.java @@ -27,15 +27,15 @@ import java.util.function.IntFunction; * @since 1.0 */ public class EntryBase { - private String key; + private IntFunction calc; private Object defaultValue; - private Object newValue; - private String modifyValue = ""; private boolean isDelete; + private String key; + private BiFunction modify; + private String modifyValue = ""; + private Object newValue; private String pattern = ""; private EntryDate.Units unit = EntryDate.Units.DAY; - private IntFunction calc; - private BiFunction modify; /** * Creates a new {@link EntryBase entry}. @@ -46,60 +46,6 @@ public class EntryBase { this.key = key; } - /** - * Returns the value to be used in the {@link #modify} function. - */ - protected String getModifyValue() { - return modifyValue; - } - - /** - * Sets the modify value. - * - * @param value the modify value. - */ - protected void setModifyValue(String value) { - this.modifyValue = value; - } - - /** - * Returns the modify function. - */ - protected BiFunction getModify() { - return modify; - } - - /** - * Sets the modify function. - */ - protected void setModify(BiFunction modify) { - this.modify = modify; - } - - /** - * Sets the modify function. - * - * @param value the value to perform a modification with - */ - protected void setModify(String value, BiFunction modify) { - this.modifyValue = value; - this.modify = modify; - } - - /** - * Returns {@code true} if the {@link java.util.Properties property} is to be deleted. - */ - protected boolean isDelete() { - return isDelete; - } - - /** - * Sets whether the {@link java.util.Properties property} should be deleted. - */ - protected void setDelete(boolean delete) { - isDelete = delete; - } - /** * Returns the calculation function. */ @@ -114,6 +60,22 @@ public class EntryBase { this.calc = calc; } + /** + * Returns the default value. + */ + protected Object getDefaultValue() { + return defaultValue; + } + + /** + * Sets the initial value to set the {@link java.util.Properties property} to, if not already defined. + * + * @param defaultValue the default value + */ + protected void setDefaultValue(Object defaultValue) { + this.defaultValue = defaultValue; + } + /** * Returns the key of the {@link java.util.Properties property}. */ @@ -131,19 +93,49 @@ public class EntryBase { } /** - * Returns the default value. + * Returns the modify function. */ - protected Object getDefaultValue() { - return defaultValue; + protected BiFunction getModify() { + return modify; } /** - * Sets the initial value to set the {@link java.util.Properties property} to, if not already defined. - * - * @param defaultValue the default value + * Sets the modify function. */ - protected void setDefaultValue(Object defaultValue) { - this.defaultValue = defaultValue; + protected void setModify(BiFunction modify) { + this.modify = modify; + } + + /** + * Returns the value to be used in the {@link #modify} function. + */ + protected String getModifyValue() { + return modifyValue; + } + + /** + * Sets the modify value. + * + * @param value the modify value. + */ + protected void setModifyValue(String value) { + this.modifyValue = value; + } + + /** + * Returns the new value to set the {@link java.util.Properties property)} to. + */ + public Object getNewValue() { + return newValue; + } + + /** + * Sets a new value for {@link java.util.Properties property}. + * + * @param newValue the new value + */ + public void setNewValue(Object newValue) { + this.newValue = newValue; } /** @@ -179,6 +171,20 @@ public class EntryBase { this.unit = unit; } + /** + * Returns {@code true} if the {@link java.util.Properties property} is to be deleted. + */ + protected boolean isDelete() { + return isDelete; + } + + /** + * Sets whether the {@link java.util.Properties property} should be deleted. + */ + protected void setDelete(boolean delete) { + isDelete = delete; + } + /** * Sets the key of the {@link java.util.Properties property}. * @@ -191,18 +197,12 @@ public class EntryBase { } /** - * Returns the new value to set the {@link java.util.Properties property)} to. - */ - public Object getNewValue() { - return newValue; - } - - /** - * Sets a new value for {@link java.util.Properties property}. + * Sets the modify function. * - * @param newValue the new value + * @param value the value to perform a modification with */ - public void setNewValue(Object newValue) { - this.newValue = newValue; + protected void setModify(String value, BiFunction modify) { + this.modifyValue = value; + this.modify = modify; } } diff --git a/src/main/java/rife/bld/extension/propertyfile/EntryDate.java b/src/main/java/rife/bld/extension/propertyfile/EntryDate.java index 3522f47..51e6630 100644 --- a/src/main/java/rife/bld/extension/propertyfile/EntryDate.java +++ b/src/main/java/rife/bld/extension/propertyfile/EntryDate.java @@ -37,6 +37,43 @@ public class EntryDate extends EntryBase { super(key); } + /** + * Creates a new {@link EntryDate entry}. + * + * @param calc the calculation function + */ + public EntryDate calc(IntFunction calc) { + setCalc(calc); + return this; + } + + /** + * Sets the {@link EntryDate entry} up for deletion. + */ + public EntryDate delete() { + setDelete(true); + return this; + } + + /** + * Sets the new {@link java.util.Properties property} value to now. + */ + public EntryDate now() { + setNewValue("now"); + return this; + } + + /** + * Sets the pattern for {@link EntryInt} and {@link EntryDate} to{@link java.text.DecimalFormat DecimalFormat} and + * {@link java.time.format.DateTimeFormatter DateTimeFormatter} respectively. + * + * @param pattern the pattern + */ + public EntryDate pattern(String pattern) { + setPattern(pattern); + return this; + } + /** * Sets the new {@link java.util.Properties property} value to an {@link Instant} * @@ -107,35 +144,6 @@ public class EntryDate extends EntryBase { return this; } - /** - * Sets the new {@link java.util.Properties property} value to now. - */ - public EntryDate now() { - setNewValue("now"); - return this; - } - - /** - * Creates a new {@link EntryDate entry}. - * - * @param calc the calculation function - */ - public EntryDate calc(IntFunction calc) { - setCalc(calc); - return this; - } - - /** - * Sets the pattern for {@link EntryInt} and {@link EntryDate} to{@link java.text.DecimalFormat DecimalFormat} and - * {@link java.time.format.DateTimeFormatter DateTimeFormatter} respectively. - * - * @param pattern the pattern - */ - public EntryDate pattern(String pattern) { - setPattern(pattern); - return this; - } - /** * Sets the {@link Units unit} value to apply to calculations for {@link EntryDate}. * @@ -146,14 +154,6 @@ public class EntryDate extends EntryBase { return this; } - /** - * Sets the {@link EntryDate entry} up for deletion. - */ - public EntryDate delete() { - setDelete(true); - return this; - } - /** * The units available for {@link EntryDate} calculations. * diff --git a/src/main/java/rife/bld/extension/propertyfile/EntryInt.java b/src/main/java/rife/bld/extension/propertyfile/EntryInt.java index f2001ef..9dd619b 100644 --- a/src/main/java/rife/bld/extension/propertyfile/EntryInt.java +++ b/src/main/java/rife/bld/extension/propertyfile/EntryInt.java @@ -35,12 +35,12 @@ public class EntryInt extends EntryBase { } /** - * Sets the new {@link java.util.Properties property} value to an integer. + * Creates a new {@link EntryInt entry}. * - * @param i The integer to set the value to + * @param calc the calculation function. */ - public EntryInt set(int i) { - setNewValue(i); + public EntryInt calc(IntFunction calc) { + setCalc(calc); return this; } @@ -55,16 +55,6 @@ public class EntryInt extends EntryBase { return this; } - /** - * Creates a new {@link EntryInt entry}. - * - * @param calc the calculation function. - */ - public EntryInt calc(IntFunction calc) { - setCalc(calc); - return this; - } - /** * Sets the {@link EntryInt entry} up for deletion. */ @@ -72,4 +62,14 @@ public class EntryInt extends EntryBase { setDelete(true); return this; } + + /** + * Sets the new {@link java.util.Properties property} value to an integer. + * + * @param i The integer to set the value to + */ + public EntryInt set(int i) { + setNewValue(i); + return this; + } } diff --git a/src/main/java/rife/bld/extension/propertyfile/PropertyFileOperation.java b/src/main/java/rife/bld/extension/propertyfile/PropertyFileOperation.java index 77cc22b..9364be2 100644 --- a/src/main/java/rife/bld/extension/propertyfile/PropertyFileOperation.java +++ b/src/main/java/rife/bld/extension/propertyfile/PropertyFileOperation.java @@ -16,7 +16,7 @@ package rife.bld.extension.propertyfile; -import rife.bld.Project; +import rife.bld.BaseProject; import rife.bld.operations.AbstractOperation; import java.io.File; @@ -32,13 +32,20 @@ import java.util.Properties; */ public class PropertyFileOperation extends AbstractOperation { private final List entries = new ArrayList<>(); - private final Project project; - private File file; private String comment = ""; private boolean failOnWarning; + private File file; + private BaseProject project; - public PropertyFileOperation(Project project) { - this.project = project; + /** + * Sets the comment to be inserted at the top of the {@link java.util.Properties} file. + * + * @param comment the header comment + */ + @SuppressWarnings("unused") + public PropertyFileOperation comment(String comment) { + this.comment = comment; + return this; } /** @@ -53,50 +60,6 @@ public class PropertyFileOperation extends AbstractOperation PropertyFileUtils.warn("command", "message", new IOException(t), true)) - .hasMessage(t).isInstanceOf(IOException.class); - assertThatCode(() -> PropertyFileUtils.warn("command", t, new Exception(t), false)) - .as("failOnWarning = false").doesNotThrowAnyException(); + @SuppressWarnings("PMD.SignatureDeclareThrowsException") + void parseDateSub() throws Exception { + var entryDate = newEntryDate(); + entryDate.setCalc(SUB); + PropertyFileUtils.processDate(t, p, entryDate.now(), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(now-3)").isEqualTo(String.valueOf(dayOfYear - 1)); + + entryDate.setCalc(v -> v - 2); + PropertyFileUtils.processDate(t, p, entryDate.now(), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(now-2)").isEqualTo(String.valueOf(dayOfYear - 2)); + + entryDate.setCalc(SUB); + PropertyFileUtils.processDate(t, p, entryDate.set(new Date()), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(date-1)").isEqualTo(String.valueOf(dayOfYear - 1)); + + entryDate.setCalc(v -> v - 2); + PropertyFileUtils.processDate(t, p, entryDate.set(Calendar.getInstance()), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(cal-2)").isEqualTo(String.valueOf(dayOfYear - 2)); + + entryDate.setCalc(v -> v - 3); + PropertyFileUtils.processDate(t, p, entryDate.set(LocalDate.now()), + true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(LocalDate-3)").isEqualTo(String.valueOf(dayOfYear - 3)); } @Test + @SuppressWarnings("PMD.SignatureDeclareThrowsException") + void parseIntSubTest() throws Exception { + var entryInt = newEntryInt(); + entryInt.calc(SUB); + entryInt.setPattern("0000"); + PropertyFileUtils.processInt(t, p, entryInt.defaultValue("0017"), true); + assertThat(p.getProperty(entryInt.getKey())).as("sub(0017)").isEqualTo("0016"); + + PropertyFileUtils.processInt(t, p, entryInt.set(16).calc(v -> v - 3), true); + assertThat(p.getProperty(entryInt.getKey())).as("sub(16)-3").isEqualTo("0013"); + } + + @Test + @SuppressWarnings("PMD.AvoidDuplicateLiterals") void parseStringAppend() { + var entry = newEntry(); + PropertyFileUtils.processString(p, entry.set(1)); PropertyFileUtils.processString(p, entry.modify("-foo", String::concat)); assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue())).isEqualTo("1-foo"); } - @Test - void parseStringPrepend() { - PropertyFileUtils.processString(p, entry.modify("foo-", (v, s) -> s + v)); - assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue())) - .isEqualTo("foo-1"); - } - @Test void parseStringCap() { + var entry = newEntry(); PropertyFileUtils.processString(p, entry.set(t).modify("", (v, s) -> v.toUpperCase(Localization.getLocale()))); assertThat(p.getProperty(entry.getKey())).as("capitalize").isEqualTo(t.toUpperCase(Localization.getLocale())); } + @Test + void parseStringCat() { + var entry = newEntry(); + entry.set(t).setModify("-foo", String::concat); + PropertyFileUtils.processString(p, entry); + assertThat(p.getProperty(entry.getKey())).as("replace").isEqualTo(t + "-foo"); + } + + @Test + void parseStringPrepend() { + var entry = newEntry(); + PropertyFileUtils.processString(p, entry.set(1)); + PropertyFileUtils.processString(p, entry.modify("foo-", (v, s) -> s + v)); + assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue())) + .isEqualTo("foo-1"); + } + @Test void parseStringReplace() { + var entry = newEntry(); entry.set(t).setModify("T", (v, s) -> v.replace("t", s)); PropertyFileUtils.processString(p, entry); assertThat(p.getProperty(entry.getKey())).as("replace(t -> T)").isEqualTo("TesT"); } - @Test - void parseStringCat() { - entry.set(t).setModify("-foo", String::concat); - PropertyFileUtils.processString(p, entry); - assertThat(p.getProperty(entry.getKey())).as("replace").isEqualTo(t + "-foo"); - } - @Test void parseStringSub() { + var entry = newEntry(); PropertyFileUtils.processString(p, entry.set(t).modify((v, s) -> v.substring(1))); assertThat(p.getProperty(entry.getKey())).as("substring(1)").isEqualTo(t.substring(1)); } @Test + @SuppressWarnings("PMD.SignatureDeclareThrowsException") + void parseTimeTest() throws Exception { + var entry = new EntryDate("time").pattern("m"); + var time = LocalTime.now(); + + entry.setCalc(ADD); + PropertyFileUtils.processDate(t, p, entry.set(time).unit(EntryDate.Units.MINUTE), true); + assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)") + .isEqualTo(String.valueOf(time.plusMinutes(1).getMinute())); + + entry.setCalc(SUB); + PropertyFileUtils.processDate(t, p, entry.set(time).unit(EntryDate.Units.HOUR).pattern("H"), true); + assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)") + .isEqualTo(String.valueOf(time.minusHours(1).getHour())); + } + + @Test + @SuppressWarnings("PMD.SignatureDeclareThrowsException") + void processDateAddTest() throws Exception { + var entryDate = newEntryDate(); + entryDate.setCalc(ADD); + PropertyFileUtils.processDate(t, p, entryDate.now(), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(now+1)").isEqualTo(String.valueOf(dayOfYear + 1)); + + PropertyFileUtils.processDate(t, p, entryDate.now().calc(v -> v + 3), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(now+3)").isEqualTo(String.valueOf(dayOfYear + 3)); + + entryDate.setCalc(ADD); + PropertyFileUtils.processDate(t, p, entryDate.set(ZonedDateTime.now()), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(ZonedDateTime+1)") + .isEqualTo(String.valueOf(dayOfYear + 1)); + + PropertyFileUtils.processDate(t, p, entryDate.set(Instant.now()).calc(v -> v + 2), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(Instant+2)").isEqualTo(String.valueOf(dayOfYear + 2)); + + entryDate.setCalc(v -> v + 3); + PropertyFileUtils.processDate(t, p, entryDate.set(LocalDateTime.now()), true); + assertThat(p.getProperty(entryDate.getKey())).as("processDate(LocalDteTime+2)").isEqualTo(String.valueOf(dayOfYear + 3)); + } + + @Test + @SuppressWarnings("PMD.SignatureDeclareThrowsException") void processIntAddTest() throws Exception { + var entryInt = newEntryInt(); entryInt.calc(ADD); entryInt.setDefaultValue("-1"); PropertyFileUtils.processInt(t, p, entryInt, true); @@ -137,92 +218,22 @@ class PropertyFileUtilsTest { } @Test - void parseIntSubTest() throws Exception { - entryInt.calc(SUB); - entryInt.setPattern("0000"); - PropertyFileUtils.processInt(t, p, entryInt.defaultValue("0017"), true); - assertThat(p.getProperty(entryInt.getKey())).as("sub(0017)").isEqualTo("0016"); + void processStringTest() { + var entry = newEntry(); + PropertyFileUtils.processString(p, entry); - PropertyFileUtils.processInt(t, p, entryInt.set(16).calc(v -> v - 3), true); - assertThat(p.getProperty(entryInt.getKey())).as("sub(16)-3").isEqualTo("0013"); - } - - @Test - void processDateAddTest() throws Exception { - entryDate.setCalc(ADD); - PropertyFileUtils.processDate(t, p, entryDate.now(), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(now+1)").isEqualTo(String.valueOf(dayOfYear + 1)); - - PropertyFileUtils.processDate(t, p, entryDate.now().calc(v -> v + 3), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(now+3)").isEqualTo(String.valueOf(dayOfYear + 3)); - - entryDate.setCalc(ADD); - PropertyFileUtils.processDate(t, p, entryDate.set(ZonedDateTime.now()), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(ZonedDateTime+1)") - .isEqualTo(String.valueOf(dayOfYear + 1)); - - PropertyFileUtils.processDate(t, p, entryDate.set(Instant.now()).calc(v -> v + 2), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(Instant+2)").isEqualTo(String.valueOf(dayOfYear + 2)); - - entryDate.setCalc(v -> v + 3); - PropertyFileUtils.processDate(t, p, entryDate.set(LocalDateTime.now()), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(LocalDteTime+2)").isEqualTo(String.valueOf(dayOfYear + 3)); - } - - @Test - void parseDateSub() throws Exception { - entryDate.setCalc(SUB); - PropertyFileUtils.processDate(t, p, entryDate.now(), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(now-3)").isEqualTo(String.valueOf(dayOfYear - 1)); - - entryDate.setCalc(v -> v - 2); - PropertyFileUtils.processDate(t, p, entryDate.now(), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(now-2)").isEqualTo(String.valueOf(dayOfYear - 2)); - - entryDate.setCalc(SUB); - PropertyFileUtils.processDate(t, p, entryDate.set(new Date()), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(date-1)").isEqualTo(String.valueOf(dayOfYear - 1)); - - entryDate.setCalc(v -> v - 2); - PropertyFileUtils.processDate(t, p, entryDate.set(Calendar.getInstance()), true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(cal-2)").isEqualTo(String.valueOf(dayOfYear - 2)); - - entryDate.setCalc(v -> v - 3); - PropertyFileUtils.processDate(t, p, entryDate.set(LocalDate.now()), - true); - assertThat(p.getProperty(entryDate.getKey())).as("processDate(LocalDate-3)").isEqualTo(String.valueOf(dayOfYear - 3)); - } - - @Test - void parseTimeTest() throws Exception { - var entry = new EntryDate("time").pattern("m"); - var time = LocalTime.now(); - - entry.setCalc(ADD); - PropertyFileUtils.processDate(t, p, entry.set(time).unit(EntryDate.Units.MINUTE), true); - assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)") - .isEqualTo(String.valueOf(time.plusMinutes(1).getMinute())); - - entry.setCalc(SUB); - PropertyFileUtils.processDate(t, p, entry.set(time).unit(EntryDate.Units.HOUR).pattern("H"), true); - assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)") - .isEqualTo(String.valueOf(time.minusHours(1).getHour())); - } - - @Test - void testCurrentValue() { - var value = "value"; - var defaultValue = "default"; - var newValue = "new"; - - assertThat(PropertyFileUtils.currentValue(value, defaultValue, newValue)).as("all").isEqualTo(newValue); - assertThat(PropertyFileUtils.currentValue(value, null, null)).as("value").isEqualTo(value); - assertThat(PropertyFileUtils.currentValue(value, defaultValue, null)).as("value not default").isEqualTo(value); - assertThat(PropertyFileUtils.currentValue(null, defaultValue, null)).as("default").isEqualTo(defaultValue); - assertThat(PropertyFileUtils.currentValue(null, null, newValue)).as("new").isEqualTo(newValue); + assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue())) + .isEqualTo(p.getProperty(entry.getKey())); + + entry.setKey("version.minor"); + + PropertyFileUtils.processString(p, entry.set(0)); + assertThat(entry.getNewValue().toString()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue())) + .isEqualTo(p.getProperty(entry.getKey())); } @Test + @SuppressWarnings("PMD.SignatureDeclareThrowsException") void savePropertiesTest() throws Exception { var p = new Properties(); var test = "test"; @@ -240,4 +251,25 @@ class PropertyFileUtilsTest { tmp.deleteOnExit(); } + + @Test + void testCurrentValue() { + var value = "value"; + var defaultValue = "default"; + var newValue = "new"; + + assertThat(PropertyFileUtils.currentValue(value, defaultValue, newValue)).as("all").isEqualTo(newValue); + assertThat(PropertyFileUtils.currentValue(value, null, null)).as("value").isEqualTo(value); + assertThat(PropertyFileUtils.currentValue(value, defaultValue, null)).as("value not default").isEqualTo(value); + assertThat(PropertyFileUtils.currentValue(null, defaultValue, null)).as("default").isEqualTo(defaultValue); + assertThat(PropertyFileUtils.currentValue(null, null, newValue)).as("new").isEqualTo(newValue); + } + + @Test + void testWarn() { + assertThatCode(() -> PropertyFileUtils.warn("command", "message", new IOException(t), true)) + .hasMessage(t).isInstanceOf(IOException.class); + assertThatCode(() -> PropertyFileUtils.warn("command", t, new Exception(t), false)) + .as("failOnWarning = false").doesNotThrowAnyException(); + } } \ No newline at end of file