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