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

File XWikiMacroServiceTest.java

 

Code metrics

0
56
1
1
149
94
1
0.02
56
1
1

Classes

Class Line # Actions
XWikiMacroServiceTest 55 56 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.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    * Unit tests for {@link XWikiMacroService}.
52    *
53    * @version $Id: 69e909128950d73cb4e9d66b92137ae642e17525 $
54    */
 
55    public class XWikiMacroServiceTest
56    {
57    /**
58    * A component manager that automatically mocks all dependencies of {@link XWikiMacroService}.
59    */
60    @Rule
61    public MockitoComponentMockingRule<MacroService> mocker = new MockitoComponentMockingRule<MacroService>(
62    XWikiMacroService.class);
63   
64    /**
65    * Unit test for {@link XWikiMacroService#getMacroDescriptor(String, String)}.
66    */
 
67  1 toggle @Test
68    public void getMacroDescriptor() throws Exception
69    {
70  1 String macroId = "foo";
71  1 String syntaxId = "syntax/x.y";
72   
73    // Verify that the specified syntax is used.
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    // Mock the macro descriptor.
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    // Verify that the specified macro is retrieved.
104  1 MacroManager macroManager = mocker.getInstance(MacroManager.class);
105  1 when(macroManager.getMacro(macroIdObject)).thenReturn(macro);
106   
107    // Verify the macro descriptor is translated.
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    }