Skip to content

Commit 0c23cd1

Browse files
authored
test: add test for proxied views (#458)
Ensure changes in vaadin/flow#19151 works correctly on CDI
1 parent 60ff371 commit 0c23cd1

File tree

4 files changed

+175
-11
lines changed

4 files changed

+175
-11
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2000-2024 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.cdi.itest.smoke;
17+
18+
import jakarta.interceptor.Interceptors;
19+
import java.util.UUID;
20+
import java.util.concurrent.atomic.AtomicInteger;
21+
22+
import com.vaadin.cdi.annotation.CdiComponent;
23+
import com.vaadin.flow.component.html.Div;
24+
import com.vaadin.flow.router.BeforeEvent;
25+
import com.vaadin.flow.router.HasUrlParameter;
26+
import com.vaadin.flow.router.OptionalParameter;
27+
import com.vaadin.flow.router.Route;
28+
import com.vaadin.flow.router.RouterLink;
29+
30+
@Route("proxied")
31+
@CdiComponent
32+
// @Interceptors annotation should cause CDI to create
33+
// a proxy class for the bean instance
34+
@Interceptors(UselessInterceptor.class)
35+
public class ProxiedNavigationTargetView extends Div
36+
implements HasUrlParameter<Integer> {
37+
38+
private final String uuid = UUID.randomUUID().toString();
39+
private final AtomicInteger counter = new AtomicInteger();
40+
private final RouterLink routerLink;
41+
private final Div clickCounter;
42+
43+
public ProxiedNavigationTargetView() {
44+
Div uuid = new Div(this.uuid);
45+
uuid.setId("COMPONENT_ID");
46+
add(uuid);
47+
48+
clickCounter = new Div("P:0, C:0");
49+
clickCounter.setId("CLICK_COUNTER");
50+
add(clickCounter);
51+
52+
// Self navigation should use the same view instance
53+
routerLink = new RouterLink("Self Link", ProxiedNavigationTargetView.class,
54+
counter.incrementAndGet());
55+
add(routerLink);
56+
}
57+
58+
@Override
59+
public void setParameter(BeforeEvent event,
60+
@OptionalParameter Integer parameter) {
61+
if (parameter != null) {
62+
clickCounter.setText("P:" + parameter + ", C:" + counter.get());
63+
routerLink.setRoute(ProxiedNavigationTargetView.class,
64+
counter.incrementAndGet());
65+
}
66+
}
67+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.vaadin.cdi.itest.smoke;
2+
3+
public class UselessInterceptor {
4+
}

vaadin-cdi-itest/src/test/java/com/vaadin/cdi/itest/SmokeTest.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,34 @@
1616

1717
package com.vaadin.cdi.itest;
1818

19+
import java.util.concurrent.atomic.AtomicReference;
20+
1921
import com.vaadin.cdi.itest.smoke.CdiView;
22+
import com.vaadin.cdi.itest.smoke.ProxiedNavigationTargetView;
23+
import com.vaadin.cdi.itest.smoke.UselessInterceptor;
2024
import org.jboss.arquillian.container.test.api.Deployment;
2125
import org.jboss.arquillian.container.test.api.OperateOnDeployment;
2226
import org.jboss.shrinkwrap.api.spec.WebArchive;
2327
import org.junit.Assert;
2428
import org.junit.Before;
2529
import org.junit.Test;
30+
import org.openqa.selenium.By;
2631

2732
@SuppressWarnings("ArquillianTooManyDeployment")
2833
public class SmokeTest extends AbstractCdiTest {
2934

3035
@Deployment(name = "noncdi", testable = false)
3136
public static WebArchive createCdiServletDisabledDeployment() {
3237
return ArchiveProvider.createWebArchive("noncdi-test",
33-
CdiView.class)
38+
CdiView.class)
3439
.addAsWebInfResource(ArchiveProvider.class.getClassLoader()
3540
.getResource("disablecdi-web.xml"), "web.xml");
3641
}
3742

3843
@Deployment(name = "cdi", testable = false)
3944
public static WebArchive createCdiServletEnabledDeployment() {
4045
return ArchiveProvider.createWebArchive("cdi-test",
41-
CdiView.class);
46+
CdiView.class, ProxiedNavigationTargetView.class, UselessInterceptor.class);
4247
}
4348

4449
@Before
@@ -59,6 +64,38 @@ public void injectionHappensWithEnabledCdiServlet() {
5964
assertLabelEquals("hello CDI");
6065
}
6166

67+
@Test
68+
@OperateOnDeployment("cdi")
69+
public void navigationCorrectlyHandlesProxiedViews() {
70+
getDriver().get(getTestURL() + "proxied");
71+
waitForDevServer();
72+
73+
String prevUuid = null;
74+
AtomicReference<String> prevCounter = new AtomicReference<>("");
75+
for (int i = 0; i < 5; i++) {
76+
String uuid = waitUntil(d -> d.findElement(By.id("COMPONENT_ID")))
77+
.getText();
78+
79+
waitUntil(d -> !prevCounter.get()
80+
.equals(d.findElement(By.id("CLICK_COUNTER")).getText()));
81+
82+
if (prevUuid != null) {
83+
Assert.assertEquals("UUID should not have been changed",
84+
prevUuid, uuid);
85+
} else {
86+
prevUuid = uuid;
87+
}
88+
String counter = findElement(By.id("CLICK_COUNTER")).getText();
89+
Assert.assertEquals(
90+
"Parameter and counter should have the same value",
91+
"P:" + i + ", C:" + i, counter);
92+
93+
prevCounter.set(counter);
94+
95+
$("a").first().click();
96+
}
97+
}
98+
6299
private void assertLabelEquals(String expected) {
63100
Assert.assertEquals(expected, $("label").first().getText());
64101
}

vaadin-cdi/src/test/java/com/vaadin/cdi/CdiInstantiatorTest.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@
1616

1717
package com.vaadin.cdi;
1818

19-
import java.util.List;
20-
import java.util.Locale;
21-
import java.util.stream.Collectors;
22-
19+
import jakarta.enterprise.context.ApplicationScoped;
2320
import jakarta.enterprise.event.Observes;
2421
import jakarta.enterprise.inject.Vetoed;
2522
import jakarta.enterprise.inject.spi.BeanManager;
2623
import jakarta.inject.Inject;
2724
import jakarta.inject.Singleton;
25+
import jakarta.interceptor.AroundInvoke;
26+
import jakarta.interceptor.Interceptors;
27+
import jakarta.interceptor.InvocationContext;
2828
import jakarta.servlet.ServletException;
29-
import org.junit.jupiter.api.AfterEach;
30-
import org.junit.jupiter.api.Assertions;
31-
import org.junit.jupiter.api.BeforeEach;
32-
import org.junit.jupiter.api.Test;
33-
import org.mockito.Mockito;
29+
import java.util.List;
30+
import java.util.Locale;
31+
import java.util.stream.Collectors;
3432

3533
import com.vaadin.cdi.annotation.VaadinServiceEnabled;
3634
import com.vaadin.cdi.context.ServiceUnderTestContext;
@@ -40,13 +38,39 @@
4038
import com.vaadin.flow.internal.UsageStatistics;
4139
import com.vaadin.flow.server.ServiceInitEvent;
4240
import com.vaadin.flow.server.VaadinService;
41+
import org.junit.jupiter.api.AfterEach;
42+
import org.junit.jupiter.api.Assertions;
43+
import org.junit.jupiter.api.BeforeEach;
44+
import org.junit.jupiter.api.Test;
45+
import org.mockito.Mockito;
4346

4447
public class CdiInstantiatorTest extends AbstractWeldTest {
4548

4649
@Singleton
4750
public static class SomeCdiBean {
4851
}
4952

53+
@ApplicationScoped
54+
public static class ScopedCdiBean {
55+
}
56+
57+
public static class MyInterceptor {
58+
59+
@AroundInvoke
60+
public Object intercept(InvocationContext ctx) throws Exception {
61+
return ctx.proceed();
62+
}
63+
}
64+
65+
@Interceptors(MyInterceptor.class)
66+
public static class InterceptedCdiBean {
67+
68+
public void exec() {
69+
70+
}
71+
}
72+
73+
5074
public static class ParentBean {
5175

5276
@Inject
@@ -206,4 +230,36 @@ public void init_callsUsageStatistics() {
206230
Assertions.assertEquals("flow/CdiInstantiator", entry.getName());
207231
}
208232

233+
@Test
234+
public void getApplicationClass_regularClass_getsSameClass()
235+
throws ServletException {
236+
SomeCdiBean instance = instantiator.getOrCreate(SomeCdiBean.class);
237+
Assertions.assertSame(SomeCdiBean.class,
238+
instantiator.getApplicationClass(instance));
239+
Assertions.assertSame(SomeCdiBean.class,
240+
instantiator.getApplicationClass(instance.getClass()));
241+
}
242+
243+
@Test
244+
public void getApplicationClass_scopedBean_getsApplicationClass()
245+
throws ServletException {
246+
ScopedCdiBean instance = instantiator.getOrCreate(ScopedCdiBean.class);
247+
Assertions.assertSame(ScopedCdiBean.class,
248+
instantiator.getApplicationClass(instance));
249+
Assertions.assertSame(ScopedCdiBean.class,
250+
instantiator.getApplicationClass(instance.getClass()));
251+
}
252+
253+
@Test
254+
public void getApplicationClass_proxiedBean_getsApplicationClass()
255+
throws ServletException {
256+
InterceptedCdiBean instance = instantiator.getOrCreate(InterceptedCdiBean.class);
257+
instance.exec();
258+
Assertions.assertNotSame(InterceptedCdiBean.class, instance.getClass());
259+
Assertions.assertSame(InterceptedCdiBean.class,
260+
instantiator.getApplicationClass(instance));
261+
Assertions.assertSame(InterceptedCdiBean.class,
262+
instantiator.getApplicationClass(instance.getClass()));
263+
}
264+
209265
}

0 commit comments

Comments
 (0)