| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 50 |
|
|
| 51 |
|
@version |
| 52 |
|
@since |
| 53 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (45) |
Complexity: 4 |
Complexity Density: 0.1 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 66 |
3 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 82 |
1 |
@Test... |
| 83 |
|
public void getBundlesNewContext() throws Exception |
| 84 |
|
{ |
| 85 |
|
|
| 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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
| 99 |
1 |
@Test... |
| 100 |
|
public void getBundlesSwitchContext() throws Exception |
| 101 |
|
{ |
| 102 |
|
|
| 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 |
|
|
| 114 |
1 |
assertEquals(1, firstBundles.size()); |
| 115 |
1 |
assertEquals(mockWiki1TranslationBundle, firstBundles.iterator().next()); |
| 116 |
|
|
| 117 |
|
|
| 118 |
1 |
when(this.mockModelContext.getCurrentEntityReference()).thenReturn(new WikiReference("otherWiki")); |
| 119 |
|
|
| 120 |
|
|
| 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 |
|
|
| 132 |
1 |
assertEquals(1, secondBundles.size()); |
| 133 |
1 |
assertEquals(mockWiki2TranslationBundle, secondBundles.iterator().next()); |
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
1 |
assertNotEquals(firstBundles.iterator().next(), secondBundles.iterator().next()); |
| 139 |
|
|
| 140 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 148 |
1 |
@Test... |
| 149 |
|
public void addBundlesToCurrentContext() throws Exception |
| 150 |
|
{ |
| 151 |
|
|
| 152 |
1 |
when(this.mockContextComponentManagerProvider.get()).thenReturn(this.mocker); |
| 153 |
|
|
| 154 |
1 |
Collection<TranslationBundle> bundles = this.mocker.getComponentUnderTest().getBundles(); |
| 155 |
|
|
| 156 |
|
|
| 157 |
1 |
assertEquals(0, bundles.size()); |
| 158 |
|
|
| 159 |
|
|
| 160 |
1 |
TranslationBundle mockBundleToAdd = mock(TranslationBundle.class); |
| 161 |
1 |
this.mocker.getComponentUnderTest().addBundle(mockBundleToAdd); |
| 162 |
|
|
| 163 |
1 |
bundles = this.mocker.getComponentUnderTest().getBundles(); |
| 164 |
|
|
| 165 |
|
|
| 166 |
1 |
assertEquals(1, bundles.size()); |
| 167 |
1 |
assertTrue(bundles.contains(mockBundleToAdd)); |
| 168 |
|
} |
| 169 |
|
} |