Skip to content

Commit

Permalink
fix: fix serialization issues
Browse files Browse the repository at this point in the history
Fixes #436
Fixes #437
  • Loading branch information
mcollovati committed Jun 21, 2023
1 parent 382f731 commit 1dee606
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 16 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-test-util</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion vaadin-cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinServletService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.WrappedSession;

import static com.vaadin.cdi.BeanLookup.SERVICE;

Expand Down Expand Up @@ -86,7 +87,7 @@ public CdiVaadinServletService(CdiVaadinServlet servlet,

@Override
public void init() throws ServiceException {
delegate.init();
delegate.init(this);
super.init();
}

Expand All @@ -96,6 +97,20 @@ public void fireUIInitListeners(UI ui) {
super.fireUIInitListeners(ui);
}

@Override
protected VaadinSession loadSession(WrappedSession wrappedSession) {
return super.loadSession(wrappedSession);
}

@Override
protected void storeSession(VaadinSession session, WrappedSession wrappedSession) {
super.storeSession(session, wrappedSession);
}

private void restoreDelegate(VaadinSession session) {

}

public Optional<Instantiator> loadInstantiators() throws ServiceException {
BeanManager beanManager = delegate.getBeanManager();

Expand Down Expand Up @@ -145,21 +160,17 @@ public CdiVaadinServlet getServlet() {
*/
public static class CdiVaadinServiceDelegate implements Serializable {

private final VaadinService vaadinService;

private transient BeanManager beanManager;

private final UIEventListener uiEventListener;

public CdiVaadinServiceDelegate(VaadinService vaadinService,
BeanManager beanManager) {
this.beanManager = beanManager;
this.vaadinService = vaadinService;

uiEventListener = new UIEventListener(this);
}

public void init() throws ServiceException {
public void init(VaadinService vaadinService) throws ServiceException {
lookup(SystemMessagesProvider.class)
.ifPresent(vaadinService::setSystemMessagesProvider);
vaadinService.addUIInitListener(e -> getBeanManager().getEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ String getUIId() {

}

static class NavigationData {
static class NavigationData implements Serializable {
private final Class<?> navigationTarget;
private final List<Class<? extends RouterLayout>> layouts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,31 @@

package com.vaadin.cdi;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import jakarta.enterprise.context.spi.Context;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.AmbiguousResolutionException;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.vaadin.cdi.annotation.VaadinServiceEnabled;
import com.vaadin.cdi.annotation.VaadinServiceScoped;
import com.vaadin.cdi.context.ServiceUnderTestContext;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.PollEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.di.Instantiator;
import com.vaadin.flow.di.InstantiatorFactory;
import com.vaadin.flow.server.CustomizedSystemMessages;
Expand All @@ -43,7 +49,9 @@
import com.vaadin.flow.server.SystemMessages;
import com.vaadin.flow.server.SystemMessagesInfo;
import com.vaadin.flow.server.SystemMessagesProvider;
import com.vaadin.flow.server.VaadinSession;

import static com.vaadin.cdi.SerializationUtils.serializeAndDeserialize;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
Expand All @@ -67,6 +75,15 @@ public SystemMessages getSystemMessages(

}

@Singleton
private static class UIListenerEventReceiver {

private UI pollEventUI;
void onPollEvent(@Observes PollEvent pollEvent) {
pollEventUI = pollEvent.getSource();
}
}

@Inject
private BeanManager beanManager;

Expand Down Expand Up @@ -176,6 +193,25 @@ public void loadInstantiators_serviceInitialized_instantiatorInstanceCreated()
Assertions.assertEquals(mockInstantiator, maybeInstantiator.get());
}

@Test
void fireUIInitListeners_serialization_UIserializableAndListenersWork() throws Exception {
initService(beanManager);

UIListenerEventReceiver uiListenerEventReceiver = service.getInstantiator().getOrCreate(UIListenerEventReceiver.class);
UI ui = new UI();
ui.getInternals().setSession(Mockito.mock(VaadinSession.class, Mockito.withSettings().serializable()));
service.fireUIInitListeners(ui);

ComponentUtil.fireEvent(ui, new PollEvent(ui, false));
Assertions.assertEquals(ui, uiListenerEventReceiver.pollEventUI);

UI ui2 = serializeAndDeserialize(ui);
Assertions.assertNotNull(ui2);

ComponentUtil.fireEvent(ui2, new PollEvent(ui2, false));
Assertions.assertEquals(ui2, uiListenerEventReceiver.pollEventUI);
}

private void initService(BeanManager beanManager) throws ServiceException {
ServiceUnderTestContext serviceUnderTestContext = new ServiceUnderTestContext(beanManager);
serviceUnderTestContext.activate();
Expand Down
44 changes: 44 additions & 0 deletions vaadin-cdi/src/test/java/com/vaadin/cdi/SerializationUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2000-2023 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.cdi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public final class SerializationUtils {

private SerializationUtils() {
}

@SuppressWarnings("unchecked")
public static <S extends Serializable> S serializeAndDeserialize(S obj)
throws Exception {
// Serialize and deserialize
ByteArrayOutputStream bs = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(bs)) {
out.writeObject(obj);
}
byte[] data = bs.toByteArray();
try (ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(data))) {
return (S) in.readObject();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@

package com.vaadin.cdi.context;

import java.io.Serializable;
import java.util.Collections;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import com.vaadin.cdi.annotation.NormalRouteScoped;
import com.vaadin.cdi.context.RouteScopedContext.NavigationData;
import com.vaadin.cdi.util.BeanProvider;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.startup.ApplicationConfiguration;

import static com.vaadin.cdi.SerializationUtils.serializeAndDeserialize;

public class RouteContextNormalTest extends
AbstractContextTest<RouteContextNormalTest.RouteScopedTestBean> {

@NormalRouteScoped
@Route("")
public static class RouteScopedTestBean extends TestBean {
public static class RouteScopedTestBean extends TestBean implements Serializable {
}

@Override
Expand Down Expand Up @@ -59,4 +71,18 @@ protected boolean isNormalScoped() {
return true;
}

@Test
void activeContext_UISerializable() throws Exception {
UIUnderTestContext context = (UIUnderTestContext) createContext();
context.activate();
BeanProvider.getContextualReference(getBeanType());
UI ui = context.getUi();
try (MockedStatic<ApplicationConfiguration> appCfg = Mockito.mockStatic(ApplicationConfiguration.class)) {
appCfg.when(() -> ApplicationConfiguration.get(ArgumentMatchers.any()))
.thenReturn(Mockito.mock(ApplicationConfiguration.class));
UI ui2 = serializeAndDeserialize(ui);
Assertions.assertNotNull(ui2);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@

package com.vaadin.cdi.context;

import java.io.Serializable;
import java.util.Collections;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.cdi.context.RouteScopedContext.NavigationData;
import com.vaadin.cdi.util.BeanProvider;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.startup.ApplicationConfiguration;

import static com.vaadin.cdi.SerializationUtils.serializeAndDeserialize;

public class RouteContextPseudoTest extends
AbstractContextTest<RouteContextPseudoTest.RouteScopedTestBean> {

@RouteScoped
@Route("")
public static class RouteScopedTestBean extends TestBean {
public static class RouteScopedTestBean extends TestBean implements Serializable {
}

@Override
Expand Down Expand Up @@ -59,4 +71,18 @@ protected boolean isNormalScoped() {
return false;
}

@Test
void activeContext_UISerializable() throws Exception {
UIUnderTestContext context = (UIUnderTestContext) createContext();
context.activate();
BeanProvider.getContextualReference(getBeanType());
UI ui = context.getUi();
try (MockedStatic<ApplicationConfiguration> appCfg = Mockito.mockStatic(ApplicationConfiguration.class)) {
appCfg.when(() -> ApplicationConfiguration.get(ArgumentMatchers.any()))
.thenReturn(Mockito.mock(ApplicationConfiguration.class));
UI ui2 = serializeAndDeserialize(ui);
Assertions.assertNotNull(ui2);
}
}

}

0 comments on commit 1dee606

Please sign in to comment.