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.archivers.cpio;
20  
21  import static org.junit.Assert.*;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.InputStream;
27  
28  import org.apache.commons.compress.AbstractTestCase;
29  import org.apache.commons.compress.utils.IOUtils;
30  import org.junit.Test;
31  
32  public class CpioArchiveOutputStreamTest extends AbstractTestCase {
33  
34      @Test
35      public void testWriteOldBinary() throws Exception {
36          final File f = getFile("test1.xml");
37          final File output = new File(dir, "test.cpio");
38          final FileOutputStream out = new FileOutputStream(output);
39          InputStream in = null;
40          try {
41              final CpioArchiveOutputStream os =
42                  new CpioArchiveOutputStream(out, CpioConstants
43                                              .FORMAT_OLD_BINARY);
44              os.putArchiveEntry(new CpioArchiveEntry(CpioConstants
45                                                      .FORMAT_OLD_BINARY,
46                                                      f, "test1.xml"));
47              IOUtils.copy(in = new FileInputStream(f), os);
48              in.close();
49              in = null;
50              os.closeArchiveEntry();
51              os.close();
52          } finally {
53              if (in != null) {
54                  in.close();
55              }
56              out.close();
57          }
58  
59          try {
60              in = new CpioArchiveInputStream(new FileInputStream(output));
61              CpioArchiveEntry e = ((CpioArchiveInputStream) in)
62                  .getNextCPIOEntry();
63              assertEquals("test1.xml", e.getName());
64              assertNull(((CpioArchiveInputStream) in).getNextEntry());
65          } finally {
66              if (in != null) {
67                  in.close();
68              }
69          }
70      }
71  }