1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.zipexplorer

File ZipExplorerTest.java

 

Code metrics

8
107
15
1
266
195
19
0.18
7.13
15
1.27

Classes

Class Line # Actions
ZipExplorerTest 47 107 0% 19 0
1.0100%
 

Contributing tests

This file is covered by 10 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 com.xpn.xwiki.plugin.zipexplorer;
21   
22    import com.xpn.xwiki.XWikiContext;
23    import com.xpn.xwiki.objects.classes.ListItem;
24    import com.xpn.xwiki.api.Document;
25    import com.xpn.xwiki.doc.XWikiAttachment;
26    import com.xpn.xwiki.doc.XWikiDocument;
27    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
28    import com.xpn.xwiki.web.XWikiRequest;
29    import org.jmock.Mock;
30   
31    import java.util.Date;
32    import java.util.List;
33    import java.util.zip.ZipOutputStream;
34    import java.util.zip.ZipEntry;
35    import java.io.ByteArrayInputStream;
36    import java.io.ByteArrayOutputStream;
37   
38    import org.junit.Assert;
39    import org.xwiki.model.reference.AttachmentReference;
40    import org.xwiki.model.reference.DocumentReference;
41   
42    /**
43    * Unit tests for the {@link com.xpn.xwiki.plugin.zipexplorer.ZipExplorerPlugin} class.
44    *
45    * @version $Id: bf825c3fa5287acc0896aa01ac20b192c20a502b $
46    */
 
47    public class ZipExplorerTest extends AbstractBridgedXWikiComponentTestCase
48    {
49    private ZipExplorerPlugin plugin;
50   
 
51  10 toggle protected void setUp() throws Exception
52    {
53  10 super.setUp();
54  10 this.plugin = new ZipExplorerPlugin("zipexplorer", ZipExplorerPlugin.class.getName(), null);
55    }
56   
 
57  1 toggle public void testIsZipFile() throws Exception
58    {
59  1 byte txtbuf[] = {0x00, 0x01, 0x02, 0x03, 0x06, 0x07};
60  1 ByteArrayInputStream txtBais = new ByteArrayInputStream(txtbuf);
61  1 Assert.assertFalse(this.plugin.isZipFile(txtBais));
62   
63  1 byte tinybuf[] = {0x00};
64  1 ByteArrayInputStream tinyBais = new ByteArrayInputStream(tinybuf);
65  1 Assert.assertFalse(this.plugin.isZipFile(tinyBais));
66   
67  1 byte zipbuf[] = createZipFile("test");
68  1 ByteArrayInputStream zipBais = new ByteArrayInputStream(zipbuf);
69  1 Assert.assertTrue(this.plugin.isZipFile(zipBais));
70   
71    }
72   
 
73  1 toggle public void testIsValidZipURL()
74    {
75  1 Assert.assertTrue(this.plugin.isValidZipURL(
76    "http://server/xwiki/bin/download/Main/Document/zipfile.zip/Directory/File.txt", "download"));
77  1 Assert.assertFalse(this.plugin.isValidZipURL(
78    "http://server/xwiki/bin/download/Main/Document/zipfile.zip/Directory/File.txt", "view"));
79  1 Assert.assertFalse(
80    this.plugin.isValidZipURL("http://server/xwiki/bin/download/Main/Document/zipfile.zip", "download"));
81  1 Assert.assertFalse(this.plugin.isValidZipURL("http://server/xwiki/bin/download/Main/Document", "download"));
82   
83    // These tests should normally fail but we haven't implemented the check to verify if the
84    // ZIP URL points to a file rather than a dir.
85  1 Assert.assertTrue(this.plugin.isValidZipURL(
86    "http://server/xwiki/bin/download/Main/Document/zipfile.zip/Directory/Dir2/", "download"));
87  1 Assert.assertTrue(this.plugin.isValidZipURL(
88    "http://server/xwiki/bin/download/Main/Document/zipfile.zip/Directory/Dir2", "download"));
89    }
90   
 
91  1 toggle public void testDownloadAttachmentWithInvalidZipURL() throws Exception
92    {
93  1 Mock mockDocument = mock(XWikiDocument.class);
94  1 mockDocument.stubs().method("getDocumentReference").will(returnValue(
95    new DocumentReference("wiki", "Main", "Document")));
96  1 XWikiAttachment originalAttachment =
97    createAttachment("someFile.txt", "Some text".getBytes(), (XWikiDocument) mockDocument.proxy());
98  1 XWikiContext context = createXWikiContext("http://server/xwiki/bin/download/Main/Document/someFile.txt");
99   
100  1 XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context);
101   
102  1 Assert.assertSame(originalAttachment, newAttachment);
103    }
104   
 
