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

File AbstractPackageTest.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
70
6
1
194
116
13
0.19
11.67
6
2.17

Classes

Class Line # Actions
AbstractPackageTest 44 70 0% 13 2
0.977777897.8%
 

Contributing tests

This file is covered by 9 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   
21    package com.xpn.xwiki.plugin.packaging;
22   
23    import java.io.BufferedReader;
24    import java.io.ByteArrayOutputStream;
25    import java.io.IOException;
26    import java.io.OutputStreamWriter;
27    import java.io.StringReader;
28    import java.util.zip.ZipEntry;
29    import java.util.zip.ZipOutputStream;
30   
31    import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
32    import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
33    import org.xwiki.extension.ExtensionId;
34   
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
37   
38    /**
39    * Utility functions used by ImportTest and PackageTest.
40    *
41    * @since 4.1M2
42    * @version $Id: dac4fb446fa3d8cc4e90f0bb3101031d8e0e559a $
43    */
 
44    public abstract class AbstractPackageTest extends AbstractBridgedXWikiComponentTestCase
45    {
46   
47    /**
48    * Create a XAR file using java.util.zip.
49    *
50    * @param docs The documents to include.
51    * @param encodings The charset for each document.
52    * @param packageXmlEncoding The encoding of package.xml
53    * @param extension the extension id and version or null if none should be added
54    * @return the XAR file as a byte array.
55    */
 
56  7 toggle protected byte[] createZipFile(XWikiDocument docs[], String[] encodings, String packageXmlEncoding,
57    ExtensionId extension) throws Exception
58    {
59  7 ByteArrayOutputStream baos = new ByteArrayOutputStream();
60  7 ZipOutputStream zos = new ZipOutputStream(baos);
61  7 ZipEntry zipp = new ZipEntry("package.xml");
62  7 zos.putNextEntry(zipp);
63  7 zos.write(getEncodedByteArray(getPackageXML(docs, packageXmlEncoding, extension), packageXmlEncoding));
64  16 for (int i = 0; i < docs.length; i++) {
65  9 String zipEntryName = docs[i].getSpace() + "/" + docs[i].getName();
66  9 if (docs[i].getTranslation() != 0) {
67  1 zipEntryName += "." + docs[i].getLanguage();
68    }
69  9 ZipEntry zipe = new ZipEntry(zipEntryName);
70  9 zos.putNextEntry(zipe);
71  9 String xmlCode = docs[i].toXML(false, false, false, false, getContext());
72  9 zos.write(getEncodedByteArray(xmlCode, encodings[i]));
73    }
74  7 zos.finish();
75  7 zos.close();
76  7 return baos.toByteArray();
77    }
78   
79    /**
80    * Create a XAR file using java.util.zip.
81    *
82    * @param docs The documents to include.
83    * @param encodings The charset for each document.
84    * @param extension the extension id and version or null if none should be added
85    * @return the XAR file as a byte array.
86    */
 
87  6 toggle protected byte[] createZipFile(XWikiDocument docs[], String[] encodings, ExtensionId extension) throws Exception
88    {
89  6 return createZipFile(docs, encodings, "ISO-8859-1", extension);
90    }
91   
92    /**
93    * Create a XAR file using commons compress.
94    *
95    * @param docs The documents to include.
96    * @param encodings The charset for each document.
97    * @param packageXmlEncoding The encoding of package.xml
98    * @return the XAR file as a byte array.
99    */
 
100  3 toggle protected byte[] createZipFileUsingCommonsCompress(XWikiDocument docs[], String[] encodings,
101    String packageXmlEncoding, ExtensionId extension) throws Exception
102    {
103  3 ByteArrayOutputStream baos = new ByteArrayOutputStream();
104  3 ZipArchiveOutputStream zos = new ZipArchiveOutputStream(baos);
105  3 ZipArchiveEntry zipp = new ZipArchiveEntry("package.xml");
106  3 zos.putArchiveEntry(zipp);
107  3 zos.write(getEncodedByteArray(getPackageXML(docs, packageXmlEncoding, extension), packageXmlEncoding));
108  7 for (int i = 0; i < docs.length; i++) {
109  4 String zipEntryName = docs[i].getSpace() + "/" + docs[i].getName();
110  4 if (docs[i].getTranslation() != 0) {
111  0 zipEntryName += "." + docs[i].getLanguage();
112    }
113  4 ZipArchiveEntry zipe = new ZipArchiveEntry(zipEntryName);
114  4 zos.putArchiveEntry(zipe);
115  4 String xmlCode = docs[i].toXML(false, false, false, false, getContext());
116  4 zos.write(getEncodedByteArray(xmlCode, encodings[i]));
117  4 zos.closeArchiveEntry();
118    }
119  3 zos.finish();
120  3 zos.close();
121  3 return baos.toByteArray();
122    }
123   
124    /**
125    * Create a XAR file using commons compress.
126    *
127    * @param docs The documents to include.
128    * @param encodings The charset for each document.
129    * @param extension the extension id and version or null if none should be added
130    * @return the XAR file as a byte array.
131    */
 
132  2 toggle protected byte[] createZipFileUsingCommonsCompress(XWikiDocument docs[], String[] encodings, ExtensionId extension)
133    throws Exception
134    {
135  2 return createZipFileUsingCommonsCompress(docs, encodings, "ISO-8859-1", extension);
136    }
137   
 
138  23 toggle private byte[] getEncodedByteArray(String content, String charset) throws IOException
139    {
140  23 StringReader rdr = new StringReader(content);
141  23 BufferedReader bfr = new BufferedReader(rdr);
142  23 ByteArrayOutputStream ostr = new ByteArrayOutputStream();
143  23 OutputStreamWriter os = new OutputStreamWriter(ostr, charset);
144   
145    // Voluntarily ignore the first line... as it's the xml declaration
146  23 String line = bfr.readLine();
147  23 os.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\n");
148   
149  23 line = bfr.readLine();
150  407 while (null != line) {
151  384 os.append(line);
152  384 os.append("\n");
153  384 line = bfr.readLine();
154    }
155  23 os.flush();
156  23 os.close();
157  23 return ostr.toByteArray();
158    }
159   
 
160  10 toggle private String getPackageXML(XWikiDocument docs[], String encoding, ExtensionId extension)
161    {
162  10 StringBuilder sb = new StringBuilder();
163  10 sb.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
164  10 sb.append("<package>\n").append("<infos>\n").append("<name>Backup</name>\n");
165  10 sb.append("<description>on Mon Jan 01 01:44:32 CET 2007 by XWiki.Admin</description>\n");
166  10 sb.append("<licence></licence>\n");
167  10 sb.append("<author>XWiki.Admin</author>\n");
168  10 sb.append("<backupPack>true</backupPack>\n");
169   
170    // Add extension related informations
171  10 if (extension != null) {
172  1 sb.append("<extensionId>");
173  1 sb.append(extension.getId());
174  1 sb.append("</extensionId>\n");
175  1 sb.append("<version>");
176  1 sb.append(extension.getVersion());
177  1 sb.append("</version>\n");
178    } else {
179  9 sb.append("<version></version>\n");
180    }
181   
182  10 sb.append("</infos>\n");
183   
184  10 sb.append("<files>\n");
185  23 for (int i = 0; i < docs.length; i++) {
186   
187  13 sb.append("<file defaultAction=\"0\" language=\"" + docs[i].getLanguage() + "\">" + docs[i].getFullName()
188    + "</file>\n");
189    }
190  10 sb.append("</files></package>\n");
191  10 return sb.toString();
192    }
193   
194    }