1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
40 |
|
|
41 |
|
@since |
42 |
|
@version |
43 |
|
|
|
|
| 97.8% |
Uncovered Elements: 2 (90) |
Complexity: 13 |
Complexity Density: 0.19 |
|
44 |
|
public abstract class AbstractPackageTest extends AbstractBridgedXWikiComponentTestCase |
45 |
|
{ |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@param |
51 |
|
@param |
52 |
|
@param |
53 |
|
@param |
54 |
|
@return |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 3 |
Complexity Density: 0.19 |
|
56 |
7 |
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 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
@param |
84 |
|
@param |
85 |
|
@return |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
87 |
6 |
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 |
|
|
94 |
|
|
95 |
|
@param |
96 |
|
@param |
97 |
|
@param |
98 |
|
@return |
99 |
|
|
|
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 3 |
Complexity Density: 0.18 |
|
100 |
3 |
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 |
|
|
126 |
|
|
127 |
|
@param |
128 |
|
@param |
129 |
|
@param |
130 |
|
@return |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
132 |
2 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 2 |
Complexity Density: 0.14 |
|
138 |
23 |
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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 3 |
Complexity Density: 0.14 |
|
160 |
10 |
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 |
|
|
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 |
|
} |