Merge branch '3.2.x'

Closes gh-40379
This commit is contained in:
Phillip Webb
2024-04-16 15:32:03 -07:00
8 changed files with 161 additions and 133 deletions

View File

@@ -303,7 +303,7 @@ class NestedJarFileTests {
Cleanable cleanable = mock(Cleanable.class);
given(cleaner.register(any(), action.capture())).willReturn(cleanable);
try (NestedJarFile jar = new NestedJarFile(this.file, null, null, false, cleaner)) {
Object channel = Extractors.byName("resources.zipContent.data.channel").apply(jar);
Object channel = Extractors.byName("resources.zipContent.data.fileAccess").apply(jar);
assertThat(channel).extracting("referenceCount").isEqualTo(1);
action.getValue().run();
assertThat(channel).extracting("referenceCount").isEqualTo(0);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,8 +25,8 @@ import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* Annotation that can be added to tests to assert that {@link FileChannelDataBlock} files
* are not left open.
* Annotation that can be added to tests to assert that {@link FileDataBlock} files are
* not left open.
*
* @author Phillip Webb
*/

View File

@@ -19,7 +19,6 @@ package org.springframework.boot.loader.zip;
import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.Cleaner.Cleanable;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashSet;
@@ -31,7 +30,7 @@ import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.boot.loader.ref.DefaultCleanerTracking;
import org.springframework.boot.loader.zip.FileChannelDataBlock.Tracker;
import org.springframework.boot.loader.zip.FileDataBlock.Tracker;
import static org.assertj.core.api.Assertions.assertThat;
@@ -45,14 +44,14 @@ class AssertFileChannelDataBlocksClosedExtension implements BeforeEachCallback,
@Override
public void beforeEach(ExtensionContext context) throws Exception {
tracker.clear();
FileChannelDataBlock.tracker = tracker;
FileDataBlock.tracker = tracker;
DefaultCleanerTracking.set(tracker::addedCleanable);
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
tracker.assertAllClosed();
FileChannelDataBlock.tracker = null;
FileDataBlock.tracker = Tracker.NONE;
}
private static final class OpenFilesTracker implements Tracker {
@@ -64,12 +63,12 @@ class AssertFileChannelDataBlocksClosedExtension implements BeforeEachCallback,
private final List<Closeable> close = new ArrayList<>();
@Override
public void openedFileChannel(Path path, FileChannel fileChannel) {
public void openedFileChannel(Path path) {
this.paths.add(path);
}
@Override
public void closedFileChannel(Path path, FileChannel fileChannel) {
public void closedFileChannel(Path path) {
this.paths.remove(path);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,10 +16,10 @@
package org.springframework.boot.loader.zip;
import org.springframework.boot.loader.zip.FileChannelDataBlock.ManagedFileChannel;
import org.springframework.boot.loader.zip.FileDataBlock.FileAccess;
/**
* Test access to {@link ManagedFileChannel} details.
* Test access to {@link FileAccess} details.
*
* @author Phillip Webb
*/
@@ -28,6 +28,6 @@ public final class FileChannelDataBlockManagedFileChannel {
private FileChannelDataBlockManagedFileChannel() {
}
public static int BUFFER_SIZE = FileChannelDataBlock.ManagedFileChannel.BUFFER_SIZE;
public static int BUFFER_SIZE = FileDataBlock.FileAccess.BUFFER_SIZE;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@ package org.springframework.boot.loader.zip;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -28,17 +27,17 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.loader.zip.FileChannelDataBlock.Tracker;
import org.springframework.boot.loader.zip.FileDataBlock.Tracker;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link FileChannelDataBlock}.
* Tests for {@link FileDataBlock}.
*
* @author Phillip Webb
*/
class FileChannelDataBlockTests {
class FileDataBlockTests {
private static final byte[] CONTENT = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
@@ -55,19 +54,19 @@ class FileChannelDataBlockTests {
@AfterEach
void resetTracker() {
FileChannelDataBlock.tracker = null;
FileDataBlock.tracker = Tracker.NONE;
}
@Test
void sizeReturnsFileSize() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
assertThat(block.size()).isEqualTo(CONTENT.length);
}
}
@Test
void readReadsFile() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
ByteBuffer buffer = ByteBuffer.allocate(CONTENT.length);
assertThat(block.read(buffer, 0)).isEqualTo(6);
assertThat(buffer.array()).containsExactly(CONTENT);
@@ -76,7 +75,8 @@ class FileChannelDataBlockTests {
@Test
void readReadsFileWhenThreadHasBeenInterrupted() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
Files.write(this.tempFile.toPath(), CONTENT);
try (FileDataBlock block = createAndOpenBlock()) {
ByteBuffer buffer = ByteBuffer.allocate(CONTENT.length);
Thread.currentThread().interrupt();
assertThat(block.read(buffer, 0)).isEqualTo(6);
@@ -89,7 +89,7 @@ class FileChannelDataBlockTests {
@Test
void readDoesNotReadPastEndOfFile() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
ByteBuffer buffer = ByteBuffer.allocate(CONTENT.length);
assertThat(block.read(buffer, 2)).isEqualTo(4);
assertThat(buffer.array()).containsExactly(0x02, 0x03, 0x04, 0x05, 0x0, 0x0);
@@ -98,7 +98,7 @@ class FileChannelDataBlockTests {
@Test
void readWhenPosAtSizeReturnsMinusOne() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
ByteBuffer buffer = ByteBuffer.allocate(CONTENT.length);
assertThat(block.read(buffer, 6)).isEqualTo(-1);
}
@@ -106,7 +106,7 @@ class FileChannelDataBlockTests {
@Test
void readWhenPosOverSizeReturnsMinusOne() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
ByteBuffer buffer = ByteBuffer.allocate(CONTENT.length);
assertThat(block.read(buffer, 7)).isEqualTo(-1);
}
@@ -114,7 +114,7 @@ class FileChannelDataBlockTests {
@Test
void readWhenPosIsNegativeThrowsException() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
ByteBuffer buffer = ByteBuffer.allocate(CONTENT.length);
assertThatIllegalArgumentException().isThrownBy(() -> block.read(buffer, -1));
}
@@ -122,7 +122,7 @@ class FileChannelDataBlockTests {
@Test
void sliceWhenOffsetIsNegativeThrowsException() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
assertThatIllegalArgumentException().isThrownBy(() -> block.slice(-1, 0))
.withMessage("Offset must not be negative");
}
@@ -130,7 +130,7 @@ class FileChannelDataBlockTests {
@Test
void sliceWhenSizeIsNegativeThrowsException() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
assertThatIllegalArgumentException().isThrownBy(() -> block.slice(0, -1))
.withMessage("Size must not be negative and must be within bounds");
}
@@ -138,7 +138,7 @@ class FileChannelDataBlockTests {
@Test
void sliceWhenSizeIsOutOfBoundsThrowsException() throws IOException {
try (FileChannelDataBlock block = createAndOpenBlock()) {
try (FileDataBlock block = createAndOpenBlock()) {
assertThatIllegalArgumentException().isThrownBy(() -> block.slice(2, 5))
.withMessage("Size must not be negative and must be within bounds");
}
@@ -146,7 +146,7 @@ class FileChannelDataBlockTests {
@Test
void sliceReturnsSlice() throws IOException {
try (FileChannelDataBlock slice = createAndOpenBlock().slice(1, 4)) {
try (FileDataBlock slice = createAndOpenBlock().slice(1, 4)) {
assertThat(slice.size()).isEqualTo(4);
ByteBuffer buffer = ByteBuffer.allocate(4);
assertThat(slice.read(buffer, 0)).isEqualTo(4);
@@ -157,72 +157,72 @@ class FileChannelDataBlockTests {
@Test
void openAndCloseHandleReferenceCounting() throws IOException {
TestTracker tracker = new TestTracker();
FileChannelDataBlock.tracker = tracker;
FileChannelDataBlock block = createBlock();
assertThat(block).extracting("channel.referenceCount").isEqualTo(0);
FileDataBlock.tracker = tracker;
FileDataBlock block = createBlock();
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(0);
tracker.assertOpenCloseCounts(0, 0);
block.open();
assertThat(block).extracting("channel.referenceCount").isEqualTo(1);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(1);
tracker.assertOpenCloseCounts(1, 0);
block.open();
assertThat(block).extracting("channel.referenceCount").isEqualTo(2);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(2);
tracker.assertOpenCloseCounts(1, 0);
block.close();
assertThat(block).extracting("channel.referenceCount").isEqualTo(1);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(1);
tracker.assertOpenCloseCounts(1, 0);
block.close();
assertThat(block).extracting("channel.referenceCount").isEqualTo(0);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(0);
tracker.assertOpenCloseCounts(1, 1);
block.open();
assertThat(block).extracting("channel.referenceCount").isEqualTo(1);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(1);
tracker.assertOpenCloseCounts(2, 1);
block.close();
assertThat(block).extracting("channel.referenceCount").isEqualTo(0);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(0);
tracker.assertOpenCloseCounts(2, 2);
}
@Test
void openAndCloseSliceHandleReferenceCounting() throws IOException {
TestTracker tracker = new TestTracker();
FileChannelDataBlock.tracker = tracker;
FileChannelDataBlock block = createBlock();
FileChannelDataBlock slice = block.slice(1, 4);
assertThat(block).extracting("channel.referenceCount").isEqualTo(0);
FileDataBlock.tracker = tracker;
FileDataBlock block = createBlock();
FileDataBlock slice = block.slice(1, 4);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(0);
tracker.assertOpenCloseCounts(0, 0);
block.open();
assertThat(block).extracting("channel.referenceCount").isEqualTo(1);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(1);
tracker.assertOpenCloseCounts(1, 0);
slice.open();
assertThat(slice).extracting("channel.referenceCount").isEqualTo(2);
assertThat(slice).extracting("fileAccess.referenceCount").isEqualTo(2);
tracker.assertOpenCloseCounts(1, 0);
slice.open();
assertThat(slice).extracting("channel.referenceCount").isEqualTo(3);
assertThat(slice).extracting("fileAccess.referenceCount").isEqualTo(3);
tracker.assertOpenCloseCounts(1, 0);
slice.close();
assertThat(slice).extracting("channel.referenceCount").isEqualTo(2);
assertThat(slice).extracting("fileAccess.referenceCount").isEqualTo(2);
tracker.assertOpenCloseCounts(1, 0);
slice.close();
assertThat(slice).extracting("channel.referenceCount").isEqualTo(1);
assertThat(slice).extracting("fileAccess.referenceCount").isEqualTo(1);
tracker.assertOpenCloseCounts(1, 0);
block.close();
assertThat(block).extracting("channel.referenceCount").isEqualTo(0);
assertThat(block).extracting("fileAccess.referenceCount").isEqualTo(0);
tracker.assertOpenCloseCounts(1, 1);
slice.open();
assertThat(slice).extracting("channel.referenceCount").isEqualTo(1);
assertThat(slice).extracting("fileAccess.referenceCount").isEqualTo(1);
tracker.assertOpenCloseCounts(2, 1);
slice.close();
assertThat(slice).extracting("channel.referenceCount").isEqualTo(0);
assertThat(slice).extracting("fileAccess.referenceCount").isEqualTo(0);
tracker.assertOpenCloseCounts(2, 2);
}
private FileChannelDataBlock createAndOpenBlock() throws IOException {
FileChannelDataBlock block = createBlock();
private FileDataBlock createAndOpenBlock() throws IOException {
FileDataBlock block = createBlock();
block.open();
return block;
}
private FileChannelDataBlock createBlock() throws IOException {
return new FileChannelDataBlock(this.tempFile.toPath());
private FileDataBlock createBlock() throws IOException {
return new FileDataBlock(this.tempFile.toPath());
}
static class TestTracker implements Tracker {
@@ -232,12 +232,12 @@ class FileChannelDataBlockTests {
private int closeCount;
@Override
public void openedFileChannel(Path path, FileChannel fileChannel) {
public void openedFileChannel(Path path) {
this.openCount++;
}
@Override
public void closedFileChannel(Path path, FileChannel fileChannel) {
public void closedFileChannel(Path path) {
this.closeCount++;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,7 +58,7 @@ class VirtualZipDataBlockTests {
@Test
void createContainsValidZipContent() throws IOException {
FileChannelDataBlock data = new FileChannelDataBlock(this.file.toPath());
FileDataBlock data = new FileDataBlock(this.file.toPath());
data.open();
List<ZipCentralDirectoryFileHeaderRecord> centralRecords = new ArrayList<>();
List<Long> centralRecordPositions = new ArrayList<>();