From ac20197262b974d0631d57a07e46e35a7c7de618 Mon Sep 17 00:00:00 2001 From: aviralgarg05 Date: Fri, 10 Apr 2026 11:37:04 +0530 Subject: [PATCH] API: Fix FileRange validation order --- .../java/org/apache/iceberg/io/FileRange.java | 6 +-- .../org/apache/iceberg/io/TestFileRange.java | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 api/src/test/java/org/apache/iceberg/io/TestFileRange.java diff --git a/api/src/main/java/org/apache/iceberg/io/FileRange.java b/api/src/main/java/org/apache/iceberg/io/FileRange.java index f6d5d9b41cca..695d516725a6 100644 --- a/api/src/main/java/org/apache/iceberg/io/FileRange.java +++ b/api/src/main/java/org/apache/iceberg/io/FileRange.java @@ -31,10 +31,8 @@ public class FileRange { public FileRange(CompletableFuture byteBuffer, long offset, int length) throws EOFException { Preconditions.checkNotNull(byteBuffer, "byteBuffer can't be null"); - Preconditions.checkArgument( - length() >= 0, "Invalid length: %s in range (must be >= 0)", length); - Preconditions.checkArgument( - offset() >= 0, "Invalid offset: %s in range (must be >= 0)", offset); + Preconditions.checkArgument(length >= 0, "Invalid length: %s in range (must be >= 0)", length); + Preconditions.checkArgument(offset >= 0, "Invalid offset: %s in range (must be >= 0)", offset); this.byteBuffer = byteBuffer; this.offset = offset; diff --git a/api/src/test/java/org/apache/iceberg/io/TestFileRange.java b/api/src/test/java/org/apache/iceberg/io/TestFileRange.java new file mode 100644 index 000000000000..76f4be7dd6e9 --- /dev/null +++ b/api/src/test/java/org/apache/iceberg/io/TestFileRange.java @@ -0,0 +1,44 @@ +/* + * 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.iceberg.io; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.nio.ByteBuffer; +import java.util.concurrent.CompletableFuture; +import org.junit.jupiter.api.Test; + +public class TestFileRange { + + @Test + public void testConstructorRejectsNegativeLength() { + assertThatThrownBy( + () -> new FileRange(CompletableFuture.completedFuture(ByteBuffer.allocate(0)), 0L, -1)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid length: -1 in range (must be >= 0)"); + } + + @Test + public void testConstructorRejectsNegativeOffset() { + assertThatThrownBy( + () -> new FileRange(CompletableFuture.completedFuture(ByteBuffer.allocate(0)), -1L, 1)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid offset: -1 in range (must be >= 0)"); + } +}