Added pattern option for String-based entries
This commit is contained in:
parent
23540cbc8e
commit
fb75d12740
3 changed files with 181 additions and 187 deletions
13
README.md
13
README.md
|
@ -69,12 +69,13 @@ The [PropertyFileOperation](https://rife2.github.io/bld-property-file/rife/bld/e
|
|||
|
||||
The [Entry](https://rife2.github.io/bld-property-file/rife/bld/extension/propertyfile/Entry.html) class is used to specify modifications to a [String property](https://docs.oracle.com/javase/tutorial/essential/environment/properties.html).
|
||||
|
||||
| Function | Description/Example |
|
||||
|:-----------------|:--------------------------------------------------------------------------------------------------------|
|
||||
| `defaultValue()` | The value to be used if the property doesn't exist. |
|
||||
| `delete()` | Delete the property. |
|
||||
| `modify()` | `modify("-foo", String::concat)`<br/>`modify("-foo", (v, s) -> v + s)`<br/>`modify((v, s) -> v.trim())` | Modify an entry value. |
|
||||
| `set()` | The value to set the property to, regardless of its previous value. |
|
||||
| Function | Description/Example |
|
||||
|:-----------------|:--------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `defaultValue()` | The value to be used if the property doesn't exist. |
|
||||
| `delete()` | Delete the property. |
|
||||
| `modify()` | `modify("-foo", String::concat)`<br/>`modify("-foo", (v, s) -> v + s)`<br/>`modify((v, s) -> v.trim())` |
|
||||
| `pattern()` | If present, will parse the value as a [Formatter](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Formatter.html) pattern. |
|
||||
| `set()` | The value to set the property to, regardless of its previous value. |
|
||||
|
||||
## EntryDate
|
||||
|
||||
|
|
|
@ -91,144 +91,132 @@ public final class PropertyFileUtils {
|
|||
return success;
|
||||
}
|
||||
|
||||
private static String objectToString(Object o) {
|
||||
if (o == null) {
|
||||
return "";
|
||||
} else {
|
||||
return String.valueOf(o);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a date {@link Properties property}.
|
||||
*
|
||||
* @param command the issuing command
|
||||
* @param p the {@link Properties property}
|
||||
* @param entry the {@link Entry} containing the {@link Properties property} edits
|
||||
* @param failOnWarning the fail on warning
|
||||
* @return the boolean
|
||||
* @throws Exception the exception
|
||||
* @param p the {@link Properties property}
|
||||
* @param entry the {@link Entry} containing the {@link Properties property} edits
|
||||
* @throws DateTimeException if a parsing error occurs
|
||||
*/
|
||||
@SuppressWarnings({"PMD.SignatureDeclareThrowsException", "PMD.ExceptionAsFlowControl"})
|
||||
public static boolean processDate(String command, Properties p, EntryDate entry, boolean failOnWarning)
|
||||
throws Exception {
|
||||
var success = true;
|
||||
var value = currentValue(null, entry.getDefaultValue(), entry.getNewValue());
|
||||
var pattern = entry.getPattern();
|
||||
@SuppressWarnings("PMD.ExceptionAsFlowControl")
|
||||
public static void processDate(Properties p, EntryDate entry) throws IllegalArgumentException {
|
||||
var currentValue = currentValue(null, entry.defaultValue(), entry.newValue());
|
||||
var pattern = objectToString(entry.pattern());
|
||||
|
||||
var parsedValue = String.valueOf(value);
|
||||
var dateValue = String.valueOf(currentValue);
|
||||
if (pattern != null && !pattern.isBlank()) {
|
||||
var offset = 0;
|
||||
|
||||
if (entry.getCalc() != null) {
|
||||
offset = entry.getCalc().apply(offset);
|
||||
if (entry.calc() != null) {
|
||||
offset = entry.calc().apply(offset);
|
||||
}
|
||||
|
||||
var dtf = DateTimeFormatter.ofPattern(pattern);
|
||||
var unit = entry.getUnit();
|
||||
var unit = entry.unit();
|
||||
|
||||
try {
|
||||
if (value instanceof String) {
|
||||
if ("now".equalsIgnoreCase((String) value)) {
|
||||
value = ZonedDateTime.now();
|
||||
if (currentValue instanceof String) {
|
||||
if ("now".equalsIgnoreCase((String) currentValue)) {
|
||||
currentValue = ZonedDateTime.now();
|
||||
} else {
|
||||
throw new DateTimeException("Excepted: Calendar, Date or java.time.");
|
||||
}
|
||||
} else if (value instanceof LocalDateTime) {
|
||||
value = ((LocalDateTime) value).atZone(ZoneId.systemDefault());
|
||||
} else if (value instanceof Date) {
|
||||
value = ((Date) value).toInstant().atZone(ZoneId.systemDefault());
|
||||
} else if (value instanceof Calendar) {
|
||||
value = ((Calendar) value).toInstant().atZone(ZoneId.systemDefault());
|
||||
} else if (value instanceof Instant) {
|
||||
value = ((Instant) value).atZone(ZoneId.systemDefault());
|
||||
} else if (currentValue instanceof LocalDateTime) {
|
||||
currentValue = ((LocalDateTime) currentValue).atZone(ZoneId.systemDefault());
|
||||
} else if (currentValue instanceof Date) {
|
||||
currentValue = ((Date) currentValue).toInstant().atZone(ZoneId.systemDefault());
|
||||
} else if (currentValue instanceof Calendar) {
|
||||
currentValue = ((Calendar) currentValue).toInstant().atZone(ZoneId.systemDefault());
|
||||
} else if (currentValue instanceof Instant) {
|
||||
currentValue = ((Instant) currentValue).atZone(ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
if (value instanceof LocalDate) {
|
||||
if (currentValue instanceof LocalDate) {
|
||||
if (offset != 0) {
|
||||
if (unit == EntryDate.Units.DAY) {
|
||||
value = ((LocalDate) value).plusDays(offset);
|
||||
currentValue = ((LocalDate) currentValue).plusDays(offset);
|
||||
} else if (unit == EntryDate.Units.MONTH) {
|
||||
value = ((LocalDate) value).plusMonths(offset);
|
||||
currentValue = ((LocalDate) currentValue).plusMonths(offset);
|
||||
} else if (unit == EntryDate.Units.WEEK) {
|
||||
value = ((LocalDate) value).plusWeeks(offset);
|
||||
currentValue = ((LocalDate) currentValue).plusWeeks(offset);
|
||||
} else if (unit == EntryDate.Units.YEAR) {
|
||||
value = ((LocalDate) value).plusYears(offset);
|
||||
currentValue = ((LocalDate) currentValue).plusYears(offset);
|
||||
}
|
||||
}
|
||||
parsedValue = dtf.format((LocalDate) value);
|
||||
} else if (value instanceof LocalTime) {
|
||||
dateValue = dtf.format((LocalDate) currentValue);
|
||||
} else if (currentValue instanceof LocalTime) {
|
||||
if (offset != 0) {
|
||||
if (unit == EntryDate.Units.SECOND) {
|
||||
value = ((LocalTime) value).plusSeconds(offset);
|
||||
currentValue = ((LocalTime) currentValue).plusSeconds(offset);
|
||||
} else if (unit == EntryDate.Units.MINUTE) {
|
||||
value = ((LocalTime) value).plusMinutes(offset);
|
||||
currentValue = ((LocalTime) currentValue).plusMinutes(offset);
|
||||
} else if (unit == EntryDate.Units.HOUR) {
|
||||
value = ((LocalTime) value).plusHours(offset);
|
||||
currentValue = ((LocalTime) currentValue).plusHours(offset);
|
||||
}
|
||||
}
|
||||
parsedValue = dtf.format((LocalTime) value);
|
||||
} else if (value instanceof ZonedDateTime) {
|
||||
dateValue = dtf.format((LocalTime) currentValue);
|
||||
} else if (currentValue instanceof ZonedDateTime) {
|
||||
if (offset != 0) {
|
||||
if (unit == EntryDate.Units.DAY) {
|
||||
value = ((ZonedDateTime) value).plusDays(offset);
|
||||
currentValue = ((ZonedDateTime) currentValue).plusDays(offset);
|
||||
} else if (unit == EntryDate.Units.MONTH) {
|
||||
value = ((ZonedDateTime) value).plusMonths(offset);
|
||||
currentValue = ((ZonedDateTime) currentValue).plusMonths(offset);
|
||||
} else if (unit == EntryDate.Units.WEEK) {
|
||||
value = ((ZonedDateTime) value).plusWeeks(offset);
|
||||
currentValue = ((ZonedDateTime) currentValue).plusWeeks(offset);
|
||||
} else if (unit == EntryDate.Units.YEAR) {
|
||||
value = ((ZonedDateTime) value).plusYears(offset);
|
||||
currentValue = ((ZonedDateTime) currentValue).plusYears(offset);
|
||||
} else if (unit == EntryDate.Units.SECOND) {
|
||||
value = ((ZonedDateTime) value).plusSeconds(offset);
|
||||
currentValue = ((ZonedDateTime) currentValue).plusSeconds(offset);
|
||||
} else if (unit == EntryDate.Units.MINUTE) {
|
||||
value = ((ZonedDateTime) value).plusMinutes(offset);
|
||||
currentValue = ((ZonedDateTime) currentValue).plusMinutes(offset);
|
||||
} else if (unit == EntryDate.Units.HOUR) {
|
||||
value = ((ZonedDateTime) value).plusHours(offset);
|
||||
currentValue = ((ZonedDateTime) currentValue).plusHours(offset);
|
||||
}
|
||||
}
|
||||
parsedValue = dtf.format((ZonedDateTime) value);
|
||||
dateValue = dtf.format((ZonedDateTime) currentValue);
|
||||
}
|
||||
} catch (DateTimeException dte) {
|
||||
warn(command, "Non-date value for \"" + entry.getKey() + "\" --> " + dte.getMessage(),
|
||||
dte, failOnWarning);
|
||||
success = false;
|
||||
throw new IllegalArgumentException(
|
||||
"Non-date value for \"" + entry.key() + "\" --> " + dte.getMessage(), dte);
|
||||
}
|
||||
}
|
||||
|
||||
if (success) {
|
||||
p.setProperty(entry.getKey(), parsedValue);
|
||||
}
|
||||
|
||||
return success;
|
||||
p.setProperty(entry.key(), dateValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes an integer {@link Properties property}.
|
||||
*
|
||||
* @param command the issuing command
|
||||
* @param p the {@link Properties property}
|
||||
* @param entry the {@link Entry} containing the {@link Properties property} edits
|
||||
* @param failOnWarning the fail on warning
|
||||
* @return the boolean
|
||||
* @throws Exception the exception
|
||||
* @param p the {@link Properties property}
|
||||
* @param entry the {@link Entry} containing the {@link Properties property} edits
|
||||
* @throws NumberFormatException if a parsing error occurs
|
||||
*/
|
||||
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
|
||||
public static boolean processInt(String command, Properties p, EntryInt entry, boolean failOnWarning)
|
||||
throws Exception {
|
||||
var success = true;
|
||||
public static void processInt(Properties p, EntryInt entry) throws IllegalArgumentException {
|
||||
int intValue = 0;
|
||||
try {
|
||||
var fmt = new DecimalFormat(entry.getPattern());
|
||||
var value = currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(),
|
||||
entry.getNewValue());
|
||||
var fmt = new DecimalFormat(objectToString(entry.pattern()));
|
||||
var currentValue = currentValue(p.getProperty(entry.key()), entry.defaultValue(), entry.newValue());
|
||||
|
||||
if (value != null) {
|
||||
intValue = fmt.parse(String.valueOf(value)).intValue();
|
||||
if (currentValue != null) {
|
||||
intValue = fmt.parse(String.valueOf(currentValue)).intValue();
|
||||
}
|
||||
|
||||
if (entry.getCalc() != null) {
|
||||
intValue = entry.getCalc().apply(intValue);
|
||||
if (entry.calc() != null) {
|
||||
intValue = entry.calc().apply(intValue);
|
||||
}
|
||||
p.setProperty(entry.getKey(), fmt.format(intValue));
|
||||
|
||||
p.setProperty(entry.key(), fmt.format(intValue));
|
||||
} catch (NumberFormatException | ParseException e) {
|
||||
warn(command, "Non-integer value for \"" + entry.getKey() + "\" --> " + e.getMessage(), e,
|
||||
failOnWarning);
|
||||
success = false;
|
||||
throw new IllegalArgumentException(
|
||||
"Non-integer value for \"" + entry.key() + "\" --> " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -236,19 +224,16 @@ public final class PropertyFileUtils {
|
|||
*
|
||||
* @param p the {@link Properties property}
|
||||
* @param entry the {@link Entry} containing the {@link Properties property} edits
|
||||
* @return the boolean
|
||||
*/
|
||||
@SuppressWarnings("SameReturnValue")
|
||||
public static boolean processString(Properties p, Entry entry) {
|
||||
var value = currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(), entry.getNewValue());
|
||||
public static void processString(Properties p, Entry entry) {
|
||||
var currentValue = currentValue(p.getProperty(entry.key()), entry.defaultValue(), entry.newValue());
|
||||
|
||||
p.setProperty(entry.getKey(), String.valueOf(value));
|
||||
p.setProperty(entry.key(), String.format(String.valueOf(currentValue), entry.pattern()));
|
||||
|
||||
if (entry.getModify() != null && entry.getModifyValue() != null) {
|
||||
p.setProperty(entry.getKey(), entry.getModify().apply(p.getProperty(entry.getKey()), entry.getModifyValue()));
|
||||
if (entry.modify() != null && entry.modifyValue() != null) {
|
||||
var modify = entry.modify().apply(p.getProperty(entry.key()), entry.modifyValue());
|
||||
p.setProperty(entry.key(), String.format(modify, entry.pattern()));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,19 +17,19 @@
|
|||
package rife.bld.extension.propertyfile;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import rife.bld.operations.exceptions.ExitStatusException;
|
||||
import rife.tools.Localization;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.*;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; // NOPMD
|
||||
import static org.assertj.core.api.Assertions.assertThatCode; // NOPMD
|
||||
import static rife.bld.extension.propertyfile.Calc.ADD; // NOPMD
|
||||
import static rife.bld.extension.propertyfile.Calc.SUB; // NOPMD
|
||||
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
|
||||
|
@ -37,6 +37,7 @@ import static rife.bld.extension.propertyfile.Calc.SUB; // NOPMD
|
|||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
|
||||
class PropertyFileUtilsTest {
|
||||
final static int dayOfYear = LocalDate.now().getDayOfYear();
|
||||
final static Properties p = new Properties();
|
||||
|
@ -49,7 +50,7 @@ class PropertyFileUtilsTest {
|
|||
|
||||
public EntryDate newEntryDate() {
|
||||
p.clear();
|
||||
return new EntryDate("adate").pattern("D");
|
||||
return new EntryDate("aDate").pattern("D");
|
||||
}
|
||||
|
||||
public EntryInt newEntryInt() {
|
||||
|
@ -58,40 +59,36 @@ class PropertyFileUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
void parseDateSub() throws Exception {
|
||||
var entryDate = newEntryDate();
|
||||
entryDate.setCalc(SUB);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.now(), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(now-3)").isEqualTo(String.valueOf(dayOfYear - 1));
|
||||
void parseDateSub() {
|
||||
var entryDate = newEntryDate().calc(SUB);
|
||||
PropertyFileUtils.processDate(p, entryDate.now());
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(now-3)").isEqualTo(String.valueOf(dayOfYear - 1));
|
||||
|
||||
entryDate.setCalc(v -> v - 2);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.now(), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(now-2)").isEqualTo(String.valueOf(dayOfYear - 2));
|
||||
entryDate.calc(v -> v - 2);
|
||||
PropertyFileUtils.processDate(p, entryDate.now());
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(now-2)").isEqualTo(String.valueOf(dayOfYear - 2));
|
||||
|
||||
entryDate.setCalc(SUB);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(new Date()), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(date-1)").isEqualTo(String.valueOf(dayOfYear - 1));
|
||||
entryDate.calc(SUB);
|
||||
PropertyFileUtils.processDate(p, entryDate.set(new Date()));
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(date-1)").isEqualTo(String.valueOf(dayOfYear - 1));
|
||||
|
||||
entryDate.setCalc(v -> v - 2);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(Calendar.getInstance()), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(cal-2)").isEqualTo(String.valueOf(dayOfYear - 2));
|
||||
entryDate.calc(v -> v - 2);
|
||||
PropertyFileUtils.processDate(p, entryDate.set(Calendar.getInstance()));
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(cal-2)").isEqualTo(String.valueOf(dayOfYear - 2));
|
||||
|
||||
entryDate.setCalc(v -> v - 3);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(LocalDate.now()),
|
||||
true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(LocalDate-3)").isEqualTo(String.valueOf(dayOfYear - 3));
|
||||
entryDate.calc(v -> v - 3);
|
||||
PropertyFileUtils.processDate(p, entryDate.set(LocalDate.now()));
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(LocalDate-3)").isEqualTo(String.valueOf(dayOfYear - 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseIntSubTest() throws Exception {
|
||||
var entryInt = newEntryInt();
|
||||
entryInt.calc(SUB);
|
||||
entryInt.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entryInt.defaultValue("0017"), true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("sub(0017)").isEqualTo("0016");
|
||||
void parseIntSubTest() {
|
||||
var entryInt = newEntryInt().calc(SUB).pattern("0000");
|
||||
PropertyFileUtils.processInt(p, entryInt.defaultValue("0017"));
|
||||
assertThat(p.getProperty(entryInt.key())).as("sub(0017)").isEqualTo("0016");
|
||||
|
||||
PropertyFileUtils.processInt(t, p, entryInt.set(16).calc(v -> v - 3), true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("sub(16)-3").isEqualTo("0013");
|
||||
PropertyFileUtils.processInt(p, entryInt.set(16).calc(v -> v - 3));
|
||||
assertThat(p.getProperty(entryInt.key())).as("sub(16)-3").isEqualTo("0013");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -100,116 +97,118 @@ class PropertyFileUtilsTest {
|
|||
var entry = newEntry();
|
||||
PropertyFileUtils.processString(p, entry.set(1));
|
||||
PropertyFileUtils.processString(p, entry.modify("-foo", String::concat));
|
||||
assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(),
|
||||
entry.getNewValue())).isEqualTo("1-foo");
|
||||
assertThat(p.getProperty(entry.key())).as(String.format("processString(%s, %s)", entry.key(),
|
||||
entry.newValue())).isEqualTo("1-foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringCap() {
|
||||
var entry = newEntry();
|
||||
PropertyFileUtils.processString(p, entry.set(t).modify("", (v, s) -> v.toUpperCase(Localization.getLocale())));
|
||||
assertThat(p.getProperty(entry.getKey())).as("capitalize").isEqualTo(t.toUpperCase(Localization.getLocale()));
|
||||
assertThat(p.getProperty(entry.key())).as("capitalize").isEqualTo(t.toUpperCase(Localization.getLocale()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringCat() {
|
||||
var entry = newEntry();
|
||||
entry.set(t).setModify("-foo", String::concat);
|
||||
entry.set(t).modify("-foo", String::concat);
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace").isEqualTo(t + "-foo");
|
||||
assertThat(p.getProperty(entry.key())).as("replace").isEqualTo(t + "-foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringFormat() {
|
||||
var entry = new Entry("foo").set("%.2f").pattern(3.14159f);
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.key())).as("format").isEqualTo("3.14");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void parseStringPrepend() {
|
||||
var entry = newEntry();
|
||||
PropertyFileUtils.processString(p, entry.set(1));
|
||||
PropertyFileUtils.processString(p, entry.modify("foo-", (v, s) -> s + v));
|
||||
assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
assertThat(p.getProperty(entry.key())).as(String.format("processString(%s, %s)", entry.key(), entry.newValue()))
|
||||
.isEqualTo("foo-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringReplace() {
|
||||
var entry = newEntry();
|
||||
entry.set(t).setModify("T", (v, s) -> v.replace("t", s));
|
||||
entry.set(t).modify("T", (v, s) -> v.replace("t", s));
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace(t -> T)").isEqualTo("TesT");
|
||||
|
||||
assertThat(p.getProperty(entry.key())).as("replace(t -> T)").isEqualTo("TesT");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringSub() {
|
||||
var entry = newEntry();
|
||||
PropertyFileUtils.processString(p, entry.set(t).modify((v, s) -> v.substring(1)));
|
||||
assertThat(p.getProperty(entry.getKey())).as("substring(1)").isEqualTo(t.substring(1));
|
||||
assertThat(p.getProperty(entry.key())).as("substring(1)").isEqualTo(t.substring(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseTimeTest() throws Exception {
|
||||
void parseTimeTest() {
|
||||
var entry = new EntryDate("time").pattern("m");
|
||||
var time = LocalTime.now();
|
||||
|
||||
entry.setCalc(ADD);
|
||||
PropertyFileUtils.processDate(t, p, entry.set(time).unit(EntryDate.Units.MINUTE), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)")
|
||||
entry.calc(ADD);
|
||||
PropertyFileUtils.processDate(p, entry.set(time).unit(EntryDate.Units.MINUTE));
|
||||
assertThat(p.getProperty(entry.key())).as("processDate(now+1)")
|
||||
.isEqualTo(String.valueOf(time.plusMinutes(1).getMinute()));
|
||||
|
||||
entry.setCalc(SUB);
|
||||
PropertyFileUtils.processDate(t, p, entry.set(time).unit(EntryDate.Units.HOUR).pattern("H"), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)")
|
||||
entry.calc(SUB);
|
||||
PropertyFileUtils.processDate(p, entry.set(time).unit(EntryDate.Units.HOUR).pattern("H"));
|
||||
assertThat(p.getProperty(entry.key())).as("processDate(now+1)")
|
||||
.isEqualTo(String.valueOf(time.minusHours(1).getHour()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void processDateAddTest() throws Exception {
|
||||
void processDateAddTest() {
|
||||
var entryDate = newEntryDate();
|
||||
entryDate.setCalc(ADD);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.now(), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(now+1)").isEqualTo(String.valueOf(dayOfYear + 1));
|
||||
entryDate.calc(ADD);
|
||||
PropertyFileUtils.processDate(p, entryDate.now());
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(now+1)").isEqualTo(String.valueOf(dayOfYear + 1));
|
||||
|
||||
PropertyFileUtils.processDate(t, p, entryDate.now().calc(v -> v + 3), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(now+3)").isEqualTo(String.valueOf(dayOfYear + 3));
|
||||
PropertyFileUtils.processDate(p, entryDate.now().calc(v -> v + 3));
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(now+3)").isEqualTo(String.valueOf(dayOfYear + 3));
|
||||
|
||||
entryDate.setCalc(ADD);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(ZonedDateTime.now()), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(ZonedDateTime+1)")
|
||||
entryDate.calc(ADD);
|
||||
PropertyFileUtils.processDate(p, entryDate.set(ZonedDateTime.now()));
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(ZonedDateTime+1)")
|
||||
.isEqualTo(String.valueOf(dayOfYear + 1));
|
||||
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(Instant.now()).calc(v -> v + 2), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(Instant+2)").isEqualTo(String.valueOf(dayOfYear + 2));
|
||||
PropertyFileUtils.processDate(p, entryDate.set(Instant.now()).calc(v -> v + 2));
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(Instant+2)").isEqualTo(String.valueOf(dayOfYear + 2));
|
||||
|
||||
entryDate.setCalc(v -> v + 3);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(LocalDateTime.now()), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(LocalDteTime+2)").isEqualTo(String.valueOf(dayOfYear + 3));
|
||||
entryDate.calc(v -> v + 3);
|
||||
PropertyFileUtils.processDate(p, entryDate.set(LocalDateTime.now()));
|
||||
assertThat(p.getProperty(entryDate.key())).as("processDate(LocalDteTime+2)").isEqualTo(String.valueOf(dayOfYear + 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void processIntAddTest() throws Exception {
|
||||
var entryInt = newEntryInt();
|
||||
entryInt.calc(ADD);
|
||||
entryInt.setDefaultValue("-1");
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("add(-1)").isEqualTo("0");
|
||||
void processIntAddTest() {
|
||||
var entryInt = newEntryInt().calc(ADD).defaultValue("-1");
|
||||
PropertyFileUtils.processInt(p, entryInt);
|
||||
assertThat(p.getProperty(entryInt.key())).as("add(-1)").isEqualTo("0");
|
||||
|
||||
entryInt.setKey("anint");
|
||||
entryInt.setDefaultValue("0");
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("add(0)").isEqualTo("1");
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("add(1)").isEqualTo("2");
|
||||
entryInt.key("anInt").defaultValue("0");
|
||||
PropertyFileUtils.processInt(p, entryInt);
|
||||
assertThat(p.getProperty(entryInt.key())).as("add(0)").isEqualTo("1");
|
||||
PropertyFileUtils.processInt(p, entryInt);
|
||||
assertThat(p.getProperty(entryInt.key())).as("add(1)").isEqualTo("2");
|
||||
|
||||
entryInt.setKey("formatted.int");
|
||||
entryInt.setDefaultValue("0013");
|
||||
entryInt.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("add(0013)").isEqualTo("0014");
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("add(0014)").isEqualTo("0015");
|
||||
entryInt.key("formatted.int").defaultValue("0013").pattern("0000");
|
||||
PropertyFileUtils.processInt(p, entryInt);
|
||||
assertThat(p.getProperty(entryInt.key())).as("add(0013)").isEqualTo("0014");
|
||||
PropertyFileUtils.processInt(p, entryInt);
|
||||
assertThat(p.getProperty(entryInt.key())).as("add(0014)").isEqualTo("0015");
|
||||
|
||||
entryInt.calc(v -> v + 2);
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("add(0015)+2").isEqualTo("0017");
|
||||
PropertyFileUtils.processInt(p, entryInt);
|
||||
assertThat(p.getProperty(entryInt.key())).as("add(0015)+2").isEqualTo("0017");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -217,14 +216,14 @@ class PropertyFileUtilsTest {
|
|||
var entry = newEntry();
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
|
||||
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo(p.getProperty(entry.getKey()));
|
||||
assertThat(entry.newValue()).as(String.format("processString(%s, %s)", entry.key(), entry.newValue()))
|
||||
.isEqualTo(p.getProperty(entry.key()));
|
||||
|
||||
entry.setKey("version.minor");
|
||||
entry.key("version.minor");
|
||||
|
||||
PropertyFileUtils.processString(p, entry.set(0));
|
||||
assertThat(entry.getNewValue().toString()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo(p.getProperty(entry.getKey()));
|
||||
assertThat(entry.newValue().toString()).as(String.format("processString(%s, %s)", entry.key(), entry.newValue()))
|
||||
.isEqualTo(p.getProperty(entry.key()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -239,13 +238,22 @@ class PropertyFileUtilsTest {
|
|||
assertThatCode(() -> PropertyFileUtils.saveProperties(tmp, "Generated file - do not modify!", p))
|
||||
.as("save properties").doesNotThrowAnyException();
|
||||
|
||||
assertThat(PropertyFileUtils.loadProperties(t, tmp, p)).as("load properties").isTrue();
|
||||
assertThat(PropertyFileUtils.loadProperties(t, tmp, p, false)).as("load properties").isTrue();
|
||||
|
||||
assertThat(p.getProperty(test)).as("read property").isEqualTo(test);
|
||||
|
||||
tmp.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangeKey() {
|
||||
var entry = new Entry("foo").key("bar");
|
||||
assertThat(entry.key()).isEqualTo("bar");
|
||||
|
||||
entry.key("foo");
|
||||
assertThat(entry.key()).isEqualTo("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCurrentValue() {
|
||||
var value = "value";
|
||||
|
@ -261,9 +269,9 @@ class PropertyFileUtilsTest {
|
|||
|
||||
@Test
|
||||
void testWarn() {
|
||||
assertThatCode(() -> PropertyFileUtils.warn("command", "message", new IOException(t), true))
|
||||
.hasMessage(t).isInstanceOf(IOException.class);
|
||||
assertThatCode(() -> PropertyFileUtils.warn("command", t, new Exception(t), false))
|
||||
assertThatCode(() -> PropertyFileUtils.warn("command", "message", true, false))
|
||||
.isInstanceOf(ExitStatusException.class);
|
||||
assertThatCode(() -> PropertyFileUtils.warn("command", t, false, false))
|
||||
.as("failOnWarning = false").doesNotThrowAnyException();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue