1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.vfs.internal

File VfsResourceReferenceHandlerTest.java

 

Code metrics

0
49
4
1
183
121
7
0.14
12.25
4
1.75

Classes

Class Line # Actions
VfsResourceReferenceHandlerTest 73 49 0% 7 1
0.981132198.1%
 

Contributing tests

This file is covered by 2 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.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.lang3.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    * Unit tests for {@link VfsResourceReferenceHandler}.
66    * <p>
67    * Note: We use a different URI for the various unit tests in this class since otherwise they're cached by
68    * TrueVFS. TODO: Find a way to flush TrueVFS caches.
69    *
70    * @version $Id: 12e625e44cfbdae1aab8109813b91aa5c3068db8 $
71    * @since 7.4M2
72    */
 
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   
 
85  2 toggle 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    // Register our custom Attach Driver in TrueVFS
136  2 TConfig config = TConfig.current();
137    // Note: Make sure we add our own Archive Detector to the existing Detector so that all archive formats
138    // supported by TrueVFS are handled properly.
139  2 config.setArchiveDetector(new TArchiveDetector(config.getArchiveDetector(), "attach",
140    new AttachDriver(this.mocker)));
141    }
142   
 
143  1 toggle @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   
 
155  1 toggle @Test
156    public void handleWhenNoGenericPermissionForScheme() throws Exception
157    {
158  1 setUp("customscheme", "wiki3", "space3", "page3", "test.zip", Arrays.asList("test.txt"));
159   
160    // Don't allow permission for "customscheme"
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   
 
172  2 toggle 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    }