-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathDotEnvConditionCheckerTest.java
More file actions
90 lines (73 loc) · 3.19 KB
/
DotEnvConditionCheckerTest.java
File metadata and controls
90 lines (73 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package greencity.utils.dotenv;
import greencity.config.dotenv.DotEnvConditionChecker;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
class DotEnvConditionCheckerTest {
private static MockedStatic<Paths> mockedPaths;
private static MockedStatic<Files> mockedFiles;
@BeforeEach
void setUp() throws NoSuchFieldException, IllegalAccessException {
Field cachedValueField = DotEnvConditionChecker.class.getDeclaredField("cachedValue");
cachedValueField.setAccessible(true);
cachedValueField.set(null, null);
mockedPaths.clearInvocations();
mockedFiles.clearInvocations();
}
@BeforeAll
public static void init() {
mockedFiles = Mockito.mockStatic(Files.class);
mockedPaths = Mockito.mockStatic(Paths.class);
}
@AfterAll
public static void cleanup() {
mockedPaths.close();
mockedFiles.close();
}
@Test
public void checkIsEnabledReturnsTrueIfPathIsValid() {
Path paths = Mockito.mock(Path.class);
mockedPaths.when(() -> Paths.get(anyString())).thenReturn(paths);
mockedFiles.when(() -> Files.exists(paths)).thenReturn(true);
assertTrue(DotEnvConditionChecker.isEnabled());
mockedPaths.verify(() -> Paths.get(anyString()), times(1));
mockedFiles.verify(() -> Files.exists(paths), times(1));
}
@Test
public void checkIsEnabledReturnsFalseIfPathNotExistsTest() {
Path paths = Mockito.mock(Path.class);
mockedPaths.when(() -> Paths.get(anyString())).thenReturn(paths);
mockedFiles.when(() -> Files.exists(paths)).thenReturn(false);
assertFalse(DotEnvConditionChecker.isEnabled());
mockedPaths.verify(() -> Paths.get(anyString()), times(1));
mockedFiles.verify(() -> Files.exists(paths), times(1));
}
@Test
public void checkIsFalseIfPathIsProtectedTest() {
mockedPaths.when(() -> Paths.get(anyString())).thenThrow(SecurityException.class);
assertFalse(DotEnvConditionChecker.isEnabled());
mockedPaths.verify(() -> Paths.get(anyString()), times(1));
mockedFiles.verify(() -> Files.exists(any(Path.class)), times(0));
}
@Test
public void checkIsNoInvocationIfAlreadyCalculatedTest() throws NoSuchFieldException, IllegalAccessException {
Field cachedValueField = DotEnvConditionChecker.class.getDeclaredField("cachedValue");
cachedValueField.setAccessible(true);
cachedValueField.set(null, false);
assertFalse(DotEnvConditionChecker.isEnabled());
mockedPaths.verify(() -> Paths.get(anyString()), times(0));
mockedFiles.verify(() -> Files.exists(any(Path.class)), times(0));
}
}