View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.pmd;
6   
7   import org.hamcrest.Matchers;
8   import org.junit.jupiter.api.Test;
9   
10  /**
11   * Test case for {@link PmdValidator}'s rule that forbids
12   * {@code Files.createFile} inside tests but allows it elsewhere.
13   *
14   * @since 0.25.1
15   */
16  final class PmdFilesCreateFileTest {
17  
18      /**
19       * Error message for the forbidden Files.createFile usage in tests.
20       */
21      private static final String FILES_CREATE_ERR =
22          "Files.createFile should not be used in tests, replace them with @Rule TemporaryFolder";
23  
24      @Test
25      void forbidsFilesCreateFileInTests() throws Exception {
26          new PmdAssert(
27              "FilesCreateFileTest.java",
28              Matchers.is(false),
29              Matchers.containsString(
30                  PmdFilesCreateFileTest.FILES_CREATE_ERR
31              )
32          ).assertOk();
33      }
34  
35      @Test
36      void allowsFilesCreateFileOutsideOfTests() throws Exception {
37          new PmdAssert(
38              "FilesCreateFileOther.java",
39              Matchers.is(true),
40              Matchers.not(
41                  Matchers.containsString(
42                      PmdFilesCreateFileTest.FILES_CREATE_ERR
43                  )
44              )
45          ).assertOk();
46      }
47  }