View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd;
    5
    6   import static org.junit.Assert.assertEquals;
    7   import static org.junit.Assert.assertTrue;
    8
    9   import java.io.IOException;
    10  import java.net.URL;
    11  import java.util.Properties;
    12
    13  import junit.framework.JUnit4TestAdapter;
    14  import net.sourceforge.pmd.PMDConfiguration;
    15  import net.sourceforge.pmd.PMD;
    16  import net.sourceforge.pmd.RulePriority;
    17  import net.sourceforge.pmd.lang.Language;
    18  import net.sourceforge.pmd.lang.LanguageVersion;
    19  import net.sourceforge.pmd.lang.LanguageVersionDiscoverer;
    20  import net.sourceforge.pmd.renderers.CSVRenderer;
    21  import net.sourceforge.pmd.renderers.Renderer;
    22  import net.sourceforge.pmd.util.ClasspathClassLoader;
    23
    24  import org.junit.Test;
    25
    26  public class ConfigurationTest {
    27
    28      @Test
    29      public void testSuppressMarker() {
    30  	PMDConfiguration configuration = new PMDConfiguration();
    31  	assertEquals("Default suppress marker", PMD.SUPPRESS_MARKER, configuration.getSuppressMarker());
    32  	configuration.setSuppressMarker("CUSTOM_MARKER");
    33  	assertEquals("Changed suppress marker", "CUSTOM_MARKER", configuration.getSuppressMarker());
    34      }
    35
    36      @Test
    37      public void testThreads() {
    38  	PMDConfiguration configuration = new PMDConfiguration();
    39  	assertEquals("Default threads", Runtime.getRuntime().availableProcessors(), configuration.getThreads());
    40  	configuration.setThreads(0);
    41  	assertEquals("Changed threads", 0, configuration.getThreads());
    42      }
    43
    44      @Test
    45      public void testClassLoader() throws IOException {
    46  	PMDConfiguration configuration = new PMDConfiguration();
    47  	assertEquals("Default ClassLoader", PMDConfiguration.class.getClassLoader(), configuration.getClassLoader());
    48  	configuration.prependClasspath("some.jar");
    49  	assertEquals("Prepended ClassLoader class", ClasspathClassLoader.class, configuration.getClassLoader()
    50  		.getClass());
    51  	URL[] urls = ((ClasspathClassLoader) configuration.getClassLoader()).getURLs();
    52  	assertEquals("urls length", 1, urls.length);
    53  	assertTrue("url[0]", urls[0].toString().endsWith("/some.jar"));
    54  	assertEquals("parent classLoader", PMDConfiguration.class.getClassLoader(), configuration.getClassLoader()
    55  		.getParent());
    56  	configuration.setClassLoader(null);
    57  	assertEquals("Revert to default ClassLoader", PMDConfiguration.class.getClassLoader(), configuration
    58  		.getClassLoader());
    59      }
    60
    61      @Test
    62      public void testLanguageVersionDiscoverer() {
    63  	PMDConfiguration configuration = new PMDConfiguration();
    64  	LanguageVersionDiscoverer languageVersionDiscoverer = configuration.getLanguageVersionDiscoverer();
    65  	assertEquals("Default Java version", LanguageVersion.JAVA_17, languageVersionDiscoverer
    66  		.getDefaultLanguageVersion(Language.JAVA));
    67  	configuration.setDefaultLanguageVersion(LanguageVersion.JAVA_15);
    68  	assertEquals("Modified Java version", LanguageVersion.JAVA_15, languageVersionDiscoverer
    69  		.getDefaultLanguageVersion(Language.JAVA));
    70      }
    71
    72      @Test
    73      public void testRuleSets() {
    74  	PMDConfiguration configuration = new PMDConfiguration();
    75  	assertEquals("Default RuleSets", null, configuration.getRuleSets());
    76  	configuration.setRuleSets("/rulesets/basic.xml");
    77  	assertEquals("Changed RuleSets", "/rulesets/basic.xml", configuration.getRuleSets());
    78      }
    79
    80      @Test
    81      public void testMinimumPriority() {
    82  	PMDConfiguration configuration = new PMDConfiguration();
    83  	assertEquals("Default minimum priority", RulePriority.LOW, configuration.getMinimumPriority());
    84  	configuration.setMinimumPriority(RulePriority.HIGH);
    85  	assertEquals("Changed minimum priority", RulePriority.HIGH, configuration.getMinimumPriority());
    86      }
    87
    88      @Test
    89      public void testSourceEncoding() {
    90  	PMDConfiguration configuration = new PMDConfiguration();
    91  	assertEquals("Default source encoding", System.getProperty("file.encoding"), configuration.getSourceEncoding());
    92  	configuration.setSourceEncoding("some_other_encoding");
    93  	assertEquals("Changed source encoding", "some_other_encoding", configuration.getSourceEncoding());
    94      }
    95
    96      @Test
    97      public void testInputPaths() {
    98  	PMDConfiguration configuration = new PMDConfiguration();
    99  	assertEquals("Default input paths", null, configuration.getInputPaths());
    100 	configuration.setInputPaths("a,b,c");
    101 	assertEquals("Changed input paths", "a,b,c", configuration.getInputPaths());
    102     }
    103
    104     @Test
    105     public void testReportShortNames() {
    106 	PMDConfiguration configuration = new PMDConfiguration();
    107 	assertEquals("Default report short names", false, configuration.isReportShortNames());
    108 	configuration.setReportShortNames(true);
    109 	assertEquals("Changed report short names", true, configuration.isReportShortNames());
    110     }
    111
    112     @Test
    113     public void testReportFormat() {
    114 	PMDConfiguration configuration = new PMDConfiguration();
    115 	assertEquals("Default report format", null, configuration.getReportFormat());
    116 	configuration.setReportFormat("csv");
    117 	assertEquals("Changed report format", "csv", configuration.getReportFormat());
    118     }
    119
    120     @Test
    121     public void testCreateRenderer() {
    122 	PMDConfiguration configuration = new PMDConfiguration();
    123 	configuration.setReportFormat("csv");
    124 	Renderer renderer = configuration.createRenderer();
    125 	assertEquals("Renderer class", CSVRenderer.class, renderer.getClass());
    126 	assertEquals("Default renderer show suppressed violations", false, renderer.isShowSuppressedViolations());
    127
    128 	configuration.setShowSuppressedViolations(true);
    129 	renderer = configuration.createRenderer();
    130 	assertEquals("Renderer class", CSVRenderer.class, renderer.getClass());
    131 	assertEquals("Changed renderer show suppressed violations", true, renderer.isShowSuppressedViolations());
    132     }
    133
    134     @Test
    135     public void testReportFile() {
    136 	PMDConfiguration configuration = new PMDConfiguration();
    137 	assertEquals("Default report file", null, configuration.getReportFile());
    138 	configuration.setReportFile("somefile");
    139 	assertEquals("Changed report file", "somefile", configuration.getReportFile());
    140     }
    141
    142     @Test
    143     public void testShowSuppressedViolations() {
    144 	PMDConfiguration configuration = new PMDConfiguration();
    145 	assertEquals("Default show suppressed violations", false, configuration.isShowSuppressedViolations());
    146 	configuration.setShowSuppressedViolations(true);
    147 	assertEquals("Changed show suppressed violations", true, configuration.isShowSuppressedViolations());
    148     }
    149
    150     @Test
    151     public void testReportProperties() {
    152 	PMDConfiguration configuration = new PMDConfiguration();
    153 	assertEquals("Default report properties size", 0, configuration.getReportProperties().size());
    154 	configuration.getReportProperties().put("key", "value");
    155 	assertEquals("Changed report properties size", 1, configuration.getReportProperties().size());
    156 	assertEquals("Changed report properties value", "value", configuration.getReportProperties().get("key"));
    157 	configuration.setReportProperties(new Properties());
    158 	assertEquals("Replaced report properties size", 0, configuration.getReportProperties().size());
    159     }
    160
    161     @Test
    162     public void testDebug() {
    163 	PMDConfiguration configuration = new PMDConfiguration();
    164 	assertEquals("Default debug", false, configuration.isDebug());
    165 	configuration.setDebug(true);
    166 	assertEquals("Changed debug", true, configuration.isDebug());
    167     }
    168
    169     @Test
    170     public void testStressTest() {
    171 	PMDConfiguration configuration = new PMDConfiguration();
    172 	assertEquals("Default stress test", false, configuration.isStressTest());
    173 	configuration.setStressTest(true);
    174 	assertEquals("Changed stress test", true, configuration.isStressTest());
    175     }
    176
    177     @Test
    178     public void testBenchmark() {
    179 	PMDConfiguration configuration = new PMDConfiguration();
    180 	assertEquals("Default benchmark", false, configuration.isBenchmark());
    181 	configuration.setBenchmark(true);
    182 	assertEquals("Changed benchmark", true, configuration.isBenchmark());
    183     }
    184
    185     public static junit.framework.Test suite() {
    186 	return new JUnit4TestAdapter(ConfigurationTest.class);
    187     }
    188 }