View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.cli;
    5
    6   import static org.junit.Assert.assertTrue;
    7   import static org.junit.Assert.fail;
    8
    9   import java.io.File;
    10  import java.io.FileNotFoundException;
    11  import java.io.FileOutputStream;
    12  import java.io.PrintStream;
    13
    14  import net.sourceforge.pmd.PMD;
    15  import net.sourceforge.pmd.util.FileUtil;
    16
    17  import org.junit.BeforeClass;
    18  import org.junit.Test;
    19
    20
    21  /**
    22   * @author Romain Pelisse <belaran@gmail.com>
    23   *
    24   */
    25  public class CLITest {
    26
    27  	private static final String TEST_OUPUT_DIRECTORY = "target/cli-tests/";
    28
    29  	// Points toward a folder without any source files, to avoid actually PMD and slowing down tests
    30  	private static final String SOURCE_FOLDER = "src/main/resources";
    31
    32  	/**
    33  	 * @throws java.lang.Exception
    34  	 */
    35  	@BeforeClass
    36  	public static void setUp() throws Exception {
    37  		System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
    38  		File testOuputDir = new File(TEST_OUPUT_DIRECTORY);
    39  		testOuputDir.mkdirs();
    40  		//assertTrue("failed to create output directory for test:" + testOuputDir.getAbsolutePath(),);
    41  	}
    42
    43  	private void createTestOutputFile(String filename) {
    44  		try {
    45  			PrintStream out = new PrintStream(new FileOutputStream(filename));
    46  			System.setOut(out);
    47  			System.setErr(out);
    48  		} catch (FileNotFoundException e) {
    49  			fail("Can't create file " + filename + " for test.");
    50  		}
    51  	}
    52
    53  	@Test
    54  	public void minimalArgs() {
    55  		String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design"};
    56  		runTest(args,"minimalArgs");
    57  	}
    58
    59  	@Test
    60  	public void usingDebug() {
    61  		String[] args = { "-d", SOURCE_FOLDER, "-f" ,"text","-R", "java-basic,java-design","-debug"};
    62  		runTest(args,"minimalArgsWithDebug");
    63  	}
    64
    65
    66  	@Test
    67  	public void changeJavaVersion() {
    68  		String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "java-basic,java-design", "-version","1.5", "-language", "java", "-debug"};
    69  		String resultFilename = runTest(args, "chgJavaVersion");
    70  		assertTrue("Invalid Java version",FileUtil.findPatternInFile(new File(resultFilename), "Using Java version: Java 1.5"));
    71  	}
    72
    73  	@Test
    74  	public void useEcmaScript() {
    75  		String[] args = { "-d", SOURCE_FOLDER, "-f", "xml", "-R", "ecmascript-basic", "-version","3","-l", "ecmascript", "-debug"};
    76  		String resultFilename = runTest(args,"useEcmaScript");
    77  		assertTrue("Invalid Java version",FileUtil.findPatternInFile(new File(resultFilename), "Using Ecmascript version: Ecmascript 3"));
    78  	}
    79
    80  	private String runTest(String[] args, String testname) {
    81  		String filename = TEST_OUPUT_DIRECTORY + testname + ".txt";
    82  		long start = System.currentTimeMillis();
    83  		createTestOutputFile(filename);
    84  		System.out.println("Start running test " + testname);
    85  		runPMDWith(args);
    86  		checkStatusCode();
    87  		System.out.println("Test finished successfully after " + (System.currentTimeMillis() - start) + "ms.");
    88  		return filename;
    89  	}
    90
    91  	private void runPMDWith(String[] args) {
    92  		try {
    93  			PMD.main(args);
    94  		} catch (Exception e) {
    95  			e.printStackTrace();
    96  			fail("Exception occurs while running PMD CLI with following args:" + args);
    97  		}
    98  	}
    99
    100 	private void checkStatusCode() {
    101 		int statusCode = Integer.valueOf(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY));
    102 		if ( statusCode > 0 )
    103 			fail("PMD failed with status code:" + statusCode);
    104 	}
    105 }