Search in sources :

Example 81 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.

the class CheckerTest method testSetupChildListener.

@Test
public void testSetupChildListener() throws Exception {
    final Checker checker = new Checker();
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    checker.setModuleFactory(factory);
    final Configuration config = new DefaultConfiguration(DebugAuditAdapter.class.getCanonicalName());
    checker.setupChild(config);
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Test(org.junit.Test)

Example 82 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.

the class ConfigurationLoaderTest method testLoadConfigurationDeprecated.

@Test
public void testLoadConfigurationDeprecated() {
    try {
        @SuppressWarnings("deprecation") final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration(new FileInputStream(getConfigPath("config_with_ignore.xml")), new PropertiesExpander(new Properties()), true);
        final Configuration[] children = config.getChildren();
        assertEquals(0, children[0].getChildren().length);
    } catch (CheckstyleException | FileNotFoundException ex) {
        fail("unexpected exception");
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) FileNotFoundException(java.io.FileNotFoundException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 83 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.

the class ConfigurationLoaderTest method testCheckstyleChecks.

@Test
public void testCheckstyleChecks() throws Exception {
    final Properties props = new Properties();
    props.setProperty("checkstyle.basedir", "basedir");
    final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration("checkstyle_checks.xml", props);
    //verify the root, and property substitution
    final Properties atts = new Properties();
    atts.setProperty("tabWidth", "4");
    atts.setProperty("basedir", "basedir");
    verifyConfigNode(config, "Checker", 3, atts);
    //verify children
    final Configuration[] children = config.getChildren();
    atts.clear();
    verifyConfigNode((DefaultConfiguration) children[1], "JavadocPackage", 0, atts);
    verifyConfigNode((DefaultConfiguration) children[2], "Translation", 0, atts);
    atts.setProperty("testName", "testValue");
    verifyConfigNode((DefaultConfiguration) children[0], "TreeWalker", 8, atts);
    //verify TreeWalker's first, last, NoWhitespaceAfterCheck
    final Configuration[] grandchildren = children[0].getChildren();
    atts.clear();
    verifyConfigNode((DefaultConfiguration) grandchildren[0], "AvoidStarImport", 0, atts);
    atts.setProperty("format", "System.out.println");
    verifyConfigNode((DefaultConfiguration) grandchildren[grandchildren.length - 1], "GenericIllegalRegexp", 0, atts);
    atts.clear();
    atts.setProperty("tokens", "DOT");
    atts.setProperty("allowLineBreaks", "true");
    verifyConfigNode((DefaultConfiguration) grandchildren[6], "NoWhitespaceAfter", 0, atts);
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 84 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.

the class ConfigurationLoaderTest method testLoadConfigurationWrongUrl.

@Test
public void testLoadConfigurationWrongUrl() {
    try {
        final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration(";config_with_ignore.xml", new PropertiesExpander(new Properties()), true);
        final Configuration[] children = config.getChildren();
        assertEquals(0, children[0].getChildren().length);
        fail("Exception is expected");
    } catch (CheckstyleException ex) {
        assertEquals("Unable to find: ;config_with_ignore.xml", ex.getMessage());
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 85 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.

the class ConfigurationLoaderTest method testCustomMessages.

@Test
public void testCustomMessages() throws CheckstyleException {
    final Properties props = new Properties();
    props.setProperty("checkstyle.basedir", "basedir");
    final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration("custom_messages.xml", props);
    final Configuration[] children = config.getChildren();
    final Configuration[] grandchildren = children[0].getChildren();
    assertTrue(grandchildren[0].getMessages().containsKey("name.invalidPattern"));
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)137 Test (org.junit.Test)127 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)16 Properties (java.util.Properties)13 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)9 File (java.io.File)9 Checker (com.puppycrawl.tools.checkstyle.Checker)5 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)4 OneTopLevelClassCheck (com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)2 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)2 RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)2 AnnotationLocationCheck (com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck)2 EmptyLineSeparatorCheck (com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck)2 MethodParamPadCheck (com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2