1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.transformation.linkchecker

File LinkCheckerThreadTest.java

 

Code metrics

0
37
4
1
151
90
4
0.11
9.25
4
1

Classes

Class Line # Actions
LinkCheckerThreadTest 57 37 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.rendering.internal.transformation.linkchecker;
21   
22    import static org.junit.Assert.assertEquals;
23    import static org.junit.Assert.assertNotNull;
24    import static org.junit.Assert.assertNull;
25    import static org.mockito.Mockito.verify;
26    import static org.mockito.Mockito.when;
27    import static org.mockito.Mockito.mock;
28   
29    import java.util.Collections;
30    import java.util.HashMap;
31    import java.util.Map;
32    import java.util.Queue;
33    import java.util.concurrent.ConcurrentLinkedQueue;
34    import java.util.regex.Pattern;
35   
36    import javax.inject.Provider;
37   
38    import org.junit.Rule;
39    import org.junit.Test;
40    import org.xwiki.component.util.DefaultParameterizedType;
41    import org.xwiki.component.util.ReflectionUtils;
42    import org.xwiki.observation.ObservationManager;
43    import org.xwiki.rendering.transformation.linkchecker.LinkCheckerThreadInitializer;
44    import org.xwiki.rendering.transformation.linkchecker.LinkCheckerTransformationConfiguration;
45    import org.xwiki.rendering.transformation.linkchecker.LinkState;
46    import org.xwiki.rendering.transformation.linkchecker.LinkStateManager;
47    import org.xwiki.test.annotation.BeforeComponent;
48    import org.xwiki.test.mockito.MockitoComponentMockingRule;
49   
50    /**
51    * Unit tests for {@link DefaultLinkCheckerThread}. Note that the Link Checker Thread is also tested indirectly by
52    * {@link LinkCheckerTransformationTest} which is where most tests are located.
53    *
54    * @version $Id: cc6856d471ac0b555d9c1ea527103664a0866dc7 $
55    * @since 4.0M2
56    */
 
57    public class LinkCheckerThreadTest
58    {
59    @Rule
60    public MockitoComponentMockingRule<DefaultLinkCheckerThread> componentManager =
61    new MockitoComponentMockingRule<>(DefaultLinkCheckerThread.class);
62   
63    private Provider<ObservationManager> observationManagerProvider;
64   
 
65  3 toggle @BeforeComponent
66    public void setUpComponents() throws Exception
67    {
68  3 ObservationManager observationManager = mock(ObservationManager.class);
69  3 this.observationManagerProvider = this.componentManager.registerMockComponent(
70    new DefaultParameterizedType(null, Provider.class, ObservationManager.class));
71  3 when(this.observationManagerProvider.get()).thenReturn(observationManager);
72    }
73   
74    /**
75    * Just verify that we can register a LinkCheckerThreadInitializer and it'll be called.
76    */
 
77  1 toggle @Test
78    public void runWithInitializer() throws Exception
79    {
80  1 LinkCheckerThreadInitializer initializer =
81    this.componentManager.registerMockComponent(LinkCheckerThreadInitializer.class);
82   
83  1 Queue<LinkQueueItem> queue = new ConcurrentLinkedQueue<>();
84   
85  1 DefaultLinkCheckerThread thread = this.componentManager.getComponentUnderTest();
86   
87    // Make sure the thread is stopped quickly
88  1 ReflectionUtils.setFieldValue(thread, "shouldStop", true);
89   
90  1 thread.run(queue);
91   
92    // This is the test, we verify that the registered Link Checker Initializer is called.
93  1 verify(initializer).initialize();
94    }
95   
 
96  1 toggle @Test
97    public void runWithExclusion() throws Exception
98    {
99  1 LinkCheckerTransformationConfiguration configuration =
100    this.componentManager.getInstance(LinkCheckerTransformationConfiguration.class);
101  1 when(configuration.getCheckTimeout()).thenReturn(3600000L);
102  1 when(configuration.getExcludedReferencePatterns()).thenReturn(
103    Collections.singletonList(Pattern.compile(".*:excludedspace\\.excludedpage")));
104   
105  1 HTTPChecker httpChecker = this.componentManager.getInstance(HTTPChecker.class);
106  1 when(httpChecker.check("linkreference1")).thenReturn(200);
107  1 when(httpChecker.check("linkreference2")).thenReturn(200);
108   
109  1 LinkStateManager linkStateManager = this.componentManager.getInstance(LinkStateManager.class);
110  1 Map<String, Map<String, LinkState>> states = new HashMap<>();
111  1 when(linkStateManager.getLinkStates()).thenReturn(states);
112   
113  1 Queue<LinkQueueItem> queue = new ConcurrentLinkedQueue<>();
114  1 queue.add(new LinkQueueItem("linkreference1", "excludedwiki:excludedspace.excludedpage",
115    Collections.<String, Object>emptyMap()));
116  1 queue.add(new LinkQueueItem("linkreference2", "someotherpage", Collections.<String, Object>emptyMap()));
117   
118  1 DefaultLinkCheckerThread thread = this.componentManager.getComponentUnderTest();
119  1 ReflectionUtils.setFieldValue(thread, "linkQueue", queue);
120   
121    // Process first element in queue
122  1 thread.processLinkQueue();
123   
124    // Process second element in queue
125  1 thread.processLinkQueue();
126   
127  1 assertEquals(1, states.size());
128  1 assertNull(states.get("linkreference1"));
129  1 assertNotNull(states.get("linkreference2"));
130    }
131   
 
132  1 toggle @Test
133    public void sendEventWhenNoObservationManager() throws Exception
134    {
135  1 HTTPChecker httpChecker = this.componentManager.getInstance(HTTPChecker.class);
136  1 when(httpChecker.check("linkreference")).thenReturn(404);
137   
138  1 Queue<LinkQueueItem> queue = new ConcurrentLinkedQueue<>();
139  1 queue.add(new LinkQueueItem("linkreference", "someref", Collections.<String, Object>emptyMap()));
140   
141  1 DefaultLinkCheckerThread thread = this.componentManager.getComponentUnderTest();
142  1 ReflectionUtils.setFieldValue(thread, "linkQueue", queue);
143   
144  1 when(this.observationManagerProvider.get()).thenThrow(new RuntimeException("error"));
145   
146  1 thread.processLinkQueue();
147   
148  1 verify(this.componentManager.getMockedLogger()).warn("The Invalid URL Event for URL [{}] (source [{}]) wasn't "
149    + "sent as no Observation Manager Component was found", "linkreference", "someref");
150    }
151    }