View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.compressors.pack200;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.OutputStream;
25  import java.util.HashMap;
26  
27  import org.apache.commons.compress.AbstractTestCase;
28  import org.apache.commons.compress.archivers.ArchiveEntry;
29  import org.apache.commons.compress.archivers.ArchiveInputStream;
30  import org.apache.commons.compress.archivers.ArchiveStreamFactory;
31  import org.apache.commons.compress.utils.IOUtils;
32  
33  public final class Pack200UtilsTest extends AbstractTestCase {
34  
35      public void testNormalize() throws Throwable {
36          final File input = getFile("bla.jar");
37          final File[] output = createTempDirAndFile();
38          try {
39              Pack200Utils.normalize(input, output[1],
40                                     new HashMap<String, String>());
41              final FileInputStream is = new FileInputStream(output[1]);
42              try {
43                  final ArchiveInputStream in = new ArchiveStreamFactory()
44                      .createArchiveInputStream("jar", is);
45  
46                  ArchiveEntry entry = in.getNextEntry();
47                  while (entry != null) {
48                      File archiveEntry = new File(dir, entry.getName());
49                      archiveEntry.getParentFile().mkdirs();
50                      if (entry.isDirectory()) {
51                          archiveEntry.mkdir();
52                          entry = in.getNextEntry();
53                          continue;
54                      }
55                      OutputStream out = new FileOutputStream(archiveEntry);
56                      IOUtils.copy(in, out);
57                      out.close();
58                      entry = in.getNextEntry();
59                  }
60  
61                  in.close();
62              } finally {
63                  is.close();
64              }
65          } finally {
66              output[1].delete();
67              output[0].delete();
68          }
69      }
70  
71      public void testNormalizeInPlace() throws Throwable {
72          final File input = getFile("bla.jar");
73          final File[] output = createTempDirAndFile();
74          try {
75              FileInputStream is = new FileInputStream(input);
76              OutputStream os = null;
77              try {
78                  os = new FileOutputStream(output[1]);
79                  IOUtils.copy(is, os);
80              } finally {
81                  is.close();
82                  if (os != null) {
83                      os.close();
84                  }
85              }
86  
87              Pack200Utils.normalize(output[1]);
88              is = new FileInputStream(output[1]);
89              try {
90                  final ArchiveInputStream in = new ArchiveStreamFactory()
91                      .createArchiveInputStream("jar", is);
92  
93                  ArchiveEntry entry = in.getNextEntry();
94                  while (entry != null) {
95                      File archiveEntry = new File(dir, entry.getName());
96                      archiveEntry.getParentFile().mkdirs();
97                      if (entry.isDirectory()) {
98                          archiveEntry.mkdir();
99                          entry = in.getNextEntry();
100                         continue;
101                     }
102                     OutputStream out = new FileOutputStream(archiveEntry);
103                     IOUtils.copy(in, out);
104                     out.close();
105                     entry = in.getNextEntry();
106                 }
107 
108                 in.close();
109             } finally {
110                 is.close();
111             }
112         } finally {
113             output[1].delete();
114             output[0].delete();
115         }
116     }
117 
118 }