105  1 toggle public void testDownloadAttachment() throws Exception
106    {
107  1 String zipFileContent = "File.txt content";
108  1 Mock mockDocument = mock(XWikiDocument.class);
109  1 mockDocument.stubs().method("setContentDirty");
110  1 mockDocument.stubs().method("setMetaDataDirty");
111  1 mockDocument.stubs().method("getDocumentReference").will(returnValue(
112    new DocumentReference("wiki", "Main", "Document")));
113   
114  1 XWikiAttachment originalAttachment =
115    createAttachment("zipfile.zip", createZipFile(zipFileContent),
116    (XWikiDocument) mockDocument.proxy());
117   
118  1 XWikiContext context =
119    createXWikiContext("http://server/xwiki/bin/download/Main/Document/zipfile.zip/Directory/File.txt");
120   
121  1 XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context);
122   
123  1 Assert.assertEquals("Directory/File.txt", newAttachment.getFilename());
124  1 Assert.assertEquals(zipFileContent.length(), newAttachment.getFilesize());
125  1 Assert.assertEquals(zipFileContent.length(), newAttachment.getContentSize(context));
126  1 Assert.assertEquals(zipFileContent, new String(newAttachment.getContent(context)));
127    }
128   
 
129  1 toggle public void testDownloadAttachmentWhenURLIsNotZipFile() throws Exception
130    {
131  1 Mock mockDocument = mock(XWikiDocument.class);
132  1 mockDocument.stubs().method("getDocumentReference").will(returnValue(
133    new DocumentReference("wiki", "Main", "Document")));
134  1 XWikiAttachment originalAttachment =
135    createAttachment("somefile.whatever", null, (XWikiDocument) mockDocument.proxy());
136   
137  1 XWikiContext context = createXWikiContext("http://server/xwiki/bin/download/Main/Document/somefile.whatever");
138   
139  1 XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context);
140   
141  1 Assert.assertSame(originalAttachment, newAttachment);
142    }
143   
 
144  1 toggle public void testDownloadAttachmentWhenURLIsZipButNotPointingInsideZip() throws Exception
145    {
146  1 Mock mockDocument = mock(XWikiDocument.class);
147  1 mockDocument.stubs().method("getDocumentReference").will(returnValue(
148    new DocumentReference("wiki", "Main", "Document")));
149  1 XWikiAttachment originalAttachment =
150    createAttachment("zipfile.zip", null, (XWikiDocument) mockDocument.proxy());
151   
152  1 XWikiContext context = createXWikiContext("http://server/xwiki/bin/download/Main/Document/zipfile.zip");
153   
154  1 XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context);
155   
156  1 Assert.assertSame(originalAttachment, newAttachment);
157    }
158   
 
159  1 toggle public void testGetFileList() throws Exception
160    {
161  1 XWikiDocument document = createXWikiDocumentWithZipFileAttachment();
162   
163  1 List<String> entries = this.plugin.getFileList(new Document(document, null), "zipfile.zip", null);
164   
165  1 Assert.assertEquals(2, entries.size());
166  1 Assert.assertEquals("Directory/File.txt", entries.get(0));
167  1 Assert.assertEquals("File2.txt", entries.get(1));
168    }
169   
 
170  1 toggle public void testGetFileTreeList() throws Exception
171    {
172  1 XWikiDocument document = createXWikiDocumentWithZipFileAttachment();
173   
174  1 List<ListItem> entries = this.plugin.getFileTreeList(new Document(document, null), "zipfile.zip", null);
175   
176  1 Assert.assertEquals(3, entries.size());
177   
178  1 Assert.assertEquals("Directory/", entries.get(0).getId());
179  1 Assert.assertEquals("Directory", entries.get(0).getValue());
180  1 Assert.assertEquals("", entries.get(0).getParent());
181   
182  1 Assert.assertEquals("Directory/File.txt", entries.get(1).getId());
183  1 Assert.assertEquals("File.txt", entries.get(1).getValue());
184  1 Assert.assertEquals("Directory/", entries.get(1).getParent());
185   
186  1 Assert.assertEquals("File2.txt", entries.get(2).getId());
187  1 Assert.assertEquals("File2.txt", entries.get(2).getValue());
188  1 Assert.assertEquals("", entries.get(2).getParent());
189    }
190   
 
