1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.macro

File DefaultMacroManagerTest.java

 

Code metrics

0
55
13
2
219
151
15
0.27
4.23
6.5
1.15

Classes

Class Line # Actions
DefaultMacroManagerTest 62 52 0% 12 2
0.967741996.8%
DefaultMacroManagerTest.TestInvalidMacro 64 3 0% 3 6
0.00%
 

Contributing tests

This file is covered by 9 tests. .

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.Collections;
23    import java.util.List;
24   
25    import javax.inject.Provider;
26   
27    import org.jmock.Expectations;
28    import org.junit.Assert;
29    import org.junit.Test;
30    import org.slf4j.Logger;
31    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.rendering.block.Block;
34    import org.xwiki.rendering.internal.transformation.macro.TestSimpleMacro;
35    import org.xwiki.rendering.macro.AbstractNoParameterMacro;
36    import org.xwiki.rendering.macro.Macro;
37    import org.xwiki.rendering.macro.MacroExecutionException;
38    import org.xwiki.rendering.macro.MacroId;
39    import org.xwiki.rendering.macro.MacroIdFactory;
40    import org.xwiki.rendering.macro.MacroLookupException;
41    import org.xwiki.rendering.macro.MacroManager;
42    import org.xwiki.rendering.macro.MacroNotFoundException;
43    import org.xwiki.rendering.syntax.Syntax;
44   
45    import org.xwiki.rendering.syntax.SyntaxType;
46    import org.xwiki.rendering.transformation.MacroTransformationContext;
47    import org.xwiki.test.annotation.AllComponents;
48    import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
49    import org.xwiki.test.jmock.annotation.MockingRequirement;
50   
51    /**
52    * Unit tests for {@link org.xwiki.rendering.internal.macro.DefaultMacroManager}.
53    *
54    * @version $Id: 0d7022384355de481110cab620f4f02dc0b81c2f $
55    * @since 1.9M1
56    */
57    @MockingRequirement(value = DefaultMacroManager.class,
58    // Mock all required components except for some for which we want to use the real implementations since they make
59    // the test easier to write (no need to mock them).
60    exceptions = { ComponentManager.class, MacroIdFactory.class, Provider.class })
61    @AllComponents
 
