xref: /OpenGrok/opengrok-web/src/test/java/org/opengrok/web/util/FileUtilTest.java (revision 5082f8ec5e11b8259d833bfd5e6749eb0030ddad)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
22  * Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
23  */
24 package org.opengrok.web.util;
25 
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.ValueSource;
29 import org.opengrok.indexer.configuration.RuntimeEnvironment;
30 
31 import static org.junit.jupiter.api.Assertions.assertNotNull;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 
35 import java.io.File;
36 import java.io.FileNotFoundException;
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.InvalidPathException;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.UUID;
43 
44 /**
45  * Represents a container for tests of {@link FileUtil}.
46  */
47 class FileUtilTest {
48 
49     RuntimeEnvironment env = RuntimeEnvironment.getInstance();
50 
51     @Test
shouldThrowOnNullArgument()52     void shouldThrowOnNullArgument() {
53         assertThrows(NoPathParameterException.class, () -> FileUtil.toFile(null),
54                 "toFile(null)");
55     }
56 
setSourceRoot(String id)57     private String setSourceRoot(String id) throws IOException {
58         String origRoot = env.getSourceRootPath();
59         Path dir = Files.createTempDirectory(id);
60         dir.toFile().deleteOnExit();
61         env.setSourceRoot(dir.toString());
62         assertTrue(env.getSourceRootFile().isDirectory());
63         return origRoot;
64     }
65 
66     @Test
shouldThrowOnInvalidFile()67     void shouldThrowOnInvalidFile() throws IOException {
68         String origRoot = setSourceRoot("shouldThrowOnInvalidFile");
69         String rndPath = ".." + File.separator + UUID.randomUUID();
70         assertThrows(InvalidPathException.class, () -> FileUtil.toFile(rndPath),
71                 "toFile(randomUUID)");
72         env.setSourceRoot(origRoot);
73     }
74 
75     @ParameterizedTest
76     @ValueSource(booleans = {true, false})
shouldThrowOnMissingFile(boolean isPresent)77     void shouldThrowOnMissingFile(boolean isPresent) throws IOException, NoPathParameterException {
78         String origRoot = setSourceRoot("shouldThrowOnMissingFile");
79         if (isPresent) {
80             String fileName = "existent";
81             Path filePath = Paths.get(env.getSourceRootPath(), fileName);
82             Files.createFile(filePath);
83             filePath.toFile().deleteOnExit();
84             assertTrue(filePath.toFile().exists());
85             assertNotNull(FileUtil.toFile(fileName));
86         } else {
87             assertThrows(FileNotFoundException.class, () -> FileUtil.toFile("nonexistent"),
88                     "toFile(nonexistent)");
89         }
90         env.setSourceRoot(origRoot);
91     }
92 }
93