|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 the original author or authors. |
| 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 org.springframework.boot.loader.tools; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests for {@link ZipHeaderPeekInputStream}. |
| 21 | + * |
| 22 | + * @author Andy Wilkinson |
| 23 | + */ |
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.IOException; |
| 26 | + |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import org.springframework.boot.loader.tools.JarWriter.ZipHeaderPeekInputStream; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +public class ZipHeaderPeekInputStreamTests { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void hasZipHeaderReturnsTrueWhenStreamStartsWithZipHeader() |
| 37 | + throws IOException { |
| 38 | + try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream( |
| 39 | + new ByteArrayInputStream(new byte[] { 0x50, 0x4b, 0x03, 0x04, 5, 6 }));) { |
| 40 | + assertThat(in.hasZipHeader()).isTrue(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void hasZipHeaderReturnsFalseWheStreamDoesNotStartWithZipHeader() |
| 46 | + throws IOException { |
| 47 | + try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream( |
| 48 | + new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) { |
| 49 | + assertThat(in.hasZipHeader()).isFalse(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void readIndividualBytes() throws IOException { |
| 55 | + try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream( |
| 56 | + new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) { |
| 57 | + assertThat(in.read()).isEqualTo(0); |
| 58 | + assertThat(in.read()).isEqualTo(1); |
| 59 | + assertThat(in.read()).isEqualTo(2); |
| 60 | + assertThat(in.read()).isEqualTo(3); |
| 61 | + assertThat(in.read()).isEqualTo(4); |
| 62 | + assertThat(in.read()).isEqualTo(5); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void readMultipleBytes() throws IOException { |
| 68 | + try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream( |
| 69 | + new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) { |
| 70 | + byte[] bytes = new byte[3]; |
| 71 | + assertThat(in.read(bytes)).isEqualTo(3); |
| 72 | + assertThat(bytes).containsExactly(0, 1, 2); |
| 73 | + assertThat(in.read(bytes)).isEqualTo(3); |
| 74 | + assertThat(bytes).containsExactly(3, 4, 5); |
| 75 | + assertThat(in.read(bytes)).isEqualTo(-1); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void readingMoreThanEntireStreamReadsToEndOfStream() throws IOException { |
| 81 | + try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream( |
| 82 | + new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) { |
| 83 | + byte[] bytes = new byte[8]; |
| 84 | + assertThat(in.read(bytes)).isEqualTo(6); |
| 85 | + assertThat(bytes).containsExactly(0, 1, 2, 3, 4, 5, 0, 0); |
| 86 | + assertThat(in.read(bytes)).isEqualTo(-1); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void readOfSomeOfTheHeaderThenMoreThanEntireStreamReadsToEndOfStream() |
| 92 | + throws IOException { |
| 93 | + try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream( |
| 94 | + new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) { |
| 95 | + byte[] bytes = new byte[8]; |
| 96 | + assertThat(in.read(bytes, 0, 3)).isEqualTo(3); |
| 97 | + assertThat(bytes).containsExactly(0, 1, 2, 0, 0, 0, 0, 0); |
| 98 | + assertThat(in.read(bytes, 3, 5)).isEqualTo(3); |
| 99 | + assertThat(bytes).containsExactly(0, 1, 2, 3, 4, 5, 0, 0); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments