/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.commons.compress.compressors.lz77support;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.compress.utils.ByteUtils;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class AbstractLZ77CompressorInputStreamTest {

    private static class TestStream extends AbstractLZ77CompressorInputStream {
        private boolean literal;
        TestStream(final InputStream in) throws IOException {
            super(in, 1024);
        }
        @Override
        public int read(final byte[] b, final int off, final int len) throws IOException {
            if (literal) {
                return readLiteral(b, off, len);
            }
            return readBackReference(b, off, len);
        }
        void literal(final int len) {
            startLiteral(len);
            literal = true;
        }
    }

    @Test
    public void cantPrefillAfterDataHasBeenRead() throws IOException {
        final byte[] data = new byte[] { 1, 2, 3, 4 };
        try (TestStream s = new TestStream(new ByteArrayInputStream(data))) {
            s.literal(3);
            assertEquals(1, s.read());
            s.prefill(new byte[] { 1, 2, 3 });
        }
    }

    @Test
    public void prefillCanBeUsedForBackReferences() throws IOException {
        final byte[] data = new byte[] { 1, 2, 3, 4 };
        try (TestStream s = new TestStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) {
            s.prefill(data);
            s.startBackReference(2, 4);
            final byte[] r = new byte[4];
            assertEquals(4, s.read(r));
            assertArrayEquals(new byte[] { 3, 4, 3, 4 }, r);
        }
    }

    @Test
    public void ifPrefillExceedsWindowSizeTheLastBytesAreUsed() throws IOException {
        final byte[] data = new byte[2048];
        data[2046] = 3;
        data[2047] = 4;
        try (TestStream s = new TestStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) {
            s.prefill(data);
            s.startBackReference(2, 4);
            final byte[] r = new byte[4];
            assertEquals(4, s.read(r));
            assertArrayEquals(new byte[] { 3, 4, 3, 4 }, r);
        }
    }
}

@@ -857,24 +857,165 @@
 77support;%0A%0A
+import static org.junit.Assert.assertArrayEquals;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertThrows;%0A%0A
 import java.
@@ -1039,16 +1039,16 @@
 Stream;%0A
-
 import j
@@ -1188,104 +1188,8 @@
 t;%0A%0A
-import static org.junit.Assert.assertArrayEquals;%0Aimport static org.junit.Assert.assertEquals;%0A%0A
 publ
@@ -1318,16 +1318,25 @@
 tream %7B%0A
+        %0A
         
@@ -1453,32 +1453,41 @@
 024);%0A        %7D%0A
+        %0A
         @Overrid
@@ -1722,32 +1722,41 @@
 len);%0A        %7D%0A
+        %0A
         void lit
@@ -1966,33 +1966,32 @@
 a = new byte%5B%5D %7B
- 
 1, 2, 3, 4 %7D;%0A  
@@ -1976,33 +1976,32 @@
 te%5B%5D %7B1, 2, 3, 4
- 
 %7D;%0A        try (
@@ -2133,32 +2133,83 @@
 ());%0A           
+ assertThrows(IllegalArgumentException.class, () -%3E
  s.prefill(new b
@@ -2219,18 +2219,17 @@
 %5B%5D %7B
- 
 1, 2, 3
- 
 %7D
+)
 );%0A 

Fix broken tests.