View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.utils;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
24  
25  import java.io.File;
26  import java.util.UUID;
27  
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.io.TempDir;
30  
31  public class FilterUtilTest {
32  
33      @TempDir
34      public File temporaryFolder;
35  
36      @Test
37      public void testIsProperUtilsClass() throws ReflectiveOperationException {
38          assertWithMessage("Constructor is not private")
39                  .that(isUtilsClassHasPrivateConstructor(FilterUtil.class))
40                  .isTrue();
41      }
42  
43      @Test
44      public void testExistingFile() throws Exception {
45          final String uniqueFileName = "junit_" + UUID.randomUUID() + ".java";
46          final File file = new File(temporaryFolder, uniqueFileName);
47          file.createNewFile();
48          assertWithMessage("Suppression file exists")
49                  .that(FilterUtil.isFileExists(file.getPath()))
50                  .isTrue();
51      }
52  
53      @Test
54      public void testNonExistentFile() {
55          assertWithMessage("Suppression file does not exist")
56                  .that(FilterUtil.isFileExists("non-existent.xml"))
57                  .isFalse();
58      }
59  
60  }