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

File AbstractRecordableEventDescriptorTest.java

 

Code metrics

0
47
11
2
213
150
11
0.23
4.27
5.5
1

Classes

Class Line # Actions
AbstractRecordableEventDescriptorTest 55 40 0% 6 0
1.0100%
AbstractRecordableEventDescriptorTest.OtherFakeRecordableEventDescriptor 151 7 0% 5 4
0.666666766.7%
 

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.eventstream;
21   
22    import java.util.Arrays;
23    import java.util.concurrent.Callable;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.slf4j.Logger;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.component.manager.NamespacedComponentManager;
31    import org.xwiki.component.namespace.Namespace;
32    import org.xwiki.component.namespace.NamespaceContextExecutor;
33    import org.xwiki.localization.ContextualLocalizationManager;
34    import org.xwiki.model.namespace.WikiNamespace;
35    import org.xwiki.test.AllLogRule;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import static org.junit.Assert.assertEquals;
39    import static org.junit.Assert.assertFalse;
40    import static org.junit.Assert.assertNotEquals;
41    import static org.junit.Assert.assertTrue;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Test for {@link AbstractRecordableEventDescriptor}.
49    *
50    * @version $Id: e961b2c6800ac3a9ce560f12069af2ac90f71cf6 $
51    * @since 10.6RC1
52    * @since 10.5
53    * @since 9.11.6
54    */
 
