| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.wysiwyg.server.internal.plugin.macro; |
| 21 |
|
|
| 22 |
|
import static org.mockito.ArgumentMatchers.any; |
| 23 |
|
import static org.mockito.Mockito.mock; |
| 24 |
|
import static org.mockito.Mockito.verify; |
| 25 |
|
import static org.mockito.Mockito.when; |
| 26 |
|
|
| 27 |
|
import java.util.Collections; |
| 28 |
|
|
| 29 |
|
import javax.inject.Provider; |
| 30 |
|
|
| 31 |
|
import org.junit.Assert; |
| 32 |
|
import org.junit.Rule; |
| 33 |
|
import org.junit.Test; |
| 34 |
|
import org.mockito.ArgumentCaptor; |
| 35 |
|
import org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroService; |
| 36 |
|
import org.xwiki.rendering.macro.Macro; |
| 37 |
|
import org.xwiki.rendering.macro.MacroId; |
| 38 |
|
import org.xwiki.rendering.macro.MacroManager; |
| 39 |
|
import org.xwiki.rendering.macro.descriptor.ContentDescriptor; |
| 40 |
|
import org.xwiki.rendering.macro.descriptor.MacroDescriptor; |
| 41 |
|
import org.xwiki.rendering.macro.descriptor.ParameterDescriptor; |
| 42 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 43 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
| 44 |
|
import org.xwiki.rendering.syntax.SyntaxType; |
| 45 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 46 |
|
import org.xwiki.wysiwyg.server.plugin.macro.MacroDescriptorTranslator; |
| 47 |
|
|
| 48 |
|
import com.xpn.xwiki.XWikiContext; |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
@link |
| 52 |
|
|
| 53 |
|
@version |
| 54 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (57) |
Complexity: 1 |
Complexity Density: 0.02 |
|
| 55 |
|
public class XWikiMacroServiceTest |
| 56 |
|
{ |
| 57 |
|
|
| 58 |
|
@link |
| 59 |
|
|
| 60 |
|
@Rule |
| 61 |
|
public MockitoComponentMockingRule<MacroService> mocker = new MockitoComponentMockingRule<MacroService>( |
| 62 |
|
XWikiMacroService.class); |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@link |
| 66 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (56) |
Complexity: 1 |
Complexity Density: 0.02 |
1PASS
|
|
| 67 |
1 |
@Test... |
| 68 |
|
public void getMacroDescriptor() throws Exception |
| 69 |
|
{ |
| 70 |
1 |
String macroId = "foo"; |
| 71 |
1 |
String syntaxId = "syntax/x.y"; |
| 72 |
|
|
| 73 |
|
|
| 74 |
1 |
SyntaxFactory syntaxFactory = mocker.getInstance(SyntaxFactory.class); |
| 75 |
1 |
Syntax syntax = new Syntax(SyntaxType.PLAIN, "x.y"); |
| 76 |
1 |
when(syntaxFactory.createSyntaxFromIdString(syntaxId)).thenReturn(syntax); |
| 77 |
|
|
| 78 |
|
|
| 79 |
1 |
ContentDescriptor contentDescriptor = mock(ContentDescriptor.class); |
| 80 |
1 |
when(contentDescriptor.getDescription()).thenReturn("content description"); |
| 81 |
1 |
when(contentDescriptor.isMandatory()).thenReturn(true); |
| 82 |
|
|
| 83 |
1 |
ParameterDescriptor parameterDescriptor = mock(ParameterDescriptor.class); |
| 84 |
1 |
when(parameterDescriptor.getId()).thenReturn("bar"); |
| 85 |
1 |
when(parameterDescriptor.getName()).thenReturn("The Bar"); |
| 86 |
1 |
when(parameterDescriptor.getDescription()).thenReturn("bar description"); |
| 87 |
1 |
when(parameterDescriptor.getDefaultValue()).thenReturn("default value"); |
| 88 |
1 |
when(parameterDescriptor.getParameterType()).thenReturn(Integer.class); |
| 89 |
1 |
when(parameterDescriptor.isMandatory()).thenReturn(true); |
| 90 |
|
|
| 91 |
1 |
MacroId macroIdObject = new MacroId(macroId, syntax); |
| 92 |
1 |
MacroDescriptor descriptor = mock(MacroDescriptor.class); |
| 93 |
1 |
when(descriptor.getId()).thenReturn(macroIdObject); |
| 94 |
1 |
when(descriptor.getName()).thenReturn("The Foo Macro"); |
| 95 |
1 |
when(descriptor.getDescription()).thenReturn("foo description"); |
| 96 |
1 |
when(descriptor.getContentDescriptor()).thenReturn(contentDescriptor); |
| 97 |
1 |
when(descriptor.getParameterDescriptorMap()).thenReturn(Collections.singletonMap("bar", parameterDescriptor)); |
| 98 |
|
|
| 99 |
1 |
Macro macro = mock(Macro.class); |
| 100 |
1 |
when(macro.getDescriptor()).thenReturn(descriptor); |
| 101 |
1 |
when(macro.supportsInlineMode()).thenReturn(true); |
| 102 |
|
|
| 103 |
|
|
| 104 |
1 |
MacroManager macroManager = mocker.getInstance(MacroManager.class); |
| 105 |
1 |
when(macroManager.getMacro(macroIdObject)).thenReturn(macro); |
| 106 |
|
|
| 107 |
|
|
| 108 |
1 |
MacroDescriptorTranslator macroDescriptorTranslator = mocker.getInstance(MacroDescriptorTranslator.class); |
| 109 |
1 |
org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor translatedDescriptor = |
| 110 |
|
new org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor(); |
| 111 |
1 |
when(macroDescriptorTranslator.translate(any(org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor.class))) |
| 112 |
|
.thenReturn(translatedDescriptor); |
| 113 |
|
|
| 114 |
1 |
XWikiContext xcontext = mock(XWikiContext.class); |
| 115 |
1 |
when(xcontext.getWikiId()).thenReturn("xwiki"); |
| 116 |
|
|
| 117 |
1 |
Provider<XWikiContext> contextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
| 118 |
1 |
when(contextProvider.get()).thenReturn(xcontext); |
| 119 |
|
|
| 120 |
1 |
Assert.assertSame(translatedDescriptor, mocker.getComponentUnderTest().getMacroDescriptor(macroId, syntaxId)); |
| 121 |
|
|
| 122 |
1 |
ArgumentCaptor<org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor> argument = |
| 123 |
|
ArgumentCaptor.forClass(org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor.class); |
| 124 |
1 |
verify(macroDescriptorTranslator).translate(argument.capture()); |
| 125 |
1 |
org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor result = argument.getValue(); |
| 126 |
|
|
| 127 |
1 |
Assert.assertEquals(descriptor.getId().getId(), result.getId()); |
| 128 |
1 |
Assert.assertEquals(descriptor.getName(), result.getName()); |
| 129 |
1 |
Assert.assertEquals(descriptor.getDescription(), result.getDescription()); |
| 130 |
1 |
Assert.assertEquals(macro.supportsInlineMode(), result.isSupportingInlineMode()); |
| 131 |
|
|
| 132 |
1 |
Assert.assertEquals("content", result.getContentDescriptor().getId()); |
| 133 |
1 |
Assert.assertEquals("Content", result.getContentDescriptor().getName()); |
| 134 |
1 |
Assert.assertEquals(contentDescriptor.getDescription(), result.getContentDescriptor().getDescription()); |
| 135 |
1 |
Assert.assertNull(result.getContentDescriptor().getDefaultValue()); |
| 136 |
1 |
Assert.assertEquals(contentDescriptor.isMandatory(), result.getContentDescriptor().isMandatory()); |
| 137 |
1 |
Assert.assertEquals("java.lang.StringBuffer", result.getContentDescriptor().getType().getName()); |
| 138 |
|
|
| 139 |
1 |
Assert.assertEquals(1, result.getParameterDescriptorMap().size()); |
| 140 |
1 |
org.xwiki.gwt.wysiwyg.client.plugin.macro.ParameterDescriptor actualParamDescriptor = |
| 141 |
|
result.getParameterDescriptorMap().get(parameterDescriptor.getId()); |
| 142 |
1 |
Assert.assertEquals(parameterDescriptor.getId(), actualParamDescriptor.getId()); |
| 143 |
1 |
Assert.assertEquals(parameterDescriptor.getName(), actualParamDescriptor.getName()); |
| 144 |
1 |
Assert.assertEquals(parameterDescriptor.getDescription(), actualParamDescriptor.getDescription()); |
| 145 |
1 |
Assert.assertEquals(parameterDescriptor.getDefaultValue(), actualParamDescriptor.getDefaultValue()); |
| 146 |
1 |
Assert.assertEquals(parameterDescriptor.isMandatory(), actualParamDescriptor.isMandatory()); |
| 147 |
1 |
Assert.assertEquals("java.lang.Integer", actualParamDescriptor.getType().getName()); |
| 148 |
|
} |
| 149 |
|
} |