1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertSame;
8   import static org.junit.Assert.assertTrue;
9   import net.sourceforge.pmd.CommandLineOptions;
10  import net.sourceforge.pmd.renderers.CSVRenderer;
11  import net.sourceforge.pmd.renderers.EmacsRenderer;
12  import net.sourceforge.pmd.renderers.HTMLRenderer;
13  import net.sourceforge.pmd.renderers.IDEAJRenderer;
14  import net.sourceforge.pmd.renderers.TextRenderer;
15  import net.sourceforge.pmd.renderers.VBHTMLRenderer;
16  import net.sourceforge.pmd.renderers.XMLRenderer;
17  
18  import org.junit.Test;
19  
20  import java.io.InputStreamReader;
21  
22  import junit.framework.JUnit4TestAdapter;
23  
24  public class CommandLineOptionsTest {
25  
26      @Test
27      public void testTargetJDKVersion() {
28          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
29          assertEquals("1.5", opt.getTargetJDK());
30          opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-targetjdk", "1.3"});
31          assertEquals("1.3", opt.getTargetJDK());
32          opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-targetjdk", "1.5"});
33          assertEquals("1.5", opt.getTargetJDK());
34          opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-targetjdk", "1.6"});
35          assertEquals("1.6", opt.getTargetJDK());
36          opt = new CommandLineOptions(new String[]{"-targetjdk", "1.6", "file", "format", "ruleset"});
37          assertEquals("1.6", opt.getTargetJDK());
38      }
39  
40      @Test
41      public void testDebug() {
42          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-debug"});
43          assertTrue(opt.debugEnabled());
44          opt = new CommandLineOptions(new String[]{"-debug", "file", "format", "basic"});
45          assertTrue(opt.debugEnabled());
46      }
47  
48      @Test
49      public void testExcludeMarker() {
50          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-excludemarker", "FOOBAR"});
51          assertEquals("FOOBAR", opt.getExcludeMarker());
52          opt = new CommandLineOptions(new String[]{"-excludemarker", "FOOBAR", "file", "format", "basic"});
53          assertEquals("FOOBAR", opt.getExcludeMarker());
54      }
55  
56      @Test
57      public void testShortNames() {
58          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-shortnames"});
59          assertTrue(opt.shortNamesEnabled());
60          opt = new CommandLineOptions(new String[]{"-shortnames", "file", "format", "basic"});
61          assertTrue(opt.shortNamesEnabled());
62      }
63  
64      @Test
65      public void testEncoding() {
66          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
67          assertTrue(opt.getEncoding().equals((new InputStreamReader(System.in)).getEncoding()));
68          opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-encoding", "UTF-8"});
69          assertTrue(opt.getEncoding().equals("UTF-8"));
70          opt = new CommandLineOptions(new String[]{"-encoding", "UTF-8", "file", "format", "ruleset"});
71          assertTrue(opt.getEncoding().equals("UTF-8"));
72      }
73  
74      @Test
75      public void testInputFileName() {
76          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
77          assertEquals("file", opt.getInputPath());
78      }
79  
80      @Test
81      public void testReportFormat() {
82          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
83          assertEquals("format", opt.getReportFormat());
84      }
85  
86      @Test
87      public void testRulesets() {
88          CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
89          assertEquals("rulesets/basic.xml", opt.getRulesets());
90      }
91  
92      @Test
93      public void testCommaSeparatedFiles() {
94          CommandLineOptions opt = new CommandLineOptions(new String[]{"file1,file2,file3", "format", "basic"});
95          assertTrue(opt.containsCommaSeparatedFileList());
96      }
97  
98      @Test(expected = RuntimeException.class)
99      public void testNotEnoughArgs() {
100         new CommandLineOptions(new String[] { "file1", "format" });
101     }
102 
103     @Test(expected = RuntimeException.class)
104     public void testNullArgs() {
105         new CommandLineOptions(null);
106     }
107     
108     @Test
109     public void testReportFile(){
110     	
111         CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-reportfile", "foo.txt"});
112         assertSame("foo.txt", opt.getReportFile());
113         opt = new CommandLineOptions(new String[]{"-reportfile", "foo.txt", "file", "format", "basic"});
114         assertSame("foo.txt", opt.getReportFile());
115     }
116 
117     @Test
118     public void testCpus() {
119 
120 		CommandLineOptions opt = new CommandLineOptions(new String[] { "file", "format", "basic", "-cpus", "2" });
121 		assertEquals(2, opt.getCpus());
122 		opt = new CommandLineOptions(new String[] { "-cpus", "2", "file", "format", "basic" });
123 		assertEquals(2, opt.getCpus());
124 	}
125 
126     @Test
127     public void testRenderer() {
128         CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "xml", "basic"});
129         assertTrue(opt.createRenderer() instanceof XMLRenderer);
130         opt = new CommandLineOptions(new String[]{"file", "html", "basic"});
131         assertTrue(opt.createRenderer() instanceof HTMLRenderer);
132         opt = new CommandLineOptions(new String[]{"file", "text", "basic"});
133         assertTrue(opt.createRenderer() instanceof TextRenderer);
134         opt = new CommandLineOptions(new String[]{"file", "emacs", "basic"});
135         assertTrue(opt.createRenderer() instanceof EmacsRenderer);
136         opt = new CommandLineOptions(new String[]{"file", "csv", "basic"});
137         assertTrue(opt.createRenderer() instanceof CSVRenderer);
138         opt = new CommandLineOptions(new String[]{"file", "vbhtml", "basic"});
139         assertTrue(opt.createRenderer() instanceof VBHTMLRenderer);
140         opt = new CommandLineOptions(new String[]{"file", "ideaj", "basic"});
141         assertTrue(opt.createRenderer() instanceof IDEAJRenderer);
142     }
143 
144     @Test(expected = IllegalArgumentException.class)
145     public void illegalArgument1() {
146         CommandLineOptions opt = new CommandLineOptions(new String[] { "file", "", "basic" });
147         opt.createRenderer();
148     }
149     
150     @Test(expected = IllegalArgumentException.class)
151     public void illegalArgument2() {
152         CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "fiddlefaddle", "basic"});
153         opt.createRenderer();
154     }
155     
156     @Test
157     public void testOptionsFirst(){
158 		CommandLineOptions opt = new CommandLineOptions(new String[] { "-cpus", "2", "-debug", "file", "format", "basic" });
159 		assertEquals(2, opt.getCpus());
160         assertEquals("file", opt.getInputPath());
161         assertEquals("format", opt.getReportFormat());
162         assertEquals("rulesets/basic.xml", opt.getRulesets());
163         assertTrue(opt.debugEnabled());
164     }
165 
166     @Test
167     public void testAuxilaryClasspath() {
168 		CommandLineOptions opt = new CommandLineOptions(new String[] { "-auxclasspath", "classpath", "file", "format", "basic" });
169 		assertEquals("classpath", opt.getAuxClasspath());
170 	}
171 
172     @Test(expected = IllegalArgumentException.class)
173     public void testAuxilaryClasspathIllegal() {
174 		CommandLineOptions opt = new CommandLineOptions(new String[] { "file", "format", "basic", "-auxclasspath" });
175 	}
176 
177     public static junit.framework.Test suite() {
178         return new JUnit4TestAdapter(CommandLineOptionsTest.class);
179     }
180 }