| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 44 |
|
|
| 45 |
|
@version |
| 46 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (130) |
Complexity: 19 |
Complexity Density: 0.18 |
|
| 47 |
|
public class ZipExplorerTest extends AbstractBridgedXWikiComponentTestCase |
| 48 |
|
{ |
| 49 |
|
private ZipExplorerPlugin plugin; |
| 50 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 51 |
10 |
protected void setUp() throws Exception... |
| 52 |
|
{ |
| 53 |
10 |
super.setUp(); |
| 54 |
10 |
this.plugin = new ZipExplorerPlugin("zipexplorer", ZipExplorerPlugin.class.getName(), null); |
| 55 |
|
} |
| 56 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 57 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 73 |
1 |
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 |
|
|
| 84 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 91 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 105 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 129 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 144 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 159 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 170 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 191 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 203 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 214 |
2 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 226 |
4 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 5 |
Complexity Density: 0.45 |
|
| 236 |
6 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
| 253 |
4 |
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 |
|
} |