Browse Source

Fix findUI throwing NullPointerException when extending Vaadin (#13556)

findUI sometimes threw a NPE when the session wasn't set but the UI ID
was. Doesn't occur in normal use, just when doing custom things with
requestStart/requestEnd or runPendingAccessTasks
Change-Id: Id7733567923fa30dcab4946c43b73200c2a0fac2
tags/7.4.0.alpha1
Juuso Valli 10 years ago
parent
commit
2e58e97e52

+ 1
- 1
server/src/com/vaadin/server/VaadinService.java View File

@@ -982,7 +982,7 @@ public abstract class VaadinService implements Serializable {
// Get UI id from the request
String uiIdString = request.getParameter(UIConstants.UI_ID_PARAMETER);
UI ui = null;
if (uiIdString != null) {
if (uiIdString != null && session != null) {
int uiId = Integer.parseInt(uiIdString);
ui = session.getUIById(uiId);
}

+ 31
- 0
server/tests/src/com/vaadin/server/VaadinPortletServiceTests.java View File

@@ -20,8 +20,15 @@ import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.concurrent.locks.ReentrantLock;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.vaadin.shared.ui.ui.UIConstants;
import com.vaadin.ui.UI;

public class VaadinPortletServiceTests {

@@ -188,4 +195,28 @@ public class VaadinPortletServiceTests {
assertThat(widgetset,
is("com.vaadin.portal.gwt.PortalDefaultWidgetSet"));
}

@Test
public void findUIDoesntThrowNPE() {
try {
ReentrantLock mockLock = Mockito.mock(ReentrantLock.class);
when(mockLock.isHeldByCurrentThread()).thenReturn(true);

WrappedSession emptyWrappedSession = Mockito
.mock(WrappedSession.class);
when(emptyWrappedSession.getAttribute("null.lock")).thenReturn(
mockLock);
VaadinRequest requestWithUIIDSet = Mockito
.mock(VaadinRequest.class);
when(requestWithUIIDSet.getParameter(UIConstants.UI_ID_PARAMETER))
.thenReturn("1");
when(requestWithUIIDSet.getWrappedSession()).thenReturn(
emptyWrappedSession);

UI ui = sut.findUI(requestWithUIIDSet);
Assert.assertNull("Unset session did not return null", ui);
} catch (NullPointerException e) {
Assert.fail("findUI threw a NullPointerException");
}
}
}

Loading…
Cancel
Save