From a1422eed4e0261824f48a1ddf647dd90cb67063a Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Wed, 16 Jul 2008 04:29:48 +0000 Subject: [PATCH] clean up a unit test and documentation. --- .../api/AbstractViolationReporterTest.java | 32 +++------ src/xdocs/config.xml | 71 ++++++++++++------- 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporterTest.java b/src/tests/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporterTest.java index af2971a51..ef8be9916 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporterTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporterTest.java @@ -2,14 +2,10 @@ package com.puppycrawl.tools.checkstyle.api; import static org.junit.Assert.assertEquals; -import javax.swing.text.DefaultCaret; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import org.junit.Assert; +import org.junit.Test; /** * Tests to ensure that default messagebundle is determined correctly. @@ -40,7 +36,7 @@ public class AbstractViolationReporterTest extends BaseCheckTestSupport assertEquals("messages", emptyCheck.getMessageBundle("MyCoolCheck")); } - + @Test public void testCustomMessage() throws CheckstyleException { @@ -55,7 +51,7 @@ public class AbstractViolationReporterTest extends BaseCheckTestSupport LocalizedMessage[] messages = collector.getMessages(); Assert.assertTrue(messages.length == 1); - + Assert.assertEquals("This is a custom message.", messages[0].getMessage()); } @@ -70,14 +66,14 @@ public class AbstractViolationReporterTest extends BaseCheckTestSupport emptyCheck.setMessages(collector); emptyCheck.log(0, "msgKey", "TestParam"); - + LocalizedMessage[] messages = collector.getMessages(); Assert.assertTrue(messages.length == 1); - + Assert.assertEquals("This is a custom message with TestParam.", messages[0].getMessage()); } - - @Test + + @Test(expected=IllegalArgumentException.class) public void testCustomMessageWithParametersNegative() throws CheckstyleException { DefaultConfiguration config = createCheckConfig(emptyCheck.getClass()); @@ -88,18 +84,12 @@ public class AbstractViolationReporterTest extends BaseCheckTestSupport emptyCheck.setMessages(collector); emptyCheck.log(0, "msgKey", "TestParam"); - + LocalizedMessage[] messages = collector.getMessages(); Assert.assertTrue(messages.length == 1); - + //we expect an exception here because of the bogus custom message //format - try { - messages[0].getMessage(); - Assert.fail("Didn't receive expected exception."); - } - catch (IllegalArgumentException e) { - //expected - } + messages[0].getMessage(); } } diff --git a/src/xdocs/config.xml b/src/xdocs/config.xml index 57cef8c21..607a4db3f 100755 --- a/src/xdocs/config.xml +++ b/src/xdocs/config.xml @@ -547,38 +547,55 @@

- As of Checkstyle 5 all checks can be configured to report custom, - configuration specific messages instead of the Checkstyle default messages.
- This can be useful in cases where the check message should reference - corresponding sections in a coding style document or the default is - too generic for junior developers to understand.
+ As of Checkstyle 5 all checks can be configured to report + custom, configuration specific messages instead of the + Checkstyle default messages. This can be useful in cases where + the check message should reference corresponding sections in a + coding style document or the default is too generic for + developers to understand.

- Example: + +

An example usage is:

+ <module name="MemberName"> <property name="format" value="^m[a-zA-Z0-9]*$"/> - <message key="name.invalidPattern" value="Member ''{0}'' must start with a lowercase ''m'' (checked pattern ''{1}'')."/> + <message key="name.invalidPattern" + value="Member ''{0}'' must start with a lowercase ''m'' (checked pattern ''{1}'')." + /> </module> +

- Each check configuration element can host 0-n message elements. - Every check uses one or more distinct message keys to log violations. If you want to customize - a certain message you need to specify the message key in the key attribute - of the message element.
- The value attribute specifies the custom message pattern, as shown in the - example.
- Placeholders used in the default message can also be used in the custom message.
- Note that the message pattern must be a valid java.text.MessageFormat style - pattern, so be careful about curly braces outside a placeholder definition. + Each check configuration element can zero or more message elements. Every check uses one or + more distinct message keys to log violations. If you want to + customize a certain message you need to specify the message key + in the key attribute of the message element.

+

- How do you know, which message keys a check uses, so that you can override them?
- Well, thats the trick part. To find out which keys a check uses you currently need to look into - the checks source code, in conjunction with the checks messages.properties file. - Tools/plugins might come to the rescue on this topic, so have a look there. + The value attribute specifies the + custom message pattern, as shown in the example above. + Placeholders used in the default message can also be used in the + custom message. Note that the message pattern must be a valid + java.text.MessageFormat style pattern, + so be careful about curly braces outside a placeholder + definition. +

+ +

+ The obvious question is how do you know which message keys a + Check uses, so that you can override them? Well, that is the + tricky part. To find out which keys a Check uses you currently + need to look into the Check's source code, in conjunction with + the Check's messages.properties file. + Tools/plugins might come to the rescue on this topic, so have a + look there.

- +

@@ -650,7 +667,7 @@ document in a file. If there is no configured suppressions file, the Filter accepts all audit events.

- +
SuppressionFilter Properties
@@ -988,15 +1005,15 @@ are distributed with Checkstyle.

- To specify other packages to apply, - create a package names XML document in a file + To specify other packages to apply, + create a package names XML document in a file named checkstyle_packages.xml, and provide that file in the root of the .jar containing your custom checks.

- Note that the support for providing a package names XML document - via command line option or as a attribute + Note that the support for providing a package names XML document + via command line option or as a attribute of an ant Checkstyle task has been dropped with Checkstyle 5.0.

@@ -1058,7 +1075,7 @@

Note

- As of Checkstyle 5.0 it is unnecessary to repeat the + As of Checkstyle 5.0 it is unnecessary to repeat the package elements for Checkstyle's packages in your custom checkstyle_packages.xml file.