|
1 | 1 | package org.cryptomator.integrations.common; |
2 | 2 |
|
| 3 | +import org.junit.jupiter.api.AfterEach; |
3 | 4 | import org.junit.jupiter.api.Assertions; |
4 | 5 | import org.junit.jupiter.api.BeforeEach; |
5 | 6 | import org.junit.jupiter.api.DisplayName; |
6 | 7 | import org.junit.jupiter.api.Nested; |
7 | 8 | import org.junit.jupiter.api.Test; |
8 | 9 | import org.junit.jupiter.api.io.TempDir; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.EmptySource; |
| 12 | +import org.junit.jupiter.params.provider.ValueSource; |
| 13 | +import org.mockito.MockedStatic; |
9 | 14 | import org.mockito.Mockito; |
10 | 15 |
|
11 | 16 | import java.io.ByteArrayInputStream; |
|
17 | 22 | import java.util.Arrays; |
18 | 23 | import java.util.Comparator; |
19 | 24 |
|
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.Mockito.never; |
| 27 | + |
20 | 28 | public class ClassLoaderFactoryTest { |
21 | 29 |
|
22 | 30 | @Nested |
@@ -91,21 +99,49 @@ public void testReadPluginDirFromSysProp() { |
91 | 99 | } |
92 | 100 | } |
93 | 101 |
|
94 | | - @Test |
95 | | - @DisplayName("read path from cryptomator.pluginDir and replace ~/ with user.home") |
96 | | - public void testReadPluginDirFromSysPropAndReplaceHome() { |
97 | | - var ucl = Mockito.mock(URLClassLoader.class, "ucl"); |
98 | | - var relPath = "~/there/will/be/plugins"; |
99 | | - var absPath = Path.of(System.getProperty("user.home")).resolve("there/will/be/plugins"); |
100 | | - try (var mockedClass = Mockito.mockStatic(ClassLoaderFactory.class)) { |
| 102 | + @Nested |
| 103 | + @DisplayName("when the system property contains invalid values") |
| 104 | + public class InvalidSystemProperty { |
| 105 | + |
| 106 | + MockedStatic<ClassLoaderFactory> mockedClass; |
| 107 | + |
| 108 | + @BeforeEach |
| 109 | + public void beforeEach() { |
| 110 | + mockedClass = Mockito.mockStatic(ClassLoaderFactory.class); |
101 | 111 | mockedClass.when(() -> ClassLoaderFactory.forPluginDir()).thenCallRealMethod(); |
102 | | - mockedClass.when(() -> ClassLoaderFactory.forPluginDirWithPath(absPath)).thenReturn(ucl); |
| 112 | + mockedClass.when(() -> ClassLoaderFactory.forPluginDirWithPath(any())).thenReturn(null); |
| 113 | + } |
103 | 114 |
|
104 | | - System.setProperty("cryptomator.pluginDir", relPath); |
| 115 | + @AfterEach |
| 116 | + public void afterEach() { |
| 117 | + mockedClass.close(); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + @Test |
| 122 | + @DisplayName("Undefined cryptomator.pluginDir returns empty URLClassLoader") |
| 123 | + public void testUndefinedSysProp() { |
| 124 | + System.clearProperty("cryptomator.pluginDir"); |
105 | 125 | var result = ClassLoaderFactory.forPluginDir(); |
106 | 126 |
|
107 | | - Assertions.assertSame(ucl, result); |
| 127 | + mockedClass.verify(() -> ClassLoaderFactory.forPluginDirWithPath(any()), never()); |
| 128 | + Assertions.assertNotNull(result); |
| 129 | + Assertions.assertEquals(0, result.getURLs().length); |
108 | 130 | } |
| 131 | + |
| 132 | + @ParameterizedTest |
| 133 | + @DisplayName("Property cryptomator.pluginDir filled with blanks returns empty URLClassLoader") |
| 134 | + @EmptySource |
| 135 | + @ValueSource(strings = {"\t\t", " "}) |
| 136 | + public void testBlankSysProp(String propValue) { |
| 137 | + System.setProperty("cryptomator.pluginDir", propValue); |
| 138 | + var result = ClassLoaderFactory.forPluginDir(); |
| 139 | + |
| 140 | + mockedClass.verify(() -> ClassLoaderFactory.forPluginDirWithPath(any()), never()); |
| 141 | + Assertions.assertNotNull(result); |
| 142 | + Assertions.assertEquals(0, result.getURLs().length); |
| 143 | + } |
| 144 | + |
109 | 145 | } |
110 | 146 |
|
111 | 147 | @Test |
|
0 commit comments