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.getExpectedThrowable;
24  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.nio.file.Files;
30  import java.util.Properties;
31  
32  import org.junit.jupiter.api.Test;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.PropertiesExpander;
36  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
37  
38  public class ChainedPropertyUtilTest extends AbstractModuleTestSupport {
39  
40      @Override
41      protected String getPackageLocation() {
42          return "com/puppycrawl/tools/checkstyle/utils/chainedpropertyutil";
43      }
44  
45      @Test
46      public void testIsProperUtilsClass() throws ReflectiveOperationException {
47          assertWithMessage("Constructor is not private.")
48              .that(isUtilsClassHasPrivateConstructor(ChainedPropertyUtil.class))
49              .isTrue();
50      }
51  
52      @Test
53      public void testPropertyChaining() throws Exception {
54          final File propertiesFile =
55              new File(getPath("InputChainedPropertyUtil.properties"));
56          final Properties properties = loadProperties(propertiesFile);
57          final Properties resolvedProperties =
58              ChainedPropertyUtil.getResolvedProperties(properties);
59          final PropertiesExpander expander = new PropertiesExpander(resolvedProperties);
60          final String message = "Unexpected property resolution.";
61  
62          assertWithMessage(message)
63              .that(expander.resolve("basedir"))
64              .isEqualTo("/home");
65          assertWithMessage(message)
66              .that(expander.resolve("checkstyle.dir"))
67              .isEqualTo("/home/checkstyle");
68          assertWithMessage(message)
69              .that(expander.resolve("config.dir"))
70              .isEqualTo("/home/checkstyle/configs");
71          assertWithMessage(message)
72              .that(expander.resolve("checkstyle.suppressions.file"))
73              .isEqualTo("/home/checkstyle/configs/suppressions.xml");
74          assertWithMessage(message)
75              .that(expander.resolve("checkstyle.dir"))
76              .isEqualTo("/home/checkstyle");
77          assertWithMessage(message)
78              .that(expander.resolve("str"))
79              .isEqualTo("value");
80      }
81  
82      @Test
83      public void testPropertyChainingPropertyNotFound() throws Exception {
84          final File propertiesFile =
85              new File(getPath("InputChainedPropertyUtilUndefinedProperty.properties"));
86          final Properties properties = loadProperties(propertiesFile);
87          final String expected =
88              ChainedPropertyUtil.UNDEFINED_PROPERTY_MESSAGE + "[property.not.found]";
89          final String message = "Undefined property reference expected.";
90  
91          final CheckstyleException exception =
92              getExpectedThrowable(CheckstyleException.class,
93                  () -> ChainedPropertyUtil.getResolvedProperties(properties));
94  
95          assertWithMessage(message)
96              .that(exception)
97              .hasMessageThat()
98              .isEqualTo(expected);
99      }
100 
101     @Test
102     public void testPropertyChainingRecursiveUnresolvable() throws Exception {
103         final File propertiesFile =
104             new File(getPath("InputChainedPropertyUtilRecursiveUnresolvable.properties"));
105         final Properties properties = loadProperties(propertiesFile);
106         final String expected = ChainedPropertyUtil.UNDEFINED_PROPERTY_MESSAGE;
107         final String message = "Undefined property reference expected.";
108 
109         final CheckstyleException exception =
110             getExpectedThrowable(CheckstyleException.class,
111                 () -> ChainedPropertyUtil.getResolvedProperties(properties));
112 
113         assertWithMessage(message)
114             .that(exception)
115             .hasMessageThat()
116             .contains(expected);
117     }
118 
119     /**
120      * Loads properties from a file. We could load properties inline
121      * with StringReader, but that would preserve the order of the
122      * properties. Since properties are not loaded/ stored in
123      * sequential order, it is important to maintain this
124      * random property order for testing.
125      *
126      * @param file the properties file
127      * @return the properties in file
128      * @throws CheckstyleException when cannot load properties file
129      */
130     private static Properties loadProperties(File file) throws CheckstyleException {
131         final Properties properties = new Properties();
132 
133         try (InputStream stream = Files.newInputStream(file.toPath())) {
134             properties.load(stream);
135         }
136         catch (final IOException ex) {
137             throw new CheckstyleException(ex.getMessage(), ex);
138         }
139 
140         return properties;
141     }
142 }