1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro

File DefaultMacroCategoryManagerTest.java

 

Code metrics

0
40
6
1
140
83
6
0.15
6.67
6
1

Classes

Class Line # Actions
DefaultMacroCategoryManagerTest 43 40 0% 6 0
1.0100%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.macro;
21   
22    import java.util.Set;
23   
24    import org.jmock.Expectations;
25    import org.junit.Assert;
26    import org.junit.Before;
27    import org.junit.Test;
28    import org.xwiki.rendering.internal.transformation.macro.DefaultMacroTransformationConfiguration;
29    import org.xwiki.rendering.macro.Macro;
30    import org.xwiki.rendering.macro.MacroCategoryManager;
31    import org.xwiki.rendering.macro.MacroId;
32    import org.xwiki.rendering.macro.descriptor.DefaultMacroDescriptor;
33    import org.xwiki.rendering.syntax.Syntax;
34    import org.xwiki.rendering.transformation.macro.MacroTransformationConfiguration;
35    import org.xwiki.test.jmock.AbstractComponentTestCase;
36   
37    /**
38    * Unit tests for {@link org.xwiki.rendering.macro.MacroCategoryManager}.
39    *
40    * @version $Id: 7d6b18850531b3a0439dadaf93c96934b9f7cab2 $
41    * @since 2.0M3
42    */
 
43    public class DefaultMacroCategoryManagerTest extends AbstractComponentTestCase
44    {
45    private MacroCategoryManager macroCategoryManager;
46   
 
47  3 toggle @Override
48    @Before
49    public void setUp() throws Exception
50    {
51  3 super.setUp();
52  3 this.macroCategoryManager = getComponentManager().getInstance(MacroCategoryManager.class);
53    }
54   
 
55  1 toggle @Test
56    public void testGetMacroCategories() throws Exception
57    {
58    // TODO: This test needs to be improved. Right now it's based on the Test Macro located in the transformation
59    // package and for 4 of them a "Test" category has been set...
60  1 DefaultMacroTransformationConfiguration configuration =
61    (DefaultMacroTransformationConfiguration) getComponentManager().getInstance(
62    MacroTransformationConfiguration.class);
63  1 configuration.addCategory(new MacroId("testcontentmacro"), "Content");
64  1 configuration.addCategory(new MacroId("testsimplemacro"), "Simple");
65   
66  1 Set<String> macroCategories = this.macroCategoryManager.getMacroCategories();
67   
68    // Check for a default category.
69  1 Assert.assertTrue(macroCategories.contains("Test"));
70   
71    // Check for null category.
72  1 Assert.assertTrue(macroCategories.contains(null));
73   
74    // Check for overwritten categories.
75  1 Assert.assertTrue(macroCategories.contains("Content"));
76  1 Assert.assertTrue(macroCategories.contains("Simple"));
77    }
78   
 
79  1 toggle @Test
80    public void testGetMacroNamesForCategory() throws Exception
81    {
82    // Create two mock macros and register them macros against the CM as macros registered for all syntaxes.
83  1 final Macro testMacro1 = registerMockComponent(Macro.class, "mytestmacro1", "mock1");
84  1 final Macro testMacro2 = registerMockComponent(Macro.class, "mytestmacro2", "mock2");
 
85  1 toggle getMockery().checking(new Expectations(){{
86  1 allowing(testMacro1).getDescriptor();
87  1 will(returnValue(new DefaultMacroDescriptor(new MacroId("macro1"), "Test macro - 1")));
88  1 allowing(testMacro2).getDescriptor();
89  1 will(returnValue(new DefaultMacroDescriptor(new MacroId("macro2"), "Test macro - 2")));
90    }});
91   
92    // Override default macro categories.
93  1 DefaultMacroTransformationConfiguration configuration =
94    (DefaultMacroTransformationConfiguration) getComponentManager().getInstance(
95    MacroTransformationConfiguration.class);
96  1 configuration.addCategory(new MacroId("mytestmacro1"), "Cat1");
97  1 configuration.addCategory(new MacroId("mytestmacro2"), "Cat2");
98   
99    // Check whether our macros are registered under correct categories.
100  1 Set<MacroId> macroIds = this.macroCategoryManager.getMacroIds("Cat1");
101  1 Assert.assertTrue(macroIds.contains(new MacroId("mytestmacro1")));
102  1 Assert.assertFalse(macroIds.contains(new MacroId("mytestmacro2")));
103   
104    // These macros should be registered for all syntaxes.
105  1 macroIds = this.macroCategoryManager.getMacroIds("Cat1", Syntax.JSPWIKI_1_0);
106  1 Assert.assertTrue(macroIds.contains(new MacroId("mytestmacro1")));
107   
108    // Finally, unregister test macros.
109  1 getComponentManager().unregisterComponent(Macro.class, "mytestmacro1");
110  1 getComponentManager().unregisterComponent(Macro.class, "mytestmacro2");
111    }
112   
 
113  1 toggle @Test
114    public void testGetMacroIdsWithSyntaxSpecificMacros() throws Exception
115    {
116    // Create a mock macro and register it against CM as a xwiki/2.0 specific macro.
117  1 final Macro mockMacro = registerMockComponent(Macro.class, "mytestmacro/xwiki/2.0");
 
118  1 toggle getMockery().checking(new Expectations(){{
119  1 allowing(mockMacro).getDescriptor();
120  1 will(returnValue(new DefaultMacroDescriptor(new MacroId("test"), "Test macro")));
121    }});
122   
123    // Override the macro category for this macro.
124  1 DefaultMacroTransformationConfiguration configuration =
125    (DefaultMacroTransformationConfiguration) getComponentManager().getInstance(
126    MacroTransformationConfiguration.class);
127  1 configuration.addCategory(new MacroId("mytestmacro", Syntax.XWIKI_2_0), "Test");
128   
129    // Make sure our macro is put into the correct category & registered under correct syntax.
130  1 Set<MacroId> macroIds = this.macroCategoryManager.getMacroIds("Test");
131  1 Assert.assertTrue(macroIds.contains(new MacroId("mytestmacro", Syntax.XWIKI_2_0)));
132  1 macroIds = this.macroCategoryManager.getMacroIds("Test", Syntax.XWIKI_2_0);
133  1 Assert.assertTrue(macroIds.contains(new MacroId("mytestmacro", Syntax.XWIKI_2_0)));
134  1 macroIds = this.macroCategoryManager.getMacroIds("Test", Syntax.JSPWIKI_1_0);
135  1 Assert.assertFalse(macroIds.contains(new MacroId("mytestmacro")));
136   
137    // Finally, unregister the test macro.
138  1 getComponentManager().unregisterComponent(Macro.class, "mytestmacro/xwiki/2.0");
139    }
140    }