Skip to content

Commit

Permalink
Merge branch 'issues/76_vaadin-24' into test/76_vaadin-24
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollovati authored Jan 6, 2024
2 parents 5792811 + 766bfa5 commit a307041
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.vaadin.flow.internal.DevModeHandler;
import com.vaadin.flow.internal.DevModeHandlerManager;
Expand Down Expand Up @@ -98,16 +98,9 @@ protected void serviceInitialized(VertxVaadinService service, Router router) {
response.setStatusCode(404);
if (devModeHandler != null) {
LOGGER.trace("======================== check start. dev mod 1");
try {
Method isRunningMethod = findIsRunningMethod(devModeHandler.getClass());
boolean isRunning = (boolean) isRunningMethod.invoke(devModeHandler);
if (isRunning) {
LOGGER.info("DevModeHandler ready");
response.setStatusCode(200);
}
} catch (NoSuchFieldException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
LOGGER.trace("======================== check start. dev mod err ");
if (isReady(devModeHandler)) {
LOGGER.info("DevModeHandler ready");
response.setStatusCode(200);
}
} else if (service.getDeploymentConfiguration().isProductionMode()) {
response.setStatusCode(200);
Expand All @@ -117,16 +110,34 @@ protected void serviceInitialized(VertxVaadinService service, Router router) {
});
}

private Method findIsRunningMethod(Class<?> clazz) throws NoSuchFieldException {
private boolean isReady(DevModeHandler devModeHandler) {
Class<? extends DevModeHandler> clazz = devModeHandler.getClass();
Field startFutureField = findStartFuture(clazz, "devServerStartFuture");
if (startFutureField == null) {
startFutureField = findStartFuture(clazz, "buildCompletedFuture");
}
if (startFutureField != null) {
boolean wasAccessible = startFutureField.canAccess(devModeHandler);
try {
startFutureField.setAccessible(true);
CompletableFuture<?> startFuture = (CompletableFuture<?>) startFutureField.get(devModeHandler);
return startFuture.isDone() && !startFuture.isCancelled() && !startFuture.isCompletedExceptionally();
} catch (Exception ex) {
startFutureField.setAccessible(wasAccessible);
LOGGER.trace("Error getting start future from {}", devModeHandler.getClass(), ex);
}
}
return false;
}

private Field findStartFuture(Class<?> clazz, String fieldName) {
do {
try {
Method method = clazz.getDeclaredMethod("isRunning");
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
return clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
}
} while (clazz != Object.class);
throw new NoSuchFieldException("Cannot find devServerStartFuture");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,54 +40,39 @@ public void testNavigationTriggers() {

assertMessageCount(1);

assertLastMessage(
"/abc", isClientRouter() ? NavigationTrigger.CLIENT_SIDE : NavigationTrigger.PAGE_LOAD, "abc");
Assert.assertEquals(
"The trailing '/' from the URL should be removed.",
assertLastMessage("/abc", NavigationTrigger.PAGE_LOAD, "abc");
Assert.assertEquals("The trailing '/' from the URL should be removed.",
url.substring(0, url.length() - 1),
getDriver().getCurrentUrl());

findElement(By.id("routerlink")).click();
assertMessageCount(2);
assertLastMessage(
"/routerlink",
isClientRouter() ? NavigationTrigger.CLIENT_SIDE : NavigationTrigger.ROUTER_LINK,
assertLastMessage("/routerlink", NavigationTrigger.ROUTER_LINK,
"routerlink");

findElement(By.id("navigate")).click();
assertMessageCount(3);
assertLastMessage("/navigate", NavigationTrigger.UI_NAVIGATE, "navigate");
}

@Test
@Ignore("Ignored because of fusion issue https://github.com/vaadin/flow/issues/10513")
public void testNavigationTriggers_back_forward() {
String url = getTestURL() + "/abc/";
getDriver().get(url);

findElement(By.id("routerlink")).click();

findElement(By.id("navigate")).click();
assertLastMessage("/navigate", NavigationTrigger.UI_NAVIGATE,
"navigate");

getDriver().navigate().back();
assertMessageCount(4);
assertLastMessage(
"/routerlink",
isClientRouter() ? NavigationTrigger.CLIENT_SIDE : NavigationTrigger.HISTORY,
assertLastMessage("/routerlink", NavigationTrigger.HISTORY,
"routerlink");

getDriver().navigate().forward();
assertMessageCount(5);
assertLastMessage(
"/navigate", isClientRouter() ? NavigationTrigger.CLIENT_SIDE : NavigationTrigger.HISTORY, "navigate");
assertLastMessage("/navigate", NavigationTrigger.HISTORY, "navigate");

findElement(By.id("forwardButton")).click();
assertMessageCount(6);
assertLastMessage("/forwarded", NavigationTrigger.PROGRAMMATIC, "forwarded");
assertLastMessage("/forwarded", NavigationTrigger.PROGRAMMATIC,
"forwarded");

findElement(By.id("rerouteButton")).click();
assertMessageCount(7);
assertLastMessage("/", NavigationTrigger.PROGRAMMATIC, "rerouted");
assertLastMessage("/rerouted", NavigationTrigger.PROGRAMMATIC,
"rerouted");
}

private void assertLastMessage(String path, NavigationTrigger trigger, String parameter) {
Expand Down

0 comments on commit a307041

Please sign in to comment.