• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tradefed.cache.remote;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertThrows;
24 import static org.junit.Assert.assertTrue;
25 
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.util.NoSuchElementException;
30 import java.util.Random;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 /** Tests for {@link Chunker}. */
36 @RunWith(JUnit4.class)
37 public class ChunkerTest {
38     @Test
chunkingShouldWork()39     public void chunkingShouldWork() throws IOException {
40         byte[] expectedData = new byte[21];
41         new Random().nextBytes(expectedData);
42         Chunker chunker =
43                 new Chunker(new ByteArrayInputStream(expectedData), expectedData.length, 10);
44         ByteArrayOutputStream actualData = new ByteArrayOutputStream();
45 
46         Chunker.Chunk first = chunker.next();
47         first.getData().writeTo(actualData);
48         Chunker.Chunk second = chunker.next();
49         second.getData().writeTo(actualData);
50         Chunker.Chunk third = chunker.next();
51         third.getData().writeTo(actualData);
52 
53         assertEquals(first.getData().size(), 10);
54         assertEquals(first.getOffset(), 0);
55         assertEquals(second.getData().size(), 10);
56         assertEquals(second.getOffset(), 10);
57         assertEquals(third.getData().size(), 1);
58         assertEquals(third.getOffset(), 20);
59         assertArrayEquals(actualData.toByteArray(), expectedData);
60     }
61 
62     @Test
hasNextReturnsTrue()63     public void hasNextReturnsTrue() throws IOException {
64         byte[] data = new byte[10];
65         Chunker chunker = new Chunker(new ByteArrayInputStream(data), 10, 10);
66 
67         boolean hasNext = chunker.hasNext();
68 
69         assertTrue(hasNext);
70     }
71 
72     @Test
nextShouldThrowIfNoMoreData()73     public void nextShouldThrowIfNoMoreData() throws IOException {
74         byte[] data = new byte[10];
75         Chunker chunker = new Chunker(new ByteArrayInputStream(data), 10, 11);
76 
77         chunker.next();
78 
79         assertFalse(chunker.hasNext());
80         assertThrows(NoSuchElementException.class, () -> chunker.next());
81     }
82 
83     @Test
nextWorksOnEmptyData()84     public void nextWorksOnEmptyData() throws Exception {
85         var inp =
86                 new ByteArrayInputStream(new byte[0]) {
87                     private boolean closed;
88 
89                     @Override
90                     public void close() throws IOException {
91                         closed = true;
92                         super.close();
93                     }
94                 };
95         Chunker chunker = new Chunker(inp, 0, 10);
96 
97         Chunker.Chunk next = chunker.next();
98 
99         assertNotNull(next);
100         assertTrue(next.getData().isEmpty());
101         assertEquals(next.getOffset(), 0);
102         assertTrue(inp.closed);
103     }
104 }
105