62    public class DefaultMacroManagerTest extends AbstractMockingComponentTestCase<MacroManager>
63    {
 
64    private class TestInvalidMacro extends AbstractNoParameterMacro
65    {
66    /**
67    * @param invalidParameter a parameter that shouldn't be there in a component
68    */
 
69  0 toggle TestInvalidMacro(String invalidParameter)
70    {
71  0 super("Invalid Macro");
72    }
73   
 
74  0 toggle @Override
75    public boolean supportsInlineMode()
76    {
77  0 throw new RuntimeException("Not used");
78    }
79   
 
80  0 toggle @Override
81    public List<Block> execute(Object parameters, String content, MacroTransformationContext context)
82    throws MacroExecutionException
83    {
84  0 throw new RuntimeException("Not used");
85    }
86    }
87   
 
88  1 toggle @Test
89    public void testMacroExists() throws Exception
90    {
91  1 Assert.assertTrue(getMockedComponent().exists(new MacroId("testsimplemacro")));
92    }
93   
 
94  1 toggle @Test
95    public void testGetExistingMacro() throws Exception
96    {
97  1 Assert.assertNotNull(getMockedComponent().getMacro(new MacroId("testsimplemacro")));
98    }
99   
 
100  1 toggle @Test
101    public void testGetNotExistingMacro() throws Exception
102    {
103  1 try {
104  1 getMockedComponent().getMacro(new MacroId("notregisteredmacro"));
105  0 Assert.fail("Expected a MacroNotFoundException when looking for not registered macro");
106    } catch (MacroNotFoundException expected) {
107  1 Assert.assertEquals("No macro [notregisteredmacro] could be found.", expected.getMessage());
108    }
109    }
110   
 
111  1 toggle @Test
112    public void testGetInvalidMacro() throws Exception
113    {
114    // Register the macro. note that we don't register it in components.txt since it would cause some errors in
115    // other tests.
116  1 DefaultComponentDescriptor<Macro> cd = new DefaultComponentDescriptor<Macro>();
117  1 cd.setRoleType(Macro.class);
118  1 cd.setRoleHint("testinvalidmacro");
119  1 cd.setImplementation(TestInvalidMacro.class);
120  1 getComponentManager().registerComponent(cd);
121   
122  1 try {
123  1 getMockedComponent().getMacro(new MacroId("testinvalidmacro"));
124  0 Assert.fail("Expected a MacroLookupException when looking for an invalid macro");
125    } catch (MacroLookupException expected) {
126  1 Assert.assertEquals("Macro [testinvalidmacro] failed to be instantiated.", expected.getMessage());
127    }
128    }
129   
 
130  1 toggle @Test
131    public void testSyntaxSpecificMacroExistsWhenMacroIsRegisteredForAllSyntaxes() throws Exception
132    {
133  1 Assert.assertFalse(getMockedComponent().exists(new MacroId("testsimplemacro",
134    new Syntax(SyntaxType.XWIKI, "2.0"))));
135    }
136   
 
137  1 toggle @Test
138    public void testGetExistingMacroForASpecificSyntaxWhenMacroIsRegisteredForAllSyntaxes() throws Exception
139    {
140  1 Assert.assertNotNull(getMockedComponent().getMacro(new MacroId("testsimplemacro",
141    new Syntax(SyntaxType.XWIKI, "2.0"))));
142    }
143   
 
144  1 toggle @Test
145    public void testMacroRegisteredForAGivenSyntaxOnly() throws Exception
146    {
147  1 Macro<?> macro = new TestSimpleMacro();
148  1 DefaultComponentDescriptor<Macro> descriptor = new DefaultComponentDescriptor<Macro>();
149  1 descriptor.setRole(Macro.class);
150  1 descriptor.setRoleHint("macro/xwiki/2.0");
151  1 getComponentManager().registerComponent(descriptor, macro);
152   
153  1 Assert.assertFalse(getMockedComponent().exists(new MacroId("macro")));
154  1 Assert.assertTrue(getMockedComponent().exists(new MacroId("macro", new Syntax(SyntaxType.XWIKI, "2.0"))));
155   
156  1 Macro<?> macroResult = getMockedComponent().getMacro(
157    new MacroId("macro", new Syntax(SyntaxType.XWIKI, "2.0")));
158  1 Assert.assertSame(macro, macroResult);
159    }
160   
 
161  1 toggle @Test
162    public void testMacroRegisteredForAGivenSyntaxOverridesMacroRegisteredForAllSyntaxes() throws Exception
163    {
164  1 Macro<?> macro1 = new TestSimpleMacro();
165  1 Macro<?> macro2 = new TestSimpleMacro();
166   
167  1 DefaultComponentDescriptor<Macro> descriptor = new DefaultComponentDescriptor<Macro>();
168  1 descriptor.setRole(Macro.class);
169  1 descriptor.setRoleHint("macro");
170  1 getComponentManager().registerComponent(descriptor, macro1);
171   
172  1 descriptor = new DefaultComponentDescriptor<Macro>();
173  1 descriptor.setRole(Macro.class);
174  1 descriptor.setRoleHint("macro/xwiki/2.0");
175  1 getComponentManager().registerComponent(descriptor, macro2);
176   
177  1 Assert.assertTrue(getMockedComponent().exists(new MacroId("macro")));
178  1 Assert.assertTrue(getMockedComponent().exists(new MacroId("macro", new Syntax(SyntaxType.XWIKI, "2.0"))));
179   
180  1 Macro<?> macroResult1 = getMockedComponent().getMacro(
181    new MacroId("macro", new Syntax(SyntaxType.XWIKI, "2.0")));
182  1 Assert.assertSame(macro2, macroResult1);
183   
184  1 Macro<?> macroResult2 = getMockedComponent().getMacro(new MacroId("macro"));
185  1 Assert.assertSame(macro1, macroResult2);
186    }
187   
188    /**
189    * Tests what happens when a macro is registered with an invalid hint.
190    */
 
191  1 toggle @Test
192    public void testInvalidMacroHint() throws Exception
193    {
194    // Control the list of macros found in the system by replacing the real ComponentManager in MacroManager with
195    // a mock one.
196  1 final ComponentManager mockRootComponentManager = registerMockComponent(ComponentManager.class, "context");
197   
198    // Note: Make sure to get the mocked component before calling getMockLogger() since this is what injects the
199    // mock loggers...
200  1 MacroManager macroManager = getMockedComponent();
201  1 final Logger logger = getMockLogger();
202   
 
203  1 toggle getMockery().checking(new Expectations() {{
204  1 allowing(mockRootComponentManager).getInstance(ComponentManager.class, "context");
205  1 will(returnValue(mockRootComponentManager));
206  1 allowing(mockRootComponentManager).getInstanceMap(Macro.class);
207  1 will(returnValue(Collections.singletonMap("macro/invalidsyntax", "dummy")));
208   
209    // Test: Make sure the logger is called with the following content. This is the assert for this test.
210  1 oneOf(logger).warn("Invalid Macro descriptor format for hint "
211    + "[{}]. The hint should contain either the macro name only or the macro name "
212    + "followed by the syntax for which it is valid. In that case the macro name should be followed by "
213    + "a \"/\" followed by the syntax name followed by another \"/\" followed by the syntax version. "
214    + "For example \"html/xwiki/2.0\". This macro will not be available in the system.", "macro/invalidsyntax");
215    }});
216   
217  1 macroManager.getMacroIds(Syntax.valueOf("macro/xwiki/2.0"));
218    }
219    }