Compare commits

..

No commits in common. "9dfde8547339c920d38fbcfa64813735680166a3" and "ff346ed9c156c7042a3fc9850eba4f2eda461def" have entirely different histories.

21 changed files with 420 additions and 596 deletions

View file

@ -1,5 +0,0 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Erik's Code Style" />
</state>
</component>

View file

@ -2,11 +2,11 @@
<library name="bld"> <library name="bld">
<CLASSES> <CLASSES>
<root url="file://$PROJECT_DIR$/lib/bld" /> <root url="file://$PROJECT_DIR$/lib/bld" />
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.20.jar!/" /> <root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.19.jar!/" />
</CLASSES> </CLASSES>
<JAVADOC /> <JAVADOC />
<SOURCES> <SOURCES>
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.20-sources.jar!/" /> <root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.19-sources.jar!/" />
</SOURCES> </SOURCES>
<excluded> <excluded>
<root url="jar://$PROJECT_DIR$/lib/bld/bld-wrapper.jar!/" /> <root url="jar://$PROJECT_DIR$/lib/bld/bld-wrapper.jar!/" />

View file

@ -7,7 +7,7 @@
], ],
"java.configuration.updateBuildConfiguration": "automatic", "java.configuration.updateBuildConfiguration": "automatic",
"java.project.referencedLibraries": [ "java.project.referencedLibraries": [
"${HOME}/.rife2/dist/rife2-1.5.20.jar", "${HOME}/.rife2/dist/rife2-1.5.19.jar",
"lib/compile/*.jar", "lib/compile/*.jar",
"lib/runtime/*.jar", "lib/runtime/*.jar",
"lib/test/*.jar" "lib/test/*.jar"

View file

@ -11,8 +11,7 @@ An extension for creating or modifying [property files](https://docs.oracle.com/
```java ```java
@BuildCommand @BuildCommand
public void updateMajor() throws Exception { public void updateMajor() throws Exception {
new PropertyFileOperation() new PropertyFileOperation(this)
.fromProject(this)
.file("version.properties") .file("version.properties")
.entry(new EntryInt("version.major").defaultValue(0).calc(ADD)) .entry(new EntryInt("version.major").defaultValue(0).calc(ADD))
.entry(new EntryInt("version.minor").set(0)) .entry(new EntryInt("version.minor").set(0))

View file

@ -1,105 +0,0 @@
<?xml version="1.0"?>
<ruleset name="erik"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Erik's Ruleset</description>
<!-- BEST PRACTICES -->
<rule ref="category/java/bestpractices.xml">
<exclude name="AvoidPrintStackTrace"/>
<exclude name="JUnit4TestShouldUseTestAnnotation"/>
<exclude name="JUnitTestContainsTooManyAsserts"/>
<exclude name="GuardLogStatement"/>
</rule>
<rule ref="category/java/bestpractices.xml/MissingOverride">
<properties>
<property name="violationSuppressXPath"
value="//MethodDeclaration[@Name='hashCode' or @Name='equals' or @Name='toString']"/>
</properties>
</rule>
<!-- CODE STYLE -->
<rule ref="category/java/codestyle.xml">
<exclude name="AtLeastOneConstructor"/>
<exclude name="ClassNamingConventions"/>
<exclude name="ConfusingTernary"/>
<exclude name="CommentDefaultAccessModifier"/>
<exclude name="DefaultPackage"/>
<exclude name="FieldNamingConventions"/>
<exclude name="LocalVariableCouldBeFinal"/>
<exclude name="LongVariable"/>
<exclude name="MethodArgumentCouldBeFinal"/>
<exclude name="OnlyOneReturn"/>
<exclude name="PackageCase"/>
<exclude name="ShortClassName"/>
<exclude name="ShortMethodName"/>
<exclude name="ShortVariable"/>
<exclude name="UselessParentheses"/>
<exclude name="UseUnderscoresInNumericLiterals"/>
</rule>
<!-- DESIGN -->
<rule ref="category/java/design.xml">
<exclude name="AvoidCatchingGenericException"/>
<exclude name="AvoidDeeplyNestedIfStmts"/>
<exclude name="AvoidUncheckedExceptionsInSignatures"/>
<exclude name="CognitiveComplexity"/>
<exclude name="CyclomaticComplexity"/>
<exclude name="ExcessiveClassLength"/>
<exclude name="ExcessiveMethodLength"/>
<exclude name="ExcessiveParameterList"/>
<exclude name="ExcessivePublicCount"/>
<exclude name="GodClass"/>
<exclude name="LawOfDemeter"/>
<exclude name="LoosePackageCoupling"/>
<exclude name="NPathComplexity"/>
<exclude name="NcssCount"/>
<exclude name="TooManyFields"/>
<exclude name="TooManyMethods"/>
<exclude name="UseObjectForClearerAPI"/>
</rule>
<!-- DOCUMENTATION -->
<rule ref="category/java/documentation.xml">
<exclude name="CommentRequired"/>
<exclude name="CommentSize"/>
</rule>
<!-- ERROR PRONE -->
<rule ref="category/java/errorprone.xml">
<exclude name="AssignmentInOperand"/>
<exclude name="AvoidCatchingNPE"/>
<exclude name="AvoidDuplicateLiterals"/>
<exclude name="AvoidFieldNameMatchingMethodName"/>
<exclude name="AvoidFieldNameMatchingTypeName"/>
<exclude name="AvoidLiteralsInIfCondition"/>
<exclude name="NullAssignment"/>
</rule>
<rule ref="category/java/errorprone.xml/AssignmentInOperand">
<properties>
<property name="allowWhile" value="true"/>
<property name="allowFor" value="true"/>
<property name="allowIf" value="true"/>
</properties>
</rule>
<rule ref="category/java/errorprone.xml/AvoidDuplicateLiterals">
<properties>
<property name="skipAnnotations" value="true"/>
</properties>
</rule>
<!-- MULTITHREADING -->
<rule ref="category/java/multithreading.xml">
</rule>
<!-- PERFORMANCE -->
<rule ref="category/java/performance.xml">
</rule>
<!-- SECURITY -->
<rule ref="category/java/security.xml">
</rule>
</ruleset>

View file

@ -2,12 +2,12 @@
<library name="bld"> <library name="bld">
<CLASSES> <CLASSES>
<root url="file://$PROJECT_DIR$/lib/bld" /> <root url="file://$PROJECT_DIR$/lib/bld" />
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.20.jar!/" /> <root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.19.jar!/" />
<root url="file://$PROJECT_DIR$/lib/bld" /> <root url="file://$PROJECT_DIR$/lib/bld" />
</CLASSES> </CLASSES>
<JAVADOC /> <JAVADOC />
<SOURCES> <SOURCES>
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.20-sources.jar!/" /> <root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.19-sources.jar!/" />
<root url="file://$PROJECT_DIR$/lib/bld" /> <root url="file://$PROJECT_DIR$/lib/bld" />
</SOURCES> </SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/lib/bld" recursive="false" /> <jarDirectory url="file://$PROJECT_DIR$/lib/bld" recursive="false" />

View file

@ -7,7 +7,7 @@
], ],
"java.configuration.updateBuildConfiguration": "automatic", "java.configuration.updateBuildConfiguration": "automatic",
"java.project.referencedLibraries": [ "java.project.referencedLibraries": [
"${HOME}/.rife2/dist/rife2-1.5.20.jar", "${HOME}/.rife2/dist/rife2-1.5.19.jar",
"lib/compile/*.jar", "lib/compile/*.jar",
"lib/runtime/*.jar", "lib/runtime/*.jar",
"lib/test/*.jar" "lib/test/*.jar"

Binary file not shown.

View file

@ -1,6 +1,6 @@
#Sun Apr 02 10:32:44 PDT 2023 #Sun Apr 02 10:32:44 PDT 2023
bld.extension=com.uwyn.rife2:bld-property-file:0.9.2-SNAPSHOT bld.extension=com.uwyn.rife2:bld-property-file:0.9.1
bld.repositories=MAVEN_LOCAL,MAVEN_LOCAL,RIFE2_RELEASES,RIFE2_SNAPSHOTS bld.repositories=MAVEN_LOCAL,RIFE2_RELEASES
bld.downloadExtensionSources=true bld.downloadExtensionSources=true
rife2.downloadLocation= rife2.downloadLocation=
rife2.version=1.5.20 rife2.version=1.5.19

View file

@ -39,8 +39,7 @@ public class PropertyFileExampleBuild extends Project {
@BuildCommand(summary = "Updates major version") @BuildCommand(summary = "Updates major version")
public void updateMajor() throws Exception { public void updateMajor() throws Exception {
new PropertyFileOperation() new PropertyFileOperation(this)
.fromProject(this)
.file("version.properties") .file("version.properties")
// set the major version to 1 if it doesn't exist, increase by 1 // set the major version to 1 if it doesn't exist, increase by 1
.entry(new EntryInt("version.major").defaultValue(0).calc(ADD)) .entry(new EntryInt("version.major").defaultValue(0).calc(ADD))
@ -55,8 +54,7 @@ public class PropertyFileExampleBuild extends Project {
@BuildCommand(summary = "Updates minor version") @BuildCommand(summary = "Updates minor version")
public void updateMinor() throws Exception { public void updateMinor() throws Exception {
new PropertyFileOperation() new PropertyFileOperation(this)
.fromProject(this)
.file("version.properties") .file("version.properties")
// set the major version to 1 if it doesn't exist // set the major version to 1 if it doesn't exist
.entry(new EntryInt("version.major").defaultValue(1)) .entry(new EntryInt("version.major").defaultValue(1))
@ -71,8 +69,7 @@ public class PropertyFileExampleBuild extends Project {
@BuildCommand(summary = "Updates patch version") @BuildCommand(summary = "Updates patch version")
public void updatePatch() throws Exception { public void updatePatch() throws Exception {
new PropertyFileOperation() new PropertyFileOperation(this)
.fromProject(this)
.file("version.properties") .file("version.properties")
// set the major version to 1 if it doesn't exist // set the major version to 1 if it doesn't exist
.entry(new EntryInt("version.major").defaultValue(1)) .entry(new EntryInt("version.major").defaultValue(1))
@ -87,8 +84,7 @@ public class PropertyFileExampleBuild extends Project {
@BuildCommand(summary = "Updates the release") @BuildCommand(summary = "Updates the release")
public void updateRelease() throws Exception { public void updateRelease() throws Exception {
new PropertyFileOperation() new PropertyFileOperation(this)
.fromProject(this)
.file("version.properties") .file("version.properties")
// set the release to current date/time // set the release to current date/time
.entry(new EntryDate("release").now().pattern("yyyyMMddHHmmss")) .entry(new EntryDate("release").now().pattern("yyyyMMddHHmmss"))
@ -99,8 +95,7 @@ public class PropertyFileExampleBuild extends Project {
@BuildCommand(summary = "Delete version properties") @BuildCommand(summary = "Delete version properties")
public void deleteVersion() throws Exception { public void deleteVersion() throws Exception {
new PropertyFileOperation() new PropertyFileOperation(this)
.fromProject(this)
.file("version.properties") .file("version.properties")
.entry(new Entry("version.major").delete()) .entry(new Entry("version.major").delete())
.entry(new Entry("version.minor").delete()) .entry(new Entry("version.minor").delete())

Binary file not shown.

View file

@ -1,6 +1,2 @@
bld.downloadExtensionJavadoc=false #Sat Apr 01 10:09:33 PDT 2023
bld.downloadExtensionSources=true rife2.version=1.5.19
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

View file

@ -1,9 +1,8 @@
package rife.bld.extension.propertyfile; package rife.bld.extension.propertyfile;
import rife.bld.BuildCommand;
import rife.bld.Project; import rife.bld.Project;
import rife.bld.extension.PmdOperation;
import rife.bld.publish.PublishDeveloper; import rife.bld.publish.PublishDeveloper;
import rife.bld.publish.PublishInfo;
import rife.bld.publish.PublishLicense; import rife.bld.publish.PublishLicense;
import rife.bld.publish.PublishScm; import rife.bld.publish.PublishScm;
@ -26,7 +25,7 @@ public class PropertyFileBuild extends Project {
repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES); repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES);
scope(compile) scope(compile)
.include(dependency("com.uwyn.rife2", "rife2", version(1, 5, 20))); .include(dependency("com.uwyn.rife2", "rife2", version(1, 5, 19)));
scope(test) scope(test)
.include(dependency("org.jsoup", "jsoup", version(1, 15, 4))) .include(dependency("org.jsoup", "jsoup", version(1, 15, 4)))
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 9, 2))) .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 9, 2)))
@ -34,27 +33,27 @@ public class PropertyFileBuild extends Project {
.include(dependency("org.assertj:assertj-joda-time:2.2.0")); .include(dependency("org.assertj:assertj-joda-time:2.2.0"));
javadocOperation() javadocOperation()
.javadocOptions() .javadocOptions()
.docLint(NO_MISSING) .docLint(NO_MISSING)
.link("https://rife2.github.io/rife2/"); .link("https://rife2.github.io/rife2/");
publishOperation() publishOperation()
.repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2")) .repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2"))
// .repository(MAVEN_LOCAL) // .repository(MAVEN_LOCAL)
.info() .info()
.groupId("com.uwyn.rife2") .groupId("com.uwyn.rife2")
.artifactId("bld-property-file") .artifactId("bld-property-file")
.description("bld Extension to Create or Modify Properties Files") .description("bld Extension to Create or Modify Properties Files")
.url("https://github.com/rife2/bld-property-file") .url("https://github.com/rife2/bld-property-file")
.developer(new PublishDeveloper().id("ethauvin").name("Erik C. Thauvin").email("erik@thauvin.net") .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") .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") .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") .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") .developerConnection("scm:git:git@github.com:rife2/bld-property-file.git")
.url("https://github.com/rife2/bld-property-file")) .url("https://github.com/rife2/bld-property-file"))
.signKey(property("sign.key")) .signKey(property("sign.key"))
.signPassphrase(property("sign.passphrase")); .signPassphrase(property("sign.passphrase"));
} }
@ -62,13 +61,4 @@ public class PropertyFileBuild extends Project {
public static void main(String[] args) { public static void main(String[] args) {
new PropertyFileBuild().start(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();
}
} }

View file

@ -29,11 +29,6 @@ public final class Calc {
public static final IntFunction<Integer> ADD = Calc::add; public static final IntFunction<Integer> ADD = Calc::add;
public static final IntFunction<Integer> SUB = Calc::sub; public static final IntFunction<Integer> SUB = Calc::sub;
private Calc() {
// no-op
}
/** /**
* Adds {@code 1} to the value. * Adds {@code 1} to the value.
* *

View file

@ -30,6 +30,16 @@ public class Entry extends EntryBase {
super(key); 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;
}
/** /**
* <p>Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.</p> * <p>Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.</p>
* *
@ -41,24 +51,6 @@ public class Entry extends EntryBase {
return this; 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<String, String, String> modify) {
setModify(modify);
return this;
}
/** /**
* Creates a new {@link Entry entry}. * Creates a new {@link Entry entry}.
* *
@ -72,12 +64,20 @@ public class Entry extends EntryBase {
} }
/** /**
* Sets the new {@link java.util.Properties property} value. * Creates a new {@link Entry entry}.
* *
* @param s The new value * @param modify the modification function
*/ */
public Entry set(Object s) { public Entry modify(BiFunction<String, String, String> modify) {
setNewValue(s); setModify(modify);
return this;
}
/**
* Sets the {@link Entry entry} up for deletion.
*/
public Entry delete() {
setDelete(true);
return this; return this;
} }
} }

View file

@ -27,15 +27,15 @@ import java.util.function.IntFunction;
* @since 1.0 * @since 1.0
*/ */
public class EntryBase { public class EntryBase {
private IntFunction<Integer> calc;
private Object defaultValue;
private boolean isDelete;
private String key; private String key;
private BiFunction<String, String, String> modify; private Object defaultValue;
private String modifyValue = "";
private Object newValue; private Object newValue;
private String modifyValue = "";
private boolean isDelete;
private String pattern = ""; private String pattern = "";
private EntryDate.Units unit = EntryDate.Units.DAY; private EntryDate.Units unit = EntryDate.Units.DAY;
private IntFunction<Integer> calc;
private BiFunction<String, String, String> modify;
/** /**
* Creates a new {@link EntryBase entry}. * Creates a new {@link EntryBase entry}.
@ -46,66 +46,6 @@ public class EntryBase {
this.key = key; this.key = key;
} }
/**
* Returns the calculation function.
*/
protected IntFunction<Integer> getCalc() {
return calc;
}
/**
* Sets the calculation function.
*/
protected void setCalc(IntFunction<Integer> calc) {
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}.
*/
protected String getKey() {
return key;
}
/**
* Sets the key of the {@link java.util.Properties property}.
*
* @param key the {@link java.util.Properties property} key
*/
protected void setKey(String key) {
this.key = key;
}
/**
* Returns the modify function.
*/
protected BiFunction<String, String, String> getModify() {
return modify;
}
/**
* Sets the modify function.
*/
protected void setModify(BiFunction<String, String, String> modify) {
this.modify = modify;
}
/** /**
* Returns the value to be used in the {@link #modify} function. * Returns the value to be used in the {@link #modify} function.
*/ */
@ -123,19 +63,87 @@ public class EntryBase {
} }
/** /**
* Returns the new value to set the {@link java.util.Properties property)} to. * Returns the modify function.
*/ */
public Object getNewValue() { protected BiFunction<String, String, String> getModify() {
return newValue; return modify;
} }
/** /**
* Sets a new value for {@link java.util.Properties property}. * Sets the modify function.
*
* @param newValue the new value
*/ */
public void setNewValue(Object newValue) { protected void setModify(BiFunction<String, String, String> modify) {
this.newValue = newValue; this.modify = modify;
}
/**
* Sets the modify function.
*
* @param value the value to perform a modification with
*/
protected void setModify(String value, BiFunction<String, String, String> 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.
*/
protected IntFunction<Integer> getCalc() {
return calc;
}
/**
* Sets the calculation function.
*/
protected void setCalc(IntFunction<Integer> calc) {
this.calc = calc;
}
/**
* Returns the key of the {@link java.util.Properties property}.
*/
protected String getKey() {
return key;
}
/**
* Sets the key of the {@link java.util.Properties property}.
*
* @param key the {@link java.util.Properties property} key
*/
protected void setKey(String key) {
this.key = key;
}
/**
* 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;
} }
/** /**
@ -171,20 +179,6 @@ public class EntryBase {
this.unit = unit; 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}. * Sets the key of the {@link java.util.Properties property}.
* *
@ -197,12 +191,18 @@ public class EntryBase {
} }
/** /**
* Sets the modify function. * Returns the new value to set the {@link java.util.Properties property)} to.
*
* @param value the value to perform a modification with
*/ */
protected void setModify(String value, BiFunction<String, String, String> modify) { public Object getNewValue() {
this.modifyValue = value; return newValue;
this.modify = modify; }
/**
* Sets a new value for {@link java.util.Properties property}.
*
* @param newValue the new value
*/
public void setNewValue(Object newValue) {
this.newValue = newValue;
} }
} }

View file

@ -37,43 +37,6 @@ public class EntryDate extends EntryBase {
super(key); super(key);
} }
/**
* Creates a new {@link EntryDate entry}.
*
* @param calc the calculation function
*/
public EntryDate calc(IntFunction<Integer> 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} * Sets the new {@link java.util.Properties property} value to an {@link Instant}
* *
@ -144,6 +107,35 @@ public class EntryDate extends EntryBase {
return this; 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<Integer> 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}. * Sets the {@link Units unit} value to apply to calculations for {@link EntryDate}.
* *
@ -154,6 +146,14 @@ public class EntryDate extends EntryBase {
return this; return this;
} }
/**
* Sets the {@link EntryDate entry} up for deletion.
*/
public EntryDate delete() {
setDelete(true);
return this;
}
/** /**
* The units available for {@link EntryDate} calculations. * The units available for {@link EntryDate} calculations.
* *

View file

@ -35,12 +35,12 @@ public class EntryInt extends EntryBase {
} }
/** /**
* Creates a new {@link EntryInt entry}. * Sets the new {@link java.util.Properties property} value to an integer.
* *
* @param calc the calculation function. * @param i The integer to set the value to
*/ */
public EntryInt calc(IntFunction<Integer> calc) { public EntryInt set(int i) {
setCalc(calc); setNewValue(i);
return this; return this;
} }
@ -55,6 +55,16 @@ public class EntryInt extends EntryBase {
return this; return this;
} }
/**
* Creates a new {@link EntryInt entry}.
*
* @param calc the calculation function.
*/
public EntryInt calc(IntFunction<Integer> calc) {
setCalc(calc);
return this;
}
/** /**
* Sets the {@link EntryInt entry} up for deletion. * Sets the {@link EntryInt entry} up for deletion.
*/ */
@ -62,14 +72,4 @@ public class EntryInt extends EntryBase {
setDelete(true); setDelete(true);
return this; 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;
}
} }

View file

@ -16,7 +16,7 @@
package rife.bld.extension.propertyfile; package rife.bld.extension.propertyfile;
import rife.bld.BaseProject; import rife.bld.Project;
import rife.bld.operations.AbstractOperation; import rife.bld.operations.AbstractOperation;
import java.io.File; import java.io.File;
@ -32,20 +32,13 @@ import java.util.Properties;
*/ */
public class PropertyFileOperation extends AbstractOperation<PropertyFileOperation> { public class PropertyFileOperation extends AbstractOperation<PropertyFileOperation> {
private final List<EntryBase> entries = new ArrayList<>(); private final List<EntryBase> entries = new ArrayList<>();
private final Project project;
private File file;
private String comment = ""; private String comment = "";
private boolean failOnWarning; private boolean failOnWarning;
private File file;
private BaseProject project;
/** public PropertyFileOperation(Project project) {
* Sets the comment to be inserted at the top of the {@link java.util.Properties} file. this.project = project;
*
* @param comment the header comment
*/
@SuppressWarnings("unused")
public PropertyFileOperation comment(String comment) {
this.comment = comment;
return this;
} }
/** /**
@ -60,6 +53,50 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
return this; return this;
} }
/**
* Sets the location of the {@link java.util.Properties} file to be edited.
*
* @param file the file to be edited
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(String file) {
this.file = new File(file);
return this;
}
/**
* Sets the location of the {@link java.util.Properties} file to be edited.
*
* @param file the file to be edited
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(File file) {
this.file = file;
return this;
}
/**
* Sets the {@link #execute() execution} to return a failure on any warnings.
*
* @param failOnWarning if set to {@code true}, the execution will fail on any warnings.
*/
@SuppressWarnings("unused")
public PropertyFileOperation failOnWarning(boolean failOnWarning) {
this.failOnWarning = failOnWarning;
return this;
}
/**
* 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;
}
/** /**
* Performs the modification(s) to the {@link java.util.Properties properties} file. * Performs the modification(s) to the {@link java.util.Properties properties} file.
*/ */
@ -91,13 +128,12 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
&& (p == null || p.isBlank())) { && (p == null || p.isBlank())) {
PropertyFileUtils.warn(commandName, "An entry must be set or have a default value: " + key); PropertyFileUtils.warn(commandName, "An entry must be set or have a default value: " + key);
} else { } else {
if (entry instanceof EntryDate) { if (entry instanceof EntryDate)
success = PropertyFileUtils.processDate(commandName, properties, (EntryDate) entry, failOnWarning); success = PropertyFileUtils.processDate(commandName, properties, (EntryDate) entry, failOnWarning);
} else if (entry instanceof EntryInt) { else if (entry instanceof EntryInt)
success = PropertyFileUtils.processInt(commandName, properties, (EntryInt) entry, failOnWarning); success = PropertyFileUtils.processInt(commandName, properties, (EntryInt) entry, failOnWarning);
} else { else
success = PropertyFileUtils.processString(properties, (Entry) entry); success = PropertyFileUtils.processString(properties, (Entry) entry);
}
} }
} }
} }
@ -107,45 +143,4 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
PropertyFileUtils.saveProperties(file, comment, properties); PropertyFileUtils.saveProperties(file, comment, properties);
} }
} }
/**
* Sets the {@link #execute() execution} to return a failure on any warnings.
*
* @param failOnWarning if set to {@code true}, the execution will fail on any warnings.
*/
@SuppressWarnings("unused")
public PropertyFileOperation failOnWarning(boolean failOnWarning) {
this.failOnWarning = failOnWarning;
return this;
}
/**
* Sets the location of the {@link java.util.Properties} file to be edited.
*
* @param file the file to be edited
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(File file) {
this.file = file;
return this;
}
/**
* Sets the location of the {@link java.util.Properties} file to be edited.
*
* @param file the file to be edited
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(String file) {
this.file = new File(file);
return this;
}
/**
* Creates a new operation.
*/
public PropertyFileOperation fromProject(BaseProject project) {
this.project = project;
return this;
}
} }

