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 20 package org.apache.commons.compress.archivers.arj; 21 22 import static org.junit.Assert.*; 23 24 import java.io.FileInputStream; 25 import java.util.Calendar; 26 import java.util.TimeZone; 27 28 import org.apache.commons.compress.AbstractTestCase; 29 import org.apache.commons.compress.archivers.ArchiveEntry; 30 import org.apache.commons.compress.utils.IOUtils; 31 import org.junit.Test; 32 33 public class ArjArchiveInputStreamTest extends AbstractTestCase { 34 35 @Test 36 public void testArjUnarchive() throws Exception { 37 final StringBuilder expected = new StringBuilder(); 38 expected.append("test1.xml<?xml version=\"1.0\"?>\n"); 39 expected.append("<empty/>test2.xml<?xml version=\"1.0\"?>\n"); 40 expected.append("<empty/>\n"); 41 42 43 final ArjArchiveInputStream in = new ArjArchiveInputStream(new FileInputStream(getFile("bla.arj"))); 44 ArjArchiveEntry entry; 45 46 final StringBuilder result = new StringBuilder(); 47 while ((entry = in.getNextEntry()) != null) { 48 result.append(entry.getName()); 49 int tmp; 50 while ((tmp = in.read()) != -1) { 51 result.append((char) tmp); 52 } 53 assertFalse(entry.isDirectory()); 54 } 55 in.close(); 56 assertEquals(result.toString(), expected.toString()); 57 } 58 59 @Test 60 public void testReadingOfAttributesDosVersion() throws Exception { 61 final ArjArchiveInputStream in = new ArjArchiveInputStream(new FileInputStream(getFile("bla.arj"))); 62 final ArjArchiveEntry entry = in.getNextEntry(); 63 assertEquals("test1.xml", entry.getName()); 64 assertEquals(30, entry.getSize()); 65 assertEquals(0, entry.getUnixMode()); 66 final Calendar cal = Calendar.getInstance(); 67 cal.set(2008, 9, 6, 23, 50, 52); 68 cal.set(Calendar.MILLISECOND, 0); 69 assertEquals(cal.getTime(), entry.getLastModifiedDate()); 70 in.close(); 71 } 72 73 @Test 74 public void testReadingOfAttributesUnixVersion() throws Exception { 75 final ArjArchiveInputStream in = new ArjArchiveInputStream(new FileInputStream(getFile("bla.unix.arj"))); 76 final ArjArchiveEntry entry = in.getNextEntry(); 77 assertEquals("test1.xml", entry.getName()); 78 assertEquals(30, entry.getSize()); 79 assertEquals(0664, entry.getUnixMode() & 07777 /* UnixStat.PERM_MASK */); 80 final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0000")); 81 cal.set(2008, 9, 6, 21, 50, 52); 82 cal.set(Calendar.MILLISECOND, 0); 83 assertEquals(cal.getTime(), entry.getLastModifiedDate()); 84 in.close(); 85 } 86 87 @Test 88 public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception { 89 try (FileInputStream in = new FileInputStream(getFile("bla.arj")); 90 ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) { 91 ArchiveEntry e = archive.getNextEntry(); 92 IOUtils.toByteArray(archive); 93 assertEquals(-1, archive.read()); 94 assertEquals(-1, archive.read()); 95 } 96 } 97 98 @Test 99 public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception { 100 byte[] buf = new byte[2]; 101 try (FileInputStream in = new FileInputStream(getFile("bla.arj")); 102 ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) { 103 ArchiveEntry e = archive.getNextEntry(); 104 IOUtils.toByteArray(archive); 105 assertEquals(-1, archive.read(buf)); 106 assertEquals(-1, archive.read(buf)); 107 } 108 } 109 110 } 111