| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.uiextension; |
| 21 |
|
|
| 22 |
|
import static org.mockito.ArgumentMatchers.any; |
| 23 |
|
import static org.mockito.ArgumentMatchers.eq; |
| 24 |
|
import static org.mockito.Mockito.mock; |
| 25 |
|
import static org.mockito.Mockito.times; |
| 26 |
|
import static org.mockito.Mockito.verify; |
| 27 |
|
import static org.mockito.Mockito.when; |
| 28 |
|
|
| 29 |
|
import java.io.StringWriter; |
| 30 |
|
|
| 31 |
|
import org.apache.commons.collections.MapUtils; |
| 32 |
|
import org.apache.velocity.VelocityContext; |
| 33 |
|
import org.junit.Assert; |
| 34 |
|
import org.junit.Before; |
| 35 |
|
import org.junit.Rule; |
| 36 |
|
import org.junit.Test; |
| 37 |
|
import org.xwiki.context.Execution; |
| 38 |
|
import org.xwiki.context.ExecutionContext; |
| 39 |
|
import org.xwiki.model.ModelContext; |
| 40 |
|
import org.xwiki.model.reference.WikiReference; |
| 41 |
|
import org.xwiki.test.LogRule; |
| 42 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
| 43 |
|
import org.xwiki.uiextension.internal.WikiUIExtensionParameters; |
| 44 |
|
import org.xwiki.velocity.VelocityEngine; |
| 45 |
|
import org.xwiki.velocity.VelocityManager; |
| 46 |
|
import org.xwiki.velocity.XWikiVelocityException; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
@link |
| 50 |
|
|
| 51 |
|
@version |
| 52 |
|
@since |
| 53 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (52) |
Complexity: 8 |
Complexity Density: 0.18 |
|
| 54 |
|
public class WikiUIExtensionParametersTest |
| 55 |
|
{ |
| 56 |
|
private VelocityEngine velocityEngine; |
| 57 |
|
|
| 58 |
|
private VelocityContext velocityContext; |
| 59 |
|
|
| 60 |
|
private ModelContext modelContext; |
| 61 |
|
|
| 62 |
|
private Execution execution; |
| 63 |
|
|
| 64 |
|
@Rule |
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 65 |
6 |
public LogRule logRule = new LogRule() {{... |
| 66 |
6 |
record(LogLevel.WARN); |
| 67 |
6 |
recordLoggingForType(WikiUIExtensionParameters.class); |
| 68 |
|
}}; |
| 69 |
|
|
| 70 |
|
@Rule |
| 71 |
|
public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); |
| 72 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 73 |
6 |
@Before... |
| 74 |
|
public void setUp() throws Exception |
| 75 |
|
{ |
| 76 |
6 |
VelocityManager velocityManager = componentManager.registerMockComponent(VelocityManager.class); |
| 77 |
6 |
execution = componentManager.registerMockComponent(Execution.class); |
| 78 |
6 |
modelContext = componentManager.registerMockComponent(ModelContext.class); |
| 79 |
6 |
velocityEngine = mock(VelocityEngine.class); |
| 80 |
6 |
velocityContext = new VelocityContext(); |
| 81 |
6 |
ExecutionContext executionContext = mock(ExecutionContext.class); |
| 82 |
|
|
| 83 |
6 |
when(execution.getContext()).thenReturn(executionContext); |
| 84 |
6 |
when(velocityManager.getVelocityContext()).thenReturn(velocityContext); |
| 85 |
6 |
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine); |
| 86 |
|
} |
| 87 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 88 |
1 |
@Test... |
| 89 |
|
public void getParametersWithAnEmptyParametersProperty() throws Exception |
| 90 |
|
{ |
| 91 |
1 |
when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki")); |
| 92 |
1 |
WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "", componentManager); |
| 93 |
1 |
Assert.assertEquals(MapUtils.EMPTY_MAP, parameters.get()); |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 96 |
1 |
@Test... |
| 97 |
|
public void getParametersWithAnEqualSignInAValue() throws Exception |
| 98 |
|
{ |
| 99 |
1 |
when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki")); |
| 100 |
1 |
when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"))) |
| 101 |
|
.thenReturn(true); |
| 102 |
1 |
WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager); |
| 103 |
|
|
| 104 |
|
|
| 105 |
1 |
Assert.assertEquals("", parameters.get().get("key")); |
| 106 |
|
} |
| 107 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 108 |
1 |
@Test... |
| 109 |
|
public void getParametersWhenVelocityFails() throws Exception |
| 110 |
|
{ |
| 111 |
1 |
when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki")); |
| 112 |
1 |
when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"))) |
| 113 |
|
.thenThrow(new XWikiVelocityException("")); |
| 114 |
1 |
WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager); |
| 115 |
|
|
| 116 |
|
|
| 117 |
1 |
Assert.assertEquals(null, parameters.get().get("key")); |
| 118 |
1 |
Assert.assertTrue( |
| 119 |
|
logRule.contains("Failed to evaluate UI extension data value, key [key], value [value]. Reason: []")); |
| 120 |
|
} |
| 121 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 122 |
1 |
@Test... |
| 123 |
|
public void getParametersFromTheSameRequestAndForTheSameWiki() throws Exception |
| 124 |
|
{ |
| 125 |
1 |
when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki")); |
| 126 |
1 |
when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"))) |
| 127 |
|
.thenReturn(true); |
| 128 |
1 |
WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager); |
| 129 |
|
|
| 130 |
|
|
| 131 |
1 |
Assert.assertEquals("", parameters.get().get("key")); |
| 132 |
1 |
Assert.assertEquals("", parameters.get().get("key")); |
| 133 |
|
|
| 134 |
|
|
| 135 |
1 |
verify(velocityEngine).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value")); |
| 136 |
|
} |
| 137 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 138 |
1 |
@Test... |
| 139 |
|
public void getParametersFromTheSameRequestButForDifferentWikis() throws Exception |
| 140 |
|
{ |
| 141 |
1 |
when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki1")) |
| 142 |
|
.thenReturn(new WikiReference("wiki2")); |
| 143 |
1 |
when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"))) |
| 144 |
|
.thenReturn(true); |
| 145 |
1 |
WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager); |
| 146 |
|
|
| 147 |
|
|
| 148 |
1 |
Assert.assertEquals("", parameters.get().get("key")); |
| 149 |
1 |
Assert.assertEquals("", parameters.get().get("key")); |
| 150 |
|
|
| 151 |
|
|
| 152 |
1 |
verify(velocityEngine, times(2)).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value")); |
| 153 |
|
} |
| 154 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 155 |
1 |
@Test... |
| 156 |
|
public void getParametersFromDifferentRequests() throws Exception |
| 157 |
|
{ |
| 158 |
1 |
when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki1")); |
| 159 |
1 |
when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"))) |
| 160 |
|
.thenReturn(true); |
| 161 |
1 |
WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager); |
| 162 |
|
|
| 163 |
1 |
ExecutionContext ec1 = mock(ExecutionContext.class, "ec1"); |
| 164 |
1 |
ExecutionContext ec2 = mock(ExecutionContext.class, "ec2"); |
| 165 |
1 |
when(execution.getContext()).thenReturn(ec1).thenReturn(ec2); |
| 166 |
|
|
| 167 |
|
|
| 168 |
1 |
Assert.assertEquals("", parameters.get().get("key")); |
| 169 |
1 |
Assert.assertEquals("", parameters.get().get("key")); |
| 170 |
|
|
| 171 |
|
|
| 172 |
1 |
verify(velocityEngine, times(2)).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value")); |
| 173 |
|
} |
| 174 |
|
} |