PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/confluence-advanced-macros-plugin
Java | 208 lines | 164 code | 44 blank | 0 comment | 0 complexity | 578fbb53bb023a701deb838f377c6fe9 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 com.atlassian.confluence.labels.Label;
  7. import com.atlassian.confluence.util.actions.AlphabeticalLabelGroupingSupport;
  8. import org.mockito.Mock;
  9. import org.mockito.MockitoAnnotations;
  10. import com.atlassian.confluence.labels.LabelManager;
  11. import com.atlassian.confluence.renderer.PageContext;
  12. import com.atlassian.confluence.spaces.Space;
  13. import com.atlassian.confluence.spaces.SpaceManager;
  14. import com.atlassian.renderer.v2.RenderMode;
  15. import com.atlassian.renderer.v2.macro.MacroException;
  16. import junit.framework.TestCase;
  17. import static org.mockito.Mockito.when;
  18. import static org.mockito.Mockito.verify;
  19. import static org.mockito.Mockito.never;
  20. public class ListLabelsMacroTestCase extends TestCase
  21. {
  22. private Map<String, Object> contextMap;
  23. private String returnText = "Execution complete";
  24. private List<Label> listLabels;
  25. @Mock private Map<String, Object> parameters;
  26. @Mock private PageContext pageContext;
  27. @Mock private LabelManager labelManager;
  28. @Mock private SpaceManager spaceManager;
  29. @Override
  30. protected void setUp() throws Exception {
  31. super.setUp();
  32. MockitoAnnotations.initMocks(this);
  33. contextMap = new HashMap<String, Object>();
  34. listLabels = new ArrayList<Label>();
  35. listLabels.add(new Label("telephone"));
  36. listLabels.add(new Label("zebra"));
  37. listLabels.add(new Label("bag"));
  38. listLabels.add(new Label("apple"));
  39. }
  40. public void testListLabelsWithOneSpaceKey() throws MacroException
  41. {
  42. String spaceKey = "ds";
  43. ListLabelsMacro listLabelsMacro = new ListLabelsMacro()
  44. {
  45. @Override
  46. protected Map<String, Object> getDefaultVelocityContext() {
  47. return contextMap;
  48. }
  49. @Override
  50. protected String getRenderedTemplate(Map contextMap) {
  51. AlphabeticalLabelGroupingSupport alphaSupport = (AlphabeticalLabelGroupingSupport) contextMap.get("alphaSupport");
  52. List listContext = alphaSupport.getContents();
  53. assertEquals(listLabels.get(0), listContext.get(0));
  54. assertEquals(listLabels.get(1), listContext.get(1));
  55. assertEquals(listLabels.get(2), listContext.get(2));
  56. assertEquals(listLabels.get(3), listContext.get(3));
  57. assertEquals("ds", (String) contextMap.get("spaceKey"));
  58. return returnText;
  59. }
  60. };
  61. listLabelsMacro.setLabelManager(labelManager);
  62. listLabelsMacro.setSpaceManager(spaceManager);
  63. when(parameters.get("spaceKey")).thenReturn(spaceKey);
  64. when(labelManager.getLabelsInSpace(spaceKey)).thenReturn(listLabels);
  65. verify(spaceManager, never()).getAllSpaces();
  66. assertEquals(returnText, listLabelsMacro.execute(parameters, "", pageContext));
  67. }
  68. public void testAlphabeticalLabelGroupingWithAllSpaceKey() throws MacroException
  69. {
  70. final String spaceKey = "@all";
  71. Space space = new Space();
  72. space.setKey("tst");
  73. List<Space> listSpace = new ArrayList<Space>();
  74. listSpace.add(space);
  75. ListLabelsMacro listLabelsMacro = new TestListLabelsMacro()
  76. {
  77. @Override
  78. protected String getRenderedTemplate(Map contextMap) {
  79. AlphabeticalLabelGroupingSupport alphaSupport = (AlphabeticalLabelGroupingSupport) contextMap.get("alphaSupport");
  80. List listContext = alphaSupport.getContents();
  81. assertEquals(listLabels.get(3), listContext.get(0));
  82. assertEquals(listLabels.get(2), listContext.get(1));
  83. assertEquals(listLabels.get(0), listContext.get(2));
  84. assertEquals(listLabels.get(1), listContext.get(3));
  85. assertEquals(spaceKey, (String) contextMap.get("spaceKey"));
  86. return returnText;
  87. }
  88. };
  89. when(parameters.get("spaceKey")).thenReturn(spaceKey);
  90. when(spaceManager.getAllSpaces()).thenReturn(listSpace);
  91. when(labelManager.getLabelsInSpace("tst")).thenReturn(listLabels);
  92. assertEquals(returnText, listLabelsMacro.execute(parameters, "", pageContext));
  93. }
  94. public void testListLabelsWithoutSpaceKey() throws MacroException
  95. {
  96. final String spaceKey = "tst";
  97. ListLabelsMacro listLabelsMacro = new TestListLabelsMacro()
  98. {
  99. @Override
  100. protected String getRenderedTemplate(Map<String, Object> contextMap) {
  101. AlphabeticalLabelGroupingSupport alphaSupport = (AlphabeticalLabelGroupingSupport) contextMap.get("alphaSupport");
  102. List listContext = alphaSupport.getContents();
  103. assertEquals(listLabels.get(0), listContext.get(0));
  104. assertEquals(listLabels.get(1), listContext.get(1));
  105. assertEquals(listLabels.get(2), listContext.get(2));
  106. assertEquals(listLabels.get(3), listContext.get(3));
  107. assertEquals(spaceKey, (String) contextMap.get("spaceKey"));
  108. return returnText;
  109. }
  110. };
  111. when(pageContext.getSpaceKey()).thenReturn(spaceKey);
  112. when(labelManager.getLabelsInSpace(spaceKey)).thenReturn(listLabels);
  113. verify(spaceManager, never()).getAllSpaces();
  114. assertEquals(returnText, listLabelsMacro.execute(parameters, "", pageContext));
  115. }
  116. public void testListLabelsWithExcludedLabels() throws MacroException
  117. {
  118. final String spaceKey = "tst";
  119. ListLabelsMacro listLabelsMacro = new TestListLabelsMacro()
  120. {
  121. @Override
  122. protected String getRenderedTemplate(Map<String, Object> contextMap) {
  123. AlphabeticalLabelGroupingSupport alphaSupport = (AlphabeticalLabelGroupingSupport) contextMap.get("alphaSupport");
  124. List listContext = alphaSupport.getContents();
  125. assertEquals(listLabels.get(2), listContext.get(0));
  126. assertEquals(listLabels.get(3), listContext.get(1));
  127. assertEquals(spaceKey, (String) contextMap.get("spaceKey"));
  128. return returnText;
  129. }
  130. };
  131. when(pageContext.getSpaceKey()).thenReturn(spaceKey);
  132. when(labelManager.getLabelsInSpace(spaceKey)).thenReturn(listLabels);
  133. verify(spaceManager, never()).getAllSpaces();
  134. when(parameters.get("excludedLabels")).thenReturn(" zebra , telephone ");
  135. assertEquals(returnText, listLabelsMacro.execute(parameters, "", pageContext));
  136. }
  137. public void testIsInline()
  138. {
  139. ListLabelsMacro listLabelsMacro = new TestListLabelsMacro();
  140. assertEquals(false, listLabelsMacro.isInline());
  141. }
  142. public void testHasBody()
  143. {
  144. ListLabelsMacro listLabelsMacro = new TestListLabelsMacro();
  145. assertEquals(false, listLabelsMacro.hasBody());
  146. }
  147. public void testGetBodyRenderMode()
  148. {
  149. ListLabelsMacro listLabelsMacro = new TestListLabelsMacro();
  150. assertEquals(RenderMode.NO_RENDER, listLabelsMacro.getBodyRenderMode());
  151. }
  152. private class TestListLabelsMacro extends ListLabelsMacro
  153. {
  154. public TestListLabelsMacro()
  155. {
  156. setLabelManager(labelManager);
  157. setSpaceManager(spaceManager);
  158. }
  159. @Override
  160. protected Map<String, Object> getDefaultVelocityContext() {
  161. return contextMap;
  162. }
  163. }
  164. }