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   * @since 0.25.1
14   */
15  final class PmdFilesCreateFileTest {
16  
17      /**
18       * Error message for the forbidden Files.createFile usage in tests.
19       */
20      private static final String FILES_CREATE_ERR =
21          "Files.createFile should not be used in tests, replace them with @Rule TemporaryFolder";
22  
23      @Test
24      void forbidsFilesCreateFileInTests() throws Exception {
25          new PmdAssert(
26              "FilesCreateFileTest.java",
27              Matchers.is(false),
28              Matchers.containsString(
29                  PmdFilesCreateFileTest.FILES_CREATE_ERR
30              )
31          ).assertOk();
32      }
33  
34      @Test
35      void allowsFilesCreateFileOutsideOfTests() throws Exception {
36          new PmdAssert(
37              "FilesCreateFileOther.java",
38              Matchers.is(true),
39              Matchers.not(
40                  Matchers.containsString(
41                      PmdFilesCreateFileTest.FILES_CREATE_ERR
42                  )
43              )
44          ).assertOk();
45      }
46  }