/*
 * Copyright 2014-2022 Real Logic Limited.
 *
 * Licensed 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
 *
 * https://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 io.aeron;

import io.aeron.driver.buffer.FileStoreLogFactory;
import io.aeron.driver.buffer.RawLog;
import io.aeron.logbuffer.ControlledFragmentHandler;
import org.agrona.concurrent.status.AtomicLongPosition;
import org.agrona.concurrent.status.Position;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.File;

import static io.aeron.logbuffer.LogBufferDescriptor.initialTermId;
import static io.aeron.logbuffer.LogBufferDescriptor.isConnected;
import static io.aeron.logbuffer.LogBufferDescriptor.mtuLength;
import static io.aeron.logbuffer.LogBufferDescriptor.pageSize;
import static io.aeron.logbuffer.LogBufferDescriptor.termLength;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ImageRangeTest
{
    @SuppressWarnings("JUnitMalformedDeclaration")
    @ParameterizedTest
    @ValueSource(booleans = {true, false})
    void shouldHandleAllPossibleOffsets(final boolean useSpareFiles, final @TempDir File baseDir)
    {
        final int termBufferLength = 65536;
        final int filePageSize = 4096;
        final FileStoreLogFactory fileStoreLogFactory = new FileStoreLogFactory(
            baseDir.getAbsolutePath(), filePageSize, false, 0, new RethrowingErrorHandler());
        final long subscriberPositionThatWillTriggerException = 3147497471L;

        try (RawLog rawLog = fileStoreLogFactory.newImage(0, termBufferLength, useSpareFiles))
        {
            initialTermId(rawLog.metaData(), 0);
            mtuLength(rawLog.metaData(), 1408);
            termLength(rawLog.metaData(), termBufferLength);
            pageSize(rawLog.metaData(), filePageSize);
            isConnected(rawLog.metaData(), true);

            final LogBuffers logBuffers = new LogBuffers(rawLog.fileName());

            assertEquals(termBufferLength, logBuffers.termLength());
            final Position subscriberPosition = new AtomicLongPosition();

            final Image image = new Image(
                null, 1, subscriberPosition, logBuffers, new RethrowingErrorHandler(), "127.0.0.1:123", 0);

            subscriberPosition.set(subscriberPositionThatWillTriggerException);

            image.boundedControlledPoll(
                (buffer, offset, length, header) -> ControlledFragmentHandler.Action.COMMIT, 1024, 1);
            image.boundedPoll(
                (buffer, offset, length, header) -> {}, 1024, 1);
        }
    }
}
@@ -1359,69 +1359,8 @@
 gth;
-%0Aimport static org.junit.jupiter.api.Assertions.assertEquals;
 %0A%0Acl
@@ -1698,16 +1698,177 @@
   final 
+long subscriberPositionThatWillTriggerException = 3147497471L;%0A        final Position subscriberPosition = new AtomicLongPosition();%0A%0A%0A        try (%0A            
 FileStor
@@ -1926,16 +1926,20 @@
 actory(%0A
+    
         
@@ -2036,91 +2036,12 @@
     
-final long subscriberPositionThatWillTriggerException = 3147497471L;%0A%0A        try (
+    
 RawL
@@ -2404,22 +2404,21 @@
         
-final 
+try (
 LogBuffe
@@ -2466,18 +2466,17 @@
 eName())
-;%0A
+)
 %0A       
@@ -2484,140 +2484,14 @@
     
-assertEquals(termBufferLength, logBuffers.termLength());%0A            final Position subscriberPosition = new AtomicLongPosition();%0A%0A
+%7B%0A    
     
@@ -2529,16 +2529,20 @@
  Image(%0A
+    
         
@@ -2654,16 +2654,20 @@
         
+    
 subscrib
@@ -2723,24 +2723,28 @@
 xception);%0A%0A
+    
             
@@ -2780,32 +2780,36 @@
                 
+    
 (buffer, offset,
@@ -2883,32 +2883,36 @@
 1);%0A            
+    
 image.boundedPol
@@ -2906,32 +2906,36 @@
 ge.boundedPoll(%0A
+    
                 
@@ -2976,32 +2976,46 @@
 %3E %7B%7D, 1024, 1);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Fix file deletion on windows.