|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.FileOutputStream;
|
21 | 21 | import java.io.IOException;
|
| 22 | +import java.lang.annotation.ElementType; |
| 23 | +import java.lang.annotation.Retention; |
| 24 | +import java.lang.annotation.RetentionPolicy; |
| 25 | +import java.lang.annotation.Target; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
22 | 28 | import java.util.jar.JarOutputStream;
|
23 | 29 | import java.util.zip.ZipEntry;
|
24 | 30 |
|
25 | 31 | import io.undertow.server.handlers.resource.Resource;
|
26 | 32 | import io.undertow.server.handlers.resource.ResourceManager;
|
27 |
| -import org.junit.jupiter.api.BeforeEach; |
28 |
| -import org.junit.jupiter.api.Test; |
29 | 33 | import org.junit.jupiter.api.io.TempDir;
|
| 34 | +import org.junit.jupiter.params.ParameterizedTest; |
| 35 | +import org.junit.jupiter.params.provider.Arguments; |
| 36 | +import org.junit.jupiter.params.provider.MethodSource; |
| 37 | + |
| 38 | +import org.springframework.util.FileCopyUtils; |
30 | 39 |
|
31 | 40 | import static org.assertj.core.api.Assertions.assertThat;
|
32 | 41 |
|
|
37 | 46 | */
|
38 | 47 | class JarResourceManagerTests {
|
39 | 48 |
|
40 |
| - private ResourceManager resourceManager; |
| 49 | + @TempDir |
| 50 | + static File tempDir; |
41 | 51 |
|
42 |
| - @BeforeEach |
43 |
| - void createJar(@TempDir File tempDir) throws IOException { |
44 |
| - File jar = new File(tempDir, "test.jar"); |
45 |
| - try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar))) { |
46 |
| - out.putNextEntry(new ZipEntry("hello.txt")); |
47 |
| - out.write("hello".getBytes()); |
48 |
| - } |
49 |
| - this.resourceManager = new JarResourceManager(jar); |
50 |
| - } |
51 |
| - |
52 |
| - @Test |
53 |
| - void emptyPathIsHandledCorrectly() throws IOException { |
54 |
| - Resource resource = this.resourceManager.getResource(""); |
| 52 | + @ResourceManagersTest |
| 53 | + void emptyPathIsHandledCorrectly(String filename, ResourceManager resourceManager) throws IOException { |
| 54 | + Resource resource = resourceManager.getResource(""); |
55 | 55 | assertThat(resource).isNotNull();
|
56 | 56 | assertThat(resource.isDirectory()).isTrue();
|
57 | 57 | }
|
58 | 58 |
|
59 |
| - @Test |
60 |
| - void rootPathIsHandledCorrectly() throws IOException { |
61 |
| - Resource resource = this.resourceManager.getResource("/"); |
| 59 | + @ResourceManagersTest |
| 60 | + void rootPathIsHandledCorrectly(String filename, ResourceManager resourceManager) throws IOException { |
| 61 | + Resource resource = resourceManager.getResource("/"); |
62 | 62 | assertThat(resource).isNotNull();
|
63 | 63 | assertThat(resource.isDirectory()).isTrue();
|
64 | 64 | }
|
65 | 65 |
|
66 |
| - @Test |
67 |
| - void resourceIsFoundInJarFile() throws IOException { |
68 |
| - Resource resource = this.resourceManager.getResource("/hello.txt"); |
| 66 | + @ResourceManagersTest |
| 67 | + void resourceIsFoundInJarFile(String filename, ResourceManager resourceManager) throws IOException { |
| 68 | + Resource resource = resourceManager.getResource("/hello.txt"); |
69 | 69 | assertThat(resource).isNotNull();
|
70 | 70 | assertThat(resource.isDirectory()).isFalse();
|
71 | 71 | assertThat(resource.getContentLength()).isEqualTo(5);
|
72 | 72 | }
|
73 | 73 |
|
74 |
| - @Test |
75 |
| - void resourceIsFoundInJarFileWithoutLeadingSlash() throws IOException { |
76 |
| - Resource resource = this.resourceManager.getResource("hello.txt"); |
| 74 | + @ResourceManagersTest |
| 75 | + void resourceIsFoundInJarFileWithoutLeadingSlash(String filename, ResourceManager resourceManager) |
| 76 | + throws IOException { |
| 77 | + Resource resource = resourceManager.getResource("hello.txt"); |
77 | 78 | assertThat(resource).isNotNull();
|
78 | 79 | assertThat(resource.isDirectory()).isFalse();
|
79 | 80 | assertThat(resource.getContentLength()).isEqualTo(5);
|
80 | 81 | }
|
81 | 82 |
|
| 83 | + static List<Arguments> resourceManagers() throws IOException { |
| 84 | + File jar = new File(tempDir, "test.jar"); |
| 85 | + try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar))) { |
| 86 | + out.putNextEntry(new ZipEntry("hello.txt")); |
| 87 | + out.write("hello".getBytes()); |
| 88 | + } |
| 89 | + File troublesomeNameJar = new File(tempDir, "test##1.0.jar"); |
| 90 | + FileCopyUtils.copy(jar, troublesomeNameJar); |
| 91 | + return Arrays.asList(Arguments.of(jar.getName(), new JarResourceManager(jar)), |
| 92 | + Arguments.of(troublesomeNameJar.getName(), new JarResourceManager(troublesomeNameJar))); |
| 93 | + } |
| 94 | + |
| 95 | + @ParameterizedTest(name = "[{index}] {0}") |
| 96 | + @MethodSource("resourceManagers") |
| 97 | + @Target(ElementType.METHOD) |
| 98 | + @Retention(RetentionPolicy.RUNTIME) |
| 99 | + private @interface ResourceManagersTest { |
| 100 | + |
| 101 | + } |
| 102 | + |
82 | 103 | }
|
0 commit comments