]> source.dussan.org Git - vaadin-framework.git/commitdiff
Ensure session is set before writing timeout interval (#13617)
authorArtur Signell <artur@vaadin.com>
Thu, 17 Apr 2014 13:32:18 +0000 (16:32 +0300)
committerVaadin Code Review <review@vaadin.com>
Mon, 28 Apr 2014 07:03:48 +0000 (07:03 +0000)
Change-Id: I4a4f85fe400b9d6e60b81fd5c6c8dbc3b3eac02c

server/src/com/vaadin/server/communication/MetadataWriter.java
server/tests/src/com/vaadin/server/communication/MetadataWriterTest.java [new file with mode: 0644]

index 5ad7186c2468bd1704c905cf4451395abdbffa15..bae06f57638320a3d39b354815bb568b6893bbaf 100644 (file)
@@ -77,7 +77,8 @@ public class MetadataWriter implements Serializable {
         // sessionExpiredURL after timer expires.
         if (messages != null && messages.getSessionExpiredMessage() == null
                 && messages.getSessionExpiredCaption() == null
-                && messages.isSessionExpiredNotificationEnabled()) {
+                && messages.isSessionExpiredNotificationEnabled()
+                && ui.getSession().getSession() != null) {
             int newTimeoutInterval = ui.getSession().getSession()
                     .getMaxInactiveInterval();
             if (repaintAll || (timeoutInterval != newTimeoutInterval)) {
diff --git a/server/tests/src/com/vaadin/server/communication/MetadataWriterTest.java b/server/tests/src/com/vaadin/server/communication/MetadataWriterTest.java
new file mode 100644 (file)
index 0000000..dee37dd
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2000-2014 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.server.communication;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.StringWriter;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import com.vaadin.server.SystemMessages;
+import com.vaadin.server.VaadinSession;
+import com.vaadin.server.WrappedSession;
+import com.vaadin.ui.UI;
+
+public class MetadataWriterTest {
+
+    private UI ui;
+    private VaadinSession session;
+    private StringWriter writer;
+    private SystemMessages messages;
+
+    @Before
+    public void setup() {
+        ui = Mockito.mock(UI.class);
+        session = Mockito.mock(VaadinSession.class);
+        Mockito.when(ui.getSession()).thenReturn(session);
+        writer = new StringWriter();
+        messages = Mockito.mock(SystemMessages.class);
+    }
+
+    private void disableSessionExpirationMessages(SystemMessages messages) {
+        when(messages.isSessionExpiredNotificationEnabled()).thenReturn(true);
+        when(messages.getSessionExpiredMessage()).thenReturn(null);
+        when(messages.getSessionExpiredCaption()).thenReturn(null);
+    }
+
+    @Test
+    public void writeAsyncTag() throws Exception {
+        new MetadataWriter().write(ui, writer, false, true, messages);
+        Assert.assertEquals("{\"async\":true}", writer.getBuffer().toString());
+    }
+
+    @Test
+    public void writeRepaintTag() throws Exception {
+        new MetadataWriter().write(ui, writer, true, false, messages);
+        Assert.assertEquals("{\"repaintAll\":true}", writer.getBuffer()
+                .toString());
+    }
+
+    @Test
+    public void writeRepaintAndAsyncTag() throws Exception {
+        new MetadataWriter().write(ui, writer, true, true, messages);
+        Assert.assertEquals("{\"repaintAll\":true, \"async\":true}", writer
+                .getBuffer().toString());
+    }
+
+    @Test
+    public void writeRedirectWithExpiredSession() throws Exception {
+        disableSessionExpirationMessages(messages);
+
+        new MetadataWriter().write(ui, writer, false, false, messages);
+        Assert.assertEquals("{}", writer.getBuffer().toString());
+    }
+
+    @Test
+    public void writeRedirectWithActiveSession() throws Exception {
+        WrappedSession wrappedSession = mock(WrappedSession.class);
+        when(session.getSession()).thenReturn(wrappedSession);
+
+        disableSessionExpirationMessages(messages);
+
+        new MetadataWriter().write(ui, writer, false, false, messages);
+        Assert.assertEquals(
+                "{\"timedRedirect\":{\"interval\":15,\"url\":\"\"}}", writer
+                        .getBuffer().toString());
+    }
+}