PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/zip/ZipOutputStreamTest.java

https://gitlab.com/cde/debian_android-tools_android-platform-libcore
Java | 292 lines | 201 code | 31 blank | 60 comment | 11 complexity | 282a10c9c5e22029b6bd71d73e0c7d7a MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.harmony.tests.java.util.zip;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.util.zip.CRC32;
  24. import java.util.zip.ZipEntry;
  25. import java.util.zip.ZipException;
  26. import java.util.zip.ZipInputStream;
  27. import java.util.zip.ZipOutputStream;
  28. public class ZipOutputStreamTest extends junit.framework.TestCase {
  29. ZipOutputStream zos;
  30. ByteArrayOutputStream bos;
  31. ZipInputStream zis;
  32. static final String data = "HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld";
  33. /**
  34. * java.util.zip.ZipOutputStream#close()
  35. */
  36. public void test_close() throws Exception {
  37. zos = new ZipOutputStream(bos);
  38. zos.putNextEntry(new ZipEntry("XX"));
  39. zos.closeEntry();
  40. zos.close();
  41. // Regression for HARMONY-97
  42. ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream());
  43. zos.putNextEntry(new ZipEntry("myFile"));
  44. zos.close();
  45. zos.close(); // Should be a no-op
  46. }
  47. /**
  48. * java.util.zip.ZipOutputStream#closeEntry()
  49. */
  50. public void test_closeEntry() throws IOException {
  51. ZipEntry ze = new ZipEntry("testEntry");
  52. ze.setTime(System.currentTimeMillis());
  53. zos.putNextEntry(ze);
  54. zos.write("Hello World".getBytes("UTF-8"));
  55. zos.closeEntry();
  56. assertTrue("closeEntry failed to update required fields",
  57. ze.getSize() == 11 && ze.getCompressedSize() == 13);
  58. }
  59. /**
  60. * java.util.zip.ZipOutputStream#finish()
  61. */
  62. public void test_finish() throws Exception {
  63. ZipEntry ze = new ZipEntry("test");
  64. zos.putNextEntry(ze);
  65. zos.write("Hello World".getBytes());
  66. zos.finish();
  67. assertEquals("Finish failed to closeCurrentEntry", 11, ze.getSize());
  68. ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream());
  69. zos.putNextEntry(new ZipEntry("myFile"));
  70. zos.finish();
  71. zos.close();
  72. try {
  73. zos.finish();
  74. fail("Assert 0: Expected IOException");
  75. } catch (IOException e) {
  76. // Expected
  77. }
  78. }
  79. /**
  80. * java.util.zip.ZipOutputStream#putNextEntry(java.util.zip.ZipEntry)
  81. */
  82. public void test_putNextEntryLjava_util_zip_ZipEntry() throws IOException {
  83. ZipEntry ze = new ZipEntry("testEntry");
  84. ze.setTime(System.currentTimeMillis());
  85. zos.putNextEntry(ze);
  86. zos.write("Hello World".getBytes());
  87. zos.closeEntry();
  88. zos.close();
  89. zis = new ZipInputStream(new ByteArrayInputStream(bos.toByteArray()));
  90. ZipEntry ze2 = zis.getNextEntry();
  91. zis.closeEntry();
  92. assertEquals("Failed to write correct entry", ze.getName(), ze2.getName());
  93. assertEquals("Failed to write correct entry", ze.getCrc(), ze2.getCrc());
  94. try {
  95. zos.putNextEntry(ze);
  96. fail("Entry with incorrect setting failed to throw exception");
  97. } catch (IOException e) {
  98. // expected
  99. }
  100. }
  101. /**
  102. * java.util.zip.ZipOutputStream#setComment(java.lang.String)
  103. */
  104. public void test_setCommentLjava_lang_String() {
  105. // There is no way to get the comment back, so no way to determine if
  106. // the comment is set correct
  107. zos.setComment("test setComment");
  108. try {
  109. zos.setComment(new String(new byte[0xFFFF + 1]));
  110. fail("Comment over 0xFFFF in length should throw exception");
  111. } catch (IllegalArgumentException e) {
  112. // Passed
  113. }
  114. }
  115. /**
  116. * java.util.zip.ZipOutputStream#setLevel(int)
  117. */
  118. public void test_setLevelI() throws IOException {
  119. ZipEntry ze = new ZipEntry("test");
  120. zos.putNextEntry(ze);
  121. zos.write(data.getBytes());
  122. zos.closeEntry();
  123. long csize = ze.getCompressedSize();
  124. zos.setLevel(9); // Max Compression
  125. zos.putNextEntry(ze = new ZipEntry("test2"));
  126. zos.write(data.getBytes());
  127. zos.closeEntry();
  128. assertTrue("setLevel failed", csize <= ze.getCompressedSize());
  129. }
  130. /**
  131. * java.util.zip.ZipOutputStream#setMethod(int)
  132. */
  133. public void test_setMethodI() throws IOException {
  134. ZipEntry ze = new ZipEntry("test");
  135. zos.setMethod(ZipOutputStream.STORED);
  136. CRC32 tempCrc = new CRC32();
  137. tempCrc.update(data.getBytes());
  138. ze.setCrc(tempCrc.getValue());
  139. ze.setSize(new String(data).length());
  140. zos.putNextEntry(ze);
  141. zos.write(data.getBytes());
  142. zos.closeEntry();
  143. long csize = ze.getCompressedSize();
  144. zos.setMethod(ZipOutputStream.DEFLATED);
  145. zos.putNextEntry(ze = new ZipEntry("test2"));
  146. zos.write(data.getBytes());
  147. zos.closeEntry();
  148. assertTrue("setLevel failed", csize >= ze.getCompressedSize());
  149. }
  150. /**
  151. * java.util.zip.ZipOutputStream#write(byte[], int, int)
  152. */
  153. public void test_write$BII() throws IOException {
  154. ZipEntry ze = new ZipEntry("test");
  155. zos.putNextEntry(ze);
  156. zos.write(data.getBytes());
  157. zos.closeEntry();
  158. zos.close();
  159. zos = null;
  160. zis = new ZipInputStream(new ByteArrayInputStream(bos.toByteArray()));
  161. zis.getNextEntry();
  162. byte[] b = new byte[data.length()];
  163. int r = 0;
  164. int count = 0;
  165. while (count != b.length && (r = zis.read(b, count, b.length)) != -1) {
  166. count += r;
  167. }
  168. zis.closeEntry();
  169. assertEquals("Write failed to write correct bytes", new String(b), data);
  170. File f = File.createTempFile("testZip", "tst");
  171. f.deleteOnExit();
  172. FileOutputStream stream = new FileOutputStream(f);
  173. ZipOutputStream zip = new ZipOutputStream(stream);
  174. zip.setMethod(ZipEntry.STORED);
  175. try {
  176. zip.putNextEntry(new ZipEntry("Second"));
  177. fail("Not set an entry. Should have thrown ZipException.");
  178. } catch (ZipException e) {
  179. // expected -- We have not set an entry
  180. }
  181. try {
  182. // We try to write data without entry
  183. zip.write(new byte[2]);
  184. fail("Writing data without an entry. Should have thrown IOException");
  185. } catch (IOException e) {
  186. // expected
  187. }
  188. try {
  189. // Try to write without an entry and with nonsense offset and
  190. // length
  191. zip.write(new byte[2], 0, 12);
  192. fail("Writing data without an entry. Should have thrown IndexOutOfBoundsException");
  193. } catch (IndexOutOfBoundsException e) {
  194. // expected
  195. }
  196. // Regression for HARMONY-4405
  197. try {
  198. zip.write(null, 0, -2);
  199. fail();
  200. } catch (NullPointerException expected) {
  201. } catch (IndexOutOfBoundsException expected) {
  202. }
  203. try {
  204. zip.write(null, 0, 2);
  205. fail();
  206. } catch (NullPointerException expected) {
  207. }
  208. try {
  209. zip.write(new byte[2], 0, -2);
  210. fail();
  211. } catch (IndexOutOfBoundsException expected) {
  212. }
  213. // Close stream because ZIP is invalid
  214. stream.close();
  215. }
  216. /**
  217. * java.util.zip.ZipOutputStream#write(byte[], int, int)
  218. */
  219. public void test_write$BII_2() throws IOException {
  220. // Regression for HARMONY-577
  221. File f1 = File.createTempFile("testZip1", "tst");
  222. f1.deleteOnExit();
  223. FileOutputStream stream1 = new FileOutputStream(f1);
  224. ZipOutputStream zip1 = new ZipOutputStream(stream1);
  225. zip1.putNextEntry(new ZipEntry("one"));
  226. zip1.setMethod(ZipOutputStream.STORED);
  227. zip1.setMethod(ZipEntry.STORED);
  228. zip1.write(new byte[2]);
  229. try {
  230. zip1.putNextEntry(new ZipEntry("Second"));
  231. fail("ZipException expected");
  232. } catch (ZipException e) {
  233. // expected - We have not set an entry
  234. }
  235. try {
  236. zip1.write(new byte[2]); // try to write data without entry
  237. fail("expected IOE there");
  238. } catch (IOException e2) {
  239. // expected
  240. }
  241. zip1.close();
  242. }
  243. @Override
  244. protected void setUp() throws Exception {
  245. super.setUp();
  246. zos = new ZipOutputStream(bos = new ByteArrayOutputStream());
  247. }
  248. @Override
  249. protected void tearDown() throws Exception {
  250. try {
  251. if (zos != null) {
  252. zos.close();
  253. }
  254. if (zis != null) {
  255. zis.close();
  256. }
  257. } catch (Exception e) {
  258. }
  259. super.tearDown();
  260. }
  261. }