55    public class AbstractRecordableEventDescriptorTest
56    {
57    private ContextualLocalizationManager contextualLocalizationManager;
58    private NamespaceContextExecutor namespaceContextExecutor;
59    private NamespacedComponentManager componentManager;
60   
61    @Rule
62    // TODO: Replace by junit5
63    public AllLogRule logRule = new AllLogRule();
64   
65    @Rule
66    // TODO: Replace by junit5
67    public MockitoComponentMockingRule<FakeRecordableEventDescriptor> mocker =
68    new MockitoComponentMockingRule<>(FakeRecordableEventDescriptor.class, Arrays.asList(Logger.class));
69   
 
70  5 toggle @Before
71    public void setUp() throws Exception
72    {
73  5 componentManager = mock(NamespacedComponentManager.class);
74  5 mocker.registerComponent(ComponentManager.class, componentManager);
75  5 contextualLocalizationManager = mocker.getInstance(ContextualLocalizationManager.class);
76  5 namespaceContextExecutor = mocker.getInstance(NamespaceContextExecutor.class);
77    }
78   
 
79  1 toggle @Test
80    public void getApplicationIcon() throws Exception
81    {
82  1 assertEquals("applicationIcon :)", mocker.getComponentUnderTest().getApplicationIcon());
83    }
84   
 
85  1 toggle @Test
86    public void getEventType() throws Exception
87    {
88  1 assertEquals("fake", mocker.getComponentUnderTest().getEventType());
89    }
90   
 
91  1 toggle @Test
92    public void getDescriptionAndApplicationTest() throws Exception
93    {
94    // Mocks
95  1 when(namespaceContextExecutor.execute(eq(new WikiNamespace("subwiki")), any(Callable.class))).thenAnswer(
96    invocationOnMock -> String.format("On namespace [%s]: %s", invocationOnMock.getArgument(0),
97    ((Callable) invocationOnMock.getArgument(1)).call())
98    );
99  1 when(contextualLocalizationManager.getTranslationPlain("descriptionKey"))
100    .thenReturn("My nice description");
101  1 when(contextualLocalizationManager.getTranslationPlain("applicationKey"))
102    .thenReturn("My nice application name");
103   
104    // On main wiki
105  1 assertEquals("My nice description",
106    mocker.getComponentUnderTest().getDescription());
107  1 assertEquals("My nice application name",
108    mocker.getComponentUnderTest().getApplicationName());
109   
110    // On sub wiki
111  1 when(componentManager.getNamespace()).thenReturn(new WikiNamespace("subwiki").toString());
112   
113  1 assertEquals("On namespace [wiki:subwiki]: My nice description",
114    mocker.getComponentUnderTest().getDescription());
115  1 assertEquals("On namespace [wiki:subwiki]: My nice application name",
116    mocker.getComponentUnderTest().getApplicationName());
117    }
118   
 
119  1 toggle @Test
120    public void getDescriptionAndApplicationWithExceptionTest() throws Exception
121    {
122    // Mocks
123  1 Exception e = new Exception("some error");
124  1 when(namespaceContextExecutor.execute(any(Namespace.class), any(Callable.class))).thenThrow(e);
125  1 when(contextualLocalizationManager.getTranslationPlain("descriptionKey"))
126    .thenReturn("My nice description");
127  1 when(contextualLocalizationManager.getTranslationPlain("applicationKey"))
128    .thenReturn("My nice application name");
129   
130    // On main wiki
131  1 assertEquals("My nice description",
132    mocker.getComponentUnderTest().getDescription());
133  1 assertEquals(0, this.logRule.size());
134  1 assertEquals("My nice application name",
135    mocker.getComponentUnderTest().getApplicationName());
136  1 assertEquals(0, this.logRule.size());
137   
138    // On sub wiki
139  1 when(componentManager.getNamespace()).thenReturn(new WikiNamespace("subwiki").toString());
140   
141  1 assertEquals("My nice description",
142    mocker.getComponentUnderTest().getDescription());
143  1 assertEquals("Failed to render the translation key [descriptionKey] in the namespace [wiki:subwiki] "
144    + "for the event descriptor of [fake].", this.logRule.getMessage(0));
145  1 assertEquals("My nice application name",
146    mocker.getComponentUnderTest().getApplicationName());
147  1 assertEquals("Failed to render the translation key [applicationKey] in the namespace [wiki:subwiki] "
148    + "for the event descriptor of [fake].", this.logRule.getMessage(1));
149    }
150   
 
151    private class OtherFakeRecordableEventDescriptor extends AbstractRecordableEventDescriptor
152    {
153    private String eventType;
154    private String applicationId;
155   
 
156  4 toggle public OtherFakeRecordableEventDescriptor(String applicationId, String eventType)
157    {
158  4 super(null, null);
159  4 this.applicationId = applicationId;
160  4 this.eventType = eventType;
161    }
162   
 
163  14 toggle @Override
164    public String getApplicationId()
165    {
166    // For test reason we just reuse the same string
167  14 return applicationId;
168    }
169   
 
170  14 toggle @Override
171    public String getEventType()
172    {
173  14 return eventType;
174    }
175   
 
176  0 toggle @Override
177    public String getApplicationIcon()
178    {
179  0 return null;
180    }
181   
 
182  0 toggle @Override
183    public String getApplicationName()
184    {
185  0 return this.applicationTranslationKey;
186    }
187    }
188   
 
189  1 toggle @Test
190    public void equalsAndHashCodeTest()
191    {
192  1 OtherFakeRecordableEventDescriptor descriptor1 = new OtherFakeRecordableEventDescriptor(
193    "app1", "type1");
194  1 OtherFakeRecordableEventDescriptor descriptor2 = new OtherFakeRecordableEventDescriptor(
195    "app1", "type1");
196  1 OtherFakeRecordableEventDescriptor descriptor3 = new OtherFakeRecordableEventDescriptor(
197    "app2", "type1");
198  1 OtherFakeRecordableEventDescriptor descriptor4 = new OtherFakeRecordableEventDescriptor(
199    "app1", "type2");
200   
201   
202  1 assertTrue(descriptor1.equals(descriptor1));
203  1 assertTrue(descriptor1.equals(descriptor2));
204  1 assertFalse(descriptor1.equals(descriptor3));
205  1 assertFalse(descriptor1.equals(descriptor4));
206  1 assertFalse(descriptor1.equals(new Object()));
207   
208  1 assertEquals(descriptor1.hashCode(), descriptor1.hashCode());
209  1 assertEquals(descriptor1.hashCode(), descriptor2.hashCode());
210  1 assertNotEquals(descriptor1.hashCode(), descriptor3.hashCode());
211  1 assertNotEquals(descriptor1.hashCode(), descriptor4.hashCode());
212    }
213    }