1818 */
1919
2020/*
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.
2222 * Copyright (c) 2019, Chris Fraire <[email protected] >. 2323 */
2424package org .opengrok .indexer .util ;
2525
2626import org .junit .jupiter .api .Test ;
27+ import org .mockito .MockedStatic ;
28+ import org .mockito .Mockito ;
2729import org .opengrok .indexer .configuration .RuntimeEnvironment ;
2830
2931import java .io .IOException ;
3537import static org .junit .jupiter .api .Assertions .assertFalse ;
3638import static org .junit .jupiter .api .Assertions .assertNotNull ;
3739import 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 ;
3843
3944/**
4045 * Represents a container for tests of {@link CtagsUtil}.
@@ -51,19 +56,40 @@ void getLanguages() {
5156 }
5257
5358 @ Test
54- void validate () throws IOException {
59+ void testIsValid () throws IOException {
5560 RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
5661 Path tmpSourceRoot = Files .createTempDirectory ("srcRootCtagsValidationTest" );
5762 env .setSourceRoot (tmpSourceRoot .toString ());
5863 assertTrue (env .getSourceRootFile ().exists ());
5964
60- assertTrue (CtagsUtil .validate (env .getCtags ()));
65+ assertTrue (CtagsUtil .isValid (env .getCtags ()));
6166
6267 Files .delete (tmpSourceRoot );
6368 }
6469
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+ */
6574 @ 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 {
6793 RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
6894 Path tmpSourceRoot = Files .createTempDirectory ("srcRootCtagsValidationTestExtraArgs" );
6995 env .setSourceRoot (tmpSourceRoot .toString ());
@@ -74,7 +100,7 @@ void testValidateWithInvalidExtraOptions() throws IOException {
74100 String extraOptionsAbsPath = extraOptionsPath .toAbsolutePath ().toString ();
75101
76102 env .setCTagsExtraOptionsFile (extraOptionsAbsPath );
77- assertFalse (CtagsUtil .validate (env .getCtags ()));
103+ assertFalse (CtagsUtil .isValid (env .getCtags ()));
78104
79105 // cleanup
80106 env .setCTagsExtraOptionsFile (null );
0 commit comments