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