| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.internal.template; |
| 21 |
|
|
| 22 |
|
import java.io.ByteArrayInputStream; |
| 23 |
|
import java.io.UnsupportedEncodingException; |
| 24 |
|
import java.io.Writer; |
| 25 |
|
import java.net.MalformedURLException; |
| 26 |
|
import java.net.URL; |
| 27 |
|
|
| 28 |
|
import org.apache.velocity.VelocityContext; |
| 29 |
|
import org.junit.Before; |
| 30 |
|
import org.junit.Rule; |
| 31 |
|
import org.junit.Test; |
| 32 |
|
import org.mockito.invocation.InvocationOnMock; |
| 33 |
|
import org.mockito.stubbing.Answer; |
| 34 |
|
import org.xwiki.component.manager.ComponentLookupException; |
| 35 |
|
import org.xwiki.configuration.ConfigurationSource; |
| 36 |
|
import org.xwiki.configuration.internal.MemoryConfigurationSource; |
| 37 |
|
import org.xwiki.environment.Environment; |
| 38 |
|
import org.xwiki.rendering.transformation.TransformationManager; |
| 39 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
| 40 |
|
import org.xwiki.template.TemplateManager; |
| 41 |
|
import org.xwiki.test.annotation.AfterComponent; |
| 42 |
|
import org.xwiki.test.annotation.AllComponents; |
| 43 |
|
import org.xwiki.test.internal.MockConfigurationSource; |
| 44 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 45 |
|
import org.xwiki.test.mockito.StringReaderMatcher; |
| 46 |
|
import org.xwiki.velocity.VelocityManager; |
| 47 |
|
import org.xwiki.velocity.XWikiVelocityException; |
| 48 |
|
|
| 49 |
|
import static org.junit.Assert.assertEquals; |
| 50 |
|
import static org.mockito.ArgumentMatchers.any; |
| 51 |
|
import static org.mockito.ArgumentMatchers.argThat; |
| 52 |
|
import static org.mockito.Mockito.when; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
@link |
| 56 |
|
|
| 57 |
|
@version |
| 58 |
|
|
| 59 |
|
@AllComponents |
| |
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 8 |
Complexity Density: 0.38 |
|
| 60 |
|
public class TemplateManagerTest |
| 61 |
|
{ |
| 62 |
|
@Rule |
| 63 |
|
public final MockitoComponentMockingRule<TemplateManager> mocker = |
| 64 |
|
new MockitoComponentMockingRule<TemplateManager>(DefaultTemplateManager.class); |
| 65 |
|
|
| 66 |
|
private Environment environmentMock; |
| 67 |
|
|
| 68 |
|
private VelocityManager velocityManagerMock; |
| 69 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 70 |
3 |
@Before... |
| 71 |
|
public void before() throws Exception |
| 72 |
|
{ |
| 73 |
3 |
when(this.velocityManagerMock.getVelocityContext()).thenReturn(new VelocityContext()); |
| 74 |
|
|
| 75 |
3 |
MemoryConfigurationSource configuration = this.mocker.registerMemoryConfigurationSource(); |
| 76 |
3 |
this.mocker.registerComponent(MockConfigurationSource.getDescriptor("all"), configuration); |
| 77 |
|
|
| 78 |
3 |
this.mocker.registerMockComponent(ContextualAuthorizationManager.class); |
| 79 |
|
} |
| 80 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 81 |
3 |
@AfterComponent... |
| 82 |
|
public void afterComponent() throws Exception |
| 83 |
|
{ |
| 84 |
3 |
this.environmentMock = this.mocker.registerMockComponent(Environment.class); |
| 85 |
3 |
this.velocityManagerMock = this.mocker.registerMockComponent(VelocityManager.class); |
| 86 |
3 |
this.mocker.registerMockComponent(ConfigurationSource.class); |
| 87 |
3 |
this.mocker.registerMockComponent(TransformationManager.class); |
| 88 |
|
} |
| 89 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 90 |
2 |
private void setTemplateContent(String content) throws UnsupportedEncodingException, MalformedURLException... |
| 91 |
|
{ |
| 92 |
2 |
when(this.environmentMock.getResourceAsStream("/templates/template")) |
| 93 |
|
.thenReturn(new ByteArrayInputStream(content.getBytes("UTF8"))); |
| 94 |
2 |
when(this.environmentMock.getResource("/templates/template")).thenReturn(new URL("http://url")); |
| 95 |
|
} |
| 96 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 97 |
2 |
private void mockVelocity(String source, String result) throws XWikiVelocityException... |
| 98 |
|
{ |
| 99 |
2 |
when(this.velocityManagerMock.evaluate(any(Writer.class), any(), |
| 100 |
|
argThat(new StringReaderMatcher(source)))).then(new Answer<Boolean>() |
| 101 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 102 |
2 |
@Override... |
| 103 |
|
public Boolean answer(InvocationOnMock invocation) throws Throwable |
| 104 |
|
{ |
| 105 |
2 |
Writer writer = (Writer) invocation.getArguments()[0]; |
| 106 |
|
|
| 107 |
2 |
writer.write(result); |
| 108 |
|
|
| 109 |
2 |
return Boolean.TRUE; |
| 110 |
|
} |
| 111 |
|
}); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 116 |
1 |
@Test... |
| 117 |
|
public void testRenderVelocity() throws Exception |
| 118 |
|
{ |
| 119 |
1 |
mockVelocity("source", "OK"); |
| 120 |
|
|
| 121 |
1 |
setTemplateContent("source"); |
| 122 |
|
|
| 123 |
1 |
assertEquals("OK", mocker.getComponentUnderTest().render("template")); |
| 124 |
|
} |
| 125 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 126 |
1 |
@Test... |
| 127 |
|
public void testRenderWiki() throws Exception |
| 128 |
|
{ |
| 129 |
1 |
setTemplateContent("##!source.syntax=xwiki/2.1\nfirst line\\\\second line"); |
| 130 |
|
|
| 131 |
1 |
assertEquals("<p>first line<br/>second line</p>", mocker.getComponentUnderTest().render("template")); |
| 132 |
|
} |
| 133 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 134 |
1 |
@Test... |
| 135 |
|
public void testRenderClassloaderTemplate() throws ComponentLookupException, Exception |
| 136 |
|
{ |
| 137 |
1 |
mockVelocity("classloader template content", "OK"); |
| 138 |
|
|
| 139 |
1 |
assertEquals("OK", this.mocker.getComponentUnderTest().render("classloader_template.vm")); |
| 140 |
|
} |
| 141 |
|
} |