Index: core/src/test/java/com/xpn/xwiki/plugin/packaging/PackageTest.java =================================================================== --- core/src/test/java/com/xpn/xwiki/plugin/packaging/PackageTest.java (revision 0) +++ core/src/test/java/com/xpn/xwiki/plugin/packaging/PackageTest.java (revision 0) @@ -0,0 +1,118 @@ +package com.xpn.xwiki.plugin.packaging; + +import com.xpn.xwiki.plugin.packaging.Package; +import com.xpn.xwiki.doc.XWikiDocument; +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.XWiki; +import com.xpn.xwiki.XWikiConfig; +import com.xpn.xwiki.XWikiException; + +import java.io.*; +import java.util.zip.ZipOutputStream; +import java.util.zip.ZipEntry; +import java.text.MessageFormat; + +import org.jmock.Mock; + +/** + * Unit tests for the {@link com.xpn.xwiki.plugin.packaging.Package} class. + * + * @version $Id: $ + */ +public class PackageTest extends org.jmock.cglib.MockObjectTestCase { + private Package pack; + private XWikiContext context; + private Mock mockDocument; + private Mock mockXWiki; + + protected void setUp() throws XWikiException + { + this.pack = new Package(); + this.context = new XWikiContext(); + + this.mockXWiki = mock(XWiki.class, new Class[] {XWikiConfig.class, XWikiContext.class}, + new Object[] {new XWikiConfig(), this.context}); + mockXWiki.stubs().method("getEncoding").will(returnValue("UTF-8")); + mockXWiki.stubs().method("checkAccess").will(returnValue(true)); + this.context.setWiki((XWiki) this.mockXWiki.proxy()); + } + + public void testHeterogeneousEncodingFilesInImport() throws Exception { + String docTitle = "Un été 36"; + String docContent = "àçéèÀÇÉÈïöëü"; + + XWikiDocument doc1 = new XWikiDocument("Main", "Document1"); + doc1.setTitle(docTitle); + doc1.setContent(docContent); + + XWikiDocument doc2 = new XWikiDocument("Main", "Document2"); + doc2.setTitle(docTitle); + doc2.setContent(docContent); + + XWikiDocument docs[] = {doc1, doc2}; + + pack.Import(this.createZipFile(docs), context); + + assertEquals(2, pack.getFiles().size()); + assertEquals(((DocumentInfo) pack.getFiles().get(0)).getDoc().getTitle(), ((DocumentInfo) pack.getFiles().get(1)).getDoc().getTitle()); + assertEquals(((DocumentInfo) pack.getFiles().get(0)).getDoc().getContent(), ((DocumentInfo) pack.getFiles().get(1)).getDoc().getContent()); + + } + + private String getPackageXML(XWikiDocument docs[]) { + StringBuilder sb = new StringBuilder(); + sb.append("\n"); + sb.append("\n").append("\n").append("Backup\n"); + sb.append("on Mon Jan 01 01:44:32 CET 2007 by XWiki.Admin\n"); + sb.append("\n"); + sb.append("XWiki.Admin\n"); + sb.append("\n"); + sb.append("true\n"); + sb.append("\n"); + sb.append("\n"); + for (int i=0; i < docs.length; i++) { + sb.append(""+ docs[i].getFullName() + "\n"); + } + sb.append("\n"); + return sb.toString(); + + } + + private byte[] getEncodedByteArray(String content, String charset) throws IOException { + StringReader rdr = new StringReader(content); + BufferedReader bfr = new BufferedReader(rdr); + ByteArrayOutputStream ostr = new ByteArrayOutputStream(); + OutputStreamWriter os = new OutputStreamWriter(ostr, charset); + String line = bfr.readLine(); + // Voluntarily ignore the first line... as it's the xml declaration + os.append(MessageFormat.format("\n", charset)); + line = bfr.readLine(); + while (null != line) { + os.append(line); + os.append("\n"); + line = bfr.readLine(); + } + os.flush(); + os.close(); + return ostr.toByteArray(); + } + + private byte[] createZipFile(XWikiDocument docs[]) throws Exception + { + String charsets[] = {"ISO-8859-1", "UTF-8"}; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ZipOutputStream zos = new ZipOutputStream(baos); + ZipEntry zipp = new ZipEntry("package.xml"); + zos.putNextEntry(zipp); + zos.write(getEncodedByteArray(getPackageXML(docs), "ISO-8859-1")); + for (int i=0; i < docs.length; i++) { + ZipEntry zipe = new ZipEntry(docs[i].getSpace() + "/" + docs[i].getName()); + zos.putNextEntry(zipe); + String xmlCode = docs[i].toXML(false, false, false, false, context); + zos.write(getEncodedByteArray(xmlCode, charsets[i%charsets.length])); + } + zos.closeEntry(); + return baos.toByteArray(); + } + +}