PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/atlassian/confluence/plugins/macros/advanced/GalleryMacroTestCase.java

https://bitbucket.org/atlassian/confluence-advanced-macros-plugin
Java | 290 lines | 224 code | 58 blank | 8 comment | 0 complexity | c8806502eeac697118dbb718ad387665 MD5 | raw file
  1. package com.atlassian.confluence.plugins.macros.advanced;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.velocity.VelocityContext;
  7. import com.atlassian.confluence.core.ConfluenceActionSupport;
  8. import com.atlassian.confluence.links.linktypes.PageLink;
  9. import com.atlassian.confluence.pages.Attachment;
  10. import com.atlassian.confluence.pages.AttachmentManager;
  11. import com.atlassian.confluence.pages.Page;
  12. import com.atlassian.confluence.pages.thumbnail.ThumbnailManager;
  13. import com.atlassian.confluence.pages.thumbnail.Thumbnails;
  14. import com.atlassian.confluence.renderer.PageContext;
  15. import com.atlassian.confluence.renderer.attachments.RendererAttachmentManager;
  16. import com.atlassian.renderer.RenderContextOutputType;
  17. import com.atlassian.renderer.links.Link;
  18. import com.atlassian.renderer.links.LinkResolver;
  19. import com.atlassian.renderer.v2.RenderMode;
  20. import com.atlassian.renderer.v2.macro.MacroException;
  21. import junit.framework.TestCase;
  22. import static org.mockito.Mockito.mock;
  23. import static org.mockito.Mockito.when;
  24. public class GalleryMacroTestCase extends TestCase
  25. {
  26. private String thumbnailNotSupportedErrorText = "Thumbnails is not supported";
  27. private PageContext pageContext;
  28. private VelocityContext velocityContext;
  29. private AttachmentManager attachmentManager;
  30. private RendererAttachmentManager rendererAttachmentManager;
  31. private ConfluenceActionSupport confluenceActionSupport;
  32. private ThumbnailManager thumbnailManager;
  33. private LinkResolver linkResolver;
  34. private PageLink pageLink;
  35. private Thumbnails mockThumbnails;
  36. private String outputText = "Execution complete";
  37. private String title = "Gallery Title";
  38. @Override
  39. protected void setUp() throws Exception
  40. {
  41. super.setUp();
  42. pageContext = mock(PageContext.class);
  43. attachmentManager = mock(AttachmentManager.class);
  44. rendererAttachmentManager = mock(RendererAttachmentManager.class);
  45. confluenceActionSupport = mock(ConfluenceActionSupport.class);
  46. thumbnailManager = mock(ThumbnailManager.class);
  47. linkResolver = mock(LinkResolver.class);
  48. velocityContext = new VelocityContext();
  49. pageLink = mock(PageLink.class);
  50. mockThumbnails = mock(Thumbnails.class);
  51. }
  52. public void testWhenThumbnailsIsNotSupported() throws MacroException
  53. {
  54. Map<String, String> params;
  55. params = new HashMap<String, String>();
  56. GalleryMacro galleryMacro = new GalleryMacro()
  57. {
  58. @Override
  59. protected boolean isThumbnailSupported()
  60. {
  61. return false;
  62. }
  63. @Override
  64. public ConfluenceActionSupport getConfluenceActionSupport()
  65. {
  66. return confluenceActionSupport;
  67. }
  68. };
  69. when(confluenceActionSupport.getText("gallery.error.thumbnails-not-supported")).thenReturn(thumbnailNotSupportedErrorText);
  70. assertEquals("<p><span class=\"error\">" + thumbnailNotSupportedErrorText + "</span></p>",
  71. galleryMacro.execute(params, "", pageContext));
  72. }
  73. // TODOXHTML CONFDEV-1224
  74. public void TODOXHTML_testReverseSortingWithoutIncludeOrExcludeAttachmentAndReverseSorting() throws MacroException
  75. {
  76. final List<Attachment> attachmentsList = new ArrayList<Attachment>();
  77. Map<String, String> params;
  78. params = new HashMap<String, String>();
  79. params.put("title", title);
  80. params.put("columns", "3");
  81. params.put("sort", "true");
  82. params.put("reverse", "false");
  83. GalleryMacro galleryMacro = new GalleryMacro()
  84. {
  85. @Override
  86. protected boolean isThumbnailSupported()
  87. {
  88. return true;
  89. }
  90. @Override
  91. protected VelocityContext newVelocityContext()
  92. {
  93. return velocityContext;
  94. }
  95. @Override
  96. protected Thumbnails createThumbnails(int columns,
  97. List<Attachment> attachments)
  98. {
  99. // Check total size of attachments after filter
  100. assertEquals(3, attachments.size());
  101. // Check whether attachments is reverse sorted
  102. assertEquals("Cattle.jpg", attachments.get(0).getFileName());
  103. assertEquals("Beatle.jpg", attachments.get(1).getFileName());
  104. assertEquals("Apple.jpg", attachments.get(2).getFileName());
  105. return mockThumbnails;
  106. }
  107. @Override
  108. protected String getRenderedTemplateWithoutSwallowingErrors(
  109. String template, VelocityContext contextMap)
  110. throws Exception {
  111. // Check whether same mocked thumbnails is returned
  112. assertEquals(mockThumbnails, (Thumbnails) contextMap.get("thumbnails"));
  113. return outputText;
  114. }
  115. };
  116. setMockObjects(galleryMacro);
  117. Page page = new Page();
  118. page.setTitle("Test Page");
  119. page.setBodyAsString("This is page content");
  120. when(pageContext.getEntity()).thenReturn(page);
  121. when(pageContext.getOutputType()).thenReturn(RenderContextOutputType.DISPLAY);
  122. when(linkResolver.createLink(pageContext, "page1")).thenReturn((Link)pageLink);
  123. when(pageLink.getDestinationContent()).thenReturn(page);
  124. Attachment attachmentOne, attachmentTwo, attachmentThree;
  125. attachmentOne = new Attachment();
  126. attachmentOne.setFileName("Cattle.jpg");
  127. attachmentsList.add(attachmentOne);
  128. attachmentTwo = new Attachment();
  129. attachmentTwo.setFileName("Apple.jpg");
  130. attachmentsList.add(attachmentTwo);
  131. attachmentThree = new Attachment();
  132. attachmentThree.setFileName("Beatle.jpg");
  133. attachmentsList.add(attachmentThree);
  134. when(attachmentManager.getLatestVersionsOfAttachments(page)).thenReturn(attachmentsList);
  135. assertEquals(outputText, galleryMacro.execute(params, "", pageContext));
  136. }
  137. private void setMockObjects(GalleryMacro galleryMacro)
  138. {
  139. galleryMacro.setAttachmentManager(attachmentManager);
  140. galleryMacro.setRendererAttachmentManager(rendererAttachmentManager);
  141. galleryMacro.setThumbnailManager(thumbnailManager);
  142. galleryMacro.setLinkResolver(linkResolver);
  143. }
  144. // TODOXHTML CONFDEV-1224
  145. public void TODOXHTML_testIncludeAndExcludeAttachmentWithNoSorting() throws MacroException
  146. {
  147. final List<Attachment> attachmentsList = new ArrayList<Attachment>();
  148. Map<String, String> params;
  149. params = new HashMap<String, String>();
  150. params.put("title", title);
  151. params.put("columns", "3");
  152. params.put("include", "Apple.jpg,Beatle.jpg,Cattle.jpg");
  153. params.put("exclude", "Ants.xml");
  154. GalleryMacro galleryMacro = new GalleryMacro()
  155. {
  156. @Override
  157. protected boolean isThumbnailSupported()
  158. {
  159. return true;
  160. }
  161. @Override
  162. protected VelocityContext newVelocityContext()
  163. {
  164. return velocityContext;
  165. }
  166. @Override
  167. protected Thumbnails createThumbnails(int columns,
  168. List<Attachment> attachments)
  169. {
  170. // Check total size of attachments after filter
  171. assertEquals(3, attachments.size());
  172. // Check whether attachments is not sorted
  173. assertEquals("Cattle.jpg", attachments.get(0).getFileName());
  174. assertEquals("Apple.jpg", attachments.get(1).getFileName());
  175. assertEquals("Beatle.jpg", attachments.get(2).getFileName());
  176. return mockThumbnails;
  177. }
  178. @Override
  179. protected String getRenderedTemplateWithoutSwallowingErrors(
  180. String template, VelocityContext contextMap)
  181. throws Exception
  182. {
  183. // Check whether same mocked thumbnails is returned
  184. assertEquals(mockThumbnails, (Thumbnails) contextMap.get("thumbnails"));
  185. return outputText;
  186. }
  187. };
  188. setMockObjects(galleryMacro);
  189. Page page = new Page();
  190. page.setTitle("Test Page");
  191. page.setBodyAsString("This is page content");
  192. when(pageContext.getEntity()).thenReturn(page);
  193. when(pageContext.getOutputType()).thenReturn(RenderContextOutputType.DISPLAY);
  194. when(linkResolver.createLink(pageContext, "page1")).thenReturn((Link)pageLink);
  195. when(pageLink.getDestinationContent()).thenReturn(page);
  196. Attachment attachmentOne, attachmentTwo, attachmentThree, attachmentFour;
  197. attachmentOne = new Attachment();
  198. attachmentOne.setFileName("Cattle.jpg");
  199. attachmentsList.add(attachmentOne);
  200. attachmentTwo = new Attachment();
  201. attachmentTwo.setFileName("Apple.jpg");
  202. attachmentsList.add(attachmentTwo);
  203. attachmentThree = new Attachment();
  204. attachmentThree.setFileName("Beatle.jpg");
  205. attachmentsList.add(attachmentThree);
  206. attachmentFour = new Attachment();
  207. attachmentFour.setFileName("Ants.xml");
  208. attachmentsList.add(attachmentFour);
  209. when(attachmentManager.getLatestVersionsOfAttachments(page)).thenReturn(attachmentsList);
  210. assertEquals(outputText, galleryMacro.execute(params, "", pageContext));
  211. }
  212. public void testGetName()
  213. {
  214. GalleryMacro galleryMacro = new GalleryMacro();
  215. assertEquals("gallery", galleryMacro.getName());
  216. }
  217. public void testIsInLine()
  218. {
  219. GalleryMacro galleryMacro = new GalleryMacro();
  220. assertFalse(galleryMacro.isInline());
  221. }
  222. public void testHasBody()
  223. {
  224. GalleryMacro galleryMacro = new GalleryMacro();
  225. assertTrue(galleryMacro.hasBody());
  226. }
  227. public void testRenderMode()
  228. {
  229. GalleryMacro galleryMacro = new GalleryMacro();
  230. assertEquals(RenderMode.ALL, galleryMacro.getBodyRenderMode());
  231. }
  232. }