Index: ZipExplorerTest.java =================================================================== --- ZipExplorerTest.java (revision 1903) +++ ZipExplorerTest.java (working copy) @@ -22,12 +22,16 @@ package com.xpn.xwiki.plugin.zipexplorer; import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.XWiki; +import com.xpn.xwiki.XWikiConfig; +import com.xpn.xwiki.test.HibernateTestCase; import com.xpn.xwiki.objects.classes.ListItem; import com.xpn.xwiki.api.Document; import com.xpn.xwiki.doc.XWikiAttachment; import com.xpn.xwiki.doc.XWikiDocument; import com.xpn.xwiki.web.XWikiRequest; import org.jmock.Mock; +import org.apache.velocity.app.Velocity; import java.util.Date; import java.util.List; @@ -35,13 +39,17 @@ import java.util.zip.ZipEntry; import java.io.ByteArrayOutputStream; + public class ZipExplorerTest extends org.jmock.cglib.MockObjectTestCase { private ZipExplorerPlugin plugin; + protected XWiki xwiki; + protected XWikiConfig config; + protected XWikiContext context; - protected void setUp() - { - this.plugin = new ZipExplorerPlugin("zipexplorer", ZipExplorerPlugin.class.getName(), null); + protected void setUp() throws Exception { + createContext(); + this.plugin = new ZipExplorerPlugin("zipexplorer", ZipExplorerPlugin.class.getName(), this.context); } public void testIsZipFile() @@ -75,46 +83,99 @@ } public void testDownloadAttachmentWithInvalidZipURL() throws Exception { + XWikiDocument document = new XWikiDocument(); + document.setSpace("Main"); + document.setName("InvalidZip"); + document.setId(document.getId()); XWikiAttachment originalAttachment = createAttachment("someFile.txt", - "Some text".getBytes(), (XWikiDocument) mock(XWikiDocument.class).proxy()); + "Some text".getBytes(), document); XWikiContext context = createXWikiContext("http://server/xwiki/bin/download/Main/Document/someFile.txt"); - XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context); assertSame(originalAttachment, newAttachment); + this.xwiki.flushCache(); } public void testDownloadAttachment() throws Exception { String zipFileContent = "File.txt content"; + XWikiDocument document = new XWikiDocument(); + document.setSpace("Main"); + document.setName("TestDownload"); + document.setId(document.getId()); XWikiAttachment originalAttachment = createAttachment("zipfile.zip", - createZipFile(zipFileContent), (XWikiDocument) mock(XWikiDocument.class).proxy()); - + createZipFile(zipFileContent), document); XWikiContext context = createXWikiContext( "http://server/xwiki/bin/download/Main/Document/zipfile.zip/Directory/File.txt"); + XWikiContext secondAttemptContext = createXWikiContext( + "http://server/xwiki/bin/download/Main/Document/zipfile.zip/Directory/File.txt"); + + long timeBeforeFirstAttempt = new Date().getTime(); XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context); + long timeTakenForFirstAttempt = new Date().getTime() - timeBeforeFirstAttempt; + assertTrue("Time taken for the first attempt ", timeTakenForFirstAttempt > 0); assertEquals("Directory/File.txt", newAttachment.getFilename()); assertEquals(zipFileContent.length(), newAttachment.getFilesize()); assertEquals(zipFileContent, new String(newAttachment.getContent(context))); + assertEquals("Version should be equal", "1.1", newAttachment.getVersion()); + + long timeBeforeSecondAttempt = new Date().getTime(); + XWikiAttachment newAttachment1 = this.plugin.downloadAttachment(originalAttachment, secondAttemptContext); + long timeTakenForSecondAttempt = new Date().getTime() - timeBeforeSecondAttempt; + assertTrue("Time taken for the second attempt should be less than first attempt ", timeTakenForSecondAttempt < timeTakenForFirstAttempt); + + assertEquals("Directory/File.txt", newAttachment1.getFilename()); + assertEquals(zipFileContent.length(), newAttachment1.getFilesize()); + assertEquals(zipFileContent, new String(newAttachment1.getContent(context))); + assertEquals("Version should be equal", "1.1", newAttachment1.getVersion()); + + this.xwiki.flushCache(); + } public void testGetFileList() throws Exception { - XWikiDocument document = createXWikiDocumentWithZipFileAttachment(); + XWikiDocument doc = new XWikiDocument(); + doc.setSpace("Main"); + doc.setName("Document"); + doc.setId(doc.getId()); + XWikiDocument document = createXWikiDocumentWithZipFileAttachment(doc,"zipfile.zip"); + long timeBeforeFirstAttempt = new Date().getTime(); List entries = this.plugin.getFileList(new Document(document, null), "zipfile.zip", null); + long timeTakenForFirstAttempt = new Date().getTime() - timeBeforeFirstAttempt; + assertTrue("Time taken to get file list is less than zero " , timeTakenForFirstAttempt > 0); assertEquals(2, entries.size()); assertEquals("Directory/File.txt", (String) entries.get(0)); assertEquals("File2.txt", (String) entries.get(1)); + + long timeBeforeSecondAttempt = new Date().getTime(); + List entries1 = this.plugin.getFileList(new Document(document, null), "zipfile.zip", null); + long timeTakenForSecondAttempt = new Date().getTime() - timeBeforeSecondAttempt; + + assertTrue("Time taken to get file list for the second attempt should be less than the first attempt " , timeTakenForSecondAttempt < timeTakenForFirstAttempt ); + + assertEquals(2, entries1.size()); + assertEquals("Directory/File.txt", (String) entries1.get(0)); + assertEquals("File2.txt", (String) entries1.get(1)); + + this.xwiki.flushCache(); } public void testGetFileTreeList() throws Exception { - XWikiDocument document = createXWikiDocumentWithZipFileAttachment(); + XWikiDocument doc = new XWikiDocument(); + doc.setSpace("Main"); + doc.setName("TestDocument"); + doc.setId(doc.getId()); + XWikiDocument document = createXWikiDocumentWithZipFileAttachment(doc, "zipfile2.zip"); + long timeBeforeFirstAttempt = new Date().getTime(); List entries = - this.plugin.getFileTreeList(new Document(document, null), "zipfile.zip", null); + this.plugin.getFileTreeList(new Document(document, null), "zipfile2.zip", null); + long timeTakenForFirstAttempt = new Date().getTime() - timeBeforeFirstAttempt; + assertTrue("Time taken to getFileTreeList is less than zero " , timeTakenForFirstAttempt > 0); assertEquals(3, entries.size()); @@ -129,6 +190,28 @@ assertEquals("File2.txt", ((ListItem) entries.get(2)).getId()); assertEquals("File2.txt", ((ListItem) entries.get(2)).getValue()); assertEquals("", ((ListItem) entries.get(2)).getParent()); + + long timeBeforeSecondAttempt = new Date().getTime(); + List entries1 = + this.plugin.getFileTreeList(new Document(document, null), "zipfile2.zip", null); + long timeTakenForSecondAttempt = new Date().getTime() - timeBeforeSecondAttempt; + assertTrue("Time taken to getFileTreeList for the second attempt should be less than the first attempt " , timeTakenForSecondAttempt < timeTakenForFirstAttempt ); + + assertEquals(3, entries1.size()); + + assertEquals("Directory/", ((ListItem) entries1.get(0)).getId()); + assertEquals("Directory", ((ListItem) entries1.get(0)).getValue()); + assertEquals("", ((ListItem) entries1.get(0)).getParent()); + + assertEquals("Directory/File.txt", ((ListItem) entries1.get(1)).getId()); + assertEquals("File.txt", ((ListItem) entries1.get(1)).getValue()); + assertEquals("Directory/", ((ListItem) entries1.get(1)).getParent()); + + assertEquals("File2.txt", ((ListItem) entries1.get(2)).getId()); + assertEquals("File2.txt", ((ListItem) entries1.get(2)).getValue()); + assertEquals("", ((ListItem) entries1.get(2)).getParent()); + + this.xwiki.flushCache(); } public void testGetFileLink() throws Exception { @@ -151,14 +234,15 @@ "http://server/xwiki/bin/download/Main/Document/zipfile.zip", "download")); } - private XWikiDocument createXWikiDocumentWithZipFileAttachment() throws Exception { + private XWikiDocument createXWikiDocumentWithZipFileAttachment(XWikiDocument document, String attachmentName) throws Exception { Mock mockDocument = mock(XWikiDocument.class); - XWikiDocument document = (XWikiDocument) mockDocument.proxy(); - XWikiAttachment attachment = - createAttachment("zipfile.zip", createZipFile("Some content"), document); + XWikiAttachment attachment = createAttachment(attachmentName, createZipFile("Some content"), document); mockDocument.stubs().method("clone").will(returnValue(mockDocument.proxy())); mockDocument.stubs().method("getAttachment").will(returnValue(attachment)); - return document; + mockDocument.stubs().method("getFullName").will(returnValue(document.getFullName())); + XWikiDocument doc = (XWikiDocument)mockDocument.proxy(); + + return doc; } private XWikiContext createXWikiContext(String url) { @@ -171,7 +255,7 @@ } private XWikiAttachment createAttachment(String filename, byte[] content, - XWikiDocument document) throws Exception + XWikiDocument document) throws Exception { Mock mockAttachment = mock(XWikiAttachment.class); mockAttachment.stubs().method("getFilename").will(returnValue(filename)); @@ -179,7 +263,9 @@ mockAttachment.stubs().method("getAuthor").will(returnValue("Vincent")); mockAttachment.stubs().method("getDate").will(returnValue(new Date())); mockAttachment.stubs().method("getContent").will(returnValue(content)); - return (XWikiAttachment) mockAttachment.proxy(); + mockAttachment.stubs().method("getVersion").will(returnValue(document.getVersion())); + mockAttachment.stubs().method("getId").will(returnValue(document.getId())); + return (XWikiAttachment)mockAttachment.proxy(); } private byte[] createZipFile(String content) throws Exception @@ -195,4 +281,26 @@ zos.closeEntry(); return baos.toByteArray(); } + + private void createContext() throws Exception { + getConfig(); + + this.context = new XWikiContext(); + this.context.setDatabase("xwikitest"); + + this.xwiki = new XWiki(this.config, this.context); + this.xwiki.setDatabase("xwikitest"); + + this.context.setWiki(this.xwiki); + + this.xwiki.flushCache(); + + Velocity.init(getClass().getResource("/velocity.properties").getFile()); + } + + protected void getConfig() { + this.config = new XWikiConfig(); + this.config.put("xwiki.store.hibernate.path", getClass().getResource(HibernateTestCase.HIB_LOCATION).getFile()); + this.config.put("xwiki.plugin.zipexplorer.cache.capacity","1000"); + } }