001: package com.puppycrawl.tools.checkstyle;
002:
003: import java.util.Properties;
004:
005: import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
006: import com.puppycrawl.tools.checkstyle.api.Configuration;
007: import junit.framework.TestCase;
008:
009: /**
010: * @author Rick Giles
011: * @author lkuehne
012: * @version $Revision$
013: */
014: public class ConfigurationLoaderTest extends TestCase {
015: private Configuration loadConfiguration(String aName)
016: throws CheckstyleException {
017: return loadConfiguration(aName, new Properties());
018: }
019:
020: private Configuration loadConfiguration(String aName,
021: Properties aProps) throws CheckstyleException {
022: final String fName = System.getProperty("testinputs.dir")
023: + "/configs/" + aName;
024:
025: return ConfigurationLoader.loadConfiguration(fName,
026: new PropertiesExpander(aProps));
027: }
028:
029: public void testEmptyConfiguration() throws Exception {
030: final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration("empty_configuration.xml");
031: verifyConfigNode(config, "Checker", 0, new Properties());
032: }
033:
034: public void testMissingPropertyName() {
035: try {
036: loadConfiguration("missing_property_name.xml");
037: fail("missing property name");
038: } catch (CheckstyleException ex) {
039: assertTrue(ex.getMessage().endsWith(
040: "Attribute \"name\" is required and must be specified "
041: + "for element type \"property\".:8:41"));
042: }
043: }
044:
045: public void testMissingPropertyValue() {
046: try {
047: loadConfiguration("missing_property_value.xml");
048: fail("missing property value");
049: } catch (CheckstyleException ex) {
050: assertTrue(ex.getMessage().endsWith(
051: "Attribute \"value\" is required and must be specified "
052: + "for element type \"property\".:8:41"));
053: }
054: }
055:
056: public void testMissingConfigName() {
057: try {
058: loadConfiguration("missing_config_name.xml");
059: fail("missing module name");
060: } catch (CheckstyleException ex) {
061: assertTrue(ex.getMessage().endsWith(
062: "Attribute \"name\" is required and must be specified "
063: + "for element type \"module\".:7:23"));
064: }
065: }
066:
067: public void testMissingConfigParent() {
068: try {
069: loadConfiguration("missing_config_parent.xml");
070: fail("missing module parent");
071: } catch (CheckstyleException ex) {
072: assertTrue(ex.getMessage().endsWith(
073: "Document root element \"property\", must match DOCTYPE "
074: + "root \"module\".:7:38"));
075: }
076: }
077:
078: public void testCheckstyleChecks() throws Exception {
079: final Properties props = new Properties();
080: props.put("checkstyle.basedir", "basedir");
081:
082: final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration(
083: "checkstyle_checks.xml", props);
084:
085: //verify the root, and property substitution
086: final Properties atts = new Properties();
087: atts.put("tabWidth", "4");
088: atts.put("basedir", "basedir");
089: verifyConfigNode(config, "Checker", 3, atts);
090:
091: //verify children
092: final Configuration[] children = config.getChildren();
093: atts.clear();
094: verifyConfigNode((DefaultConfiguration) children[1],
095: "PackageHtml", 0, atts);
096: verifyConfigNode((DefaultConfiguration) children[2],
097: "Translation", 0, atts);
098: atts.put("testName", "testValue");
099: verifyConfigNode((DefaultConfiguration) children[0],
100: "TreeWalker", 8, atts);
101:
102: //verify TreeWalker's first, last, NoWhitespaceAfterCheck
103: final Configuration[] grandchildren = children[0].getChildren();
104: atts.clear();
105: verifyConfigNode((DefaultConfiguration) grandchildren[0],
106: "AvoidStarImport", 0, atts);
107: atts.put("format", "System.out.println");
108: verifyConfigNode(
109: (DefaultConfiguration) grandchildren[grandchildren.length - 1],
110: "GenericIllegalRegexp", 0, atts);
111: atts.clear();
112: atts.put("tokens", "DOT");
113: atts.put("allowLineBreaks", "true");
114: verifyConfigNode((DefaultConfiguration) grandchildren[6],
115: "NoWhitespaceAfter", 0, atts);
116: }
117:
118: private void verifyConfigNode(DefaultConfiguration aConfig,
119: String aName, int aChildrenLength, Properties atts)
120: throws Exception {
121: assertEquals("name.", aName, aConfig.getName());
122: assertEquals("children.length.", aChildrenLength, aConfig
123: .getChildren().length);
124:
125: final String[] attNames = aConfig.getAttributeNames();
126: assertEquals("attributes.length", atts.size(), attNames.length);
127:
128: for (int i = 0; i < attNames.length; i++) {
129: assertEquals("attribute[" + attNames[i] + "]", atts
130: .get(attNames[i]), aConfig
131: .getAttribute(attNames[i]));
132: }
133: }
134:
135: public void testReplacePropertiesNoReplace()
136: throws CheckstyleException {
137: final String[] testValues = { null, "", "a", "$a", "{a", "{a}",
138: "a}", "$a}", "$", "a$b" };
139: final Properties props = initProperties();
140: for (int i = 0; i < testValues.length; i++) {
141: final String value = ConfigurationLoader.replaceProperties(
142: testValues[i], new PropertiesExpander(props), null);
143: assertEquals("\"" + testValues[i] + "\"", value,
144: testValues[i]);
145: }
146: }
147:
148: public void testReplacePropertiesSyntaxError() {
149: final Properties props = initProperties();
150: try {
151: final String value = ConfigurationLoader.replaceProperties(
152: "${a", new PropertiesExpander(props), null);
153: fail("expected to fail, instead got: " + value);
154: } catch (CheckstyleException ex) {
155: assertEquals("Syntax error in property: ${a", ex
156: .getMessage());
157: }
158: }
159:
160: public void testReplacePropertiesMissingProperty() {
161: final Properties props = initProperties();
162: try {
163: final String value = ConfigurationLoader.replaceProperties(
164: "${c}", new PropertiesExpander(props), null);
165: fail("expected to fail, instead got: " + value);
166: } catch (CheckstyleException ex) {
167: assertEquals("Property ${c} has not been set", ex
168: .getMessage());
169: }
170: }
171:
172: public void testReplacePropertiesReplace()
173: throws CheckstyleException {
174: final String[][] testValues = { { "${a}", "A" },
175: { "x${a}", "xA" }, { "${a}x", "Ax" },
176: { "${a}${b}", "AB" }, { "x${a}${b}", "xAB" },
177: { "${a}x${b}", "AxB" }, { "${a}${b}x", "ABx" },
178: { "x${a}y${b}", "xAyB" }, { "${a}x${b}y", "AxBy" },
179: { "x${a}${b}y", "xABy" }, { "x${a}y${b}z", "xAyBz" },
180: { "$$", "$" }, };
181: final Properties props = initProperties();
182: for (int i = 0; i < testValues.length; i++) {
183: final String value = ConfigurationLoader.replaceProperties(
184: testValues[i][0], new PropertiesExpander(props),
185: null);
186: assertEquals("\"" + testValues[i][0] + "\"",
187: testValues[i][1], value);
188: }
189: }
190:
191: private Properties initProperties() {
192: final Properties props = new Properties();
193: props.put("a", "A");
194: props.put("b", "B");
195: return props;
196: }
197:
198: }
|