Sidebar tests

Signed-off-by: Lukasz Dywicki <luke@code-house.org>

git-svn-id: https://svn.apache.org/repos/asf/karaf/webconsole/trunk@1167227 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/main/java/org/apache/karaf/webconsole/core/page/SidebarPage.java b/core/src/main/java/org/apache/karaf/webconsole/core/page/SidebarPage.java
index 48e6f68..b9f8de4 100644
--- a/core/src/main/java/org/apache/karaf/webconsole/core/page/SidebarPage.java
+++ b/core/src/main/java/org/apache/karaf/webconsole/core/page/SidebarPage.java
@@ -34,10 +34,7 @@
     }
 
     protected void setSidebarProvider(SidebarProvider provider) {
-        if (sidebar == null) {
-            sidebar = new SidebarPanel("sidebar", provider);
-            add(sidebar);
-        }
+        addOrReplace(sidebar = new SidebarPanel("sidebar", provider));
     }
 
     /**
diff --git a/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java b/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java
index c31c556..13e87fd 100644
--- a/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java
+++ b/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java
@@ -1,12 +1,18 @@
 package org.apache.karaf.webconsole.core;
 
+import static org.easymock.EasyMock.anyObject;
+
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.karaf.webconsole.core.brand.DefaultBrandProvider;
 import org.apache.karaf.webconsole.core.internal.WebConsoleApplication;
 import org.apache.karaf.webconsole.core.security.KarafJaasWebSession;
+import org.apache.wicket.Page;
 import org.apache.wicket.authentication.AuthenticatedWebSession;
+import org.apache.wicket.markup.html.link.Link;
 import org.apache.wicket.protocol.http.WebApplication;
 import org.junit.Before;
 
@@ -45,4 +51,20 @@
     protected Class<? extends AuthenticatedWebSession> getWebSessionClass() {
         return KarafJaasWebSession.class;
     }
+
+    // utility methods for easy mock
+
+    /**
+     * Easy mock argument matcher.
+     */
+    protected final static String anyString() {
+        return anyObject();
+    }
+
+    /**
+     * Empty list stub.
+     */
+    protected final static List<Link<Page>> emptyLinkList() {
+        return Collections.<Link<Page>>emptyList();
+    }
 }
\ No newline at end of file
diff --git a/core/src/test/java/org/apache/karaf/webconsole/core/page/SecuredPageTest.java b/core/src/test/java/org/apache/karaf/webconsole/core/page/SecuredPageTest.java
index d1f8117..0b68c91 100644
--- a/core/src/test/java/org/apache/karaf/webconsole/core/page/SecuredPageTest.java
+++ b/core/src/test/java/org/apache/karaf/webconsole/core/page/SecuredPageTest.java
@@ -200,20 +200,6 @@
         return AlwaysAuthenticatedWebSession.class;
     }
 
-    /**
-     * Easy mock argument matcher.
-     */
-    private static String anyString() {
-        return anyObject();
-    }
-
-    /**
-     * Empty list stub.
-     */
-    private static List<Link<Page>> emptyLinkList() {
-        return Collections.<Link<Page>>emptyList();
-    }
-
     // Marker interface for tests, normally serialization is controlled by paxwicket
     // as ConsoleTabProviders are OSGi services.
     interface SerializableConsoleTabProvider extends Serializable, ConsoleTabProvider {}
diff --git a/core/src/test/java/org/apache/karaf/webconsole/core/page/SidebarPageTest.java b/core/src/test/java/org/apache/karaf/webconsole/core/page/SidebarPageTest.java
new file mode 100644
index 0000000..af1ec89
--- /dev/null
+++ b/core/src/test/java/org/apache/karaf/webconsole/core/page/SidebarPageTest.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.karaf.webconsole.core.page;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+import java.util.Collections;
+
+import org.apache.karaf.webconsole.core.WebConsoleTest;
+import org.apache.karaf.webconsole.core.dashboard.DashboardPage;
+import org.apache.karaf.webconsole.core.navigation.SidebarProvider;
+import org.apache.karaf.webconsole.core.test.AlwaysAuthenticatedWebSession;
+import org.apache.karaf.webconsole.core.test.LinkAnswer;
+import org.apache.karaf.webconsole.core.widget.WidgetProvider;
+import org.apache.wicket.authentication.AuthenticatedWebSession;
+import org.apache.wicket.markup.MarkupException;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.BlockJUnit4ClassRunner;
+
+/**
+ * Test sidebar page and sidebar rendering.
+ */
+@RunWith(BlockJUnit4ClassRunner.class)
+public class SidebarPageTest extends WebConsoleTest {
+
+    @Test
+    public void testSidebarWithoutSubItems() {
+        WicketTester tester = new WicketTester(application);
+
+        SidebarProvider provider = createMock(SidebarProvider.class);
+        expect(provider.getMasterPageLink(anyString(), anyString())).andAnswer(new LinkAnswer("Test link", DashboardPage.class));
+        expect(provider.getItems(anyString(), anyString())).andReturn(emptyLinkList());
+        expect(provider.getWidgetProviders()).andReturn(Collections.<WidgetProvider>emptyList());
+
+        replay(provider);
+
+        tester.startPage(new TestSidebarPage(provider));
+        tester.assertBookmarkablePageLink("sidebar:masterPageLink", DashboardPage.class, "");
+        tester.assertLabel("sidebar:masterPageLink:masterPageLabel", "Test link");
+        tester.assertListView("sidebar:subPageLinks", emptyLinkList());
+
+        verify(provider);
+    }
+
+    @Test(expected = MarkupException.class)
+    public void testNoSidebarProvider() {
+        WicketTester tester = new WicketTester(application);
+
+        tester.startPage(SidebarPage.class);
+    }
+
+    @Override
+    protected Class<? extends AuthenticatedWebSession> getWebSessionClass() {
+        return AlwaysAuthenticatedWebSession.class;
+    }
+
+    static class TestSidebarPage extends SidebarPage {
+        public TestSidebarPage(SidebarProvider provider) {
+            setSidebarProvider(provider);
+        }
+    }
+}