Added calc and modify functions
This commit is contained in:
parent
3fe54858fa
commit
753f3bcf45
19 changed files with 382 additions and 343 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -53,3 +53,6 @@ atlassian-ide-plugin.xml
|
|||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Local Properties
|
||||
local.properties
|
4
.idea/libraries/bld.xml
generated
4
.idea/libraries/bld.xml
generated
|
@ -2,11 +2,11 @@
|
|||
<library name="bld">
|
||||
<CLASSES>
|
||||
<root url="file://$PROJECT_DIR$/lib/bld" />
|
||||
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.14.jar!/" />
|
||||
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.16.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES>
|
||||
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.14-sources.jar!/" />
|
||||
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.16-sources.jar!/" />
|
||||
</SOURCES>
|
||||
<excluded>
|
||||
<root url="jar://$PROJECT_DIR$/lib/bld/bld-wrapper.jar!/" />
|
||||
|
|
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
|
@ -6,6 +6,9 @@
|
|||
<pattern value="rife.bld.extension.propertyFile.Entry" method="key" />
|
||||
<pattern value="rife.bld.extension.propertyFile.Entry" method="operation" />
|
||||
<pattern value="rife.bld.extension.propertyFile.Entry" method="unit" />
|
||||
<pattern value="rife.bld.extension.propertyfile.Entry" method="delete" />
|
||||
<pattern value="rife.bld.extension.propertyfile.Entry" />
|
||||
<pattern value="rife.bld.extension.propertyfile.Entry" method="setDelete" />
|
||||
</component>
|
||||
<component name="PDMPlugin">
|
||||
<option name="skipTestSources" value="false" />
|
||||
|
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
|
@ -7,7 +7,7 @@
|
|||
],
|
||||
"java.configuration.updateBuildConfiguration": "automatic",
|
||||
"java.project.referencedLibraries": [
|
||||
"${HOME}/.rife2/dist/rife2-1.5.14.jar",
|
||||
"${HOME}/.rife2/dist/rife2-1.5.16.jar",
|
||||
"lib/compile/*.jar",
|
||||
"lib/runtime/*.jar",
|
||||
"lib/test/*.jar"
|
||||
|
|
71
README.md
71
README.md
|
@ -1,6 +1,7 @@
|
|||
# [Bld](https://github.com/rife2/rife2/wiki/What-Is-Bld) Extension to Create or Modify Properties Files
|
||||
|
||||
[](http://opensource.org/licenses/BSD-3-Clause)
|
||||
[](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
|
||||
[](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).
|
||||
|
@ -10,13 +11,12 @@ 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(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))
|
||||
.entry(new Entry("version.major", Types.INT).defaultValue(0).calc(ADD))
|
||||
.entry(new Entry("version.minor").set(0))
|
||||
.entry(new Entry("version.patch").set(0))
|
||||
.entry(new Entry("build.date", Types.DATE).set("now").pattern("yyyy-MM-dd"))
|
||||
.execute();
|
||||
}
|
||||
|
||||
```
|
||||
Invoking the `updateMajor` command, will create the `version.propertees`file:
|
||||
|
||||
|
@ -52,42 +52,41 @@ version.patch=0
|
|||
|
||||
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 |
|
||||
| Function | 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 execution to fail on any warnings. | No |
|
||||
|
||||
## Entry
|
||||
|
||||
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. |
|
||||
| `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). |
|
||||
| Function | Description |
|
||||
|:-----------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `key()` | The name of the property name/value pair. |
|
||||
| `set()` | The value to set the property to, regardless of its previous value. |
|
||||
| `defaultValue()` | The initial value to set for the property to, if not already defined. |
|
||||
| `type()` | The value datatype, either `Types.INT`, `Types.DATE`, or `Types.STRING`. If none specified, `Types.STRING` is assumed. |
|
||||
| `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 used with `Types.DATE` calculations. See [Units](#units). |
|
||||
|
||||
`key` is required. `value` or `defaultValue` are required unless the `operation` is `Operations.DELETE`.
|
||||
- For convenience the `key` (and optional `type`) is first set in the constructor.
|
||||
- `key` is required. `value` or `defaultValue` are required except when deleting.
|
||||
- For `Type.DATE`, the `now` keyword can be used as the property value.
|
||||
|
||||
## Operations
|
||||
## Functions
|
||||
|
||||
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. |
|
||||
The following function are available:
|
||||
|
||||
| Function | Example | Description |
|
||||
|:-----------|:--------------------------------------------------------------------------------------------------------|:-------------------------------------------|
|
||||
| `calc()` | `calc(ADD)`<br/>`calc(v -> v + 1)`<br/>`calc(SUB)`<br/>`calc(v -> v - 1)` | Perform a calculation with an entry value. |
|
||||
| `modify()` | `modify("-foo", String::concat)`<br/>`modify("-foo", (v, s) -> v + s)`<br/>`modify((v, s) -> v.trim())` | Modify an entry value. |
|
||||
| `delete()` | `delete()` | Delete an entry. |
|
||||
## Units
|
||||
|
||||
The following units are available for `Types.DATE` with `Operations.ADD` and `Operations.SUBTRACT`:
|
||||
The following units are available for `Types.DATE`:
|
||||
|
||||
* `Units.MILLISECOND`
|
||||
* `Units.SECOND`
|
||||
|
@ -98,18 +97,6 @@ The following units are available for `Types.DATE` with `Operations.ADD` and `Op
|
|||
* `Units.MONTH`
|
||||
* `Units.YEAR`
|
||||
|
||||
## Rules
|
||||
|
||||
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 `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.
|
||||
|
||||
## Differences with the [ant PropertyFile task](https://ant.apache.org/manual/Tasks/propertyfile.html)
|
||||
|
||||
* The comments and layout of the original property file will not be preserved.
|
||||
|
|
4
examples/.idea/libraries/bld.xml
generated
4
examples/.idea/libraries/bld.xml
generated
|
@ -2,12 +2,12 @@
|
|||
<library name="bld">
|
||||
<CLASSES>
|
||||
<root url="file://$PROJECT_DIR$/lib/bld" />
|
||||
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.14.jar!/" />
|
||||
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.16.jar!/" />
|
||||
<root url="file://$PROJECT_DIR$/lib/bld" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES>
|
||||
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.14-sources.jar!/" />
|
||||
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.16-sources.jar!/" />
|
||||
<root url="file://$PROJECT_DIR$/lib/bld" />
|
||||
</SOURCES>
|
||||
<jarDirectory url="file://$PROJECT_DIR$/lib/bld" recursive="false" />
|
||||
|
|
4
examples/.idea/misc.xml
generated
4
examples/.idea/misc.xml
generated
|
@ -1,10 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="EntryPointsManager">
|
||||
<list size="1">
|
||||
<item index="0" class="java.lang.String" itemvalue="rife.bld.BuildCommand" />
|
||||
</list>
|
||||
<pattern value="com.example.PropertyFileExampleBuild" />
|
||||
<pattern value="com.example.PropertyFileExampleBuild" method="updateMajor" />
|
||||
<pattern value="com.example.PropertyFileExampleBuild" method="updateMinor" />
|
||||
<pattern value="com.example.PropertyFileExampleBuild" method="updatePatch" />
|
||||
<pattern value="com.example.PropertyFileExampleBuild" method="deleteVersion" />
|
||||
</component>
|
||||
<component name="PDMPlugin">
|
||||
<option name="skipTestSources" value="false" />
|
||||
|
|
2
examples/.vscode/settings.json
vendored
2
examples/.vscode/settings.json
vendored
|
@ -7,7 +7,7 @@
|
|||
],
|
||||
"java.configuration.updateBuildConfiguration": "automatic",
|
||||
"java.project.referencedLibraries": [
|
||||
"${HOME}/.rife2/dist/rife2-1.5.14.jar",
|
||||
"${HOME}/.rife2/dist/rife2-1.5.16.jar",
|
||||
"lib/compile/*.jar",
|
||||
"lib/runtime/*.jar",
|
||||
"lib/test/*.jar"
|
||||
|
|
Binary file not shown.
|
@ -1,6 +1,6 @@
|
|||
#Sun Apr 02 10:32:44 PDT 2023
|
||||
bld.extension=com.uwyn.rife2:bld-property-file:0.9.0
|
||||
bld.repositories=MAVEN_LOCAL,https\://repo1.maven.org/maven2/
|
||||
bld.repositories=MAVEN_LOCAL,RIFE2,https\://repo1.maven.org/maven2/
|
||||
bld.downloadExtensionSources=true
|
||||
rife2.downloadLocation=
|
||||
rife2.version=1.5.14
|
||||
rife2.version=1.5.16
|
|
@ -3,7 +3,6 @@ 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;
|
||||
|
||||
|
@ -12,9 +11,10 @@ import java.util.List;
|
|||
import static rife.bld.dependencies.Repository.MAVEN_CENTRAL;
|
||||
import static rife.bld.dependencies.Repository.SONATYPE_SNAPSHOTS;
|
||||
import static rife.bld.dependencies.Scope.test;
|
||||
import static rife.bld.extension.propertyfile.Calc.ADD;
|
||||
|
||||
public class PropertyFileExampleBuild extends Project {
|
||||
final Entry buildDateEntry = new Entry("build.date").value("now").pattern("yyyy-MM-dd").type(Types.DATE);
|
||||
final Entry buildDateEntry = new Entry("build.date").set("now").pattern("yyyy-MM-dd").type(Types.DATE);
|
||||
|
||||
public PropertyFileExampleBuild() {
|
||||
pkg = "com.example";
|
||||
|
@ -27,7 +27,6 @@ public class PropertyFileExampleBuild extends Project {
|
|||
scope(test)
|
||||
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 9, 2)))
|
||||
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 9, 2)));
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -39,11 +38,11 @@ public class PropertyFileExampleBuild extends Project {
|
|||
new PropertyFileOperation(this)
|
||||
.file("version.properties")
|
||||
// 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))
|
||||
.entry(new Entry("version.major").defaultValue(0).type(Types.INT).calc(ADD))
|
||||
// set the minor version to 0
|
||||
.entry(new Entry("version.minor").value(0))
|
||||
.entry(new Entry("version.minor").set(0))
|
||||
// set the patch version to 0
|
||||
.entry(new Entry("version.patch").value(0))
|
||||
.entry(new Entry("version.patch").set(0))
|
||||
// set the build date to the current date
|
||||
.entry(buildDateEntry)
|
||||
.execute();
|
||||
|
@ -56,9 +55,9 @@ public class PropertyFileExampleBuild extends Project {
|
|||
// 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))
|
||||
.entry(new Entry("version.minor").defaultValue(-1).type(Types.INT).calc(ADD))
|
||||
// set the patch version to 0
|
||||
.entry(new Entry("version.patch").value(0))
|
||||
.entry(new Entry("version.patch").set(0))
|
||||
// set the build date to the current date
|
||||
.entry(buildDateEntry)
|
||||
.execute();
|
||||
|
@ -73,9 +72,20 @@ public class PropertyFileExampleBuild extends Project {
|
|||
// 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").value(10).defaultValue(0).type(Types.INT).operation(Operations.ADD))
|
||||
.entry(new Entry("version.patch").defaultValue(0).type(Types.INT).calc(v -> v + 10))
|
||||
// set the build date to the current date
|
||||
.entry(buildDateEntry)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@BuildCommand(summary = "Delete version properties")
|
||||
public void deleteVersion() throws Exception {
|
||||
new PropertyFileOperation(this)
|
||||
.file("version.properties")
|
||||
.entry(new Entry("version.major").delete())
|
||||
.entry(new Entry("version.minor").delete())
|
||||
.entry(new Entry("version.patch").delete())
|
||||
.entry(buildDateEntry.delete())
|
||||
.execute();
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -1,2 +1,2 @@
|
|||
#Sat Apr 01 10:09:33 PDT 2023
|
||||
rife2.version=1.5.14
|
||||
rife2.version=1.5.16
|
||||
|
|
|
@ -24,6 +24,8 @@ public class PropertyFileBuild extends Project {
|
|||
.url("https://github.com/rife2/bld-property-file")
|
||||
.developer(new PublishDeveloper().id("ethauvin").name("Erik C. Thauvin").email("erik@thauvin.net")
|
||||
.url("https://erik.thauvin.net/"))
|
||||
.developer(new PublishDeveloper().id("gbevin").name("Geert Bevin").email("geert@uwyn.com")
|
||||
.url("https://github.com/gbevin"))
|
||||
.license(new PublishLicense().name("The Apache License, Version 2.0")
|
||||
.url("http://www.apache.org/licenses/LICENSE-2.0.txt"))
|
||||
.scm(new PublishScm().connection("scm:git:https://github.com/rife2/bld-property-file.git")
|
||||
|
|
50
src/main/java/rife/bld/extension/propertyfile/Calc.java
Normal file
50
src/main/java/rife/bld/extension/propertyfile/Calc.java
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package rife.bld.extension.propertyfile;
|
||||
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
/**
|
||||
* Implements the calculation functions.
|
||||
*
|
||||
* @author <a href="https://github.com/gbevin">Geert Bevin</a>
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class Calc {
|
||||
public static final IntFunction<Integer> ADD = Calc::add;
|
||||
public static final IntFunction<Integer> SUB = Calc::sub;
|
||||
|
||||
/**
|
||||
* Adds {@code 1} to the value.
|
||||
*
|
||||
* @param v the value
|
||||
*/
|
||||
public static Integer add(int v) {
|
||||
return v + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts {@code 1} to the value.
|
||||
*
|
||||
* @param v the value
|
||||
*/
|
||||
public static Integer sub(int v) {
|
||||
return v - 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -16,35 +16,30 @@
|
|||
|
||||
package rife.bld.extension.propertyfile;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
/**
|
||||
* <p>Declares the edits to be made to a {@link java.util.Properties Properties} file.</p>
|
||||
*
|
||||
* <p>The rules used when setting a {@link java.util.Properties property} value are:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>If only value is specified, the property is set to it regardless of its previous value.</li>
|
||||
* <li>If only default value is specified and the property previously existed, it is unchanged.</li>
|
||||
* <li>If only default value is specified and the property did not exist, the property is set to the default value.</li>
|
||||
* <li>If value and default value are both specified and the property previously existed, the property is set to value.</li>
|
||||
* <li>If value and default value are both specified and the property did not exist, the property is set to the default value.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>{@link Operations Operations} occur after the rules are evaluated.</p>
|
||||
* Declares the modifications to be made to a {@link java.util.Properties Properties} file.
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @author <a href="https://github.com/gbevin">Geert Bevin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Entry {
|
||||
private String key;
|
||||
private String value;
|
||||
private String defaultValue;
|
||||
private String newValue;
|
||||
private String modifyValue;
|
||||
private boolean isDelete;
|
||||
private Types type = Types.STRING;
|
||||
private Operations operation = Operations.SET;
|
||||
private String pattern = "";
|
||||
private Units unit = Units.DAY;
|
||||
private IntFunction<Integer> calc;
|
||||
private BiFunction<String, String, String> modify;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Entry entry} Entry.
|
||||
* Creates a new {@link Entry entry}.
|
||||
*
|
||||
* @param key the required property key
|
||||
*/
|
||||
|
@ -53,14 +48,116 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the {@link java.util.Properties property} name/value pair.
|
||||
* Creates a new {@link Entry entry}.
|
||||
*
|
||||
* @param key the required property key
|
||||
* @param type the value {@link Types Type}
|
||||
*/
|
||||
public Entry(String key, Types type) {
|
||||
this.key = key;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to be used in the {@link #modify} function.
|
||||
*/
|
||||
public String getModifyValue() {
|
||||
return modifyValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the modify function.
|
||||
*/
|
||||
public BiFunction<String, String, String> getModify() {
|
||||
return modify;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the modify function.
|
||||
*/
|
||||
public void setModify(BiFunction<String, String, String> modify) {
|
||||
this.modify = modify;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the modify function.
|
||||
*
|
||||
* @param value the value to perform a modification with
|
||||
*/
|
||||
public void setModify(String value, BiFunction<String, String, String> modify) {
|
||||
this.modifyValue = value;
|
||||
this.modify = modify;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Entry entry}.
|
||||
*
|
||||
* @param value the value to perform a modification with
|
||||
* @param modify the modification function
|
||||
*/
|
||||
public Entry modify(String value, BiFunction<String, String, String> modify) {
|
||||
modifyValue = value;
|
||||
setModify(modify);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the {@link Entry} is to be deleted.
|
||||
*/
|
||||
public boolean isDelete() {
|
||||
return isDelete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the {@link Entry} should be deleted.
|
||||
*/
|
||||
public void setDelete(boolean delete) {
|
||||
isDelete = delete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the calculation function.
|
||||
*/
|
||||
public IntFunction<Integer> getCalc() {
|
||||
return calc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the calculation function.
|
||||
*/
|
||||
public void setCalc(IntFunction<Integer> calc) {
|
||||
this.calc = calc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Entry entry}.
|
||||
*
|
||||
* @param calc the calculation function.
|
||||
*/
|
||||
public Entry calc(IntFunction<Integer> calc) {
|
||||
setCalc(calc);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the key of the {@link java.util.Properties property}.
|
||||
*/
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of the {@link java.util.Properties property} name/value pair.
|
||||
* Sets the key of the {@link java.util.Properties property}.
|
||||
*
|
||||
* @param key the {@link java.util.Properties property} key
|
||||
*/
|
||||
|
@ -68,22 +165,6 @@ public class Entry {
|
|||
this.key = key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the {@link java.util.Properties property}.
|
||||
*/
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the {@link java.util.Properties property}.
|
||||
*
|
||||
* @param value the {@link java.util.Properties property} value
|
||||
*/
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default value.
|
||||
*/
|
||||
|
@ -92,7 +173,7 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Sets the initial value to set for the {@link java.util.Properties property} if not already defined.</p>
|
||||
* <p>Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.</p>
|
||||
*
|
||||
* <p>The {@code now} keyword can be used for {@link Types#DATE Types.DATE}</p>
|
||||
*
|
||||
|
@ -103,7 +184,7 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the value {@link Types Type}/
|
||||
* Return the value {@link Types Type}.
|
||||
*/
|
||||
public Types getType() {
|
||||
return type;
|
||||
|
@ -118,22 +199,6 @@ public class Entry {
|
|||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Operations Operation}.
|
||||
*/
|
||||
public Operations getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link Operations Operation} to be performed on the {@link java.util.Properties property} value,
|
||||
*
|
||||
* @param operation the entry {@link Operations Operation}
|
||||
*/
|
||||
public void setOperation(Operations operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the pattern.
|
||||
*/
|
||||
|
@ -142,9 +207,8 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Parses the value of {@link Types#INT Types.INT} and {@link Types#DATE Types.DATE} to
|
||||
* {@link java.text.DecimalFormat DecimalFormat} and {@link java.text.SimpleDateFormat SimpleDateFormat}
|
||||
* respectively.</p>
|
||||
* Sets the {@link java.text.DecimalFormat DecimalFormat} or {@link java.text.SimpleDateFormat SimpleDateFormat}
|
||||
* pattern to be used with {@link Types#INT Types.INT} or {@link Types#DATE Types.DATE} respectively.
|
||||
*
|
||||
* @param pattern the pattern
|
||||
*/
|
||||
|
@ -160,8 +224,8 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link Units unit} value to apply to {@link Operations#ADD Operations.ADD}
|
||||
* and {@link Operations#SUBTRACT Operations.SUBTRACT} for {@link Types#DATE Types.DATE}.
|
||||
* Sets the {@link Units unit} value to apply to {@link Calc#ADD add} and {@link Calc#SUB subtract} calculations
|
||||
* for {@link Types#DATE Types.DATE}.
|
||||
*
|
||||
* @param unit the {@link Units unit}
|
||||
*/
|
||||
|
@ -170,11 +234,10 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the name of the {@link java.util.Properties property} name/value pair.
|
||||
* Sets the key of the {@link java.util.Properties property}.
|
||||
*
|
||||
* @param key the {@link java.util.Properties property} key
|
||||
*/
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public Entry key(String key) {
|
||||
setKey(key);
|
||||
|
@ -182,22 +245,31 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the {@link java.util.Properties property}.
|
||||
* Returns the new value to set the {@link java.util.Properties property)} to.
|
||||
*/
|
||||
public String getNewValue() {
|
||||
return newValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new value to set the {@link java.util.Properties property} to, regardless of its previous value.
|
||||
*
|
||||
* @param value the {@link java.util.Properties property} value
|
||||
* <p>The {@code now} keyword can be used for {@link Types#DATE Types.DATE}</p>
|
||||
*
|
||||
* @param newValue the {@link java.util.Properties property} new value
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public Entry value(Object value) {
|
||||
if (value != null) {
|
||||
setValue(String.valueOf(value));
|
||||
public Entry set(Object newValue) {
|
||||
if (newValue != null) {
|
||||
this.newValue = String.valueOf(newValue);
|
||||
} else {
|
||||
setValue(null);
|
||||
this.newValue = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Sets the initial value to set for the {@link java.util.Properties property} if not already defined.</p>
|
||||
* <p>Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.</p>
|
||||
*
|
||||
* <p>The {@code now} keyword can be used for {@link Types#DATE Types.DATE}</p>
|
||||
*
|
||||
|
@ -224,18 +296,7 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link Operations Operation} to be performed on the {@link java.util.Properties property} value,
|
||||
*
|
||||
* @param operation the entry {@link Operations Operation}
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public Entry operation(Operations operation) {
|
||||
setOperation(operation);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Parses the value of {@link Types#INT Types.INT} and {@link Types#DATE Types.DATE} to
|
||||
* <p>Sets the pattern for {@link Types#INT Types.INT} and {@link Types#DATE Types.DATE} to
|
||||
* {@link java.text.DecimalFormat DecimalFormat} and {@link java.text.SimpleDateFormat SimpleDateFormat}
|
||||
* respectively.</p>
|
||||
*
|
||||
|
@ -247,8 +308,8 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link Units unit} value to apply to {@link Operations#ADD Operations.ADD}
|
||||
* and {@link Operations#SUBTRACT Operations.SUBTRACT} for {@link Types#DATE Types.DATE}.
|
||||
* Sets the {@link Units unit} value to apply to {@link Calc#ADD add} and {@link Calc#SUB subtract} calculations
|
||||
* for {@link Types#DATE Types.DATE}.
|
||||
*
|
||||
* @param unit the {@link Units unit}
|
||||
*/
|
||||
|
@ -258,6 +319,14 @@ public class Entry {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link Entry entry} up for deletion.
|
||||
*/
|
||||
public Entry delete() {
|
||||
isDelete = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The available datatypes.
|
||||
*
|
||||
|
@ -272,23 +341,8 @@ public class Entry {
|
|||
}
|
||||
|
||||
/**
|
||||
* The operations available for all {@link Types Types}.
|
||||
*
|
||||
* <uL>
|
||||
* <li>{@link Operations#ADD ADD} adds a value to an {@link Entry entry}</li>
|
||||
* <li>{@link Operations#DELETE DELETE} deletes an entry</li>
|
||||
* <li>{@link Operations#SET SET} sets the entry value. This is the default operation</li>
|
||||
* <li>{@link Operations#SUBTRACT SUBTRACT} subtracts a value from the {@link Entry entry}.
|
||||
* For {@link Types#INT Types.INT} and {@link Types#DATE Types.DATE} only.</li>
|
||||
* </uL>
|
||||
*/
|
||||
public enum Operations {
|
||||
ADD, DELETE, SET, SUBTRACT
|
||||
}
|
||||
|
||||
/**
|
||||
* The units available for {@link Types#DATE Type.DATE} with {@link Operations#ADD Operations>ADD}
|
||||
* and {@link Operations#SUBTRACT Operations.SUBTRACT}.
|
||||
* The units available for {@link Types#DATE Type.DATE} {@link Calc#ADD add}
|
||||
* and {@link Calc#SUB subtract} calculations.
|
||||
*
|
||||
* <uL>
|
||||
* <li>{@link Units#SECOND SECOND}</li>
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
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.operations.AbstractOperation;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -36,9 +34,9 @@ import java.util.Properties;
|
|||
public class PropertyFileOperation extends AbstractOperation<PropertyFileOperation> {
|
||||
private final List<Entry> entries = new ArrayList<>();
|
||||
private final Project project;
|
||||
private File file = null;
|
||||
private File file;
|
||||
private String comment = "";
|
||||
private boolean failOnWarning = false;
|
||||
private boolean failOnWarning;
|
||||
|
||||
public PropertyFileOperation(Project project) {
|
||||
this.project = project;
|
||||
|
@ -122,17 +120,13 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
|
|||
success = false;
|
||||
} else {
|
||||
var key = entry.getKey();
|
||||
var value = entry.getValue();
|
||||
var value = entry.getNewValue();
|
||||
var defaultValue = entry.getDefaultValue();
|
||||
if ((value == null || value.isBlank()) && (defaultValue == null || defaultValue.isBlank())
|
||||
&& entry.getOperation() != Operations.DELETE) {
|
||||
PropertyFileUtils.warn(commandName, "An entry value or default must be specified: " + key);
|
||||
success = false;
|
||||
} else if (entry.getType() == Types.STRING && entry.getOperation() == Operations.SUBTRACT) {
|
||||
PropertyFileUtils.warn(commandName, "Subtraction is not supported for String properties: " + key);
|
||||
success = false;
|
||||
} else if (entry.getOperation() == Operations.DELETE) {
|
||||
if (entry.isDelete()) {
|
||||
properties.remove(key);
|
||||
} else if ((value == null || value.isBlank()) && (defaultValue == null || defaultValue.isBlank())) {
|
||||
PropertyFileUtils.warn(commandName, "An entry must be set or have a default value: " + key);
|
||||
success = false;
|
||||
} else {
|
||||
switch (entry.getType()) {
|
||||
case DATE ->
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package rife.bld.extension.propertyfile;
|
||||
|
||||
import rife.bld.extension.propertyfile.Entry.Operations;
|
||||
import rife.bld.extension.propertyfile.Entry.Units;
|
||||
import rife.tools.Localization;
|
||||
|
||||
|
@ -45,7 +44,7 @@ import java.util.logging.Logger;
|
|||
public final class PropertyFileUtils {
|
||||
private final static Logger LOGGER = Logger.getLogger(PropertyFileUtils.class.getName());
|
||||
|
||||
private final static Map<Units, Integer> calendarFields =
|
||||
private final static Map<Units, Integer> CALENDAR_FIELDS =
|
||||
Map.of(Units.MILLISECOND, Calendar.MILLISECOND,
|
||||
Units.SECOND, Calendar.SECOND,
|
||||
Units.MINUTE, Calendar.MINUTE,
|
||||
|
@ -70,8 +69,8 @@ public final class PropertyFileUtils {
|
|||
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(),
|
||||
entry.getDefaultValue(), entry.getOperation());
|
||||
String value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(),
|
||||
entry.getNewValue());
|
||||
|
||||
var pattern = entry.getPattern();
|
||||
SimpleDateFormat fmt;
|
||||
|
@ -80,6 +79,7 @@ public final class PropertyFileUtils {
|
|||
} else {
|
||||
fmt = new SimpleDateFormat(entry.getPattern(), Localization.getLocale());
|
||||
}
|
||||
|
||||
if ("now".equalsIgnoreCase(value) || value.isBlank()) {
|
||||
cal.setTime(new Date());
|
||||
} else {
|
||||
|
@ -92,74 +92,36 @@ public final class PropertyFileUtils {
|
|||
}
|
||||
}
|
||||
|
||||
if (entry.getOperation() != Entry.Operations.SET) {
|
||||
var offset = 0;
|
||||
var offset = 0;
|
||||
|
||||
try {
|
||||
offset = Integer.parseInt(entry.getValue());
|
||||
if (entry.getOperation() == Entry.Operations.SUBTRACT) {
|
||||
offset *= -1;
|
||||
}
|
||||
} catch (NumberFormatException nfe) {
|
||||
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);
|
||||
if (entry.getCalc() != null) {
|
||||
offset = entry.getCalc().apply(offset);
|
||||
}
|
||||
|
||||
//noinspection MagicConstant
|
||||
cal.add(CALENDAR_FIELDS.getOrDefault(entry.getUnit(), Calendar.DATE), offset);
|
||||
|
||||
p.setProperty(entry.getKey(), fmt.format(cal.getTime()));
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current value, new value or default value based on the specified {@link Operations operation}.
|
||||
* 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
|
||||
* @param operation the {@link Operations operation}
|
||||
* @return the current value
|
||||
*/
|
||||
public static String currentValue(String value, String newValue, String defaultValue, Operations operation) {
|
||||
String result = null;
|
||||
|
||||
if (operation == Entry.Operations.SET) {
|
||||
if (newValue != null && defaultValue == null) {
|
||||
result = newValue;
|
||||
}
|
||||
if (defaultValue != null) {
|
||||
if (newValue == null && value != null) {
|
||||
result = value;
|
||||
}
|
||||
|
||||
if (newValue == null && value == null) {
|
||||
result = defaultValue;
|
||||
}
|
||||
|
||||
if (newValue != null && value != null) {
|
||||
result = newValue;
|
||||
}
|
||||
|
||||
if (newValue != null && value == null) {
|
||||
result = defaultValue;
|
||||
}
|
||||
}
|
||||
public static String currentValue(String value, String defaultValue, String newValue) {
|
||||
if (newValue != null) {
|
||||
return newValue;
|
||||
} else if (value == null) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
if (value == null) {
|
||||
result = defaultValue;
|
||||
} else {
|
||||
result = value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
if (result == null) {
|
||||
result = "";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -172,28 +134,18 @@ public final class PropertyFileUtils {
|
|||
*/
|
||||
public static boolean processInt(String command, Properties p, Entry entry, boolean failOnWarning) {
|
||||
var success = true;
|
||||
int intValue;
|
||||
int intValue = 0;
|
||||
try {
|
||||
var fmt = new DecimalFormat(entry.getPattern());
|
||||
var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getValue(),
|
||||
entry.getDefaultValue(), entry.getOperation());
|
||||
String value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(),
|
||||
entry.getNewValue());
|
||||
|
||||
if (value.isBlank()) {
|
||||
intValue = fmt.parse("0").intValue();
|
||||
} else {
|
||||
if (value != null) {
|
||||
intValue = fmt.parse(value).intValue();
|
||||
}
|
||||
|
||||
if (entry.getOperation() != Entry.Operations.SET) {
|
||||
var opValue = 1;
|
||||
if (entry.getValue() != null) {
|
||||
opValue = fmt.parse(entry.getValue()).intValue();
|
||||
}
|
||||
if (entry.getOperation() == Entry.Operations.ADD) {
|
||||
intValue += opValue;
|
||||
} else if (entry.getOperation() == Entry.Operations.SUBTRACT) {
|
||||
intValue -= opValue;
|
||||
}
|
||||
if (entry.getCalc() != null) {
|
||||
intValue = entry.getCalc().apply(intValue);
|
||||
}
|
||||
p.setProperty(entry.getKey(), fmt.format(intValue));
|
||||
} catch (NumberFormatException | ParseException e) {
|
||||
|
@ -213,15 +165,13 @@ public final class PropertyFileUtils {
|
|||
* @return {@code true} if successful
|
||||
*/
|
||||
public static boolean processString(Properties p, Entry entry) {
|
||||
var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getValue(),
|
||||
entry.getDefaultValue(), entry.getOperation());
|
||||
var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(),
|
||||
entry.getNewValue());
|
||||
|
||||
if (entry.getOperation() == Entry.Operations.SET) {
|
||||
p.setProperty(entry.getKey(), value);
|
||||
} else if (entry.getOperation() == Entry.Operations.ADD) {
|
||||
if (entry.getValue() != null) {
|
||||
p.setProperty(entry.getValue(), "$value${entry.value}");
|
||||
}
|
||||
p.setProperty(entry.getKey(), value);
|
||||
|
||||
if (entry.getModify() != null && entry.getModifyValue() != null) {
|
||||
p.setProperty(entry.getKey(), entry.getModify().apply(p.getProperty(entry.getKey()), entry.getModifyValue()));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -27,6 +27,8 @@ import java.util.Properties;
|
|||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static rife.bld.extension.propertyfile.Calc.ADD;
|
||||
import static rife.bld.extension.propertyfile.Calc.SUB;
|
||||
|
||||
/**
|
||||
* PropertyFileUtilsTest class
|
||||
|
@ -38,151 +40,131 @@ class PropertyFileUtilsTest {
|
|||
final Properties p = new Properties();
|
||||
final String t = "test";
|
||||
|
||||
@Test
|
||||
void currentValueTest() {
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
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;
|
||||
|
||||
assertThat(PropertyFileUtils.currentValue(null, value, defaultValue, operation))
|
||||
.as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(defaultValue);
|
||||
|
||||
assertThat(PropertyFileUtils.currentValue(prev, value, null, operation))
|
||||
.as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(prev);
|
||||
|
||||
assertThat(PropertyFileUtils.currentValue(null, value, null, operation))
|
||||
.as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo("");
|
||||
|
||||
assertThat(PropertyFileUtils.currentValue(null, value, defaultValue, operation))
|
||||
.as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo(defaultValue);
|
||||
|
||||
assertThat(PropertyFileUtils.currentValue(null, null, null, operation))
|
||||
.as(String.format("currentValue(%s,%s,%s,%s)", prev, value, defaultValue, operation)).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
void processStringTest() {
|
||||
var entry = new Entry("version.major").value("1");
|
||||
var entry = new Entry("version.major").set("1");
|
||||
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
|
||||
assertThat(entry.getValue()).as("processString(entry.getKey(), entry.getValue())").isEqualTo(p.getProperty(entry.getKey()));
|
||||
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo(p.getProperty(entry.getKey()));
|
||||
|
||||
entry.setKey("version.minor");
|
||||
entry.setValue("0");
|
||||
|
||||
PropertyFileUtils.processString(p, entry.set(0));
|
||||
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo(p.getProperty(entry.getKey()));
|
||||
|
||||
// APPEND
|
||||
PropertyFileUtils.processString(p, entry.modify("-foo", String::concat));
|
||||
assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo("0-foo");
|
||||
|
||||
// PREPEND
|
||||
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-0");
|
||||
// CAP
|
||||
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()));
|
||||
|
||||
// REPLACE
|
||||
entry.set(t).setModify("T", (v, s) -> v.replace("t", s));
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(entry.getValue()).as("processString(entry.getKey(), entry.getValue())").isEqualTo(p.getProperty(entry.getKey()));
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace").isEqualTo("TesT");
|
||||
|
||||
// SUBSTRING
|
||||
entry.set(t).setModify((v, s) -> v.substring(1));
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.getKey())).as("substring").isEqualTo(t.substring(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void processIntTest() {
|
||||
var entry = new Entry("version.patch").value("a").type(Entry.Types.INT);
|
||||
assertThat(PropertyFileUtils.processInt(t, p, entry, false)).as("parseInt(entry.getKey(), a)");
|
||||
var entry = new Entry("version.patch").type(Entry.Types.INT);
|
||||
|
||||
// ADD
|
||||
entry.setOperation(Entry.Operations.ADD);
|
||||
|
||||
entry.setValue("1");
|
||||
entry.calc(ADD);
|
||||
entry.setDefaultValue("-1");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0)").isEqualTo("0");
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(-1)").isEqualTo("0");
|
||||
|
||||
entry.setKey("anint");
|
||||
entry.setValue(null);
|
||||
entry.setDefaultValue("0");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 1)").isEqualTo("1");
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(0)").isEqualTo("1");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 2)").isEqualTo("2");
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(1)").isEqualTo("2");
|
||||
|
||||
entry.setKey("formated.int");
|
||||
entry.setValue(null);
|
||||
entry.setDefaultValue("0013");
|
||||
entry.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0014)").isEqualTo("0014");
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(0013)").isEqualTo("0014");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0015)").isEqualTo("0015");
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(0014)").isEqualTo("0015");
|
||||
|
||||
entry.setKey("formated.int");
|
||||
entry.setValue("2");
|
||||
entry.calc(v -> v + 2);
|
||||
entry.setDefaultValue("0013");
|
||||
entry.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0017)").isEqualTo("0017");
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(0013)+2").isEqualTo("0017");
|
||||
|
||||
// SUBTRACT
|
||||
entry.setOperation(Entry.Operations.SUBTRACT);
|
||||
entry.setValue(null);
|
||||
entry.setDefaultValue("0013");
|
||||
entry.calc(SUB);
|
||||
entry.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processInt(entry.getKey(), 0016)").isEqualTo("0016");
|
||||
assertThat(p.getProperty(entry.getKey())).as("sub(0017)").isEqualTo("0016");
|
||||
|
||||
PropertyFileUtils.processInt(t, p, entry.calc(v -> v - 3), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("sub(0017)-3").isEqualTo("0013");
|
||||
}
|
||||
|
||||
@Test
|
||||
void processDateTest() {
|
||||
var entry = new Entry("adate").type(Entry.Types.DATE).pattern("D").value("1");
|
||||
var entry = new Entry("adate", Entry.Types.DATE).pattern("D");
|
||||
var day = new SimpleDateFormat(entry.getPattern(), Localization.getLocale()).format(new Date());
|
||||
var dayInt = Integer.parseInt(day);
|
||||
|
||||
entry.setValue("a");
|
||||
assertThat(PropertyFileUtils.processDate(t, p, entry, false)).as("processDate(entry.getKey(), a)").isFalse();
|
||||
assertThat(PropertyFileUtils.processDate(t, p, entry.set("a"), false)).as("processDate(a)").isFalse();
|
||||
|
||||
entry.setValue("99");
|
||||
PropertyFileUtils.processDate(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), 99)").isEqualTo("99");
|
||||
PropertyFileUtils.processDate(t, p, entry.set("99"), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(99)").isEqualTo("99");
|
||||
|
||||
entry.setValue("now");
|
||||
PropertyFileUtils.processDate(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now)").isEqualTo(day);
|
||||
PropertyFileUtils.processDate(t, p, entry.set("noew"), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now)").isEqualTo(day);
|
||||
|
||||
// ADD
|
||||
entry.setOperation(Entry.Operations.ADD);
|
||||
entry.setCalc(ADD);
|
||||
PropertyFileUtils.processDate(t, p, entry.set(dayInt), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)").isEqualTo(String.valueOf(dayInt + 1));
|
||||
|
||||
entry.setValue("1");
|
||||
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(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now+3)").isEqualTo(String.valueOf(dayInt + 3));
|
||||
entry.setCalc(v -> v + 3);
|
||||
PropertyFileUtils.processDate(t, p, entry.set(dayInt), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now+3)").isEqualTo(String.valueOf(dayInt + 3));
|
||||
|
||||
// SUBTRACT
|
||||
entry.setOperation(Entry.Operations.SUBTRACT);
|
||||
entry.setValue("3");
|
||||
PropertyFileUtils.processDate(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now-3)").isEqualTo(String.valueOf(dayInt));
|
||||
entry.setCalc(SUB);
|
||||
PropertyFileUtils.processDate(t, p, entry.set(dayInt), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now-3)").isEqualTo(String.valueOf(dayInt - 1));
|
||||
|
||||
entry.setOperation(Entry.Operations.SUBTRACT);
|
||||
entry.setValue("2");
|
||||
PropertyFileUtils.processDate(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(entry.getKey(), now-2)").isEqualTo(String.valueOf(dayInt - 2));
|
||||
entry.setCalc(v -> v - 2);
|
||||
PropertyFileUtils.processDate(t, p, entry.set(dayInt), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now-2)").isEqualTo(String.valueOf(dayInt - 2));
|
||||
}
|
||||
|
||||
@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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue