1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.vfs.internal; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.ByteArrayOutputStream; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.net.URI; |
26 |
|
import java.util.Arrays; |
27 |
|
import java.util.Date; |
28 |
|
import java.util.List; |
29 |
|
import java.util.zip.ZipEntry; |
30 |
|
import java.util.zip.ZipOutputStream; |
31 |
|
|
32 |
|
import javax.inject.Provider; |
33 |
|
|
34 |
|
import org.apache.commons.lang.exception.ExceptionUtils; |
35 |
|
import org.apache.commons.lang3.StringUtils; |
36 |
|
import org.junit.Rule; |
37 |
|
import org.junit.Test; |
38 |
|
import org.xwiki.component.manager.ComponentManager; |
39 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
40 |
|
import org.xwiki.container.Container; |
41 |
|
import org.xwiki.container.Response; |
42 |
|
import org.xwiki.model.reference.DocumentReference; |
43 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
44 |
|
import org.xwiki.resource.ResourceReferenceHandlerChain; |
45 |
|
import org.xwiki.resource.ResourceReferenceHandlerException; |
46 |
|
import org.xwiki.resource.ResourceReferenceSerializer; |
47 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
48 |
|
import org.xwiki.vfs.VfsException; |
49 |
|
import org.xwiki.vfs.VfsPermissionChecker; |
50 |
|
import org.xwiki.vfs.VfsResourceReference; |
51 |
|
import org.xwiki.vfs.internal.attach.AttachDriver; |
52 |
|
|
53 |
|
import com.xpn.xwiki.XWiki; |
54 |
|
import com.xpn.xwiki.XWikiContext; |
55 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
56 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
57 |
|
|
58 |
|
import net.java.truevfs.access.TArchiveDetector; |
59 |
|
import net.java.truevfs.access.TConfig; |
60 |
|
|
61 |
|
import static org.junit.Assert.*; |
62 |
|
import static org.mockito.Mockito.*; |
63 |
|
|
64 |
|
|
65 |
|
@link |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@version |
71 |
|
@since |
72 |
|
|
|
|
| 98.1% |
Uncovered Elements: 1 (53) |
Complexity: 7 |
Complexity Density: 0.14 |
|
73 |
|
public class VfsResourceReferenceHandlerTest |
74 |
|
{ |
75 |
|
@Rule |
76 |
|
public MockitoComponentMockingRule<VfsResourceReferenceHandler> mocker = |
77 |
|
new MockitoComponentMockingRule<>(VfsResourceReferenceHandler.class); |
78 |
|
|
79 |
|
private ByteArrayOutputStream baos; |
80 |
|
|
81 |
|
private DocumentReference documentReference; |
82 |
|
|
83 |
|
private VfsResourceReference reference; |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 1 |
Complexity Density: 0.03 |
|
85 |
2 |
private void setUp(String scheme, String wikiName, String spaceName, String pageName, String attachmentName,... |
86 |
|
List<String> path) throws Exception |
87 |
|
{ |
88 |
2 |
Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent( |
89 |
|
new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context"); |
90 |
2 |
when(componentManagerProvider.get()).thenReturn(this.mocker); |
91 |
|
|
92 |
2 |
String attachmentReferenceAsString = |
93 |
|
String.format("%s:%s:%s.%s@%s", scheme, wikiName, spaceName, pageName, attachmentName); |
94 |
2 |
this.reference = new VfsResourceReference(URI.create(attachmentReferenceAsString), path); |
95 |
|
|
96 |
2 |
ResourceReferenceSerializer<VfsResourceReference, URI> trueVfsResourceReferenceSerializer = |
97 |
|
this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, |
98 |
|
VfsResourceReference.class, URI.class), "truevfs"); |
99 |
2 |
String truevfsURIFragment = String.format("%s://%s:%s.%s/%s/%s", scheme, wikiName, spaceName, pageName, |
100 |
|
attachmentName, StringUtils.join(path, '/')); |
101 |
2 |
when(trueVfsResourceReferenceSerializer.serialize(this.reference)).thenReturn(URI.create(truevfsURIFragment)); |
102 |
|
|
103 |
2 |
Provider<XWikiContext> xwikiContextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
104 |
2 |
XWikiContext xcontext = mock(XWikiContext.class); |
105 |
2 |
when(xwikiContextProvider.get()).thenReturn(xcontext); |
106 |
|
|
107 |
2 |
XWiki xwiki = mock(XWiki.class); |
108 |
2 |
when(xcontext.getWiki()).thenReturn(xwiki); |
109 |
|
|
110 |
2 |
DocumentReferenceResolver<String> documentReferenceResolver = |
111 |
|
mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING); |
112 |
2 |
this.documentReference = new DocumentReference(wikiName, Arrays.asList(spaceName), pageName); |
113 |
2 |
String documentReferenceAsString = String.format("%s:%s.%s", wikiName, spaceName, pageName); |
114 |
2 |
when(documentReferenceResolver.resolve(documentReferenceAsString)).thenReturn(this.documentReference); |
115 |
|
|
116 |
2 |
XWikiDocument document = mock(XWikiDocument.class); |
117 |
2 |
when(xwiki.getDocument(this.documentReference, xcontext)).thenReturn(document); |
118 |
|
|
119 |
2 |
XWikiAttachment attachment = mock(XWikiAttachment.class); |
120 |
2 |
when(document.getAttachment(attachmentName)).thenReturn(attachment); |
121 |
|
|
122 |
2 |
when(attachment.getDate()).thenReturn(new Date()); |
123 |
2 |
when(attachment.getContentSize(xcontext)).thenReturn(1000); |
124 |
|
|
125 |
2 |
when(attachment.getContentInputStream(xcontext)).thenReturn( |
126 |
|
createZipInputStream(StringUtils.join(path, '/'), "success!")); |
127 |
|
|
128 |
2 |
Container container = this.mocker.getInstance(Container.class); |
129 |
2 |
Response response = mock(Response.class); |
130 |
2 |
when(container.getResponse()).thenReturn(response); |
131 |
|
|
132 |
2 |
this.baos = new ByteArrayOutputStream(); |
133 |
2 |
when(response.getOutputStream()).thenReturn(this.baos); |
134 |
|
|
135 |
|
|
136 |
2 |
TConfig config = TConfig.current(); |
137 |
|
|
138 |
|
|
139 |
2 |
config.setArchiveDetector(new TArchiveDetector(config.getArchiveDetector(), "attach", |
140 |
|
new AttachDriver(this.mocker))); |
141 |
|
} |
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
143 |
1 |
@Test... |
144 |
|
public void handleOk() throws Exception |
145 |
|
{ |
146 |
1 |
setUp("attach", "wiki1", "space1", "page1", "test.zip", Arrays.asList("test.txt")); |
147 |
|
|
148 |
1 |
assertEquals(Arrays.asList(VfsResourceReference.TYPE), |
149 |
|
this.mocker.getComponentUnderTest().getSupportedResourceReferences()); |
150 |
1 |
this.mocker.getComponentUnderTest().handle(this.reference, mock(ResourceReferenceHandlerChain.class)); |
151 |
|
|
152 |
1 |
assertEquals("success!", this.baos.toString()); |
153 |
|
} |
154 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
1PASS
|
|
155 |
1 |
@Test... |
156 |
|
public void handleWhenNoGenericPermissionForScheme() throws Exception |
157 |
|
{ |
158 |
1 |
setUp("customscheme", "wiki3", "space3", "page3", "test.zip", Arrays.asList("test.txt")); |
159 |
|
|
160 |
|
|
161 |
1 |
VfsPermissionChecker checker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading"); |
162 |
1 |
doThrow(new VfsException("no permission")).when(checker).checkPermission(this.reference); |
163 |
|
|
164 |
1 |
try { |
165 |
1 |
this.mocker.getComponentUnderTest().handle(this.reference, mock(ResourceReferenceHandlerChain.class)); |
166 |
0 |
fail("Should have thrown exception here"); |
167 |
|
} catch (ResourceReferenceHandlerException expected) { |
168 |
1 |
assertEquals("VfsException: no permission", ExceptionUtils.getRootCauseMessage(expected)); |
169 |
|
} |
170 |
|
} |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.38 |
|
172 |
2 |
private InputStream createZipInputStream(String fileName, String content) throws Exception... |
173 |
|
{ |
174 |
2 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
175 |
2 |
try (ZipOutputStream zos = new ZipOutputStream(baos)) { |
176 |
2 |
ZipEntry entry = new ZipEntry(fileName); |
177 |
2 |
zos.putNextEntry(entry); |
178 |
2 |
zos.write(content.getBytes()); |
179 |
2 |
zos.closeEntry(); |
180 |
|
} |
181 |
2 |
return new ByteArrayInputStream(baos.toByteArray()); |
182 |
|
} |
183 |
|
} |