| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 52 |
|
@link |
| 53 |
|
|
| 54 |
|
@version |
| 55 |
|
@since |
| 56 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (41) |
Complexity: 4 |
Complexity Density: 0.11 |
|
| 57 |
|
public class LinkCheckerThreadTest |
| 58 |
|
{ |
| 59 |
|
@Rule |
| 60 |
|
public MockitoComponentMockingRule<DefaultLinkCheckerThread> componentManager = |
| 61 |
|
new MockitoComponentMockingRule<>(DefaultLinkCheckerThread.class); |
| 62 |
|
|
| 63 |
|
private Provider<ObservationManager> observationManagerProvider; |
| 64 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 65 |
3 |
@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 |
|
|
| 76 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 77 |
1 |
@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 |
|
|
| 88 |
1 |
ReflectionUtils.setFieldValue(thread, "shouldStop", true); |
| 89 |
|
|
| 90 |
1 |
thread.run(queue); |
| 91 |
|
|
| 92 |
|
|
| 93 |
1 |
verify(initializer).initialize(); |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
| 96 |
1 |
@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 |
|
|
| 122 |
1 |
thread.processLinkQueue(); |
| 123 |
|
|
| 124 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 132 |
1 |
@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 |
|
} |