Added pmd extension
Added fromProject instead of using the constructor Improved tests
This commit is contained in:
parent
b5af26c074
commit
9dfde85473
18 changed files with 573 additions and 401 deletions
|
@ -38,79 +38,160 @@ import static rife.bld.extension.propertyfile.Calc.SUB;
|
|||
* @since 1.0
|
||||
*/
|
||||
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";
|
||||
final static int dayOfYear = LocalDate.now().getDayOfYear();
|
||||
final static Properties p = new Properties();
|
||||
final static String t = "test";
|
||||
|
||||
@Test
|
||||
void processStringTest() {
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
public EntryDate newEntryDate() {
|
||||
p.clear();
|
||||
return new EntryDate("adate").pattern("D");
|
||||
}
|
||||
|
||||
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo(p.getProperty(entry.getKey()));
|
||||
public Entry newEntry() {
|
||||
p.clear();
|
||||
return new Entry("version.major").set("1");
|
||||
}
|
||||
|
||||
entry.setKey("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()));
|
||||
public EntryInt newEntryInt() {
|
||||
p.clear();
|
||||
return new EntryInt("version.patch");
|
||||
}
|
||||
|
||||
@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();
|
||||
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
|
||||
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));
|
||||
|
||||
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
|
||||
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
|
||||
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");
|
||||
|
||||
PropertyFileUtils.processInt(t, p, entryInt.set(16).calc(v -> v - 3), true);
|
||||
assertThat(p.getProperty(entryInt.getKey())).as("sub(16)-3").isEqualTo("0013");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
|
||||
void parseStringAppend() {
|
||||
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");
|
||||
}
|
||||
|
||||
@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-1");
|
||||
}
|
||||
|
||||
@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()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringCat() {
|
||||
var entry = newEntry();
|
||||
entry.set(t).setModify("-foo", String::concat);
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace").isEqualTo(t + "-foo");
|
||||
}
|
||||
|
||||
@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()))
|
||||
.isEqualTo("foo-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringReplace() {
|
||||
var entry = newEntry();
|
||||
entry.set(t).setModify("T", (v, s) -> v.replace("t", s));
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace(t -> T)").isEqualTo("TesT");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStringCat() {
|
||||
entry.set(t).setModify("-foo", String::concat);
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
assertThat(p.getProperty(entry.getKey())).as("replace").isEqualTo(t + "-foo");
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
|
||||
void parseTimeTest() throws Exception {
|
||||
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)")
|
||||
.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)")
|
||||
.isEqualTo(String.valueOf(time.minusHours(1).getHour()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
|
||||
void processDateAddTest() throws Exception {
|
||||
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));
|
||||
|
||||
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
|
||||
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
|
||||
void processIntAddTest() throws Exception {
|
||||
var entryInt = newEntryInt();
|
||||
entryInt.calc(ADD);
|
||||
entryInt.setDefaultValue("-1");
|
||||
PropertyFileUtils.processInt(t, p, entryInt, true);
|
||||
|
@ -137,92 +218,22 @@ class PropertyFileUtilsTest {
|
|||
}
|
||||
|
||||
@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");
|
||||
void processStringTest() {
|
||||
var entry = newEntry();
|
||||
PropertyFileUtils.processString(p, entry);
|
||||
|
||||
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();
|
||||
|
||||
entry.setCalc(ADD);
|
||||
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(SUB);
|
||||
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
|
||||
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);
|
||||
assertThat(entry.getNewValue()).as(String.format("processString(%s, %s)", entry.getKey(), entry.getNewValue()))
|
||||
.isEqualTo(p.getProperty(entry.getKey()));
|
||||
|
||||
entry.setKey("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()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
|
||||
void savePropertiesTest() throws Exception {
|
||||
var p = new Properties();
|
||||
var test = "test";
|
||||
|
@ -240,4 +251,25 @@ class PropertyFileUtilsTest {
|
|||
|
||||
tmp.deleteOnExit();
|
||||
}
|
||||
|
||||
@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
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue