You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VaadinPortletServiceTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package com.vaadin.server;
  2. import static org.hamcrest.MatcherAssert.assertThat;
  3. import static org.hamcrest.core.Is.is;
  4. import static org.junit.Assert.assertNull;
  5. import static org.junit.Assert.fail;
  6. import static org.mockito.Mockito.mock;
  7. import static org.mockito.Mockito.when;
  8. import java.util.concurrent.locks.ReentrantLock;
  9. import javax.portlet.PortletSession;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import org.mockito.Mockito;
  13. import com.vaadin.shared.ui.ui.UIConstants;
  14. import com.vaadin.ui.UI;
  15. public class VaadinPortletServiceTest {
  16. private VaadinPortletService sut;
  17. private VaadinPortletRequest request;
  18. private DeploymentConfiguration conf;
  19. @Before
  20. public void setup() throws ServiceException {
  21. VaadinPortlet portlet = mock(VaadinPortlet.class);
  22. conf = mock(DeploymentConfiguration.class);
  23. sut = new VaadinPortletService(portlet, conf);
  24. request = mock(VaadinPortletRequest.class);
  25. }
  26. private void mockFileLocationProperty(String location) {
  27. mockPortalProperty(Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH,
  28. location);
  29. }
  30. private void mockPortalProperty(String name, String value) {
  31. when(request.getPortalProperty(name)).thenReturn(value);
  32. }
  33. private void mockFileLocationPreference(String location) {
  34. when(request.getPortletPreference(
  35. Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH))
  36. .thenReturn(location);
  37. }
  38. private void mockLocationDeploymentConfiguration(String location) {
  39. when(conf.getApplicationOrSystemProperty(
  40. Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH, null))
  41. .thenReturn(location);
  42. }
  43. private String getStaticFileLocation() {
  44. return sut.getStaticFileLocation(request);
  45. }
  46. private String getTheme() {
  47. return sut.getConfiguredTheme(request);
  48. }
  49. private void mockThemeProperty(String theme) {
  50. mockPortalProperty(Constants.PORTAL_PARAMETER_VAADIN_THEME, theme);
  51. }
  52. private void mockWidgetsetProperty(String widgetset) {
  53. mockPortalProperty(Constants.PORTAL_PARAMETER_VAADIN_WIDGETSET,
  54. widgetset);
  55. }
  56. private void mockWidgetsetConfiguration(String widgetset) {
  57. when(conf.getWidgetset(null)).thenReturn(widgetset);
  58. }
  59. @Test
  60. public void preferencesOverrideDeploymentConfiguration() {
  61. mockFileLocationPreference("prefs");
  62. mockLocationDeploymentConfiguration("conf");
  63. String location = getStaticFileLocation();
  64. assertThat(location, is("prefs"));
  65. }
  66. @Test
  67. public void deploymentConfigurationOverridesProperties() {
  68. mockFileLocationPreference(null);
  69. mockLocationDeploymentConfiguration("conf");
  70. mockFileLocationProperty("props");
  71. String location = getStaticFileLocation();
  72. assertThat(location, is("conf"));
  73. }
  74. @Test
  75. public void defaultFileLocationIsSet() {
  76. mockFileLocationPreference(null);
  77. mockLocationDeploymentConfiguration(null);
  78. mockFileLocationProperty(null);
  79. String location = getStaticFileLocation();
  80. assertThat(location, is("/html"));
  81. }
  82. @Test
  83. public void trailingSlashesAreTrimmedFromStaticFileLocation() {
  84. mockFileLocationPreference("/content////");
  85. String staticFileLocation = getStaticFileLocation();
  86. assertThat(staticFileLocation, is("/content"));
  87. }
  88. @Test
  89. public void themeCanBeOverridden() {
  90. mockThemeProperty("foobar");
  91. String theme = getTheme();
  92. assertThat(theme, is("foobar"));
  93. }
  94. @Test
  95. public void defaultThemeIsSet() {
  96. mockThemeProperty(null);
  97. String theme = getTheme();
  98. assertThat(theme, is(Constants.DEFAULT_THEME_NAME));
  99. }
  100. private String getWidgetset() {
  101. return sut.getConfiguredWidgetset(request);
  102. }
  103. @Test
  104. public void defaultWidgetsetIsSet() {
  105. mockWidgetsetProperty(null);
  106. mockWidgetsetConfiguration(null);
  107. String widgetset = getWidgetset();
  108. assertThat(widgetset, is(Constants.DEFAULT_WIDGETSET));
  109. }
  110. @Test
  111. public void configurationWidgetsetOverridesProperty() {
  112. mockWidgetsetProperty("foo");
  113. mockWidgetsetConfiguration("bar");
  114. String widgetset = getWidgetset();
  115. assertThat(widgetset, is("bar"));
  116. }
  117. @Test
  118. public void oldDefaultWidgetsetIsMappedToDefaultWidgetset() {
  119. mockWidgetsetConfiguration(null);
  120. mockWidgetsetProperty("com.vaadin.portal.gwt.PortalDefaultWidgetSet");
  121. String widgetset = getWidgetset();
  122. assertThat(widgetset, is(Constants.DEFAULT_WIDGETSET));
  123. }
  124. @Test
  125. public void oldDefaultWidgetSetIsNotMappedToDefaultWidgetset() {
  126. mockWidgetsetConfiguration(
  127. "com.vaadin.portal.gwt.PortalDefaultWidgetSet");
  128. mockWidgetsetProperty(null);
  129. String widgetset = getWidgetset();
  130. assertThat(widgetset,
  131. is("com.vaadin.portal.gwt.PortalDefaultWidgetSet"));
  132. }
  133. @Test
  134. public void findUIDoesntThrowNPE() {
  135. try {
  136. ReentrantLock mockLock = Mockito.mock(ReentrantLock.class);
  137. when(mockLock.isHeldByCurrentThread()).thenReturn(true);
  138. WrappedPortletSession emptyWrappedSession = Mockito
  139. .mock(WrappedPortletSession.class);
  140. when(emptyWrappedSession.getAttribute("null.lock",PortletSession.APPLICATION_SCOPE))
  141. .thenReturn(mockLock);
  142. VaadinRequest requestWithUIIDSet = Mockito
  143. .mock(VaadinRequest.class);
  144. when(requestWithUIIDSet.getParameter(UIConstants.UI_ID_PARAMETER))
  145. .thenReturn("1");
  146. when(requestWithUIIDSet.getWrappedSession())
  147. .thenReturn(emptyWrappedSession);
  148. UI ui = sut.findUI(requestWithUIIDSet);
  149. assertNull("Unset session did not return null", ui);
  150. } catch (NullPointerException e) {
  151. fail("findUI threw a NullPointerException");
  152. }
  153. }
  154. }