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

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View file

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

View file

@ -11,7 +11,8 @@ 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(this) new PropertyFileOperation()
.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))

105
config/pmd.xml Normal file
View file

@ -0,0 +1,105 @@
<?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.19.jar!/" /> <root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.20.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.19-sources.jar!/" /> <root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.20-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.19.jar", "${HOME}/.rife2/dist/rife2-1.5.20.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.1 bld.extension=com.uwyn.rife2:bld-property-file:0.9.2-SNAPSHOT
bld.repositories=MAVEN_LOCAL,RIFE2_RELEASES bld.repositories=MAVEN_LOCAL,MAVEN_LOCAL,RIFE2_RELEASES,RIFE2_SNAPSHOTS
bld.downloadExtensionSources=true bld.downloadExtensionSources=true
rife2.downloadLocation= rife2.downloadLocation=
rife2.version=1.5.19 rife2.version=1.5.20

View file

@ -39,7 +39,8 @@ 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(this) new PropertyFileOperation()
.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))
@ -54,7 +55,8 @@ 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(this) new PropertyFileOperation()
.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))
@ -69,7 +71,8 @@ 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(this) new PropertyFileOperation()
.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))
@ -84,7 +87,8 @@ 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(this) new PropertyFileOperation()
.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"))
@ -95,7 +99,8 @@ 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(this) new PropertyFileOperation()
.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())

View file

@ -1,6 +1,6 @@
bld.downloadExtensionJavadoc=false bld.downloadExtensionJavadoc=false
bld.downloadExtensionSources=true bld.downloadExtensionSources=true
bld.extensions= bld.extension-pmd=com.uwyn.rife2:bld-pmd:0.9.1-SNAPSHOT
bld.repositories=MAVEN_CENTRAL,RIFE2_RELEASES bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
rife2.downloadLocation= rife2.downloadLocation=
rife2.version=1.5.20 rife2.version=1.5.20

View file

