Added pattern option for String-based entries

This commit is contained in:
Erik C. Thauvin 2024-07-16 16:09:21 -07:00
parent 23540cbc8e
commit fb75d12740
Signed by: erik
GPG key ID: 776702A6A2DA330E
3 changed files with 181 additions and 187 deletions

View file

@ -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();
}
}