Added EntryBase, EntryDate and EntryInt
This commit is contained in:
parent
99c3774227
commit
473a70329e
13 changed files with 794 additions and 497 deletions
|
@ -21,7 +21,8 @@ import rife.tools.Localization;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.*;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -38,12 +39,14 @@ import static rife.bld.extension.propertyfile.Calc.SUB;
|
|||
*/
|
||||
class PropertyFileUtilsTest {
|
||||
final Properties p = new Properties();
|
||||
final Entry entry = new Entry("version.major").set("1");
|
||||
final EntryDate entryDate = new EntryDate("adate").pattern("D");
|
||||
final EntryInt entryInt = new EntryInt("version.patch");
|
||||
final int dayOfYear = LocalDate.now().getDayOfYear();
|
||||
final String t = "test";
|
||||
|
||||
@Test
|
||||
void processStringTest() {
|
||||
var entry = new Entry("version.major").set("1");
|
||||
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
|
||||
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
|
@ -52,106 +55,158 @@ class PropertyFileUtilsTest {
|
|||
entry.setKey("version.minor");
|
||||
|
||||
PropertyFileUtils.processString(p, entry.set(0));
|
||||
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
assertThat(entry.getNewValue().toString()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo(p.getProperty(entry.getKey()));
|
||||
}
|
||||
|
||||
// APPEND
|
||||
@Test
|
||||
void testWarn() {
|
||||
assertThatCode(() -> PropertyFileUtils.warn("command", "message", new IOException(t), true))
|
||||
.hasMessage(t).isInstanceOf(IOException.class);
|
||||
assertThatCode(() -> PropertyFileUtils.warn("command", t, new Exception(t), false))
|
||||
.as("failOnWarning = false").doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringAppend() {
|
||||
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");
|
||||
assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(),
|
||||
entry.getNewValue())).isEqualTo("1-foo");
|
||||
}
|
||||
|
||||
// PREPEND
|
||||
@Test
|
||||
void parseStringPrepend() {
|
||||
PropertyFileUtils.processString(p, entry.modify("foo-", (v, s) -> s + v));
|
||||
assertThat(p.getProperty(entry.getKey())).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo("foo-0");
|
||||
// CAP
|
||||
PropertyFileUtils.processString(p, entry.set(t).modify((v, s) -> v.toUpperCase(Localization.getLocale())));
|
||||
.isEqualTo("foo-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringCap() {
|
||||
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
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringReplace() {
|
||||
entry.set(t).setModify("T", (v, s) -> v.replace("t", s));
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace").isEqualTo("TesT");
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace(t -> T)").isEqualTo("TesT");
|
||||
|
||||
// SUBSTRING
|
||||
entry.set(t).setModify((v, s) -> v.substring(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringCat() {
|
||||
entry.set(t).setModify("-foo", String::concat);
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.getKey())).as("substring").isEqualTo(t.substring(1));
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace").isEqualTo(t + "-foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
void processIntTest() {
|
||||
var entry = new Entry("version.patch").type(Entry.Types.INT);
|
||||
|
||||
// ADD
|
||||
entry.calc(ADD);
|
||||
entry.setDefaultValue("-1");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(-1)").isEqualTo("0");
|
||||
|
||||
entry.setKey("anint");
|
||||
entry.setDefaultValue("0");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(0)").isEqualTo("1");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(1)").isEqualTo("2");
|
||||
|
||||
entry.setKey("formated.int");
|
||||
entry.setDefaultValue("0013");
|
||||
entry.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(0013)").isEqualTo("0014");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(0014)").isEqualTo("0015");
|
||||
|
||||
entry.setKey("formated.int");
|
||||
entry.calc(v -> v + 2);
|
||||
entry.setDefaultValue("0013");
|
||||
entry.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("add(0013)+2").isEqualTo("0017");
|
||||
|
||||
// SUBTRACT
|
||||
entry.calc(SUB);
|
||||
entry.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entry, true);
|
||||
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");
|
||||
void parseStringSub() {
|
||||
PropertyFileUtils.processString(p, entry.set(t).modify((v, s) -> v.substring(1)));
|
||||
assertThat(p.getProperty(entry.getKey())).as("substring(1)").isEqualTo(t.substring(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void processDateTest() {
|
||||
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);
|
||||
void processIntAddTest() throws Exception {
|
||||
entryInt.calc(ADD);
|
||||
entryInt.setDefaultValue("-1");
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("add(-1)").isEqualTo("0");
|
||||
|
||||
assertThat(PropertyFileUtils.processDate(t, p, entry.set("a"), false)).as("processDate(a)").isFalse();
|
||||
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");
|
||||
|
||||
PropertyFileUtils.processDate(t, p, entry.set("99"), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(99)").isEqualTo("99");
|
||||
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");
|
||||
|
||||
PropertyFileUtils.processDate(t, p, entry.set("noew"), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now)").isEqualTo(day);
|
||||
entryInt.calc(v -> v + 2);
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("add(0015)+2").isEqualTo("0017");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseIntSubTest() throws Exception {
|
||||
entryInt.calc(SUB);
|
||||
entryInt.setPattern("0000");
|
||||
PropertyFileUtils.processInt(t, p, entryInt.defaultValue("0017"), true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("sub(0017)").isEqualTo("0016");
|
||||
|
||||
PropertyFileUtils.processInt(t, p, entryInt.set(16).calc(v -> v - 3), true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("sub(16)-3").isEqualTo("0013");
|
||||
}
|
||||
|
||||
@Test
|
||||
void processDateAddTest() throws Exception {
|
||||
entryDate.setCalc(ADD);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.now(), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(now+1)").isEqualTo(String.valueOf(dayOfYear + 1));
|
||||
|
||||
PropertyFileUtils.processDate(t, p, entryDate.now().calc(v -> v + 3), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(now+3)").isEqualTo(String.valueOf(dayOfYear + 3));
|
||||
|
||||
entryDate.setCalc(ADD);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(ZonedDateTime.now()), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(ZonedDateTime+1)")
|
||||
.isEqualTo(String.valueOf(dayOfYear + 1));
|
||||
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(Instant.now()).calc(v -> v + 2), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(Instant+2)").isEqualTo(String.valueOf(dayOfYear + 2));
|
||||
|
||||
entryDate.setCalc(v -> v + 3);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(LocalDateTime.now()), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(LocalDteTime+2)").isEqualTo(String.valueOf(dayOfYear + 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseDateSub() throws Exception {
|
||||
entryDate.setCalc(SUB);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.now(), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(now-3)").isEqualTo(String.valueOf(dayOfYear - 1));
|
||||
|
||||
entryDate.setCalc(v -> v - 2);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.now(), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(now-2)").isEqualTo(String.valueOf(dayOfYear - 2));
|
||||
|
||||
entryDate.setCalc(SUB);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(new Date()), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(date-1)").isEqualTo(String.valueOf(dayOfYear - 1));
|
||||
|
||||
entryDate.setCalc(v -> v - 2);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(Calendar.getInstance()), true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(cal-2)").isEqualTo(String.valueOf(dayOfYear - 2));
|
||||
|
||||
entryDate.setCalc(v -> v - 3);
|
||||
PropertyFileUtils.processDate(t, p, entryDate.set(LocalDate.now()),
|
||||
true);
|
||||
assertThat(p.getProperty(entryDate.getKey())).as("processDate(LocalDate-3)").isEqualTo(String.valueOf(dayOfYear - 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseTimeTest() throws Exception {
|
||||
var entry = new EntryDate("time").pattern("m");
|
||||
var time = LocalTime.now();
|
||||
|
||||
// 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));
|
||||
PropertyFileUtils.processDate(t, p, entry.set(time).unit(EntryDate.Units.MINUTE), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)")
|
||||
.isEqualTo(String.valueOf(time.plusMinutes(1).getMinute()));
|
||||
|
||||
entry.setCalc(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.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.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));
|
||||
PropertyFileUtils.processDate(t, p, entry.set(time).unit(EntryDate.Units.HOUR).pattern("H"), true);
|
||||
assertThat(p.getProperty(entry.getKey())).as("processDate(now+1)")
|
||||
.isEqualTo(String.valueOf(time.minusHours(1).getHour()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -168,7 +223,7 @@ class PropertyFileUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
void savePropertiesTest() throws IOException {
|
||||
void savePropertiesTest() throws Exception {
|
||||
var p = new Properties();
|
||||
var test = "test";
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue