123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package com.vaadin.server;
-
- import static org.hamcrest.MatcherAssert.assertThat;
- 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 javax.portlet.PortletSession;
-
- 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 VaadinPortletServiceTest {
-
- private VaadinPortletService sut;
- private VaadinPortletRequest request;
- private DeploymentConfiguration conf;
-
- @Before
- public void setup() throws ServiceException {
- VaadinPortlet portlet = mock(VaadinPortlet.class);
- conf = mock(DeploymentConfiguration.class);
-
- sut = new VaadinPortletService(portlet, conf);
-
- request = mock(VaadinPortletRequest.class);
- }
-
- private void mockFileLocationProperty(String location) {
- mockPortalProperty(Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH,
- location);
- }
-
- private void mockPortalProperty(String name, String value) {
- when(request.getPortalProperty(name)).thenReturn(value);
- }
-
- private void mockFileLocationPreference(String location) {
- when(request.getPortletPreference(
- Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH))
- .thenReturn(location);
- }
-
- private void mockLocationDeploymentConfiguration(String location) {
- when(conf.getApplicationOrSystemProperty(
- Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH, null))
- .thenReturn(location);
- }
-
- private String getStaticFileLocation() {
- return sut.getStaticFileLocation(request);
- }
-
- private String getTheme() {
- return sut.getConfiguredTheme(request);
- }
-
- private void mockThemeProperty(String theme) {
- mockPortalProperty(Constants.PORTAL_PARAMETER_VAADIN_THEME, theme);
- }
-
- private void mockWidgetsetProperty(String widgetset) {
- mockPortalProperty(Constants.PORTAL_PARAMETER_VAADIN_WIDGETSET,
- widgetset);
- }
-
- private void mockWidgetsetConfiguration(String widgetset) {
- when(conf.getWidgetset(null)).thenReturn(widgetset);
- }
-
- @Test
- public void preferencesOverrideDeploymentConfiguration() {
- mockFileLocationPreference("prefs");
- mockLocationDeploymentConfiguration("conf");
-
- String location = getStaticFileLocation();
-
- assertThat(location, is("prefs"));
- }
-
- @Test
- public void deploymentConfigurationOverridesProperties() {
- mockFileLocationPreference(null);
- mockLocationDeploymentConfiguration("conf");
- mockFileLocationProperty("props");
-
- String location = getStaticFileLocation();
-
- assertThat(location, is("conf"));
- }
-
- @Test
- public void defaultFileLocationIsSet() {
- mockFileLocationPreference(null);
- mockLocationDeploymentConfiguration(null);
- mockFileLocationProperty(null);
-
- String location = getStaticFileLocation();
-
- assertThat(location, is("/html"));
- }
-
- @Test
- public void trailingSlashesAreTrimmedFromStaticFileLocation() {
- mockFileLocationPreference("/content////");
-
- String staticFileLocation = getStaticFileLocation();
-
- assertThat(staticFileLocation, is("/content"));
- }
-
- @Test
- public void themeCanBeOverridden() {
- mockThemeProperty("foobar");
-
- String theme = getTheme();
-
- assertThat(theme, is("foobar"));
- }
-
- @Test
- public void defaultThemeIsSet() {
- mockThemeProperty(null);
-
- String theme = getTheme();
-
- assertThat(theme, is(Constants.DEFAULT_THEME_NAME));
- }
-
- private String getWidgetset() {
- return sut.getConfiguredWidgetset(request);
- }
-
- @Test
- public void defaultWidgetsetIsSet() {
- mockWidgetsetProperty(null);
- mockWidgetsetConfiguration(null);
-
- String widgetset = getWidgetset();
-
- assertThat(widgetset, is(Constants.DEFAULT_WIDGETSET));
- }
-
- @Test
- public void configurationWidgetsetOverridesProperty() {
- mockWidgetsetProperty("foo");
- mockWidgetsetConfiguration("bar");
-
- String widgetset = getWidgetset();
-
- assertThat(widgetset, is("bar"));
- }
-
- @Test
- public void oldDefaultWidgetsetIsMappedToDefaultWidgetset() {
- mockWidgetsetConfiguration(null);
- mockWidgetsetProperty("com.vaadin.portal.gwt.PortalDefaultWidgetSet");
-
- String widgetset = getWidgetset();
-
- assertThat(widgetset, is(Constants.DEFAULT_WIDGETSET));
- }
-
- @Test
- public void oldDefaultWidgetSetIsNotMappedToDefaultWidgetset() {
- mockWidgetsetConfiguration(
- "com.vaadin.portal.gwt.PortalDefaultWidgetSet");
- mockWidgetsetProperty(null);
-
- String widgetset = getWidgetset();
-
- assertThat(widgetset,
- is("com.vaadin.portal.gwt.PortalDefaultWidgetSet"));
- }
-
- @Test
- public void findUIDoesntThrowNPE() {
- try {
- ReentrantLock mockLock = Mockito.mock(ReentrantLock.class);
- when(mockLock.isHeldByCurrentThread()).thenReturn(true);
-
- WrappedPortletSession emptyWrappedSession = Mockito
- .mock(WrappedPortletSession.class);
- when(emptyWrappedSession.getAttribute("null.lock",PortletSession.APPLICATION_SCOPE))
- .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");
- }
- }
- }
|