Skip to content

Commit 69b5789

Browse files
committed
add FileClasspathEntry validation
Signed-off-by: Andrey Litvitski <[email protected]>
1 parent 27e9bc6 commit 69b5789

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/server/exec/FileClasspathEntry.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,8 +24,13 @@ public class FileClasspathEntry implements ClasspathEntry {
2424

2525
private final File file;
2626

27-
public FileClasspathEntry(String file) {
28-
this(new File(file));
27+
public FileClasspathEntry(String filePath) {
28+
File file = new File(filePath);
29+
if (!file.isDirectory() && !file.getName().toLowerCase().endsWith("jar")) {
30+
String absolutePath = file.getAbsolutePath();
31+
throw new IllegalArgumentException("File must be a jar file or directory '" + absolutePath + "'");
32+
}
33+
this.file = file;
2934
}
3035

3136
public FileClasspathEntry(File file) {

spring-boot-testjars/src/test/java/org/springframework/experimental/boot/server/exec/FileClasspathEntryTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2324
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2425

2526
class FileClasspathEntryTests {
@@ -32,4 +33,11 @@ void resolveWhenDoesNotExist() {
3233
.withMessageContaining("Could not find file to add to the classpath");
3334
}
3435

36+
@Test
37+
void constructorWhenNotJarOrDirectoryThrowsIllegalArgumentException() {
38+
String invalidPath = "some-file.txt";
39+
assertThatIllegalArgumentException().isThrownBy(() -> new FileClasspathEntry(invalidPath))
40+
.withMessageContaining("File must be a jar file or directory");
41+
}
42+
3543
}

0 commit comments

Comments
 (0)