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 com.jcabi.matchers.RegexMatchers;
8   import org.hamcrest.Matchers;
9   import org.junit.jupiter.api.Test;
10  
11  /**
12   * Test case for {@link PmdValidator}'s rule that static fields and
13   * methods must be accessed via the class name, not via an instance
14   * or {@code this}.
15   * @since 0.25.1
16   */
17  final class PmdStaticAccessTest {
18  
19      /**
20       * Pattern: static field accessed directly or via instance.
21       */
22      private static final String STATIC_ACCESS =
23          "%s\\[\\d+-\\d+\\]: Static fields should be accessed in a static way \\[CLASS_NAME.FIELD_NAME\\]\\.";
24  
25      /**
26       * Pattern: static member accessed via {@code this} reference.
27       */
28      private static final String STATIC_VIA_THIS =
29          "%s\\[\\d+-\\d+\\]: Static members should be accessed in a static way \\[CLASS_NAME.FIELD_NAME\\], not via instance reference.";
30  
31      @Test
32      void acceptsCallToStaticFieldsInStaticWay() throws Exception {
33          final String file = "StaticAccessToStaticFields.java";
34          new PmdAssert(
35              file,
36              Matchers.is(true),
37              Matchers.allOf(
38                  Matchers.not(
39                      RegexMatchers.containsPattern(
40                          String.format(PmdStaticAccessTest.STATIC_ACCESS, file)
41                      )
42                  ),
43                  Matchers.not(
44                      RegexMatchers.containsPattern(
45                          String.format(PmdStaticAccessTest.STATIC_VIA_THIS, file)
46                      )
47                  )
48              )
49          ).assertOk();
50      }
51  
52      @Test
53      void acceptsDirectAccessToStaticFieldInStaticInitializer() throws Exception {
54          final String file = "StaticInitializerAssignsFinalField.java";
55          new PmdAssert(
56              file,
57              Matchers.is(true),
58              Matchers.not(
59                  RegexMatchers.containsPattern(
60                      String.format(PmdStaticAccessTest.STATIC_ACCESS, file)
61                  )
62              )
63          ).assertOk();
64      }
65  
66      @Test
67      void forbidsCallToStaticFieldsDirectly() throws Exception {
68          final String file = "DirectAccessToStaticFields.java";
69          new PmdAssert(
70              file,
71              Matchers.is(false),
72              RegexMatchers.containsPattern(
73                  String.format(PmdStaticAccessTest.STATIC_ACCESS, file)
74              )
75          ).assertOk();
76      }
77  
78      @Test
79      void forbidsCallToStaticFieldsViaThis() throws Exception {
80          final String file = "AccessToStaticFieldsViaThis.java";
81          new PmdAssert(
82              file,
83              Matchers.is(false),
84              RegexMatchers.containsPattern(
85                  String.format(PmdStaticAccessTest.STATIC_VIA_THIS, file)
86              )
87          ).assertOk();
88      }
89  
90      @Test
91      void forbidsCallToStaticMethodsViaThis() throws Exception {
92          final String file = "AccessToStaticMethodsViaThis.java";
93          new PmdAssert(
94              file,
95              Matchers.is(false),
96              RegexMatchers.containsPattern(
97                  String.format(PmdStaticAccessTest.STATIC_VIA_THIS, file)
98              )
99          ).assertOk();
100     }
101 }