diff --git a/.github/workflows/bld.yml b/.github/workflows/bld.yml index 94c4f66..b94976b 100644 --- a/.github/workflows/bld.yml +++ b/.github/workflows/bld.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: - java-version: [ 17, 19 ] + java-version: [ 17, 19, 20 ] steps: - name: Checkout source repository @@ -19,7 +19,7 @@ jobs: - name: Set up JDK ${{ matrix.java-version }} uses: actions/setup-java@v3 with: - distribution: 'temurin' + distribution: 'zulu' java-version: ${{ matrix.java-version }} - name: Grant execute permission for bld diff --git a/.idea/libraries/bld.xml b/.idea/libraries/bld.xml index 657c6e2..f713fc9 100644 --- a/.idea/libraries/bld.xml +++ b/.idea/libraries/bld.xml @@ -2,11 +2,11 @@ - + - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index e13f368..d4838b5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "type": "java", "name": "Run Main", "request": "launch", - "mainClass": "rife.bld.extension.propertyFile.PropertyFileOperation" + "mainClass": "rife.bld.extension.propertyfile.PropertyFileOperation" }, { "type": "java", diff --git a/.vscode/settings.json b/.vscode/settings.json index 44ad385..bce4c6c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}/.rife2/dist/rife2-1.5.11.jar", + "${HOME}/.rife2/dist/rife2-1.5.14.jar", "lib/compile/*.jar", "lib/runtime/*.jar", "lib/test/*.jar" diff --git a/README.md b/README.md index c119f25..44d334e 100755 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ [![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](http://opensource.org/licenses/BSD-3-Clause) [![GitHub CI](https://github.com/rife2/bld-property-file/actions/workflows/bld.yml/badge.svg)](https://github.com/rife2/bld-property-file/actions/workflows/bld.yml) - An extension for creating or modifying [property files](https://docs.oracle.com/javase/tutorial/essential/environment/properties.html) with [bld](https://github.com/rife2/rife2/wiki/What-Is-Bld). It is inspired by the [ant PropertyFile task](https://ant.apache.org/manual/Tasks/propertyfile.html). ```java @@ -11,54 +10,80 @@ An extension for creating or modifying [property files](https://docs.oracle.com/ public void updateMajor() throws Exception { new PropertyFileOperation(this) .file("version.properties") - .entry(new Entry("version.major").defaultValue(1).type(Types.INT).operation(Operations.ADD)) + .entry(new Entry("version.major").defaultValue(0).type(Types.INT).operation(Operations.ADD)) .entry(new Entry("version.minor").value(0)) .entry(new Entry("version.patch").value(0)) + .entry(new Entry("build.date").value("now").pattern("yyyy-MM-dd").type(Types.DATE)) .execute(); } ``` - -To invoke the `updateMajor` command: +Invoking the `updateMajor` command, will create the `version.propertees`file: ```sh -./bld updateMajor +./bld updateMajor ... ``` -## PropertyFileOperation +```ini +# version.properties +build.date=2023-04-02 +version.major=1 +version.minor=0 +version.patch=0 +``` -Attribute | Description | Required -:---------------|:----------------------------------------------------------|:-------- -`file` | The location of the properties files to modify. | Yes -`comment` | Comment to be inserted at the top of the properties file. | No -`failOnWarning` | If set to `true`, will fail on any warnings. | No +Invoking the `updateMajor` command again, will increase the `version.major` property: + +```sh +./bld updateMajor ... +``` + +```ini +# version.properties +build.date=2023-04-02 +version.major=2 +version.minor=0 +version.patch=0 +``` + +- [View Examples](https://github.com/rife2/bld-property-file/tree/master/examples) + +## Property File + +The `PropertyFileOperation` class is used to configure the [properties file](https://docs.oracle.com/javase/tutorial/essential/environment/properties.html) location, etc. + +| Attribute | Description | Required | +|:----------------|:-----------------------------------------------------------------|:---------| +| `file` | The location of the properties files to modify. | Yes | +| `comment` | Comment to be inserted at the top of the properties file. | No | +| `failOnWarning` | If set to `true`, will cause executiion to fail on any warnings. | No | ## Entry -The `entry` function is used to specify edits to be made to the properties file. +The `Entry` class is used to specify modifications to be made to the [properties file](https://docs.oracle.com/javase/tutorial/essential/environment/properties.html). -Attribute | Description -:-----------|:----------------------------------------------------------------------------------------------------------------- -`key` | The name of the property name/value pair. -`value` | The value of the property. -`default` | The initial value to set for the property if not already defined. For `Type.DATE`, the `now` keyword can be used. -`type` | Tread the value as `Types.INT`, `Types.DATE`, or `Types.STRING`. If none specified, `Types.STRING` is assumed. -`operation` | See [operations](#operations). -`pattern` | For `Types.INT` and `Types.DATE` only. If present, will parse the value as [DecimalFormat](https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html) or [SimpleDateFormat](https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html) patterns, respectively. -`unit` | The unit value to be applied to `Operations.ADD` and `Operations.SUBTRACT` for `Types.DATE`. See [Units](#units). +| Attribute | Description | +|:---------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `key` | The name of the property name/value pair. | +| `value` | The value of the property. | +| `defaultVlaue` | The initial value to set for the property if not already defined. For `Type.DATE`, the `now` keyword can be used. | +| `type` | Tread the value as `Types.INT`, `Types.DATE`, or `Types.STRING`. If none specified, `Types.STRING` is assumed. | +| `operation` | See [operations](#operations). | +| `pattern` | For `Types.INT` and `Types.DATE` only. If present, will parse the value as [DecimalFormat](https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html) or [SimpleDateFormat](https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html) patterns, respectively. | +| `unit` | The unit value to be applied to `Operations.ADD` and `Operations.SUBTRACT` for `Types.DATE`. See [Units](#units). | -`key` is required. `value` or `default` are required unless the `operation` is `Operations.DELETE`. +`key` is required. `value` or `defaultValue` are required unless the `operation` is `Operations.DELETE`. ## Operations The following operations are available: -Operation | Description -:---------------------|:------------------------------------------------------------------------- -`Operations.ADD` | Adds a value to an entry. -`Operations.DELETE` | Deletes an entry. -`Operations.SET` | Sets the entry value. This is the default operation. -`Operations.SUBTRACT` | Subtracts a value from the entry. For `Types.INT` and `Types.DATE` only. +| Operation | Description | +|:-----------------------|:--------------------------------------------------------------------------| +| `Operations.ADD` | Adds a value to an entry. | +| `Operations.DELETE` | Deletes an entry. | +| `Operations.SET` | Sets the entry value. This is the default operation. | +| `Operations.SUBTRACT` | Subtracts a value from the entry. For `Types.INT` and `Types.DATE` only. | ## Units @@ -78,10 +103,10 @@ The following units are available for `Types.DATE` with `Operations.ADD` and `Op The rules used when setting a property value are: * If only `value` is specified, the property is set to it regardless of its previous value. -* If only `default` is specified and the property previously existed, it is unchanged. -* If only `default` is specified and the property did not exist, the property is set to `default`. -* If `value` and `default` are both specified and the property previously existed, the property is set to `value`. -* If `value` and `default` are both specified and the property did not exist, the property is set to `default`. +* If only `defaultValue` is specified and the property previously existed, it is unchanged. +* If only `defaultValue` is specified and the property did not exist, the property is set to `defaultValue`. +* If `value` and `defaultValue` are both specified and the property previously existed, the property is set to `value`. +* If `value` and `defaultValue` are both specified and the property did not exist, the property is set to `defaultValue`. Operations occur after the rules are evaluated. @@ -89,4 +114,4 @@ Operations occur after the rules are evaluated. * The comments and layout of the original property file will not be preserved. * The `jdkproperties` parameter is not implemented. -* The default `Entry.Types.DATE` pattern is `yyyy-MM-dd HH:mm` and not `yyyy/MM/dd HH:mm`. \ No newline at end of file +* The default `Types.DATE` pattern is `yyyy-MM-dd HH:mm` and not `yyyy/MM/dd HH:mm`. \ No newline at end of file diff --git a/bld b/bld index 94044a2..460941b 100755 --- a/bld +++ b/bld @@ -1,2 +1,2 @@ #!/usr/bin/env sh -java -jar "$(dirname "$0")/lib/bld/bld-wrapper.jar" "$0" --build rife.bld.extension.propertyFile.PropertyFileBuild "$@" \ No newline at end of file +java -jar "$(dirname "$0")/lib/bld/bld-wrapper.jar" "$0" --build rife.bld.extension.propertyfile.PropertyFileBuild "$@" \ No newline at end of file diff --git a/bld.bat b/bld.bat index cc4a868..91acc4b 100644 --- a/bld.bat +++ b/bld.bat @@ -1,4 +1,4 @@ @echo off set DIRNAME=%~dp0 if "%DIRNAME%" == "" set DIRNAME=. -java -jar "%DIRNAME%/lib/bld/bld-wrapper.jar" "%0" --build rife.bld.extension.propertyFile.PropertyFileBuild %* \ No newline at end of file +java -jar "%DIRNAME%/lib/bld/bld-wrapper.jar" "%0" --build rife.bld.extension.propertyfile.PropertyFileBuild %* \ No newline at end of file diff --git a/examples/.idea/libraries/bld.xml b/examples/.idea/libraries/bld.xml index 657c6e2..a0a25eb 100644 --- a/examples/.idea/libraries/bld.xml +++ b/examples/.idea/libraries/bld.xml @@ -2,15 +2,15 @@ - + + - + + - - - + diff --git a/examples/.idea/misc.xml b/examples/.idea/misc.xml index 542659b..4af0048 100644 --- a/examples/.idea/misc.xml +++ b/examples/.idea/misc.xml @@ -1,5 +1,11 @@ + + + + + + diff --git a/examples/.vscode/settings.json b/examples/.vscode/settings.json index 44ad385..bce4c6c 100644 --- a/examples/.vscode/settings.json +++ b/examples/.vscode/settings.json @@ -7,7 +7,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}/.rife2/dist/rife2-1.5.11.jar", + "${HOME}/.rife2/dist/rife2-1.5.14.jar", "lib/compile/*.jar", "lib/runtime/*.jar", "lib/test/*.jar" diff --git a/examples/README.md b/examples/README.md index cce7e7f..96a664a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -2,10 +2,10 @@ ## Compile -First make sure the project is compiled, by issuing the following command: +First make sure the project up-to-date and compiled: ```shell -./bld compile +./bld download compile ``` ## Run @@ -16,11 +16,10 @@ To run the examples, issue one of the following command or combination there off ./bld updateMinor run ./bld updatePatch run ``` -Upon execution, the content of the `verison.properties` file will be displayed, reflecting the modification to the -`version.major`, `version.minor` or `version.patch` properties. +Upon execution, the `version.properties` file will be created and displayed: ```shell -./bld updatePatch run +./bld updateMajor run ``` ```shell @@ -28,8 +27,28 @@ Upon execution, the content of the `verison.properties` file will be displayed, | version.properties | +---------------------------+ # -#Sun Apr 02 17:19:10 PDT 2023 +#Sun Apr 02 23:51:39 PDT 2023 +build.date=2023-04-02 version.major=1 version.minor=0 -version.patch=1 +version.patch=0 +``` + +Subsequent commands will reflect the modifications to the +`version.major`, `version.minor` or `version.patch` properties: + +```shell +./bld upatePatch run +``` + +```shell ++---------------------------+ +| version.properties | ++---------------------------+ +# +#Sun Apr 02 23:55:09 PDT 2023 +build.date=2023-04-02 +version.major=1 +version.minor=0 +version.patch=10 ``` diff --git a/examples/lib/bld/bld-wrapper.jar b/examples/lib/bld/bld-wrapper.jar index 050a6bd..b372bbc 100644 Binary files a/examples/lib/bld/bld-wrapper.jar and b/examples/lib/bld/bld-wrapper.jar differ diff --git a/examples/lib/bld/bld-wrapper.properties b/examples/lib/bld/bld-wrapper.properties index ccddd15..b61179e 100644 --- a/examples/lib/bld/bld-wrapper.properties +++ b/examples/lib/bld/bld-wrapper.properties @@ -1,7 +1,6 @@ #Sun Apr 02 10:32:44 PDT 2023 bld.extension=com.uwyn.rife2:bld-property-file:0.9.0 -bld.repositories=https\://repo1.maven.org/maven2/,/home/erik/.m2/repository +bld.repositories=MAVEN_LOCAL,https\://repo1.maven.org/maven2/ +bld.downloadExtensionSources=true rife2.downloadLocation= -rife2.version=1.5.11 - - +rife2.version=1.5.14 \ No newline at end of file diff --git a/examples/src/bld/java/com/example/PropertyFileExampleBuild.java b/examples/src/bld/java/com/example/PropertyFileExampleBuild.java index 5ff2531..e3e88a6 100644 --- a/examples/src/bld/java/com/example/PropertyFileExampleBuild.java +++ b/examples/src/bld/java/com/example/PropertyFileExampleBuild.java @@ -2,10 +2,10 @@ package com.example; import rife.bld.BuildCommand; import rife.bld.Project; -import rife.bld.extension.propertyFile.Entry; -import rife.bld.extension.propertyFile.Entry.Operations; -import rife.bld.extension.propertyFile.Entry.Types; -import rife.bld.extension.propertyFile.PropertyFileOperation; +import rife.bld.extension.propertyfile.Entry; +import rife.bld.extension.propertyfile.Entry.Operations; +import rife.bld.extension.propertyfile.Entry.Types; +import rife.bld.extension.propertyfile.PropertyFileOperation; import java.util.List; @@ -14,6 +14,7 @@ import static rife.bld.dependencies.Repository.SONATYPE_SNAPSHOTS; import static rife.bld.dependencies.Scope.test; public class PropertyFileExampleBuild extends Project { + final Entry buildDateEntry = new Entry("build.date").value("now").pattern("yyyy-MM-dd").type(Types.DATE); public PropertyFileExampleBuild() { pkg = "com.example"; @@ -33,30 +34,48 @@ public class PropertyFileExampleBuild extends Project { new PropertyFileExampleBuild().start(args); } - @BuildCommand + @BuildCommand(summary = "Updates major version") public void updateMajor() throws Exception { new PropertyFileOperation(this) .file("version.properties") - .entry(new Entry("version.major").defaultValue(1).type(Types.INT).operation(Operations.ADD)) + // set the major version to 1 if it doesn't exist, increase by 1 + .entry(new Entry("version.major").defaultValue(0).type(Types.INT).operation(Operations.ADD)) + // set the minor version to 0 .entry(new Entry("version.minor").value(0)) + // set the patch version to 0 .entry(new Entry("version.patch").value(0)) + // set the build date to the current date + .entry(buildDateEntry) .execute(); } - @BuildCommand + @BuildCommand(summary = "Updates minor version") public void updateMinor() throws Exception { new PropertyFileOperation(this) .file("version.properties") - .entry(new Entry("version.minor").defaultValue(0).type(Types.INT).operation(Operations.ADD)) + // set the major version to 1 if it doesn't exist + .entry(new Entry("version.major").defaultValue(1)) + // set the minor version to 0 if it doesn't exist, increase by 1 + .entry(new Entry("version.minor").defaultValue(-1).type(Types.INT).operation(Operations.ADD)) + // set the patch version to 0 .entry(new Entry("version.patch").value(0)) + // set the build date to the current date + .entry(buildDateEntry) .execute(); } - @BuildCommand + @BuildCommand(summary = "Updates patch version") public void updatePatch() throws Exception { new PropertyFileOperation(this) .file("version.properties") - .entry(new Entry("version.patch").defaultValue(0).type(Types.INT).operation(Operations.ADD)) + // set the major version to 1 if it doesn't exist + .entry(new Entry("version.major").defaultValue(1)) + // set the minor version to 0 if it doesn't exist + .entry(new Entry("version.minor").defaultValue(0)) + // set the patch version to 10 if it doesn't exist, increase by 10 + .entry(new Entry("version.patch").defaultValue(0).type(Types.INT).operation(Operations.ADD).value(10)) + // set the build date to the current date + .entry(buildDateEntry) .execute(); } } \ No newline at end of file diff --git a/examples/src/main/java/com/example/PropertyFileExampleMain.java b/examples/src/main/java/com/example/PropertyFileExampleMain.java index b23e1e8..df451ba 100644 --- a/examples/src/main/java/com/example/PropertyFileExampleMain.java +++ b/examples/src/main/java/com/example/PropertyFileExampleMain.java @@ -21,7 +21,7 @@ public class PropertyFileExampleMain { } } - public String getMessage() { + String getMessage() { return "Hello World!"; } } \ No newline at end of file diff --git a/examples/src/test/java/com/example/PropertyFileExampleTest.java b/examples/src/test/java/com/example/PropertyFileExampleTest.java index 7eedb4a..7ea91a3 100644 --- a/examples/src/test/java/com/example/PropertyFileExampleTest.java +++ b/examples/src/test/java/com/example/PropertyFileExampleTest.java @@ -2,9 +2,9 @@ package com.example; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class PropertyFileExampleTest { +class PropertyFileExampleTest { @Test void verifyHello() { assertEquals("Hello World!", new PropertyFileExampleMain().getMessage()); diff --git a/examples/version.properties b/examples/version.properties deleted file mode 100644 index 435570e..0000000 --- a/examples/version.properties +++ /dev/null @@ -1,5 +0,0 @@ -# -#Sun Apr 02 17:19:10 PDT 2023 -version.major=1 -version.minor=0 -version.patch=1 diff --git a/lib/bld/bld-wrapper.jar b/lib/bld/bld-wrapper.jar index 02b522b..d80796e 100644 Binary files a/lib/bld/bld-wrapper.jar and b/lib/bld/bld-wrapper.jar differ diff --git a/lib/bld/bld-wrapper.properties b/lib/bld/bld-wrapper.properties index 84e9308..f40fc3f 100644 --- a/lib/bld/bld-wrapper.properties +++ b/lib/bld/bld-wrapper.properties @@ -1,2 +1,2 @@ #Sat Apr 01 10:09:33 PDT 2023 -rife2.version=1.5.11 +rife2.version=1.5.14 diff --git a/src/bld/java/rife/bld/extension/propertyFile/PropertyFileBuild.java b/src/bld/java/rife/bld/extension/propertyfile/PropertyFileBuild.java similarity index 97% rename from src/bld/java/rife/bld/extension/propertyFile/PropertyFileBuild.java rename to src/bld/java/rife/bld/extension/propertyfile/PropertyFileBuild.java index d452cb7..0708b54 100644 --- a/src/bld/java/rife/bld/extension/propertyFile/PropertyFileBuild.java +++ b/src/bld/java/rife/bld/extension/propertyfile/PropertyFileBuild.java @@ -1,4 +1,4 @@ -package rife.bld.extension.propertyFile; +package rife.bld.extension.propertyfile; import rife.bld.Project; import rife.bld.publish.PublishDeveloper; @@ -35,7 +35,7 @@ public class PropertyFileBuild extends Project { downloadSources = true; repositories = List.of(MAVEN_CENTRAL, SONATYPE_SNAPSHOTS); scope(compile) - .include(dependency("com.uwyn.rife2", "rife2", version(1, 5, 11))); + .include(dependency("com.uwyn.rife2", "rife2", version(1, 5, 15))); scope(test) .include(dependency("org.jsoup", "jsoup", version(1, 15, 4))) .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 9, 2))) diff --git a/src/main/java/rife/bld/extension/propertyFile/Entry.java b/src/main/java/rife/bld/extension/propertyfile/Entry.java similarity index 97% rename from src/main/java/rife/bld/extension/propertyFile/Entry.java rename to src/main/java/rife/bld/extension/propertyfile/Entry.java index 99c1903..7dafd76 100644 --- a/src/main/java/rife/bld/extension/propertyFile/Entry.java +++ b/src/main/java/rife/bld/extension/propertyfile/Entry.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package rife.bld.extension.propertyFile; +package rife.bld.extension.propertyfile; /** *

Declares the edits to be made to a {@link java.util.Properties Properties} file.

@@ -174,6 +174,8 @@ public class Entry { * * @param key the {@link java.util.Properties property} key */ + + @SuppressWarnings("unused") public Entry key(String key) { setKey(key); return this; @@ -184,6 +186,7 @@ public class Entry { * * @param value the {@link java.util.Properties property} value */ + @SuppressWarnings("unused") public Entry value(Object value) { if (value != null) { setValue(String.valueOf(value)); @@ -200,6 +203,7 @@ public class Entry { * * @param defaultValue the default value */ + @SuppressWarnings("unused") public Entry defaultValue(Object defaultValue) { if (defaultValue != null) { setDefaultValue(String.valueOf(defaultValue)); @@ -224,6 +228,7 @@ public class Entry { * * @param operation the entry {@link Operations Operation} */ + @SuppressWarnings("unused") public Entry operation(Operations operation) { setOperation(operation); return this; @@ -247,6 +252,7 @@ public class Entry { * * @param unit the {@link Units unit} */ + @SuppressWarnings("unused") public Entry unit(Units unit) { setUnit(unit); return this; diff --git a/src/main/java/rife/bld/extension/propertyFile/PropertyFileOperation.java b/src/main/java/rife/bld/extension/propertyfile/PropertyFileOperation.java similarity index 78% rename from src/main/java/rife/bld/extension/propertyFile/PropertyFileOperation.java rename to src/main/java/rife/bld/extension/propertyfile/PropertyFileOperation.java index 80306c2..1692a99 100644 --- a/src/main/java/rife/bld/extension/propertyFile/PropertyFileOperation.java +++ b/src/main/java/rife/bld/extension/propertyfile/PropertyFileOperation.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package rife.bld.extension.propertyFile; +package rife.bld.extension.propertyfile; import rife.bld.Project; -import rife.bld.extension.propertyFile.Entry.Operations; -import rife.bld.extension.propertyFile.Entry.Types; +import rife.bld.extension.propertyfile.Entry.Operations; +import rife.bld.extension.propertyfile.Entry.Types; import rife.bld.operations.AbstractOperation; import java.io.File; @@ -50,6 +50,7 @@ public class PropertyFileOperation extends AbstractOperation success = PropertyFileUtils.processDate(properties, entry, failOnWarning); - case INT -> success = PropertyFileUtils.processInt(properties, entry, failOnWarning); + case DATE -> + success = PropertyFileUtils.processDate(commandName, properties, entry, failOnWarning); + case INT -> + success = PropertyFileUtils.processInt(commandName, properties, entry, failOnWarning); default -> success = PropertyFileUtils.processString(properties, entry); } } diff --git a/src/main/java/rife/bld/extension/propertyFile/PropertyFileUtils.java b/src/main/java/rife/bld/extension/propertyfile/PropertyFileUtils.java similarity index 77% rename from src/main/java/rife/bld/extension/propertyFile/PropertyFileUtils.java rename to src/main/java/rife/bld/extension/propertyfile/PropertyFileUtils.java index dd7af8b..f3d6869 100644 --- a/src/main/java/rife/bld/extension/propertyFile/PropertyFileUtils.java +++ b/src/main/java/rife/bld/extension/propertyfile/PropertyFileUtils.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package rife.bld.extension.propertyFile; +package rife.bld.extension.propertyfile; -import rife.bld.extension.propertyFile.Entry.Operations; -import rife.bld.extension.propertyFile.Entry.Units; +import rife.bld.extension.propertyfile.Entry.Operations; +import rife.bld.extension.propertyfile.Entry.Units; import rife.tools.Localization; import javax.imageio.IIOException; @@ -62,11 +62,12 @@ public final class PropertyFileUtils { /** * Processes a date {@link Properties property}. * - * @param p the {@link Properties property} - * @param entry the {@link Entry} containing the {@link Properties property} edits + * @param command the issuing command + * @param p the {@link Properties property} + * @param entry the {@link Entry} containing the {@link Properties property} edits * @return {@code true} if successful */ - public static boolean processDate(Properties p, Entry entry, boolean failOnWarning) { + public static boolean processDate(String command, Properties p, Entry entry, boolean failOnWarning) { var success = true; var cal = Calendar.getInstance(); var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getValue(), @@ -85,7 +86,8 @@ public final class PropertyFileUtils { try { cal.setTime(fmt.parse(value)); } catch (ParseException pe) { - warn("Date parse exception for: " + entry.getKey() + " --> " + pe.getMessage(), pe, failOnWarning); + warn(command, "Non-date value for \"" + entry.getKey() + "\" --> " + pe.getMessage(), + pe, failOnWarning); success = false; } } @@ -99,10 +101,12 @@ public final class PropertyFileUtils { offset *= -1; } } catch (NumberFormatException nfe) { - warn("Non-integer value for: " + entry.getKey() + " --> " + nfe.getMessage(), nfe, failOnWarning); + warn(command, "Non-date value for \"" + entry.getKey() + "\" --> " + nfe.getMessage(), nfe, + failOnWarning); success = false; } + //noinspection MagicConstant cal.add(calendarFields.getOrDefault(entry.getUnit(), Calendar.DATE), offset); } @@ -158,25 +162,15 @@ public final class PropertyFileUtils { return result; } - /** - * Ensure that the given value is an integer. - * - * @param value the value - * @return the parsed value - * @throws NumberFormatException if the value could not be parsed as an integer - */ - static String parseInt(String value) throws NumberFormatException { - return String.valueOf(Integer.parseInt(value)); - } - /** * Processes an integer {@link Properties property}. * - * @param p the {@link Properties property} - * @param entry the {@link Entry} containing the {@link Properties property} edits + * @param command the issuing command + * @param p the {@link Properties property} + * @param entry the {@link Entry} containing the {@link Properties property} edits * @return {@code true} if successful */ - public static boolean processInt(Properties p, Entry entry, boolean failOnWarning) { + public static boolean processInt(String command, Properties p, Entry entry, boolean failOnWarning) { var success = true; int intValue; try { @@ -187,13 +181,13 @@ public final class PropertyFileUtils { if (value.isBlank()) { intValue = fmt.parse("0").intValue(); } else { - intValue = fmt.parse(parseInt(value)).intValue(); + intValue = fmt.parse(value).intValue(); } if (entry.getOperation() != Entry.Operations.SET) { var opValue = 1; if (entry.getValue() != null) { - opValue = fmt.parse(parseInt(entry.getValue())).intValue(); + opValue = fmt.parse(entry.getValue()).intValue(); } if (entry.getOperation() == Entry.Operations.ADD) { intValue += opValue; @@ -202,11 +196,9 @@ public final class PropertyFileUtils { } } p.setProperty(entry.getKey(), fmt.format(intValue)); - } catch (NumberFormatException nfe) { - warn("Number format exception for: " + entry.getKey() + " --> " + nfe.getMessage(), nfe, failOnWarning); - success = false; - } catch (ParseException pe) { - warn("Number parsing exception for: " + entry.getKey() + " --> " + pe.getMessage(), pe, failOnWarning); + } catch (NumberFormatException | ParseException e) { + warn(command, "Non-integer value for \"" + entry.getKey() + "\" --> " + e.getMessage(), e, + failOnWarning); success = false; } @@ -238,27 +230,29 @@ public final class PropertyFileUtils { /** * Logs a warning. * + * @param command the issuing command * @param message the message to log */ - static void warn(String message) { + static void warn(String command, String message) { if (LOGGER.isLoggable(Level.WARNING)) { - LOGGER.warning(message); + LOGGER.warning('[' + command + "] " + message); } } /** * Logs a warning. * + * @param command The command name * @param message the message log * @param e the related exception * @param failOnWarning skips logging the exception if set to {@code false} */ - static void warn(String message, Exception e, boolean failOnWarning) { + static void warn(String command, String message, Exception e, boolean failOnWarning) { if (LOGGER.isLoggable(Level.WARNING)) { if (failOnWarning) { - LOGGER.log(Level.WARNING, message, e); + LOGGER.log(Level.WARNING, '[' + command + "] " + message, e); } else { - LOGGER.warning(message); + LOGGER.warning('[' + command + "] " + message); } } } @@ -266,26 +260,24 @@ public final class PropertyFileUtils { /** * Loads a {@link Properties properties} file. * - * @param file the file location. - * @param p the {@link Properties properties} to load into. + * @param command the issuing command + * @param file the file location. + * @param p the {@link Properties properties} to load into. * @return {@code true} if successful */ - public static boolean loadProperties(File file, Properties p) { + public static boolean loadProperties(String command, File file, Properties p) { 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("Could not load properties file: " + ioe.getMessage(), ioe, true); + warn(command, "Could not load properties file: " + ioe.getMessage(), ioe, true); success = false; } - } else { - warn("The '" + file + "' properties file could not be found."); - success = false; } } else { - warn("Please specify the properties file location."); + warn(command, "Please specify the properties file location."); success = false; } return success; diff --git a/src/test/java/rife/bld/extension/propertyFile/PropertyFileUtilsTest.java b/src/test/java/rife/bld/extension/propertyfile/PropertyFileUtilsTest.java similarity index 63% rename from src/test/java/rife/bld/extension/propertyFile/PropertyFileUtilsTest.java rename to src/test/java/rife/bld/extension/propertyfile/PropertyFileUtilsTest.java index ee03704..4448272 100644 --- a/src/test/java/rife/bld/extension/propertyFile/PropertyFileUtilsTest.java +++ b/src/test/java/rife/bld/extension/propertyfile/PropertyFileUtilsTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package rife.bld.extension.propertyFile; +package rife.bld.extension.propertyfile; import org.junit.jupiter.api.Test; import rife.tools.Localization; @@ -36,65 +36,52 @@ import static org.assertj.core.api.Assertions.assertThatCode; */ class PropertyFileUtilsTest { final Properties p = new Properties(); + final String t = "test"; @Test void currentValueTest() { - String prev; - String value; - String defaultValue = null; + String prev = "previous"; + String value = "value"; + String defaultValue = "defaultValue"; var operation = Entry.Operations.SET; // If only value is specified, the property is set to it regardless of its previous value. - prev = "previous"; - value = "value"; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(value); + assertThat(PropertyFileUtils.currentValue(prev, value, null, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(value); // If only defaultValue is specified and the property previously existed, it is unchanged. - prev = "previous"; - value = null; - defaultValue = "defaultValue"; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(prev); + assertThat(PropertyFileUtils.currentValue(prev, null, defaultValue, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(prev); // If only defaultValue is specified and the property did not exist, the property is set to defaultValue. - prev = null; - defaultValue = "defaultValue"; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(defaultValue); + assertThat(PropertyFileUtils.currentValue(null, null, defaultValue, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(defaultValue); // If value and defaultValue are both specified and the property previously existed, the property is set to value. - prev = "previous"; - value = "value"; - defaultValue = "defaultValue"; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(value); + assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(value); // If value and defaultValue are both specified and the property did not exist, the property is set to defaultValue. - prev = null; - value = "value"; - defaultValue = "defaultValue"; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(defaultValue); + assertThat(PropertyFileUtils.currentValue(null, value, defaultValue, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(defaultValue); // ADD operation = Entry.Operations.ADD; - value = "value"; - defaultValue = "defaultValue"; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(defaultValue); + assertThat(PropertyFileUtils.currentValue(null, value, defaultValue, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(defaultValue); - prev = "prev"; - value = "value"; - defaultValue = null; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(prev); + assertThat(PropertyFileUtils.currentValue(prev, value, null, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(prev); - prev = null; - value = "value"; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(""); + assertThat(PropertyFileUtils.currentValue(null, value, null, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(""); - value = "value"; - defaultValue = "defaultValue"; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(defaultValue); + assertThat(PropertyFileUtils.currentValue(null, value, defaultValue, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(defaultValue); - value = null; - defaultValue = null; - assertThat(PropertyFileUtils.currentValue(prev, value, defaultValue, operation)).as("currentValue(prev,value,defaultValue,operation)").isEqualTo(""); + assertThat(PropertyFileUtils.currentValue(null, null, null, operation)) + .as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(""); } @Test @@ -115,38 +102,38 @@ class PropertyFileUtilsTest { @Test void processIntTest() { var entry = new Entry("version.patch").value("a").type(Entry.Types.INT); - assertThat(PropertyFileUtils.processInt(p, entry, false)).as("parseInt(entry.getKey(), a)"); + assertThat(PropertyFileUtils.processInt(t, p, entry, false)).as("parseInt(entry.getKey(), a)"); // ADD entry.setOperation(Entry.Operations.ADD); entry.setValue("1"); entry.setDefaultValue("-1"); - PropertyFileUtils.processInt(p, entry, true); + PropertyFileUtils.processInt(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0)").isEqualTo("0"); entry.setKey("anint"); entry.setValue(null); entry.setDefaultValue("0"); - PropertyFileUtils.processInt(p, entry, true); + PropertyFileUtils.processInt(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 1)").isEqualTo("1"); - PropertyFileUtils.processInt(p, entry, true); + PropertyFileUtils.processInt(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 2)").isEqualTo("2"); entry.setKey("formated.int"); entry.setValue(null); entry.setDefaultValue("0013"); entry.setPattern("0000"); - PropertyFileUtils.processInt(p, entry, true); + PropertyFileUtils.processInt(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0014)").isEqualTo("0014"); - PropertyFileUtils.processInt(p, entry, true); + PropertyFileUtils.processInt(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0015)").isEqualTo("0015"); entry.setKey("formated.int"); entry.setValue("2"); entry.setDefaultValue("0013"); entry.setPattern("0000"); - PropertyFileUtils.processInt(p, entry, true); + PropertyFileUtils.processInt(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0017)").isEqualTo("0017"); // SUBTRACT @@ -154,7 +141,7 @@ class PropertyFileUtilsTest { entry.setValue(null); entry.setDefaultValue("0013"); entry.setPattern("0000"); - PropertyFileUtils.processInt(p, entry, true); + PropertyFileUtils.processInt(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0016)").isEqualTo("0016"); } @@ -165,36 +152,36 @@ class PropertyFileUtilsTest { var dayInt = Integer.parseInt(day); entry.setValue("a"); - assertThat(PropertyFileUtils.processDate(p, entry, false)).as("processDate(entry.getKey(), a)").isFalse(); + assertThat(PropertyFileUtils.processDate(t, p, entry, false)).as("processDate(entry.getKey(), a)").isFalse(); entry.setValue("99"); - PropertyFileUtils.processDate(p, entry, true); + PropertyFileUtils.processDate(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), 99)").isEqualTo("99"); entry.setValue("now"); - PropertyFileUtils.processDate(p, entry, true); + PropertyFileUtils.processDate(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now)").isEqualTo(day); // ADD entry.setOperation(Entry.Operations.ADD); entry.setValue("1"); - PropertyFileUtils.processDate(p, entry, true); + PropertyFileUtils.processDate(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now+1)").isEqualTo(String.valueOf(dayInt + 1)); entry.setValue("2"); - PropertyFileUtils.processDate(p, entry, true); + PropertyFileUtils.processDate(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now+3)").isEqualTo(String.valueOf(dayInt + 3)); // SUBTRACT entry.setOperation(Entry.Operations.SUBTRACT); entry.setValue("3"); - PropertyFileUtils.processDate(p, entry, true); + PropertyFileUtils.processDate(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now-3)").isEqualTo(String.valueOf(dayInt)); entry.setOperation(Entry.Operations.SUBTRACT); entry.setValue("2"); - PropertyFileUtils.processDate(p, entry, true); + PropertyFileUtils.processDate(t, p, entry, true); assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now-2)").isEqualTo(String.valueOf(dayInt - 2)); } @@ -210,10 +197,10 @@ class PropertyFileUtilsTest { assertThatCode(() -> PropertyFileUtils.saveProperties(tmp, "Generated file - do not modify!", p)) .as("save properties").doesNotThrowAnyException(); - assertThat(PropertyFileUtils.loadProperties(tmp, p)).as("load properties").isTrue(); + assertThat(PropertyFileUtils.loadProperties(t, tmp, p)).as("load properties").isTrue(); assertThat(p.getProperty(test)).as("read property").isEqualTo(test); tmp.deleteOnExit(); } -} +} \ No newline at end of file