-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15360 from jaikiran/qk-15358
Return properly encoded URLs from JarResource#getResourceURL()
- Loading branch information
Showing
2 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...-projects/bootstrap/runner/src/test/java/io/quarkus/bootstrap/runner/JarResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkus.bootstrap.runner; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.exporter.ZipExporter; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests {@link JarResource} | ||
*/ | ||
public class JarResourceTest { | ||
|
||
/** | ||
* Tests that the URL(s) returned from {@link JarResource#getResourceURL(String)} are properly encoded and can be used | ||
* to open connection to the URL to read data | ||
*/ | ||
@Test | ||
public void testResourceURLEncoding() throws Exception { | ||
final JavaArchive jar = ShrinkWrap.create(JavaArchive.class); | ||
final String[] files = new String[] { "a.txt", "a b.txt", ",;~!@#$%^&().txt" }; | ||
for (final String file : files) { | ||
jar.add(new StringAsset("hello"), file); | ||
} | ||
final Path testDir = Files.createTempDirectory("test"); | ||
// create a child dir with special characters | ||
final Path specialCharDir = Files.createDirectory(Paths.get(testDir.toString(), ",;~!@#$%^&()")); | ||
final Path jarFilePath = Files.createTempFile(specialCharDir, "test", "quarkus-test.jar"); | ||
// create a jar file under the directory which has the special characters | ||
jar.as(ZipExporter.class).exportTo(jarFilePath.toFile(), true); | ||
final JarResource jarResource = new JarResource(null, jarFilePath); | ||
for (final String resource : files) { | ||
final URL url = jarResource.getResourceURL(resource); | ||
Assertions.assertNotNull(url, resource + " is missing in jar"); | ||
// check that opening the resource URL works and data can be read | ||
final URLConnection conn = url.openConnection(); | ||
try (final InputStream is = conn.getInputStream()) { | ||
drainFully(is); | ||
} | ||
} | ||
} | ||
|
||
private static void drainFully(final InputStream inputStream) throws IOException { | ||
int read = -1; | ||
final byte[] data = new byte[1024]; | ||
while ((read = inputStream.read(data)) != -1) { | ||
} | ||
} | ||
} |