PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/confluence-advanced-macros-plugin
Java | 352 lines | 274 code | 78 blank | 0 comment | 0 complexity | 8c7e27f9d338ebc4dc1b1e03c52f4fb3 MD5 | raw file
  1. package com.atlassian.confluence.plugins.macros.advanced;
  2. import com.atlassian.confluence.core.ContentEntityObject;
  3. import com.atlassian.confluence.core.FormatSettingsManager;
  4. import com.atlassian.confluence.core.TimeZone;
  5. import com.atlassian.confluence.labels.Label;
  6. import com.atlassian.confluence.labels.LabelManager;
  7. import com.atlassian.confluence.labels.Labelling;
  8. import com.atlassian.confluence.languages.LocaleManager;
  9. import com.atlassian.confluence.pages.Page;
  10. import com.atlassian.confluence.spaces.Space;
  11. import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
  12. import com.atlassian.confluence.user.ConfluenceUser;
  13. import com.atlassian.confluence.user.ConfluenceUserImpl;
  14. import com.atlassian.confluence.user.ConfluenceUserPreferences;
  15. import com.atlassian.confluence.user.UserAccessor;
  16. import com.atlassian.renderer.v2.RenderMode;
  17. import com.atlassian.renderer.v2.macro.MacroException;
  18. import com.atlassian.user.User;
  19. import junit.framework.TestCase;
  20. import org.mockito.Mock;
  21. import static org.mockito.Mockito.anyObject;
  22. import static org.mockito.Mockito.when;
  23. import org.mockito.MockitoAnnotations;
  24. import java.util.Arrays;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Map;
  29. public class RecentlyUsedLabelsMacroTestCase extends TestCase
  30. {
  31. @Mock private LabelManager labelManager;
  32. @Mock private UserAccessor userAccessor;
  33. @Mock private FormatSettingsManager formatSettingsManager;
  34. @Mock private LocaleManager localeManager;
  35. @Mock private ConfluenceUserPreferences confluenceUserPreferences;
  36. private RecentlyUsedLabelsMacro recentlyUsedLabelsMacro;
  37. private Map<String, String> macroParams;
  38. private Space space;
  39. private Page pageToRenderOn;
  40. private Map<String, Object> macroVelocityContext;
  41. private ConfluenceUser user;
  42. private Label labelOne;
  43. private Label personalLabelOne;
  44. private ContentEntityObject labelledContent;
  45. @Override
  46. protected void setUp() throws Exception
  47. {
  48. super.setUp();
  49. MockitoAnnotations.initMocks(this);
  50. when(userAccessor.getConfluenceUserPreferences((User) anyObject())).thenReturn(confluenceUserPreferences);
  51. when(confluenceUserPreferences.getTimeZone()).thenReturn(TimeZone.getDefault());
  52. when(formatSettingsManager.getDateFormat()).thenReturn("dd/MM/yyyy");
  53. when(formatSettingsManager.getDateTimeFormat()).thenReturn("dd/MM/yyyy HH:mm");
  54. when(formatSettingsManager.getTimeFormat()).thenReturn("HH:mm:ss SSS");
  55. when(localeManager.getLocale((User) anyObject())).thenReturn(Locale.UK);
  56. recentlyUsedLabelsMacro = new TestRecentlyUsedLabelsMacro();
  57. macroParams = new HashMap<String, String>();
  58. space = new Space("ds");
  59. pageToRenderOn = new Page();
  60. pageToRenderOn.setSpace(space);
  61. macroVelocityContext = new HashMap<String, Object>();
  62. user = new ConfluenceUserImpl("admin", null, null);
  63. labelOne = new Label("labelone");
  64. labelOne.setId(1);
  65. personalLabelOne = new Label("my:labelone");
  66. personalLabelOne.setId(1);
  67. labelledContent = new Page();
  68. labelledContent.setId(1);
  69. }
  70. public void testDefaultResultCountIsTen() throws MacroException
  71. {
  72. final List<Label> labels = Arrays.asList(labelOne);
  73. when(labelManager.getRecentlyUsedLabels(10)).thenReturn(labels);
  74. final String fakeOutput = "fakeOutput";
  75. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  76. {
  77. @Override
  78. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  79. {
  80. assertEquals(labels, contextMap.get("recentlyUsedLabels"));
  81. return fakeOutput;
  82. }
  83. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  84. }
  85. public void testSetTitle() throws MacroException
  86. {
  87. final String fakeOutput = "fakeOutput";
  88. final String title = "title";
  89. macroParams.put("title", title);
  90. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  91. {
  92. @Override
  93. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  94. {
  95. assertEquals(title, contextMap.get("title"));
  96. return fakeOutput;
  97. }
  98. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  99. }
  100. public void testResultCountCannotBeMoreThanOneHundred() throws MacroException
  101. {
  102. final List<Label> labels = Arrays.asList(labelOne);
  103. when(labelManager.getRecentlyUsedLabels(100)).thenReturn(labels);
  104. final String fakeOutput = "fakeOutput";
  105. macroParams.put("count", String.valueOf(Integer.MAX_VALUE));
  106. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  107. {
  108. @Override
  109. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  110. {
  111. assertEquals(labels, contextMap.get("recentlyUsedLabels"));
  112. return fakeOutput;
  113. }
  114. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  115. }
  116. public void testSpaceScopeLimitsRecentLabelsOfParticularSpace() throws MacroException
  117. {
  118. final List<Label> labels = Arrays.asList(labelOne);
  119. when(labelManager.getRecentlyUsedLabelsInSpace(space.getKey(), 10)).thenReturn(labels);
  120. final String fakeOutput = "fakeOutput";
  121. macroParams.put("scope", "space");
  122. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  123. {
  124. @Override
  125. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  126. {
  127. assertEquals(labels, contextMap.get("recentlyUsedLabels"));
  128. return fakeOutput;
  129. }
  130. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  131. }
  132. public void testPersonalScopeLimitsRecentLabelsOfParticularPerson() throws MacroException
  133. {
  134. final List<Label> labels = Arrays.asList(personalLabelOne);
  135. try
  136. {
  137. AuthenticatedUserThreadLocal.setUser(user);
  138. when(labelManager.getRecentlyUsedPersonalLabels(user.getName(), 10)).thenReturn(labels);
  139. final String fakeOutput = "fakeOutput";
  140. macroParams.put("scope", "personal");
  141. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  142. {
  143. @Override
  144. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  145. {
  146. assertEquals(labels, contextMap.get("recentlyUsedLabels"));
  147. return fakeOutput;
  148. }
  149. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  150. }
  151. finally
  152. {
  153. AuthenticatedUserThreadLocal.setUser(null);
  154. }
  155. }
  156. public void testPersonalScopeOfRecentlyUsedLabelsDefaultsToGlobalForAnonymousUsers() throws MacroException
  157. {
  158. final List<Label> labels = Arrays.asList(personalLabelOne);
  159. when(labelManager.getRecentlyUsedLabels(10)).thenReturn(labels);
  160. final String fakeOutput = "fakeOutput";
  161. macroParams.put("scope", "personal");
  162. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  163. {
  164. @Override
  165. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  166. {
  167. assertEquals(labels, contextMap.get("recentlyUsedLabels"));
  168. return fakeOutput;
  169. }
  170. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  171. }
  172. public void testDefaultScopeOfRecentLabellingsIsGlobal() throws MacroException
  173. {
  174. final List<Labelling> labellings = Arrays.asList(new Labelling(labelOne, labelledContent, user));
  175. when(labelManager.getRecentlyUsedLabellings(10)).thenReturn(labellings);
  176. final String fakeOutput = "fakeOutput";
  177. macroParams.put("scope", "personal");
  178. macroParams.put("style", "table");
  179. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  180. {
  181. @Override
  182. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  183. {
  184. assertEquals(labellings, contextMap.get("recentlyUsedLabellings"));
  185. return fakeOutput;
  186. }
  187. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  188. }
  189. public void testSpaceScopeLimitsRecentLabellingsOfParticularSpaceInTabularFormat() throws MacroException
  190. {
  191. final List<Labelling> labellings = Arrays.asList(new Labelling(labelOne, labelledContent, user));
  192. when(labelManager.getRecentlyUsedLabellingsInSpace(space.getKey(), 10)).thenReturn(labellings);
  193. final String fakeOutput = "fakeOutput";
  194. macroParams.put("scope", "space");
  195. macroParams.put("style", "table");
  196. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  197. {
  198. @Override
  199. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  200. {
  201. assertEquals(labellings, contextMap.get("recentlyUsedLabellings"));
  202. return fakeOutput;
  203. }
  204. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  205. }
  206. public void testPersonalScopeLimitsRecentLabellingsOfParticularPerson() throws MacroException
  207. {
  208. final List<Labelling> labellings = Arrays.asList(new Labelling(personalLabelOne, labelledContent, user));
  209. try
  210. {
  211. AuthenticatedUserThreadLocal.setUser(user);
  212. when(labelManager.getRecentlyUsedPersonalLabellings(user.getName(), 10)).thenReturn(labellings);
  213. final String fakeOutput = "fakeOutput";
  214. macroParams.put("scope", "personal");
  215. macroParams.put("style", "table");
  216. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  217. {
  218. @Override
  219. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  220. {
  221. assertEquals(labellings, contextMap.get("recentlyUsedLabellings"));
  222. return fakeOutput;
  223. }
  224. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  225. }
  226. finally
  227. {
  228. AuthenticatedUserThreadLocal.setUser(null);
  229. }
  230. }
  231. public void testPersonalScopeOfRecentLabellingsDefaultsToGlobalForAnonymousUsers() throws MacroException
  232. {
  233. final List<Labelling> labellings = Arrays.asList(new Labelling(personalLabelOne, labelledContent, user));
  234. when(labelManager.getRecentlyUsedLabellings(10)).thenReturn(labellings);
  235. final String fakeOutput = "fakeOutput";
  236. macroParams.put("scope", "personal");
  237. macroParams.put("style", "table");
  238. assertEquals(fakeOutput, new TestRecentlyUsedLabelsMacro()
  239. {
  240. @Override
  241. protected String renderRecentlyUsedLabels(Map<String, Object> contextMap, String template)
  242. {
  243. assertEquals(labellings, contextMap.get("recentlyUsedLabellings"));
  244. return fakeOutput;
  245. }
  246. }.execute(macroParams, null, pageToRenderOn.toPageContext()));
  247. }
  248. public void testDoesNotRenderBody()
  249. {
  250. assertEquals(RenderMode.NO_RENDER, recentlyUsedLabelsMacro.getBodyRenderMode());
  251. }
  252. public void testHasNoBody()
  253. {
  254. assertFalse(recentlyUsedLabelsMacro.hasBody());
  255. }
  256. public void testRendersBlock()
  257. {
  258. assertFalse(recentlyUsedLabelsMacro.isInline());
  259. }
  260. private class TestRecentlyUsedLabelsMacro extends RecentlyUsedLabelsMacro
  261. {
  262. private TestRecentlyUsedLabelsMacro()
  263. {
  264. setLabelManager(labelManager);
  265. setUserAccessor(userAccessor);
  266. setFormatSettingsManager(formatSettingsManager);
  267. }
  268. @Override
  269. protected Map<String, Object> getMacroVelocityContext()
  270. {
  271. return macroVelocityContext;
  272. }
  273. }
  274. }