View file

@ -45,49 +45,6 @@ public final class PropertyFileUtils {
// no-op // 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}. * Processes a date {@link Properties property}.
* *
@ -95,7 +52,6 @@ public final class PropertyFileUtils {
* @param p the {@link Properties property} * @param p the {@link Properties property}
* @param entry the {@link Entry} containing the {@link Properties property} edits * @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) public static boolean processDate(String command, Properties p, EntryDate entry, boolean failOnWarning)
throws Exception { throws Exception {
var success = true; var success = true;
@ -190,6 +146,23 @@ public final class PropertyFileUtils {
return success; 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}. * Processes an integer {@link Properties property}.
* *
@ -197,7 +170,6 @@ public final class PropertyFileUtils {
* @param p the {@link Properties property} * @param p the {@link Properties property}
* @param entry the {@link Entry} containing the {@link Properties property} edits * @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) public static boolean processInt(String command, Properties p, EntryInt entry, boolean failOnWarning)
throws Exception { throws Exception {
var success = true; var success = true;
@ -243,21 +215,6 @@ public final class PropertyFileUtils {
return true; 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. * Logs a warning.
* *
@ -278,7 +235,6 @@ public final class PropertyFileUtils {
* @param e the related exception * @param e the related exception
* @param failOnWarning logs and throws exception if set to {@code true} * @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 { static void warn(String command, String message, Exception e, boolean failOnWarning) throws Exception {
if (failOnWarning) { if (failOnWarning) {
LOGGER.log(Level.SEVERE, '[' + command + "] " + message, e); LOGGER.log(Level.SEVERE, '[' + command + "] " + message, e);
@ -287,4 +243,44 @@ public final class PropertyFileUtils {
warn(command, message); 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);
}
}
} }

View file

@ -38,160 +38,79 @@ import static rife.bld.extension.propertyfile.Calc.SUB;
* @since 1.0 * @since 1.0
*/ */
class PropertyFileUtilsTest { class PropertyFileUtilsTest {
final static int dayOfYear = LocalDate.now().getDayOfYear(); final Properties p = new Properties();
final static Properties p = new Properties(); final Entry entry = new Entry("version.major").set("1");
final static String t = "test"; final EntryDate entryDate = new EntryDate("adate").pattern("D");
final EntryInt entryInt = new EntryInt("version.patch");
final int dayOfYear = LocalDate.now().getDayOfYear();
final String t = "test";
public EntryDate newEntryDate() { @Test
p.clear(); void processStringTest() {
return new EntryDate("adate").pattern("D"); PropertyFileUtils.processString(p, entry);
}
public Entry newEntry() { assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
p.clear(); .isEqualTo(p.getProperty(entry.getKey()));
return new Entry("version.major").set("1");
}
public EntryInt newEntryInt() { entry.setKey("version.minor");
p.clear();
return new EntryInt("version.patch"); 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 @Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException") void testWarn() {
void parseDateSub() throws Exception { assertThatCode(() -> PropertyFileUtils.warn("command", "message", new IOException(t), true))
var entryDate = newEntryDate(); .hasMessage(t).isInstanceOf(IOException.class);
entryDate.setCalc(SUB); assertThatCode(() -> PropertyFileUtils.warn("command", t, new Exception(t), false))
PropertyFileUtils.processDate(t, p, entryDate.now(), true); .as("failOnWarning = false").doesNotThrowAnyException();
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 @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() { void parseStringAppend() {
var entry = newEntry();
PropertyFileUtils.processString(p, entry.set(1));
PropertyFileUtils.processString(p, entry.modify("-foo", String::concat)); PropertyFileUtils.processString(p, entry.modify("-foo", String::concat));
assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(), assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(),
entry.getNewValue())).isEqualTo("1-foo"); entry.getNewValue())).isEqualTo("1-foo");
} }
@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 @Test
void parseStringPrepend() { void parseStringPrepend() {
var entry = newEntry();
PropertyFileUtils.processString(p, entry.set(1));
PropertyFileUtils.processString(p, entry.modify("foo-", (v, s) -> s + v)); 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())) assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
.isEqualTo("foo-1"); .isEqualTo("foo-1");
} }
@Test
void parseStringCap() {
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 @Test
void parseStringReplace() { void parseStringReplace() {
var entry = newEntry();
entry.set(t).setModify("T", (v, s) -> v.replace("t", s)); entry.set(t).setModify("T", (v, s) -> v.replace("t", s));
PropertyFileUtils.processString(p, entry); PropertyFileUtils.processString(p, entry);
assertThat(p.getProperty(entry.getKey())).as("replace(t -> T)").isEqualTo("TesT"); 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 @Test
void parseStringSub() { void parseStringSub() {
var entry = newEntry();
PropertyFileUtils.processString(p, entry.set(t).modify((v, s) -> v.substring(1))); PropertyFileUtils.processString(p, entry.set(t).modify((v, s) -> v.substring(1)));
assertThat(p.getProperty(entry.getKey())).as("substring(1)").isEqualTo(t.substring(1)); assertThat(p.getProperty(entry.getKey())).as("substring(1)").isEqualTo(t.substring(1));
} }
@Test @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 { void processIntAddTest() throws Exception {
var entryInt = newEntryInt();
entryInt.calc(ADD); entryInt.calc(ADD);
entryInt.setDefaultValue("-1"); entryInt.setDefaultValue("-1");
PropertyFileUtils.processInt(t, p, entryInt, true); PropertyFileUtils.processInt(t, p, entryInt, true);
@ -218,22 +137,92 @@ class PropertyFileUtilsTest {
} }
@Test @Test
void processStringTest() { void parseIntSubTest() throws Exception {
var entry = newEntry(); entryInt.calc(SUB);
PropertyFileUtils.processString(p, entry); entryInt.setPattern("0000");
PropertyFileUtils.processInt(t, p, entryInt.defaultValue("0017"), true);
assertThat(p.getProperty(entryInt.getKey())).as("sub(0017)").isEqualTo("0016");
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue())) PropertyFileUtils.processInt(t, p, entryInt.set(16).calc(v -> v - 3), true);
.isEqualTo(p.getProperty(entry.getKey())); assertThat(p.getProperty(entryInt.getKey())).as("sub(16)-3").isEqualTo("0013");
}
entry.setKey("version.minor");
@Test
PropertyFileUtils.processString(p, entry.set(0)); void processDateAddTest() throws Exception {
assertThat(entry.getNewValue().toString()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue())) entryDate.setCalc(ADD);
.isEqualTo(p.getProperty(entry.getKey())); 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);
} }
@Test @Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
void savePropertiesTest() throws Exception { void savePropertiesTest() throws Exception {
var p = new Properties(); var p = new Properties();
var test = "test"; var test = "test";
@ -251,25 +240,4 @@ class PropertyFileUtilsTest {
tmp.deleteOnExit(); 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();
}
} }