| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
| 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 |
|
@link |
| 53 |
|
|
| 54 |
|
@version |
| 55 |
|
@since |
| 56 |
|
|
| 57 |
|
@MockingRequirement(value = DefaultMacroManager.class, |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
exceptions = { ComponentManager.class, MacroIdFactory.class, Provider.class }) |
| 61 |
|
@AllComponents |
| |
|
| 96.8% |
Uncovered Elements: 2 (63) |
Complexity: 12 |
Complexity Density: 0.23 |
|
| 62 |
|
public class DefaultMacroManagerTest extends AbstractMockingComponentTestCase<MacroManager> |
| 63 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
| 64 |
|
private class TestInvalidMacro extends AbstractNoParameterMacro |
| 65 |
|
{ |
| 66 |
|
|
| 67 |
|
@param |
| 68 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 69 |
0 |
TestInvalidMacro(String invalidParameter)... |
| 70 |
|
{ |
| 71 |
0 |
super("Invalid Macro"); |
| 72 |
|
} |
| 73 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 74 |
0 |
@Override... |
| 75 |
|
public boolean supportsInlineMode() |
| 76 |
|
{ |
| 77 |
0 |
throw new RuntimeException("Not used"); |
| 78 |
|
} |
| 79 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 80 |
0 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 88 |
1 |
@Test... |
| 89 |
|
public void testMacroExists() throws Exception |
| 90 |
|
{ |
| 91 |
1 |
Assert.assertTrue(getMockedComponent().exists(new MacroId("testsimplemacro"))); |
| 92 |
|
} |
| 93 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 94 |
1 |
@Test... |
| 95 |
|
public void testGetExistingMacro() throws Exception |
| 96 |
|
{ |
| 97 |
1 |
Assert.assertNotNull(getMockedComponent().getMacro(new MacroId("testsimplemacro"))); |
| 98 |
|
} |
| 99 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
| 100 |
1 |
@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 |
|
|
| |
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
| 111 |
1 |
@Test... |
| 112 |
|
public void testGetInvalidMacro() throws Exception |
| 113 |
|
{ |
| 114 |
|
|
| 115 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 130 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 137 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 144 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 161 |
1 |
@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 |
|
|
| 190 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 191 |
1 |
@Test... |
| 192 |
|
public void testInvalidMacroHint() throws Exception |
| 193 |
|
{ |
| 194 |
|
|
| 195 |
|
|
| 196 |
1 |
final ComponentManager mockRootComponentManager = registerMockComponent(ComponentManager.class, "context"); |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
1 |
MacroManager macroManager = getMockedComponent(); |
| 201 |
1 |
final Logger logger = getMockLogger(); |
| 202 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 203 |
1 |
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 |
|
|
| 210 |
1 |
oneOf(logger).warn("Invalid Macro descriptor format for hint " |
| 211 |
|
+ "[macro/invalidsyntax]. 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."); |
| 215 |
|
}}); |
| 216 |
|
|
| 217 |
1 |
SyntaxFactory syntaxFactory = getComponentManager().getInstance(SyntaxFactory.class); |
| 218 |
1 |
macroManager.getMacroIds(syntaxFactory.createSyntaxFromIdString("macro/xwiki/2.0")); |
| 219 |
|
} |
| 220 |
|
} |