1   /***
2    * <copyright>
3    *  Copyright 1997-2002 InfoEther, LLC
4    *  under sponsorship of the Defense Advanced Research Projects Agency
5    (DARPA).
6    *
7    *  This program is free software; you can redistribute it and/or modify
8    *  it under the terms of the Cougaar Open Source License as published
9    by
10   *  DARPA on the Cougaar Open Source Website (www.cougaar.org).
11   *
12   *  THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
13   *  PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
14   *  IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
15   *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
16   *  ANY WARRANTIES AS TO NON-INFRINGEMENT.  IN NO EVENT SHALL COPYRIGHT
17   *  HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
18   *  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
19   *  TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20   *  PERFORMANCE OF THE COUGAAR SOFTWARE.
21   * </copyright>
22   */
23  package test.net.sourceforge.pmd;
24  
25  import junit.framework.TestCase;
26  import net.sourceforge.pmd.CommandLineOptions;
27  import net.sourceforge.pmd.renderers.CSVRenderer;
28  import net.sourceforge.pmd.renderers.EmacsRenderer;
29  import net.sourceforge.pmd.renderers.HTMLRenderer;
30  import net.sourceforge.pmd.renderers.IDEAJRenderer;
31  import net.sourceforge.pmd.renderers.TextRenderer;
32  import net.sourceforge.pmd.renderers.VBHTMLRenderer;
33  import net.sourceforge.pmd.renderers.XMLRenderer;
34  
35  import java.io.InputStreamReader;
36  
37  public class CommandLineOptionsTest extends TestCase {
38  
39      public void testTargetJDKVersion() {
40          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
41          assertEquals("1.4", opt.getTargetJDK());
42          opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-targetjdk", "1.3"});
43          assertEquals("1.3", opt.getTargetJDK());
44          opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-targetjdk", "1.5"});
45          assertEquals("1.5", opt.getTargetJDK());
46      }
47  
48      public void testDebug() {
49          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-debug"});
50          assertTrue(opt.debugEnabled());
51      }
52  
53      public void testExcludeMarker() {
54          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-excludemarker", "FOOBAR"});
55          assertEquals("FOOBAR", opt.getExcludeMarker());
56      }
57  
58      public void testShortNames() {
59          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-shortnames"});
60          assertTrue(opt.shortNamesEnabled());
61      }
62  
63      public void testEncoding() {
64          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
65          assertTrue(opt.getEncoding().equals((new InputStreamReader(System.in)).getEncoding()));
66          opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-encoding", "UTF-8"});
67          assertTrue(opt.getEncoding().equals("UTF-8"));
68      }
69  
70      public void testInputFileName() {
71          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
72          assertEquals("file", opt.getInputPath());
73      }
74  
75      public void testReportFormat() {
76          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
77          assertEquals("format", opt.getReportFormat());
78      }
79  
80      public void testRulesets() {
81          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
82          assertEquals("rulesets/basic.xml", opt.getRulesets());
83      }
84  
85      public void testCommaSeparatedFiles() {
86          CommandLineOptions opt = new CommandLineOptions(new String[]{"file1,file2,file3", "format", "basic"});
87          assertTrue(opt.containsCommaSeparatedFileList());
88      }
89  
90      public void testNotEnoughArgs() {
91          try {
92              new CommandLineOptions(new String[]{"file1", "format"});
93              fail("Should have thrown an exception when only array contained < 3 args");
94          } catch (RuntimeException re) {
95              // cool
96          }
97      }
98  
99      public void testNullArgs() {
100         try {
101             new CommandLineOptions(null);
102             fail("Should have thrown an exception when null passed to constructor");
103         } catch (RuntimeException re) {
104             // cool
105         }
106     }
107 
108     public void testRenderer() {
109         CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "xml", "basic"});
110         assertTrue(opt.createRenderer() instanceof XMLRenderer);
111         opt = new CommandLineOptions(new String[]{"file", "html", "basic"});
112         assertTrue(opt.createRenderer() instanceof HTMLRenderer);
113         opt = new CommandLineOptions(new String[]{"file", "text", "basic"});
114         assertTrue(opt.createRenderer() instanceof TextRenderer);
115         opt = new CommandLineOptions(new String[]{"file", "emacs", "basic"});
116         assertTrue(opt.createRenderer() instanceof EmacsRenderer);
117         opt = new CommandLineOptions(new String[]{"file", "csv", "basic"});
118         assertTrue(opt.createRenderer() instanceof CSVRenderer);
119         opt = new CommandLineOptions(new String[]{"file", "vbhtml", "basic"});
120         assertTrue(opt.createRenderer() instanceof VBHTMLRenderer);
121         opt = new CommandLineOptions(new String[]{"file", "ideaj", "basic"});
122         assertTrue(opt.createRenderer() instanceof IDEAJRenderer);
123 
124         try {
125             opt = new CommandLineOptions(new String[]{"file", "fiddlefaddle", "basic"});
126             opt.createRenderer();
127         } catch (IllegalArgumentException iae) {
128             // cool
129         }
130 
131         try {
132             opt = new CommandLineOptions(new String[]{"file", "", "basic"});
133             opt.createRenderer();
134         } catch (IllegalArgumentException iae) {
135             // cool
136         }
137     }
138 }