@ -1,8 +1,9 @@
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;
@ -33,27 +34,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"));
} }
@ -61,4 +62,13 @@ 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,6 +29,11 @@ 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,16 +30,6 @@ 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>
* *
@ -51,6 +41,24 @@ 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}.
* *
@ -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<String, String, String> modify) { public Entry set(Object s) {
setModify(modify); setNewValue(s);
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 String key; private IntFunction<Integer> calc;
private Object defaultValue; private Object defaultValue;
private Object newValue;
private String modifyValue = "";
private boolean isDelete; private boolean isDelete;
private String key;
private BiFunction<String, String, String> modify;
private String modifyValue = "";
private Object newValue;
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,60 +46,6 @@ public class EntryBase {
this.key = key; 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<String, String, String> getModify() {
return modify;
}
/**
* Sets the modify function.
*/
protected void setModify(BiFunction<String, String, String> modify) {
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. * Returns the calculation function.
*/ */
@ -114,6 +60,22 @@ public class EntryBase {
this.calc = 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}. * 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() { protected BiFunction<String, String, String> getModify() {
return defaultValue; return modify;
} }
/** /**
* Sets the initial value to set the {@link java.util.Properties property} to, if not already defined. * Sets the modify function.
*
* @param defaultValue the default value
*/ */
protected void setDefaultValue(Object defaultValue) { protected void setModify(BiFunction<String, String, String> modify) {
this.defaultValue = defaultValue; 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; 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}.
* *
@ -191,18 +197,12 @@ public class EntryBase {
} }
/** /**
* Returns the new value to set the {@link java.util.Properties property)} to. * Sets the modify function.
*/
public Object getNewValue() {
return newValue;
}
/**
* Sets a new value for {@link java.util.Properties property}.
* *
* @param newValue the new value * @param value the value to perform a modification with
*/ */
public void setNewValue(Object newValue) { protected void setModify(String value, BiFunction<String, String, String> modify) {
this.newValue = newValue; this.modifyValue = value;
this.modify = modify;
} }
} }

View file

@ -37,6 +37,43 @@ 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}
* *
@ -107,35 +144,6 @@ 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}.
* *
@ -146,14 +154,6 @@ 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 {
} }
/** /**
* 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) { public EntryInt calc(IntFunction<Integer> calc) {
setNewValue(i); setCalc(calc);
return this; return this;
} }
@ -55,16 +55,6 @@ 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.
*/ */
@ -72,4 +62,14 @@ 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.Project; import rife.bld.BaseProject;
import rife.bld.operations.AbstractOperation; import rife.bld.operations.AbstractOperation;
import java.io.File; import java.io.File;
@ -32,13 +32,20 @@ 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) { /**
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<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.
*/ */
@ -128,12 +91,13 @@ 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);
}
} }
} }
} }
@ -143,4 +107,45 @@ 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,6 +45,49 @@ 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}.
* *
@ -52,6 +95,7 @@ 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;
@ -146,23 +190,6 @@ 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}.
* *
@ -170,6 +197,7 @@ 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;
@ -215,6 +243,21 @@ 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.
* *
@ -235,6 +278,7 @@ 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);
@ -243,44 +287,4 @@ 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,79 +38,160 @@ import static rife.bld.extension.propertyfile.Calc.SUB;
* @since 1.0 * @since 1.0
*/ */
class PropertyFileUtilsTest { class PropertyFileUtilsTest {
final Properties p = new Properties(); final static int dayOfYear = LocalDate.now().getDayOfYear();
final Entry entry = new Entry("version.major").set("1"); final static Properties p = new Properties();
final EntryDate entryDate = new EntryDate("adate").pattern("D"); final static String t = "test";
final EntryInt entryInt = new EntryInt("version.patch");
final int dayOfYear = LocalDate.now().getDayOfYear();
final String t = "test";
@Test public EntryDate newEntryDate() {
void processStringTest() { p.clear();
PropertyFileUtils.processString(p, entry); return new EntryDate("adate").pattern("D");
}
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue())) public Entry newEntry() {
.isEqualTo(p.getProperty(entry.getKey())); p.clear();
return new Entry("version.major").set("1");
}
entry.setKey("version.minor"); public EntryInt newEntryInt() {
p.clear();
PropertyFileUtils.processString(p, entry.set(0)); return new EntryInt("version.patch");
assertThat(entry.getNewValue().toString()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
.isEqualTo(p.getProperty(entry.getKey()));
} }
@Test @Test
void testWarn() { @SuppressWarnings("PMD.SignatureDeclareThrowsException")
assertThatCode(() -> PropertyFileUtils.warn("command", "message", new IOException(t), true)) void parseDateSub() throws Exception {
.hasMessage(t).isInstanceOf(IOException.class); var entryDate = newEntryDate();
assertThatCode(() -> PropertyFileUtils.warn("command", t, new Exception(t), false)) entryDate.setCalc(SUB);
.as("failOnWarning = false").doesNotThrowAnyException(); 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 @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 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 @Test
void parseStringCap() { void parseStringCap() {
var entry = newEntry();
PropertyFileUtils.processString(p, entry.set(t).modify("", (v, s) -> v.toUpperCase(Localization.getLocale()))); 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())); 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 @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);
@ -137,92 +218,22 @@ class PropertyFileUtilsTest {
} }
@Test @Test
void parseIntSubTest() throws Exception { void processStringTest() {
entryInt.calc(SUB); var entry = newEntry();
entryInt.setPattern("0000"); PropertyFileUtils.processString(p, entry);
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(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
assertThat(p.getProperty(entryInt.getKey())).as("sub(16)-3").isEqualTo("0013"); .isEqualTo(p.getProperty(entry.getKey()));
}
entry.setKey("version.minor");
@Test
void processDateAddTest() throws Exception { PropertyFileUtils.processString(p, entry.set(0));
entryDate.setCalc(ADD); assertThat(entry.getNewValue().toString()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
PropertyFileUtils.processDate(t, p, entryDate.now(), true); .isEqualTo(p.getProperty(entry.getKey()));
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";
@ -240,4 +251,25 @@ 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();
}
} }