1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
121
122
123
124
125
126
127
128
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 }