View Javadoc
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.commons.compress.archivers.zip;
18  
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  import static org.mockito.ArgumentMatchers.any;
21  import static org.mockito.ArgumentMatchers.eq;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.times;
24  import static org.mockito.Mockito.verify;
25  import static org.mockito.Mockito.when;
26  
27  import java.io.IOException;
28  import java.nio.ByteBuffer;
29  import java.nio.channels.FileChannel;
30  import java.nio.channels.SeekableByteChannel;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.apache.commons.compress.AbstractTempDirTest;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests {@link ZipIoUtil}.
38   */
39  public class ZipIoUtilTest extends AbstractTempDirTest {
40  
41      @Test
42      public void testWriteFully_whenFullAtOnce_thenSucceed() throws IOException {
43          final SeekableByteChannel channel = mock(SeekableByteChannel.class);
44  
45          when(channel.write((ByteBuffer) any())).thenAnswer(answer -> {
46              ((ByteBuffer) answer.getArgument(0)).position(5);
47              return 5;
48          }).thenAnswer(answer -> {
49              ((ByteBuffer) answer.getArgument(0)).position(6);
50              return 6;
51          });
52  
53          ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)));
54          ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)));
55  
56          verify(channel, times(2)).write((ByteBuffer) any());
57      }
58  
59      @Test
60      public void testWriteFully_whenFullButPartial_thenSucceed() throws IOException {
61          final SeekableByteChannel channel = mock(SeekableByteChannel.class);
62  
63          when(channel.write((ByteBuffer) any())).thenAnswer(answer -> {
64              ((ByteBuffer) answer.getArgument(0)).position(3);
65              return 3;
66          }).thenAnswer(answer -> {
67              ((ByteBuffer) answer.getArgument(0)).position(5);
68              return 2;
69          }).thenAnswer(answer -> {
70              ((ByteBuffer) answer.getArgument(0)).position(6);
71              return 6;
72          });
73  
74          ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)));
75          ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)));
76  
77          verify(channel, times(3)).write((ByteBuffer) any());
78      }
79  
80      @Test
81      public void testWriteFully_whenPartial_thenFail() throws IOException {
82          final SeekableByteChannel channel = mock(SeekableByteChannel.class);
83  
84          when(channel.write((ByteBuffer) any())).thenAnswer(answer -> {
85              ((ByteBuffer) answer.getArgument(0)).position(3);
86              return 3;
87          }).thenAnswer(answer -> 0);
88  
89          assertThrows(IOException.class, () -> ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))));
90  
91          verify(channel, times(2)).write((ByteBuffer) any());
92      }
93  
94      @Test
95      public void testWriteFullyAt_whenFullAtOnce_thenSucceed() throws IOException {
96          final FileChannel channel = mock(FileChannel.class);
97  
98          when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> {
99              ((ByteBuffer) answer.getArgument(0)).position(5);
100             return 5;
101         });
102         when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> {
103             ((ByteBuffer) answer.getArgument(0)).position(6);
104             return 6;
105         });
106 
107         ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20);
108         ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30);
109 
110         verify(channel, times(1)).write((ByteBuffer) any(), eq(20L));
111         verify(channel, times(1)).write((ByteBuffer) any(), eq(30L));
112     }
113 
114     @Test
115     public void testWriteFullyAt_whenFullButPartial_thenSucceed() throws IOException {
116         final FileChannel channel = mock(FileChannel.class);
117 
118         when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> {
119             ((ByteBuffer) answer.getArgument(0)).position(3);
120             return 3;
121         });
122         when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> {
123             ((ByteBuffer) answer.getArgument(0)).position(5);
124             return 2;
125         });
126         when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> {
127             ((ByteBuffer) answer.getArgument(0)).position(6);
128             return 6;
129         });
130 
131         ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20);
132         ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30);
133 
134         verify(channel, times(1)).write((ByteBuffer) any(), eq(20L));
135         verify(channel, times(1)).write((ByteBuffer) any(), eq(23L));
136         verify(channel, times(1)).write((ByteBuffer) any(), eq(30L));
137     }
138 
139     @Test
140     public void testWriteFullyAt_whenPartial_thenFail() throws IOException {
141         final FileChannel channel = mock(FileChannel.class);
142 
143         when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> {
144             ((ByteBuffer) answer.getArgument(0)).position(3);
145             return 3;
146         });
147         when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> 0);
148         assertThrows(IOException.class, () -> ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20));
149 
150         verify(channel, times(1)).write((ByteBuffer) any(), eq(20L));
151         verify(channel, times(1)).write((ByteBuffer) any(), eq(23L));
152         verify(channel, times(0)).write((ByteBuffer) any(), eq(25L));
153     }
154 }