Skip to content

Commit 85c0b44

Browse files
committed
Test that LaunchedURLClassLoader works when thread is interrupted
Previously, RandomAccessDataFile used a semaphore and acquired it interruptibly. This meant that an interrupted thread was unable to access the file. Notably, this would prevent LaunchedURLClassLoader from loading classes or resources on an interrupted thread. The previous commit (937f857) updates RandomAccessDataFile to acquire the semaphore uninterruptibly. This commit adds a test to LaunchedURLClassLoader to verify that it can now load a resource from an interrupted thread. Closes gh-6683
1 parent 937f857 commit 85c0b44

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/LaunchedURLClassLoaderTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -36,6 +36,7 @@
3636
*
3737
* @author Dave Syer
3838
* @author Phillip Webb
39+
* @author Andy Wilkinson
3940
*/
4041
@SuppressWarnings("resource")
4142
public class LaunchedURLClassLoaderTests {
@@ -101,4 +102,23 @@ public void resolveFromNested() throws Exception {
101102
assertThat(resource.openConnection().getInputStream().read(), equalTo(3));
102103
}
103104

105+
@Test
106+
public void resolveFromNestedWhileThreadIsInterrupted() throws Exception {
107+
File file = this.temporaryFolder.newFile();
108+
TestJarCreator.createTestJar(file);
109+
JarFile jarFile = new JarFile(file);
110+
URL url = jarFile.getUrl();
111+
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { url },
112+
null);
113+
try {
114+
Thread.currentThread().interrupt();
115+
URL resource = loader.getResource("nested.jar!/3.dat");
116+
assertThat(resource.toString(), equalTo(url + "nested.jar!/3.dat"));
117+
assertThat(resource.openConnection().getInputStream().read(), equalTo(3));
118+
}
119+
finally {
120+
Thread.interrupted();
121+
}
122+
}
123+
104124
}

0 commit comments

Comments
 (0)