KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > ConfigurationLoaderTest


1 package com.puppycrawl.tools.checkstyle;
2
3 import java.util.Properties JavaDoc;
4
5 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
6 import com.puppycrawl.tools.checkstyle.api.Configuration;
7 import junit.framework.TestCase;
8
9
10 /**
11  * @author Rick Giles
12  * @author lkuehne
13  * @version $Revision$
14  */

15 public class ConfigurationLoaderTest extends TestCase
16 {
17     private Configuration loadConfiguration(String JavaDoc aName)
18         throws CheckstyleException
19     {
20         return loadConfiguration(aName, new Properties JavaDoc());
21     }
22
23     private Configuration loadConfiguration(
24         String JavaDoc aName, Properties JavaDoc aProps) throws CheckstyleException
25     {
26         final String JavaDoc fName =
27             System.getProperty("testinputs.dir") + "/configs/" + aName;
28
29         return ConfigurationLoader.loadConfiguration(
30             fName, new PropertiesExpander(aProps));
31     }
32
33     public void testEmptyConfiguration() throws Exception JavaDoc
34     {
35         final DefaultConfiguration config =
36             (DefaultConfiguration) loadConfiguration("empty_configuration.xml");
37         verifyConfigNode(config, "Checker", 0, new Properties JavaDoc());
38     }
39
40     public void testMissingPropertyName()
41     {
42         try {
43             loadConfiguration("missing_property_name.xml");
44             fail("missing property name");
45         }
46         catch (CheckstyleException ex) {
47             assertTrue(
48                 ex.getMessage().endsWith(
49                     "Attribute \"name\" is required and must be specified "
50                         + "for element type \"property\".:8:41"));
51         }
52     }
53
54     public void testMissingPropertyValue()
55     {
56         try {
57             loadConfiguration("missing_property_value.xml");
58             fail("missing property value");
59         }
60         catch (CheckstyleException ex) {
61             assertTrue(
62                 ex.getMessage().endsWith(
63                     "Attribute \"value\" is required and must be specified "
64                         + "for element type \"property\".:8:41"));
65         }
66     }
67
68     public void testMissingConfigName()
69     {
70         try {
71             loadConfiguration("missing_config_name.xml");
72             fail("missing module name");
73         }
74         catch (CheckstyleException ex) {
75             assertTrue(
76                 ex.getMessage().endsWith(
77                     "Attribute \"name\" is required and must be specified "
78                         + "for element type \"module\".:7:23"));
79         }
80     }
81
82     public void testMissingConfigParent()
83     {
84         try {
85             loadConfiguration("missing_config_parent.xml");
86             fail("missing module parent");
87         }
88         catch (CheckstyleException ex) {
89             assertTrue(
90                 ex.getMessage().endsWith(
91                     "Document root element \"property\", must match DOCTYPE "
92                         + "root \"module\".:7:38"));
93         }
94     }
95
96     public void testCheckstyleChecks() throws Exception JavaDoc
97     {
98         final Properties JavaDoc props = new Properties JavaDoc();
99         props.put("checkstyle.basedir", "basedir");
100
101         final DefaultConfiguration config =
102             (DefaultConfiguration) loadConfiguration(
103                 "checkstyle_checks.xml", props);
104
105         //verify the root, and property substitution
106
final Properties JavaDoc atts = new Properties JavaDoc();
107         atts.put("tabWidth", "4");
108         atts.put("basedir", "basedir");
109         verifyConfigNode(config, "Checker", 3, atts);
110
111         //verify children
112
final Configuration[] children = config.getChildren();
113         atts.clear();
114         verifyConfigNode(
115             (DefaultConfiguration) children[1], "PackageHtml", 0, atts);
116         verifyConfigNode(
117             (DefaultConfiguration) children[2], "Translation", 0, atts);
118         atts.put("testName", "testValue");
119         verifyConfigNode(
120             (DefaultConfiguration) children[0],
121             "TreeWalker",
122             8,
123             atts);
124
125         //verify TreeWalker's first, last, NoWhitespaceAfterCheck
126
final Configuration[] grandchildren = children[0].getChildren();
127         atts.clear();
128         verifyConfigNode(
129             (DefaultConfiguration) grandchildren[0],
130             "AvoidStarImport",
131             0,
132             atts);
133         atts.put("format", "System.out.println");
134         verifyConfigNode(
135             (DefaultConfiguration) grandchildren[grandchildren.length - 1],
136             "GenericIllegalRegexp",
137             0,
138             atts);
139         atts.clear();
140         atts.put("tokens", "DOT");
141         atts.put("allowLineBreaks", "true");
142         verifyConfigNode(
143             (DefaultConfiguration) grandchildren[6],
144             "NoWhitespaceAfter",
145             0,
146             atts);
147     }
148
149     private void verifyConfigNode(
150         DefaultConfiguration aConfig, String JavaDoc aName, int aChildrenLength,
151         Properties JavaDoc atts) throws Exception JavaDoc
152     {
153         assertEquals("name.", aName, aConfig.getName());
154         assertEquals(
155             "children.length.",
156             aChildrenLength,
157             aConfig.getChildren().length);
158
159         final String JavaDoc[] attNames = aConfig.getAttributeNames();
160         assertEquals("attributes.length", atts.size(), attNames.length);
161
162         for (int i = 0; i < attNames.length; i++) {
163             assertEquals(
164                 "attribute[" + attNames[i] + "]",
165                 atts.get(attNames[i]),
166                 aConfig.getAttribute(attNames[i]));
167         }
168     }
169
170     public void testReplacePropertiesNoReplace()
171         throws CheckstyleException
172     {
173         final String JavaDoc[] testValues = {null, "", "a", "$a", "{a",
174                                        "{a}", "a}", "$a}", "$", "a$b"};
175         final Properties JavaDoc props = initProperties();
176         for (int i = 0; i < testValues.length; i++) {
177             final String JavaDoc value = ConfigurationLoader.replaceProperties(
178                 testValues[i], new PropertiesExpander(props), null);
179             assertEquals("\"" + testValues[i] + "\"", value, testValues[i]);
180         }
181     }
182
183     public void testReplacePropertiesSyntaxError()
184     {
185         final Properties JavaDoc props = initProperties();
186         try {
187             final String JavaDoc value = ConfigurationLoader.replaceProperties(
188                 "${a", new PropertiesExpander(props), null);
189             fail("expected to fail, instead got: " + value);
190         }
191         catch (CheckstyleException ex) {
192             assertEquals("Syntax error in property: ${a", ex.getMessage());
193         }
194     }
195
196     public void testReplacePropertiesMissingProperty()
197     {
198         final Properties JavaDoc props = initProperties();
199         try {
200             final String JavaDoc value = ConfigurationLoader.replaceProperties(
201                 "${c}", new PropertiesExpander(props), null);
202             fail("expected to fail, instead got: " + value);
203         }
204         catch (CheckstyleException ex) {
205             assertEquals("Property ${c} has not been set", ex.getMessage());
206         }
207     }
208
209     public void testReplacePropertiesReplace()
210         throws CheckstyleException
211     {
212         final String JavaDoc[][] testValues = {
213             {"${a}", "A"},
214             {"x${a}", "xA"},
215             {"${a}x", "Ax"},
216             {"${a}${b}", "AB"},
217             {"x${a}${b}", "xAB"},
218             {"${a}x${b}", "AxB"},
219             {"${a}${b}x", "ABx"},
220             {"x${a}y${b}", "xAyB"},
221             {"${a}x${b}y", "AxBy"},
222             {"x${a}${b}y", "xABy"},
223             {"x${a}y${b}z", "xAyBz"},
224             {"$$", "$"},
225             };
226         final Properties JavaDoc props = initProperties();
227         for (int i = 0; i < testValues.length; i++) {
228             final String JavaDoc value = ConfigurationLoader.replaceProperties(
229                 testValues[i][0], new PropertiesExpander(props), null);
230             assertEquals("\"" + testValues[i][0] + "\"",
231                 testValues[i][1], value);
232         }
233     }
234
235     private Properties JavaDoc initProperties()
236     {
237         final Properties JavaDoc props = new Properties JavaDoc();
238         props.put("a", "A");
239         props.put("b", "B");
240         return props;
241     }
242
243 }
244
Popular Tags