Skip to content

Commit

Permalink
fix: fix date/time/datetime picker tester validation (#1845)
Browse files Browse the repository at this point in the history
Validation API of Date/Time/DateTime picker has been changed since Vaadin 24.5,
and the UI Unit testers are failing with recent Vaadin version.
This change makes the testers compatible with all Vaadin 24 series.

Fixes #1840
  • Loading branch information
mcollovati authored Jan 13, 2025
1 parent 6cad5c7 commit 23de5be
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ internal fun DynaNodeGroup.locatorTest2() {
_expectNoDialogs() // should succeed on no dialogs
val dlg = Dialog()
dlg.open()
expectThrows(AssertionError::class,
"""Too many visible Dialogs (1) in MockedUI[] matching Dialog and count=0..0: [Dialog[opened='true', virtualChildNodeIds='[]']]. Component tree:
└── MockedUI[]
└── Dialog[opened='true', virtualChildNodeIds='[]']
""") {
val exception = expectThrows(AssertionError::class,
"Too many visible Dialogs (1) in MockedUI[] matching Dialog and count=0..0:") {
_expectNoDialogs()
}
expect(true) {
val msg = exception.message!!
println(msg)
msg.matches(""".*Too many visible Dialogs \(1\) in MockedUI\[] matching Dialog and count=0\.\.0: \[Dialog\[.*]]\. Component tree:
└── MockedUI\[]
\s+└── Dialog\[.*].*
""".toRegex(RegexOption.MULTILINE)
)
}
dlg.close()
_expectNoDialogs()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,30 @@ public DatePickerTester(T component) {
public void setValue(LocalDate date) {
ensureComponentIsUsable();

final Method isInvalid = getMethod("isInvalid", LocalDate.class);
try {
if ((boolean) isInvalid.invoke(getComponent(), date)) {
if (isInvalid(date)) {
throw new IllegalArgumentException(
"Given date is not a valid value");
}

} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}

getComponent().setValue(date);
}

private boolean isInvalid(LocalDate date)
throws InvocationTargetException, IllegalAccessException {
try {
// Vaadin 24.4
final Method isInvalid = getMethod("isInvalid", LocalDate.class);
return (boolean) isInvalid.invoke(getComponent(), date);
} catch (RuntimeException ex) {
if (!(ex.getCause() instanceof NoSuchMethodException)) {
throw ex;
}
}
// Vaadin 24.5+
return getComponent().getDefaultValidator().apply(date, null).isError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;

import com.vaadin.testbench.unit.ComponentTester;
Expand Down Expand Up @@ -47,9 +48,8 @@ public DateTimePickerTester(T component) {
public void setValue(LocalDateTime dateTime) {
ensureComponentIsUsable();

final Method isInvalid = getMethod("isInvalid", LocalDateTime.class);
try {
if ((boolean) isInvalid.invoke(getComponent(), dateTime)) {
if (isInvalid(dateTime)) {
throw new IllegalArgumentException(
"Given date is not a valid value");
}
Expand All @@ -60,4 +60,19 @@ public void setValue(LocalDateTime dateTime) {
getComponent().setValue(dateTime);
}

private boolean isInvalid(LocalDateTime date)
throws InvocationTargetException, IllegalAccessException {
try {
// Vaadin 24.4
final Method isInvalid = getMethod("isInvalid", LocalDate.class);
return (boolean) isInvalid.invoke(getComponent(), date);
} catch (RuntimeException ex) {
if (!(ex.getCause() instanceof NoSuchMethodException)) {
throw ex;
}
}
// Vaadin 24.5+
return getComponent().getDefaultValidator().apply(date, null).isError();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalTime;

import com.vaadin.testbench.unit.ComponentTester;
Expand Down Expand Up @@ -46,9 +47,8 @@ public TimePickerTester(T component) {
public void setValue(LocalTime time) {
ensureComponentIsUsable();

final Method isInvalid = getMethod("isInvalid", LocalTime.class);
try {
if ((boolean) isInvalid.invoke(getComponent(), time)) {
if (isInvalid(time)) {
throw new IllegalArgumentException(
"Given time is not a valid value");
}
Expand All @@ -59,4 +59,19 @@ public void setValue(LocalTime time) {
getComponent().setValue(time);
}

private boolean isInvalid(LocalTime date)
throws InvocationTargetException, IllegalAccessException {
try {
// Vaadin 24.4
final Method isInvalid = getMethod("isInvalid", LocalDate.class);
return (boolean) isInvalid.invoke(getComponent(), date);
} catch (RuntimeException ex) {
if (!(ex.getCause() instanceof NoSuchMethodException)) {
throw ex;
}
}
// Vaadin 24.5+
return getComponent().getDefaultValidator().apply(date, null).isError();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ internal fun DynaNodeGroup.locatorTest2() {
_expectNoDialogs() // should succeed on no dialogs
val dlg = Dialog()
dlg.open()
expectThrows(AssertionError::class,
"""Too many visible Dialogs (1) in MockedUI[] matching Dialog and count=0..0: [Dialog[opened='true', virtualChildNodeIds='[]']]. Component tree:
└── MockedUI[]
└── Dialog[opened='true', virtualChildNodeIds='[]']
""") {
val exception = expectThrows(AssertionError::class,
"Too many visible Dialogs (1) in MockedUI[] matching Dialog and count=0..0:") {
_expectNoDialogs()
}
expect(true) {
val msg = exception.message!!
println(msg)
msg.matches(""".*Too many visible Dialogs \(1\) in MockedUI\[] matching Dialog and count=0\.\.0: \[Dialog\[.*]]\. Component tree:
└── MockedUI\[]
\s+└── Dialog\[.*].*
""".toRegex(RegexOption.MULTILINE)
)
}
dlg.close()
_expectNoDialogs()
}
Expand Down

0 comments on commit 23de5be

Please # to comment.