1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.platform.blog.internal

File BlogUpgradeEventListenerTest.java

 

Code metrics

2
30
9
1
156
98
10
0.33
3.33
9
1.11

Classes

Class Line # Actions
BlogUpgradeEventListenerTest 44 30 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 7 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.platform.blog.internal;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.extension.ExtensionId;
28    import org.xwiki.extension.InstalledExtension;
29    import org.xwiki.extension.event.ExtensionUpgradedEvent;
30    import org.xwiki.model.reference.WikiReference;
31    import org.xwiki.platform.blog.BlogVisibilityMigration;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
34   
35    import static org.mockito.ArgumentMatchers.eq;
36    import static org.mockito.Mockito.mock;
37    import static org.mockito.Mockito.verify;
38    import static org.mockito.Mockito.verifyZeroInteractions;
39    import static org.mockito.Mockito.when;
40   
41    /**
42    * @version $Id: 5d07c9d48f0cf0942f49b3a2243f78f4e29504af $
43    */
 
44    public class BlogUpgradeEventListenerTest
45    {
46    @Rule
47    public MockitoComponentMockingRule<BlogUpgradeEventListener> mocker =
48    new MockitoComponentMockingRule<>(BlogUpgradeEventListener.class);
49   
50    private BlogVisibilityMigration blogVisibilityMigration;
51    private WikiDescriptorManager wikiDescriptorManager;
52   
 
53  7 toggle @Before
54    public void setUp() throws Exception
55    {
56  7 blogVisibilityMigration = mocker.getInstance(BlogVisibilityMigration.class);
57  7 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
58    }
59   
 
60  5 toggle private void testWithVersion(String version, boolean executedExpected) throws Exception
61    {
62    // Mocks
63  5 InstalledExtension installedExtension1 = mock(InstalledExtension.class);
64  5 InstalledExtension installedExtension2 = mock(InstalledExtension.class);
65  5 InstalledExtension installedExtension3 = mock(InstalledExtension.class);
66   
67  5 ExtensionUpgradedEvent event = new ExtensionUpgradedEvent(
68    new ExtensionId("org.xwiki.platform:xwiki-platform-blog-ui", "9.0"), "wiki:chocolate");
69   
70  5 when(installedExtension1.getId()).thenReturn(new ExtensionId("foo", "8"));
71  5 when(installedExtension2.getId()).thenReturn(new ExtensionId("bar", "9.0"));
72  5 when(installedExtension2.getId()).thenReturn(new ExtensionId("org.xwiki.platform:xwiki-platform-blog-ui",
73    version));
74   
75    // Test
76  5 mocker.getComponentUnderTest().onEvent(event, null,
77    Arrays.asList(installedExtension1, installedExtension2, installedExtension3));
78   
79    // Verify
80  5 if (executedExpected) {
81  2 verify(blogVisibilityMigration).execute(eq(new WikiReference("chocolate")));
82    } else {
83  3 verifyZeroInteractions(blogVisibilityMigration);
84    }
85    }
86   
 
87  1 toggle @Test
88    public void onEventWithVersion82() throws Exception
89    {
90  1 testWithVersion("8.2", true);
91    }
92   
 
93  1 toggle @Test
94    public void onEventWithVersion51() throws Exception
95    {
96  1 testWithVersion("5.1", true);
97    }
98   
 
99  1 toggle @Test
100    public void onEventWithVersion746() throws Exception
101    {
102  1 testWithVersion("7.4.6", false);
103    }
104   
 
105  1 toggle @Test
106    public void onEventWithVersion843() throws Exception
107    {
108  1 testWithVersion("8.4.3", false);
109    }
110   
 
111  1 toggle @Test
112    public void onEventWithVersion90() throws Exception
113    {
114  1 testWithVersion("9.0", false);
115    }
116   
 
117  1 toggle @Test
118    public void onEventWithNoBlogInstalled() throws Exception
119    {
120    // Mocks
121  1 InstalledExtension installedExtension1 = mock(InstalledExtension.class);
122   
123  1 ExtensionUpgradedEvent event = new ExtensionUpgradedEvent(
124    new ExtensionId("org.xwiki.platform:xwiki-platform-blog-ui", "9.0"), "wiki:chocolate");
125   
126  1 when(installedExtension1.getId()).thenReturn(new ExtensionId("foobar", "8"));
127   
128    // Test
129  1 mocker.getComponentUnderTest().onEvent(event, null, Arrays.asList(installedExtension1));
130   
131    // Verify
132  1 verifyZeroInteractions(blogVisibilityMigration);
133    }
134   
 
135  1 toggle @Test
136    public void onEventWithNoNamespace() throws Exception
137    {
138    // Mocks
139  1 InstalledExtension installedExtension1 = mock(InstalledExtension.class);
140   
141  1 ExtensionUpgradedEvent event = new ExtensionUpgradedEvent(
142    new ExtensionId("org.xwiki.platform:xwiki-platform-blog-ui", "9.0"), null);
143   
144  1 when(installedExtension1.getId()).thenReturn(new ExtensionId("org.xwiki.platform:xwiki-platform-blog-ui",
145    "2.3"));
146   
147  1 when(wikiDescriptorManager.getAllIds()).thenReturn(Arrays.asList("wiki1", "wiki2"));
148   
149    // Test
150  1 mocker.getComponentUnderTest().onEvent(event, null, Arrays.asList(installedExtension1));
151   
152    // Verify
153  1 verify(blogVisibilityMigration).execute(eq(new WikiReference("wiki1")));
154  1 verify(blogVisibilityMigration).execute(eq(new WikiReference("wiki2")));
155    }
156    }