PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/confluence/plugins/macros/advanced/xhtml/ExcerptIncludeTestCase.java

https://bitbucket.org/atlassian/confluence-advanced-macros-plugin
Java | 250 lines | 197 code | 48 blank | 5 comment | 0 complexity | 7d95aed8195410b9c0eaa7037c1efc55 MD5 | raw file
  1. package com.atlassian.confluence.plugins.macros.advanced.xhtml;
  2. import com.atlassian.confluence.content.render.xhtml.ConversionContext;
  3. import com.atlassian.confluence.content.render.xhtml.ConversionContextOutputDeviceType;
  4. import com.atlassian.confluence.content.render.xhtml.DefaultConversionContext;
  5. import com.atlassian.confluence.content.render.xhtml.DefaultRenderer;
  6. import com.atlassian.confluence.languages.LocaleManager;
  7. import com.atlassian.confluence.links.linktypes.BlogPostLink;
  8. import com.atlassian.confluence.links.linktypes.PageLink;
  9. import com.atlassian.confluence.macro.MacroExecutionException;
  10. import com.atlassian.confluence.pages.BlogPost;
  11. import com.atlassian.confluence.pages.Page;
  12. import com.atlassian.confluence.renderer.ContentIncludeStack;
  13. import com.atlassian.confluence.renderer.PageContext;
  14. import com.atlassian.confluence.security.PermissionManager;
  15. import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
  16. import com.atlassian.confluence.util.ExcerptHelper;
  17. import com.atlassian.confluence.util.i18n.I18NBean;
  18. import com.atlassian.confluence.util.i18n.I18NBeanFactory;
  19. import com.atlassian.renderer.links.LinkResolver;
  20. import junit.framework.TestCase;
  21. import org.jsoup.Jsoup;
  22. import org.jsoup.nodes.Document;
  23. import org.mockito.Mock;
  24. import org.mockito.MockitoAnnotations;
  25. import org.mockito.invocation.InvocationOnMock;
  26. import org.mockito.stubbing.Answer;
  27. import java.text.ParseException;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import static com.atlassian.confluence.security.Permission.VIEW;
  31. import static org.mockito.Matchers.anyObject;
  32. import static org.mockito.Matchers.anyString;
  33. import static org.mockito.Mockito.*;
  34. public class ExcerptIncludeTestCase extends TestCase
  35. {
  36. // Wording of error doesn't matter as long as the correct key is requested.
  37. private static final String PAGE_NOT_FOUND_ERROR = "This does not exist...";
  38. private static final String NO_LINK_ERROR = "No link!";
  39. private ExcerptIncludeMacro excerptIncludeMacro;
  40. private Map<String, String> parameters;
  41. @Mock private DefaultConversionContext conversionContext;
  42. @Mock private PageContext pageContext;
  43. @Mock private LinkResolver linkResolver;
  44. @Mock private PermissionManager permissionManager;
  45. @Mock private ExcerptHelper excerptHelper;
  46. @Mock private DefaultRenderer viewRenderer;
  47. private I18NBean i18nBean;
  48. @Override
  49. protected void setUp() throws Exception
  50. {
  51. super.setUp();
  52. MockitoAnnotations.initMocks(this);
  53. parameters = new HashMap<String, String>();
  54. // Most tests can run with just the body content rendered.
  55. parameters.put("nopanel", "true");
  56. when(viewRenderer.render(anyString(), (ConversionContext)anyObject())).thenAnswer(new Answer<String>() {
  57. public String answer(InvocationOnMock invocationOnMock) {
  58. return (String)invocationOnMock.getArguments()[0];
  59. }
  60. });
  61. when(pageContext.getOutputDeviceType()).thenReturn(ConversionContextOutputDeviceType.DESKTOP);
  62. when(conversionContext.getPageContext()).thenReturn(pageContext);
  63. I18NBeanFactory i18nBeanFactory = mock(I18NBeanFactory.class);
  64. LocaleManager localeManager = mock(LocaleManager.class);
  65. i18nBean = mock(I18NBean.class);
  66. when(i18nBean.getText(eq("excerptinclude.error.page-does-not-exists"), any(Object[].class))).thenReturn(PAGE_NOT_FOUND_ERROR);
  67. when(i18nBean.getText(eq("excerptinclude.error.cannot-link-to"), any(Object[].class))).thenReturn(NO_LINK_ERROR);
  68. when(i18nBeanFactory.getI18NBean(null)).thenReturn(i18nBean);
  69. excerptIncludeMacro = new ExcerptIncludeMacro();
  70. excerptIncludeMacro.setViewRenderer(viewRenderer);
  71. excerptIncludeMacro.setLinkResolver(linkResolver);
  72. excerptIncludeMacro.setPermissionManager(permissionManager);
  73. excerptIncludeMacro.setExcerptHelper(excerptHelper);
  74. excerptIncludeMacro.setI18NBeanFactory(i18nBeanFactory);
  75. excerptIncludeMacro.setLocaleManager(localeManager);
  76. }
  77. public void testWithValidPage() throws MacroExecutionException, ParseException
  78. {
  79. String excerpt = "This is excerpt text";
  80. parameters.put("0", "Testpage");
  81. Page page = new Page();
  82. PageLink link = mock(PageLink.class);
  83. when(link.getDestinationContent()).thenReturn(page);
  84. when(linkResolver.createLink(pageContext, "Testpage")).thenReturn(link);
  85. when(permissionManager.hasPermission(null, VIEW, page)).thenReturn(true);
  86. when(excerptHelper.getExcerpt(page)).thenReturn(excerpt);
  87. assertEquals("This is excerpt text", executeMacroAndGetBodyContent());
  88. }
  89. public void testWithUnpermittedPage() throws MacroExecutionException, ParseException
  90. {
  91. String excerpt = "This is excerpt text";
  92. parameters.put("0", "Testpage");
  93. Page page = new Page();
  94. PageLink link = mock(PageLink.class);
  95. when(link.getDestinationContent()).thenReturn(page);
  96. when(linkResolver.createLink(pageContext, "Testpage")).thenReturn(link);
  97. when(permissionManager.hasPermission(null, VIEW, page)).thenReturn(false); // just making this clear
  98. when(excerptHelper.getExcerpt(page)).thenReturn(excerpt);
  99. assertEquals(PAGE_NOT_FOUND_ERROR, executeMacroAndGetBodyContent());
  100. }
  101. public void testWithPageTitleParameter() throws MacroExecutionException
  102. {
  103. String excerpt = "This is excerpt text";
  104. parameters.put("pageTitle", "Testpage");
  105. Page page = new Page();
  106. PageLink link = mock(PageLink.class);
  107. when(link.getDestinationContent()).thenReturn(page);
  108. when(linkResolver.createLink(pageContext, "Testpage")).thenReturn(link);
  109. when(permissionManager.hasPermission(null, VIEW, page)).thenReturn(true);
  110. when(excerptHelper.getExcerpt(page)).thenReturn(excerpt);
  111. assertEquals("This is excerpt text", executeMacroAndGetBodyContent());
  112. }
  113. public void testGoodBlogPostReference() throws MacroExecutionException
  114. {
  115. String excerpt = "This is excerpt text";
  116. final String title = "Test BlogPost";
  117. parameters.put("blogPost", "2009/10/01/Test BlogPost");
  118. BlogPost blogPost = new BlogPost();
  119. blogPost.setTitle(title);
  120. BlogPostLink link = mock(BlogPostLink.class);
  121. when(link.getDestinationContent()).thenReturn(blogPost);
  122. when(linkResolver.createLink(pageContext, "2009/10/01/Test BlogPost")).thenReturn(link);
  123. when(permissionManager.hasPermission(AuthenticatedUserThreadLocal.getUser(), VIEW, blogPost)).thenReturn(true);
  124. when(excerptHelper.getExcerpt(blogPost)).thenReturn(excerpt);
  125. assertEquals("This is excerpt text", executeMacroAndGetBodyContent());
  126. }
  127. public void testBadBlogPostReference() throws MacroExecutionException
  128. {
  129. parameters.put("blogPost", "tst:/2009/10/01/This is a non-existent post");
  130. BlogPostLink link = mock(BlogPostLink.class);
  131. when(link.getPageTitle()).thenReturn("This is a non-existent post");
  132. when(link.getDestinationContent()).thenReturn(null); // i.e. not found
  133. when(linkResolver.createLink(pageContext, "tst:/2009/10/01/This is a non-existent post")).thenReturn(link);
  134. Document document = executeWithPanelAndGetDocument();
  135. assertEquals("This is a non-existent post", document.getElementsByClass("panelHeader").get(0).text());
  136. assertEquals(PAGE_NOT_FOUND_ERROR, document.getElementsByClass("panelContent").get(0).text());
  137. verify(linkResolver).createLink(pageContext, "tst:/2009/10/01/This is a non-existent post");
  138. }
  139. public void testWithoutContentEntityObject() throws MacroExecutionException
  140. {
  141. assertEquals(NO_LINK_ERROR, executeMacroAndGetBodyContent());
  142. }
  143. public void testBlogPostPanelHeader() throws MacroExecutionException
  144. {
  145. String blogTitle = "Testing Blog Title";
  146. String linkText = "tst:2009/12/20/" + blogTitle;
  147. parameters.put("blogPost", linkText);
  148. BlogPostLink link = mock(BlogPostLink.class);
  149. when(link.getPageTitle()).thenReturn(blogTitle);
  150. when(linkResolver.createLink(pageContext, linkText)).thenReturn(link);
  151. Document doc = executeWithPanelAndGetDocument();
  152. String renderedTitle = doc.getElementsByClass("panelHeader").get(0).text();
  153. assertEquals(blogTitle, renderedTitle);
  154. }
  155. public void testBlogPostPanelHeaderWithNoSpaceKey() throws MacroExecutionException
  156. {
  157. String blogTitle = "Testing Blog Title";
  158. String linkText = "/2009/12/20/" + blogTitle;
  159. parameters.put("blogPost", linkText);
  160. BlogPostLink link = mock(BlogPostLink.class);
  161. when(link.getPageTitle()).thenReturn(blogTitle);
  162. when(linkResolver.createLink(pageContext, linkText)).thenReturn(link);
  163. Document doc = executeWithPanelAndGetDocument();
  164. String renderedTitle = doc.getElementsByClass("panelHeader").get(0).text();
  165. assertEquals(blogTitle, renderedTitle);
  166. }
  167. public void testBlockInfiniteIncludes() throws Exception
  168. {
  169. Page page = new Page();
  170. page.setTitle("Testpage");
  171. parameters.put("pageTitle", "Testpage");
  172. PageLink link = mock(PageLink.class);
  173. when(link.getDestinationContent()).thenReturn(page);
  174. when(linkResolver.createLink(pageContext, "Testpage")).thenReturn(link);
  175. when(permissionManager.hasPermission(null, VIEW, page)).thenReturn(true);
  176. String recursionError = "Oh no, recursion! recursion! recursion!";
  177. when(i18nBean.getText("excerptinclude.error.recursive.message")).thenReturn(recursionError);
  178. try
  179. {
  180. // If the page is already on the stack, then we've already started rendering that page, which includes
  181. // some other content which includes the {excerpt-include} macro which includes the page again. That could
  182. // turn out badly - instead we return a message.
  183. ContentIncludeStack.push(page);
  184. String body = executeMacroAndGetBodyContent();
  185. assertTrue("Body is: " + body, body.contains(recursionError));
  186. }
  187. finally
  188. {
  189. ContentIncludeStack.pop();
  190. }
  191. }
  192. private String executeMacroAndGetBodyContent() throws MacroExecutionException
  193. {
  194. return excerptIncludeMacro.execute(parameters, "", conversionContext);
  195. }
  196. private org.jsoup.nodes.Document executeWithPanelAndGetDocument() throws MacroExecutionException
  197. {
  198. parameters.put("nopanel", "false");
  199. String html = executeMacroAndGetBodyContent();
  200. return Jsoup.parseBodyFragment(html);
  201. }
  202. }