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