View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.coverage;
5   
6   import static org.junit.Assert.fail;
7   
8   import java.io.BufferedReader;
9   import java.io.File;
10  import java.io.IOException;
11  import java.io.InputStream;
12  import java.io.InputStreamReader;
13  
14  import net.sourceforge.pmd.PMD;
15  
16  import org.junit.Test;
17  
18  
19  
20  public class PMDCoverageTest {
21  
22  	/**
23  	 * Test some of the PMD command line options
24  	 */
25  	@Test
26  	public void testPmdOptions() {
27  		runPmd("-d src/main/java/net/sourceforge/pmd/lang/java/rule/design -f text -R rulesets/internal/all-java.xml -version 1.5 -language java -stress -benchmark");
28  	}
29  
30  	/**
31  	 * Run the PMD command line tool, i.e. call PMD.main().
32  	 * 
33  	 * @param commandLine
34  	 */
35  	private void runPmd(String commandLine) {
36  		String[] args;
37  		args = commandLine.split("\\s");
38  
39  		File f = null;
40  		try {
41  			f = File.createTempFile("pmd", ".txt");
42  			int n = args.length;
43  			String[] a = new String[n + 2];
44  			System.arraycopy(args, 0, a, 0, n);
45  			a[n] = "-reportfile";
46  			a[n + 1] = f.getAbsolutePath();
47  			args = a;
48  
49  			PMD.run(args);
50  
51  			// FIXME: check that output doesn't have parsing errors
52  		} catch (IOException ioe) {
53  			fail("Problem creating temporary file: " + ioe.getLocalizedMessage());
54  		} finally {
55  			if (f != null) f.delete();
56  		}
57  	}
58  
59  	/**
60  	 * Name of the configuration file used by testResourceFileCommands().
61  	 */
62  	private static final String PMD_CONFIG_FILE = "pmd_tests.conf";
63  
64  	/**
65  	 * Run PMD using the command lines found in PMD_CONFIG_FILE.
66  	 */
67  	@Test
68  	public void testResourceFileCommands() {
69  
70  		InputStream is = getClass().getResourceAsStream(PMD_CONFIG_FILE);
71  
72  		if (is != null) {
73  			try {
74  				BufferedReader r = new BufferedReader(new InputStreamReader(is));
75  				String l;
76  				while ((l = r.readLine()) != null) {
77  					l = l.trim();
78  					if (l.length() == 0 || l.charAt(0) == '#') {
79  						continue;
80  					}
81  
82  					runPmd(l);
83  				}
84  				r.close();
85  			} catch (IOException ioe) {
86  				fail("Problem reading config file: " + ioe.getLocalizedMessage());
87  			}
88  		} else {
89  			fail("Missing config file: " + PMD_CONFIG_FILE);
90  		}
91  	}
92  
93  	public static junit.framework.Test suite() {
94  		return new junit.framework.JUnit4TestAdapter(PMDCoverageTest.class);
95  	}
96  }