18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
22
22
* Copyright (c) 2019, Chris Fraire <cfraire@me.com>.
23
23
*/
24
24
package org .opengrok .indexer .util ;
25
25
26
26
import org .junit .jupiter .api .Test ;
27
+ import org .mockito .MockedStatic ;
28
+ import org .mockito .Mockito ;
27
29
import org .opengrok .indexer .configuration .RuntimeEnvironment ;
28
30
29
31
import java .io .IOException ;
35
37
import static org .junit .jupiter .api .Assertions .assertFalse ;
36
38
import static org .junit .jupiter .api .Assertions .assertNotNull ;
37
39
import static org .junit .jupiter .api .Assertions .assertTrue ;
40
+ import static org .mockito .ArgumentMatchers .eq ;
41
+ import static org .mockito .Mockito .mockStatic ;
42
+ import static org .mockito .Mockito .times ;
38
43
39
44
/**
40
45
* Represents a container for tests of {@link CtagsUtil}.
@@ -51,19 +56,40 @@ void getLanguages() {
51
56
}
52
57
53
58
@ Test
54
- void validate () throws IOException {
59
+ void testIsValid () throws IOException {
55
60
RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
56
61
Path tmpSourceRoot = Files .createTempDirectory ("srcRootCtagsValidationTest" );
57
62
env .setSourceRoot (tmpSourceRoot .toString ());
58
63
assertTrue (env .getSourceRootFile ().exists ());
59
64
60
- assertTrue (CtagsUtil .validate (env .getCtags ()));
65
+ assertTrue (CtagsUtil .isValid (env .getCtags ()));
61
66
62
67
Files .delete (tmpSourceRoot );
63
68
}
64
69
70
+ /**
71
+ * Simulate non-writable source root and verify that {@link CtagsUtil#isValid(String)} still returns true
72
+ * as it should fall back to default temporary directory.
73
+ */
65
74
@ Test
66
- void testValidateWithInvalidExtraOptions () throws IOException {
75
+ void testIsValidNoWritableSourceRoot () throws IOException {
76
+ RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
77
+ Path tmpSourceRoot = Files .createTempDirectory ("negativeCtagsValidationTest" );
78
+ env .setSourceRoot (tmpSourceRoot .toString ());
79
+ assertTrue (env .getSourceRootFile ().exists ());
80
+
81
+ try (MockedStatic <CtagsUtil > mocked = mockStatic (CtagsUtil .class , Mockito .CALLS_REAL_METHODS )) {
82
+ mocked .when (() -> CtagsUtil .canProcessFiles (env .getSourceRootFile ())).thenReturn (false );
83
+ assertTrue (CtagsUtil .isValid (env .getCtags ()));
84
+ mocked .verify (() -> CtagsUtil .canProcessFiles (eq (env .getSourceRootFile ())),
85
+ times (2 )); // one extra for the lambda call above
86
+ }
87
+
88
+ Files .delete (tmpSourceRoot );
89
+ }
90
+
91
+ @ Test
92
+ void testIsValidWithInvalidExtraOptions () throws IOException {
67
93
RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
68
94
Path tmpSourceRoot = Files .createTempDirectory ("srcRootCtagsValidationTestExtraArgs" );
69
95
env .setSourceRoot (tmpSourceRoot .toString ());
@@ -74,7 +100,7 @@ void testValidateWithInvalidExtraOptions() throws IOException {
74
100
String extraOptionsAbsPath = extraOptionsPath .toAbsolutePath ().toString ();
75
101
76
102
env .setCTagsExtraOptionsFile (extraOptionsAbsPath );
77
- assertFalse (CtagsUtil .validate (env .getCtags ()));
103
+ assertFalse (CtagsUtil .isValid (env .getCtags ()));
78
104
79
105
// cleanup
80
106
env .setCTagsExtraOptionsFile (null );
0 commit comments