Skip to content

Decorator should honour implementation checks #11438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,25 @@ protected final <Z> Z createProxy(final Decorated<Z> decorated, Class<Z> clazz)
|| decoratedInterfaces.contains(method.getDeclaringClass())) {
return method.invoke(decorated, args);
}
if (originalInterfaces.contains(method.getDeclaringClass())) {
// Check if the class in which the method resides, implements any one of the
// interfaces that we extracted from the decorated class.
boolean isCompatible = originalInterfaces.stream()
.anyMatch(eachInterface -> eachInterface.isAssignableFrom(method.getDeclaringClass()));

if (isCompatible) {
decorated.beforeCall(method, args);
Object result = decorated.call(method, args);
decorated.afterCall(method, result, args);
return result;
}
if (derivedInterfaces.containsKey(method.getDeclaringClass())) {

// Check if the class in which the current method resides, implements any of the
// additional interfaces that we extracted from the decorated class.
// E.g., of additional interfaces include WrapsDriver,WrapsElement,JsonSerializer
isCompatible = derivedInterfaces.keySet().stream()
.anyMatch(eachInterface -> eachInterface.isAssignableFrom(method.getDeclaringClass()));

if (isCompatible) {
return derivedInterfaces.get(method.getDeclaringClass()).invoke(proxy, method, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Tag;
import org.openqa.selenium.Alert;
Expand Down Expand Up @@ -436,4 +438,21 @@ public void onError(Object target, Method method, Object[] args, InvocationTarge
assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(decorated::getWindowHandle);
}

@Test
public void ensureListenersAreInvokedWhenUsingDecoratedSubClasses() {
RemoteWebDriver originalDriver = mock(RemoteWebDriver.class);
doNothing().when(originalDriver).get(any());
AtomicInteger invocationCount = new AtomicInteger(0);
WebDriverListener listener = new WebDriverListener() {
@Override
public void beforeAnyCall(Object target, Method method, Object[] args) {
invocationCount.incrementAndGet();
}
};
RemoteWebDriver rem = new EventFiringDecorator<>(RemoteWebDriver.class, listener)
.decorate(originalDriver);
rem.get("http://localhost:4444");
assertThat(invocationCount.get()).isEqualTo(1);
}
}