191  1 toggle public void testGetFileLink() throws Exception
192    {
193  1 Mock mockDocument = mock(XWikiDocument.class);
194  1 mockDocument.expects(once()).method("getAttachmentURL").will(
195    returnValue("http://server/xwiki/bin/download/Main/Document/zipfile.zip"));
196  1 Document document = new Document((XWikiDocument) mockDocument.proxy(), null);
197   
198  1 String link = this.plugin.getFileLink(document, "zipfile.zip", "filename", null);
199   
200  1 Assert.assertEquals("http://server/xwiki/bin/download/Main/Document/zipfile.zip/filename", link);
201    }
202   
 
203  1 toggle public void testGetFileLocationFromZipURL()
204    {
205  1 String urlPrefix = "server/xwiki/bin/download/Main/Document/zipfile.zip";
206   
207  1 assertEquals("Directory/File.txt", this.plugin.getFileLocationFromZipURL(urlPrefix + "/Directory/File.txt",
208    "download"));
209  1 assertEquals("", this.plugin.getFileLocationFromZipURL(urlPrefix, "download"));
210  1 assertEquals("Some Directory/File WithSpace.txt", this.plugin.getFileLocationFromZipURL(urlPrefix
211    + "/Some%20Directory/File%20WithSpace.txt", "download"));
212    }
213   
 
214  2 toggle private XWikiDocument createXWikiDocumentWithZipFileAttachment() throws Exception
215    {
216  2 Mock mockDocument = mock(XWikiDocument.class);
217  2 XWikiDocument document = (XWikiDocument) mockDocument.proxy();
218  2 mockDocument.stubs().method("getDocumentReference").will(returnValue(
219    new DocumentReference("wiki", "Main", "Document")));
220  2 XWikiAttachment attachment = createAttachment("zipfile.zip", createZipFile("Some content"), document);
221  2 mockDocument.stubs().method("clone").will(returnValue(mockDocument.proxy()));
222  2 mockDocument.stubs().method("getAttachment").will(returnValue(attachment));
223  2 return document;
224    }
225   
 
226  4 toggle private XWikiContext createXWikiContext(String url)
227    {
228  4 Mock mockRequest = mock(XWikiRequest.class);
229  4 mockRequest.expects(once()).method("getRequestURI").will(returnValue(url));
230  4 XWikiContext context = new XWikiContext();
231  4 context.setRequest((XWikiRequest) mockRequest.proxy());
232  4 context.setAction("download");
233  4 return context;
234    }
235   
 
236  6 toggle private XWikiAttachment createAttachment(String filename, byte[] content, XWikiDocument document) throws Exception
237    {
238  6 Mock mockAttachment = mock(XWikiAttachment.class);
239  6 mockAttachment.stubs().method("getFilename").will(returnValue(filename));
240  6 mockAttachment.stubs().method("getDoc").will(returnValue(document));
241  6 mockAttachment.stubs().method("getAuthor").will(returnValue("Vincent"));
242  6 mockAttachment.stubs().method("getDate").will(returnValue(new Date()));
243  6 mockAttachment.stubs().method("getFilesize").will(returnValue((content == null) ? 0 : content.length));
244  6 mockAttachment.stubs().method("getContentSize").will(returnValue((content == null) ? 0 : content.length));
245  6 mockAttachment.stubs().method("getContent").will(returnValue((content == null) ? new byte[0] : content));
246  6 mockAttachment.stubs().method("getContentInputStream").will(
247  6 returnValue(new ByteArrayInputStream((content == null) ? new byte[0] : content)));
248  6 mockAttachment.stubs().method("getReference").will(returnValue(
249    new AttachmentReference(filename, document.getDocumentReference())));
250  6 return (XWikiAttachment) mockAttachment.proxy();
251    }
252   
 
253  4 toggle private byte[] createZipFile(String content) throws Exception
254    {
255  4 ByteArrayOutputStream baos = new ByteArrayOutputStream();
256  4 ZipOutputStream zos = new ZipOutputStream(baos);
257  4 ZipEntry zipe = new ZipEntry("Directory/File.txt");
258  4 zos.putNextEntry(zipe);
259  4 zos.write(content.getBytes());
260  4 ZipEntry zipe2 = new ZipEntry("File2.txt");
261  4 zos.putNextEntry(zipe2);
262  4 zos.write(content.getBytes());
263  4 zos.closeEntry();
264  4 return baos.toByteArray();
265    }
266    }