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 public 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
90 final CheckstyleException exception =
91 getExpectedThrowable(CheckstyleException.class,
92 () -> ChainedPropertyUtil.getResolvedProperties(properties));
93
94 assertWithMessage("Undefined property reference expected.")
95 .that(exception)
96 .hasMessageThat()
97 .isEqualTo(expected);
98 }
99
100 @Test
101 public void testPropertyChainingRecursiveUnresolvable() throws Exception {
102 final File propertiesFile =
103 new File(getPath("InputChainedPropertyUtilRecursiveUnresolvable.properties"));
104 final Properties properties = loadProperties(propertiesFile);
105 final String expected = ChainedPropertyUtil.UNDEFINED_PROPERTY_MESSAGE;
106
107 final CheckstyleException exception =
108 getExpectedThrowable(CheckstyleException.class,
109 () -> ChainedPropertyUtil.getResolvedProperties(properties));
110
111 assertWithMessage("Undefined property reference expected.")
112 .that(exception)
113 .hasMessageThat()
114 .contains(expected);
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 private static Properties loadProperties(File file) throws CheckstyleException {
129 final Properties properties = new Properties();
130
131 try (InputStream stream = Files.newInputStream(file.toPath())) {
132 properties.load(stream);
133 }
134 catch (final IOException exc) {
135 throw new CheckstyleException("Failed to load properties ", exc);
136 }
137
138 return properties;
139 }
140 }