1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.localization.script

File LocalizationScriptServiceTest.java

 

Code metrics

0
29
9
1
130
92
9
0.31
3.22
9
1

Classes

Class Line # Actions
LocalizationScriptServiceTest 47 29 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 5 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.localization.script;
21   
22    import java.util.Arrays;
23    import java.util.Locale;
24   
25    import javax.inject.Provider;
26   
27    import org.apache.commons.lang3.ArrayUtils;
28    import org.jmock.Expectations;
29    import org.jmock.api.Invocation;
30    import org.jmock.lib.action.CustomAction;
31    import org.junit.Assert;
32    import org.junit.Test;
33    import org.xwiki.component.internal.ContextComponentManagerProvider;
34    import org.xwiki.localization.LocalizationContext;
35    import org.xwiki.localization.LocalizationManager;
36    import org.xwiki.localization.Translation;
37    import org.xwiki.rendering.block.WordBlock;
38    import org.xwiki.rendering.renderer.BlockRenderer;
39    import org.xwiki.rendering.renderer.printer.WikiPrinter;
40    import org.xwiki.rendering.syntax.Syntax;
41    import org.xwiki.test.annotation.ComponentList;
42    import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
43    import org.xwiki.test.jmock.annotation.MockingRequirement;
44   
45    @MockingRequirement(value = LocalizationScriptService.class, exceptions = {Provider.class})
46    @ComponentList({ContextComponentManagerProvider.class})
 
47    public class LocalizationScriptServiceTest extends AbstractMockingComponentTestCase<LocalizationScriptService>
48    {
 
49  4 toggle public void prepareRenderTest() throws Exception
50    {
51  4 final BlockRenderer renderer = registerMockComponent(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString());
52   
53  4 final LocalizationManager localizationManager = getComponentManager().getInstance(LocalizationManager.class);
54  4 final LocalizationContext localizationContext = getComponentManager().getInstance(LocalizationContext.class);
55  4 final Translation translation = getMockery().mock(Translation.class);
56  4 getMockery().checking(new Expectations()
57    {
 
58  4 toggle {
59  4 oneOf(renderer).render(with(equal(new WordBlock("message"))), with(any(WikiPrinter.class)));
60  4 will(new CustomAction("render")
61    {
 
62  4 toggle @Override
63    public Object invoke(Invocation invocation) throws Throwable
64    {
65  4 WikiPrinter printer = (WikiPrinter) invocation.getParameter(1);
66  4 printer.print("print result");
67   
68  4 return null;
69    }
70    });
71   
72  4 oneOf(translation).render(Locale.ROOT, ArrayUtils.EMPTY_OBJECT_ARRAY);
73  4 will(returnValue(new WordBlock("message")));
74   
75  4 oneOf(localizationManager).getTranslation("key", Locale.ROOT);
76  4 will(returnValue(translation));
77   
78  4 oneOf(localizationContext).getCurrentLocale();
79  4 will(returnValue(Locale.ROOT));
80    }
81    });
82    }
83   
 
84  1 toggle @Test
85    public void render() throws Exception
86    {
87  1 prepareRenderTest();
88   
89  1 Assert.assertEquals("print result", getMockedComponent().render("key"));
90    }
91   
 
92  1 toggle @Test
93    public void renderWithSyntax() throws Exception
94    {
95  1 prepareRenderTest();
96   
97  1 Assert.assertEquals("print result", getMockedComponent().render("key", Syntax.PLAIN_1_0));
98    }
99   
 
100  1 toggle @Test
101    public void renderWithSyntaxAndParameters() throws Exception
102    {
103  1 prepareRenderTest();
104   
105  1 Assert.assertEquals("print result", getMockedComponent().render("key", Syntax.PLAIN_1_0, Arrays.asList()));
106    }
107   
 
108  1 toggle @Test
109    public void renderWithParameters() throws Exception
110    {
111  1 prepareRenderTest();
112   
113  1 Assert.assertEquals("print result", getMockedComponent().render("key", Arrays.asList()));
114    }
115   
 
116  1 toggle @Test
117    public void getCurrentLocale() throws Exception
118    {
119  1 final LocalizationContext localizationContext = getComponentManager().getInstance(LocalizationContext.class);
120  1 getMockery().checking(new Expectations()
121    {
 
122  1 toggle {
123  1 oneOf(localizationContext).getCurrentLocale();
124  1 will(returnValue(Locale.ENGLISH));
125    }
126    });
127   
128  1 Assert.assertEquals(Locale.ENGLISH, getMockedComponent().getCurrentLocale());
129    }
130    }