Author: ldywicki Date: Mon Sep 5 19:19:39 2011 New Revision: 1165401 URL: http://svn.apache.org/viewvc?rev=1165401&view=rev Log: Tests for dashboard page. Small changes in test injector code.
Added: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/BasePageTest.java karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestTargetLocator.java karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/dashboard/ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/dashboard/DashboardPageTest.java karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/test/ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/test/AlwaysAuthenticatedWebSession.java karaf/sandbox/webconsole/trunk/core/src/test/resources/log4j.properties Modified: karaf/sandbox/webconsole/trunk/core/pom.xml karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java Modified: karaf/sandbox/webconsole/trunk/core/pom.xml URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/pom.xml?rev=1165401&r1=1165400&r2=1165401&view=diff ============================================================================== --- karaf/sandbox/webconsole/trunk/core/pom.xml (original) +++ karaf/sandbox/webconsole/trunk/core/pom.xml Mon Sep 5 19:19:39 2011 @@ -81,6 +81,12 @@ <version>${slf4j.version}</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.easymock</groupId> + <artifactId>easymock</artifactId> + <version>3.0</version> + <scope>test</scope> + </dependency> </dependencies> <build> @@ -135,6 +141,10 @@ <name>java.security.auth.login.config</name> <value>${basedir}/src/test/resources/jaas.conf</value> </property> + <property> + <name>sun.io.serialization.extendedDebugInfo</name> + <value>true</value> + </property> </systemProperties> </configuration> </plugin> Added: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/BasePageTest.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/BasePageTest.java?rev=1165401&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/BasePageTest.java (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/BasePageTest.java Mon Sep 5 19:19:39 2011 @@ -0,0 +1,69 @@ +/* + * 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; + +import static org.easymock.EasyMock.*; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.karaf.webconsole.core.brand.BrandProvider; +import org.apache.karaf.webconsole.core.brand.DefaultBrandProvider; +import org.apache.wicket.behavior.IBehavior; +import org.apache.wicket.markup.html.image.Image; +import org.apache.wicket.util.tester.WicketTester; +import org.easymock.IAnswer; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * Test which checks usage of {@link BrandProvider} inside {@link BasePage}. + */ +@RunWith(BlockJUnit4ClassRunner.class) +public class BasePageTest extends WebConsoleTest { + + private String imageId; + + @Test + public void testBrandProvider() { + BrandProvider brandProvider = createMock(DefaultBrandProvider.class); + expect(brandProvider.getHeaderImage((String) anyObject())).andAnswer(new IAnswer<Image>() { + public Image answer() throws Throwable { + imageId = (String) getCurrentArguments()[0]; + return new Image(imageId); + } + }); + expect(brandProvider.getBehaviors()).andReturn(Collections.<IBehavior>emptyList()); + + replay(brandProvider); + + Map<String, Object> values = new HashMap<String, Object>(); + values.put("brandProvider", brandProvider); + + injector.setValues(values); + + WicketTester tester = new WicketTester(application); + tester.startPage(BasePage.class); + + tester.assertVisible(imageId); + + verify(brandProvider); + } + +} Modified: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java?rev=1165401&r1=1165400&r2=1165401&view=diff ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java (original) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java Mon Sep 5 19:19:39 2011 @@ -16,18 +16,11 @@ */ package org.apache.karaf.webconsole.core; -import java.util.HashMap; -import java.util.Map; - -import org.apache.karaf.webconsole.core.brand.DefaultBrandProvider; import org.apache.karaf.webconsole.core.dashboard.DashboardPage; -import org.apache.karaf.webconsole.core.internal.WebConsoleApplication; import org.apache.karaf.webconsole.core.page.LoginPage; import org.apache.wicket.authorization.UnauthorizedInstantiationException; -import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.util.tester.FormTester; import org.apache.wicket.util.tester.WicketTester; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; @@ -36,22 +29,7 @@ import org.junit.runners.BlockJUnit4Clas * Test login operation and role verification. */ @RunWith(BlockJUnit4ClassRunner.class) -public class LoginTest { - - /** - * Application instance. - */ - private WebApplication application; - - @Before - public void setUp() { - application = new WebConsoleApplication(); - - final Map<String, Object> values = new HashMap<String, Object>(); - values.put("brandProvider", new DefaultBrandProvider()); - - application.addComponentInstantiationListener(new TestInjector(values)); - } +public class LoginTest extends WebConsoleTest { @Test public void testSuccessLogin() throws Exception { Modified: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java?rev=1165401&r1=1165400&r2=1165401&view=diff ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java (original) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java Mon Sep 5 19:19:39 2011 @@ -16,17 +16,14 @@ */ package org.apache.karaf.webconsole.core; -import java.io.Serializable; import java.lang.reflect.Field; import java.util.Map; -import net.sf.cglib.proxy.Enhancer; - import org.apache.wicket.Component; import org.apache.wicket.application.IComponentInstantiationListener; import org.ops4j.pax.wicket.api.PaxWicketBean; import org.ops4j.pax.wicket.internal.injection.AbstractPaxWicketInjector; -import org.ops4j.pax.wicket.internal.injection.ComponentProxy; +import org.ops4j.pax.wicket.util.proxy.LazyInitProxyFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,17 +41,23 @@ public class TestInjector extends Abstra /** * Values to inject. */ - private final Map<String, Object> values; + private Map<String, Object> values; public TestInjector(Map<String, Object> values) { this.values = values; } /** + * Set values to be injected. + */ + public void setValues(Map<String, Object> values) { + this.values = values; + } + + /** * {@inheritDoc} */ public void onInstantiation(Component component) { - for (Field field : getFields(component.getClass())) { // iterate over fields and check if there is values to inject String name = field.getName(); @@ -62,24 +65,17 @@ public class TestInjector extends Abstra if (values.containsKey(name)) { // we have value.. - Object value = values.get(name); + final Object value = values.get(name); // create instance proxy, just like pax-wicket does if (type.isAssignableFrom(value.getClass())){ - Enhancer enhancer = new Enhancer(); - enhancer.setSuperclass(value.getClass()); - if (!(value instanceof Serializable)) { - logger.warn("Value for field {}.{} is not serializable. Adding Serializable interface for test, result in production may vary", - field.getDeclaringClass(), name); - enhancer.setInterfaces(new Class[] {Serializable.class}); - } - enhancer.setCallback(new ComponentProxy("test", null)); - setField(component, field, enhancer.create()); + logger.debug("Setting value {} for field {}.{}", new Object[] {value, field.getDeclaringClass().getName(), name}); + Object createProxy = LazyInitProxyFactory.createProxy(field.getType(), new TestTargetLocator(value)); + setField(component, field, createProxy); } else { logger.error("Type mismath for field {}.{}. Expected {} but {} given", new Object[] { field.getDeclaringClass().getName(), name, field.getType().getName(), value.getClass().getName() }); - System.err.println("Unknown type " + field.getType()); } } else { logger.warn("No value provided for field {}.{}", field.getDeclaringClass().getName(), name); Added: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestTargetLocator.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestTargetLocator.java?rev=1165401&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestTargetLocator.java (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestTargetLocator.java Mon Sep 5 19:19:39 2011 @@ -0,0 +1,36 @@ +/* + * 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; + +import org.ops4j.pax.wicket.util.proxy.IProxyTargetLocator; + +/** + * Test target locator for test injections. + */ +public class TestTargetLocator implements IProxyTargetLocator { + + private Object value; + + public TestTargetLocator(Object value) { + this.value = value; + } + + public Object locateProxyTarget() { + return value; + } + +} \ No newline at end of file Added: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java?rev=1165401&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/WebConsoleTest.java Mon Sep 5 19:19:39 2011 @@ -0,0 +1,48 @@ +package org.apache.karaf.webconsole.core; + +import java.util.HashMap; +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.JaasWebSession; +import org.apache.wicket.authentication.AuthenticatedWebSession; +import org.apache.wicket.protocol.http.WebApplication; +import org.junit.Before; + +public class WebConsoleTest { + + /** + * Application instance. + */ + protected WebApplication application; + + /** + * Test injector. + */ + protected TestInjector injector; + + public WebConsoleTest() { + super(); + } + + @Before + public void setUp() { + application = new WebConsoleApplication() { + protected java.lang.Class<? extends AuthenticatedWebSession> getWebSessionClass() { + return WebConsoleTest.this.getWebSessionClass(); + } + }; + + // default configurations values. + Map<String, Object> values = new HashMap<String, Object>(); + values.put("brandProvider", new DefaultBrandProvider()); + + injector = new TestInjector(values); + application.addComponentInstantiationListener(injector); + } + + protected Class<? extends AuthenticatedWebSession> getWebSessionClass() { + return JaasWebSession.class; + } +} \ No newline at end of file Added: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/dashboard/DashboardPageTest.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/dashboard/DashboardPageTest.java?rev=1165401&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/dashboard/DashboardPageTest.java (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/dashboard/DashboardPageTest.java Mon Sep 5 19:19:39 2011 @@ -0,0 +1,73 @@ +/* + * 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.dashboard; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.apache.karaf.webconsole.core.WebConsoleTest; +import org.apache.karaf.webconsole.core.brand.DefaultBrandProvider; +import org.apache.karaf.webconsole.core.dashboard.DashboardPage; +import org.apache.karaf.webconsole.core.test.AlwaysAuthenticatedWebSession; +import org.apache.karaf.webconsole.core.widget.WidgetProvider; +import org.apache.wicket.authentication.AuthenticatedWebSession; +import org.apache.wicket.markup.html.panel.EmptyPanel; +import org.apache.wicket.markup.html.panel.Panel; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * Test which covers dashboard page. + */ +@RunWith(BlockJUnit4ClassRunner.class) +public class DashboardPageTest extends WebConsoleTest { + + static class TestWidgetProvider implements WidgetProvider, Serializable { + public Panel getWidgetPanel(String id) { + return new EmptyPanel(id); + } + } + + @Test + public void testDashboardNoWidgets() { + Map<String, Object> values = new HashMap<String, Object>(); + + ArrayList<WidgetProvider> providers = new ArrayList<WidgetProvider>(); + providers.add(new TestWidgetProvider()); + + values.put("widgets", providers); + values.put("brandProvider", new DefaultBrandProvider()); + + injector.setValues(values); + + WicketTester tester = new WicketTester(application); + tester.startPage(DashboardPage.class); + + tester.assertRenderedPage(DashboardPage.class); + tester.assertListView("widgets", providers); + tester.assertInvisible("noWidgets"); + } + + @Override + protected Class<? extends AuthenticatedWebSession> getWebSessionClass() { + return AlwaysAuthenticatedWebSession.class; + } +} Added: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/test/AlwaysAuthenticatedWebSession.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/test/AlwaysAuthenticatedWebSession.java?rev=1165401&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/test/AlwaysAuthenticatedWebSession.java (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/test/AlwaysAuthenticatedWebSession.java Mon Sep 5 19:19:39 2011 @@ -0,0 +1,46 @@ +/* + * 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.test; + +import org.apache.wicket.Request; +import org.apache.wicket.authentication.AuthenticatedWebSession; +import org.apache.wicket.authorization.strategies.role.Roles; + +/** + * Dummy session which does not handle authentication, simply returns true for + * all input values and only one role. + */ +public class AlwaysAuthenticatedWebSession extends AuthenticatedWebSession { + + private Roles roles; + + public AlwaysAuthenticatedWebSession(Request request) { + super(request); + roles = new Roles("admin"); + } + + @Override + public boolean authenticate(String username, String password) { + return true; + } + + @Override + public Roles getRoles() { + return roles; + } + +} Added: karaf/sandbox/webconsole/trunk/core/src/test/resources/log4j.properties URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/resources/log4j.properties?rev=1165401&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/resources/log4j.properties (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/resources/log4j.properties Mon Sep 5 19:19:39 2011 @@ -0,0 +1,9 @@ +log4j.rootCategory=INFO, CONSOLE + +log4j.logger.org.apache.karaf.webconsole.core = DEBUG + +log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender +log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout +log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %c:%L - %m%n +#log4j.appender.CONSOLE.threshold = DEBUG +