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

File DefaultTranslationBundleContextTest.java

 

Code metrics

0
41
4
1
169
93
4
0.1
10.25
4
1

Classes

Class Line # Actions
DefaultTranslationBundleContextTest 54 41 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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.internal;
21   
22    import java.util.Arrays;
23    import java.util.Collection;
24    import java.util.List;
25   
26    import javax.inject.Provider;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.component.util.DefaultParameterizedType;
33    import org.xwiki.context.Execution;
34    import org.xwiki.context.ExecutionContext;
35    import org.xwiki.localization.TranslationBundle;
36    import org.xwiki.localization.TranslationBundleContext;
37    import org.xwiki.model.ModelContext;
38    import org.xwiki.model.reference.WikiReference;
39    import org.xwiki.test.mockito.MockitoComponentMockingRule;
40   
41    import static org.junit.Assert.assertEquals;
42    import static org.junit.Assert.assertNotEquals;
43    import static org.junit.Assert.assertNotNull;
44    import static org.junit.Assert.assertTrue;
45    import static org.mockito.Mockito.mock;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * Unit tests for {@link DefaultTranslationBundleContext}.
50    *
51    * @version $Id: aea01895d7d08e3d4884a37db337e8f23f897ff3 $
52    * @since 8.0RC1
53    */
 
54    public class DefaultTranslationBundleContextTest
55    {
56    @Rule
57    public final MockitoComponentMockingRule<TranslationBundleContext> mocker =
58    new MockitoComponentMockingRule<TranslationBundleContext>(DefaultTranslationBundleContext.class);
59   
60    private ExecutionContext mockExecutionContext;
61   
62    private ModelContext mockModelContext;
63   
64    private Provider<ComponentManager> mockContextComponentManagerProvider;
65   
 
66  3 toggle @Before
67    public void before() throws Exception
68    {
69  3 this.mockExecutionContext = new ExecutionContext();
70   
71  3 Execution mockExecution = this.mocker.getInstance(Execution.class);
72  3 when(mockExecution.getContext()).thenReturn(mockExecutionContext);
73   
74  3 this.mockModelContext = this.mocker.getInstance(ModelContext.class);
75  3 when(this.mockModelContext.getCurrentEntityReference()).thenReturn(new WikiReference("currentWiki"));
76   
77  3 this.mockContextComponentManagerProvider =
78    this.mocker.registerMockComponent(
79    new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
80    }
81   
 
82  1 toggle @Test
83    public void getBundlesNewContext() throws Exception
84    {
85    // Map the context component manager to the test component manager for easier test setup.
86  1 when(this.mockContextComponentManagerProvider.get()).thenReturn(this.mocker);
87   
88  1 TranslationBundle mockTranslationBundle = this.mocker.registerMockComponent(TranslationBundle.class);
89   
90  1 Collection<TranslationBundle> bundles = this.mocker.getComponentUnderTest().getBundles();
91   
92    // Verify that an internal bundles cache is created and stored in the ExecutionContext.
93  1 assertNotNull(this.mockExecutionContext.getProperty(DefaultTranslationBundleContext.CKEY_BUNDLES));
94   
95  1 assertEquals(1, bundles.size());
96  1 assertEquals(mockTranslationBundle, bundles.iterator().next());
97    }
98   
 
99  1 toggle @Test
100    public void getBundlesSwitchContext() throws Exception
101    {
102    // Mock the first wiki bundles.
103  1 ComponentManager mockWiki1ComponentManager = mock(ComponentManager.class);
104  1 when(this.mockContextComponentManagerProvider.get()).thenReturn(mockWiki1ComponentManager);
105   
106  1 TranslationBundle mockWiki1TranslationBundle = mock(TranslationBundle.class);
107  1 List<TranslationBundle> wiki1Bundles = Arrays.asList(mockWiki1TranslationBundle);
108  1 when(mockWiki1ComponentManager.<TranslationBundle>getInstanceList(TranslationBundle.class)).thenReturn(
109    wiki1Bundles);
110   
111  1 Collection<TranslationBundle> firstBundles = this.mocker.getComponentUnderTest().getBundles();
112   
113    // Check the output
114  1 assertEquals(1, firstBundles.size());
115  1 assertEquals(mockWiki1TranslationBundle, firstBundles.iterator().next());
116   
117    // Switch context wiki to the second wiki.
118  1 when(this.mockModelContext.getCurrentEntityReference()).thenReturn(new WikiReference("otherWiki"));
119   
120    // Mock the second wiki bundles.
121  1 ComponentManager mockWiki2ComponentManager = mock(ComponentManager.class);
122  1 when(this.mockContextComponentManagerProvider.get()).thenReturn(mockWiki2ComponentManager);
123   
124  1 TranslationBundle mockWiki2TranslationBundle = mock(TranslationBundle.class);
125  1 List<TranslationBundle> wiki2Bundles = Arrays.asList(mockWiki2TranslationBundle);
126  1 when(mockWiki2ComponentManager.<TranslationBundle>getInstanceList(TranslationBundle.class)).thenReturn(
127    wiki2Bundles);
128   
129  1 Collection<TranslationBundle> secondBundles = this.mocker.getComponentUnderTest().getBundles();
130   
131    // Check the output.
132  1 assertEquals(1, secondBundles.size());
133  1 assertEquals(mockWiki2TranslationBundle, secondBundles.iterator().next());
134   
135    // Check that we get different results on different wikis.
136    // Note: We are comparing values instead of the actual sets because we did not mock the compareTo method on the
137    // bundles to simplify the test.
138  1 assertNotEquals(firstBundles.iterator().next(), secondBundles.iterator().next());
139   
140    // Switch back to the first wiki and get the same (cached) first bundles.
141  1 when(this.mockModelContext.getCurrentEntityReference()).thenReturn(new WikiReference("currentWiki"));
142   
143  1 Collection<TranslationBundle> thirdBundles = this.mocker.getComponentUnderTest().getBundles();
144   
145  1 assertEquals(firstBundles.iterator().next(), thirdBundles.iterator().next());
146    }
147   
 
148  1 toggle @Test
149    public void addBundlesToCurrentContext() throws Exception
150    {
151    // Map the context component manager to the test component manager for easier test setup.
152  1 when(this.mockContextComponentManagerProvider.get()).thenReturn(this.mocker);
153   
154  1 Collection<TranslationBundle> bundles = this.mocker.getComponentUnderTest().getBundles();
155   
156    // No bundles.
157  1 assertEquals(0, bundles.size());
158   
159    // Add a bundle
160  1 TranslationBundle mockBundleToAdd = mock(TranslationBundle.class);
161  1 this.mocker.getComponentUnderTest().addBundle(mockBundleToAdd);
162   
163  1 bundles = this.mocker.getComponentUnderTest().getBundles();
164   
165    // Check that it was added.
166  1 assertEquals(1, bundles.size());
167  1 assertTrue(bundles.contains(mockBundleToAdd));
168    }
169    }