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 opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-targetjdk", "1.6"});
47 assertEquals("1.6", opt.getTargetJDK());
48 opt = new CommandLineOptions(new String[]{"-targetjdk", "1.6", "file", "format", "ruleset"});
49 assertEquals("1.6", opt.getTargetJDK());
50 }
51
52 public void testDebug() {
53 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-debug"});
54 assertTrue(opt.debugEnabled());
55 opt = new CommandLineOptions(new String[]{"-debug", "file", "format", "basic"});
56 assertTrue(opt.debugEnabled());
57 }
58
59 public void testExcludeMarker() {
60 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-excludemarker", "FOOBAR"});
61 assertEquals("FOOBAR", opt.getExcludeMarker());
62 opt = new CommandLineOptions(new String[]{"-excludemarker", "FOOBAR", "file", "format", "basic"});
63 assertEquals("FOOBAR", opt.getExcludeMarker());
64 }
65
66 public void testShortNames() {
67 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-shortnames"});
68 assertTrue(opt.shortNamesEnabled());
69 opt = new CommandLineOptions(new String[]{"-shortnames", "file", "format", "basic"});
70 assertTrue(opt.shortNamesEnabled());
71 }
72
73 public void testEncoding() {
74 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
75 assertTrue(opt.getEncoding().equals((new InputStreamReader(System.in)).getEncoding()));
76 opt = new CommandLineOptions(new String[]{"file", "format", "ruleset", "-encoding", "UTF-8"});
77 assertTrue(opt.getEncoding().equals("UTF-8"));
78 opt = new CommandLineOptions(new String[]{"-encoding", "UTF-8", "file", "format", "ruleset"});
79 assertTrue(opt.getEncoding().equals("UTF-8"));
80 }
81
82 public void testInputFileName() {
83 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
84 assertEquals("file", opt.getInputPath());
85 }
86
87 public void testReportFormat() {
88 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
89 assertEquals("format", opt.getReportFormat());
90 }
91
92 public void testRulesets() {
93 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic"});
94 assertEquals("rulesets/basic.xml", opt.getRulesets());
95 }
96
97 public void testCommaSeparatedFiles() {
98 CommandLineOptions opt = new CommandLineOptions(new String[]{"file1,file2,file3", "format", "basic"});
99 assertTrue(opt.containsCommaSeparatedFileList());
100 }
101
102 public void testNotEnoughArgs() {
103 try {
104 new CommandLineOptions(new String[]{"file1", "format"});
105 fail("Should have thrown an exception when only array contained < 3 args");
106 } catch (RuntimeException re) {
107
108 }
109 }
110
111 public void testNullArgs() {
112 try {
113 new CommandLineOptions(null);
114 fail("Should have thrown an exception when null passed to constructor");
115 } catch (RuntimeException re) {
116
117 }
118 }
119
120 public void testReportFile(){
121
122 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "format", "basic", "-reportfile", "foo.txt"});
123 assertSame("foo.txt", opt.getReportFile());
124 opt = new CommandLineOptions(new String[]{"-reportfile", "foo.txt", "file", "format", "basic"});
125 assertSame("foo.txt", opt.getReportFile());
126 }
127
128 public void testCpus() {
129
130 CommandLineOptions opt = new CommandLineOptions(new String[] { "file", "format", "basic", "-cpus", "2" });
131 assertEquals(2, opt.getCpus());
132 opt = new CommandLineOptions(new String[] { "-cpus", "2", "file", "format", "basic" });
133 assertEquals(2, opt.getCpus());
134 }
135
136 public void testRenderer() {
137 CommandLineOptions opt = new CommandLineOptions(new String[]{"file", "xml", "basic"});
138 assertTrue(opt.createRenderer() instanceof XMLRenderer);
139 opt = new CommandLineOptions(new String[]{"file", "html", "basic"});
140 assertTrue(opt.createRenderer() instanceof HTMLRenderer);
141 opt = new CommandLineOptions(new String[]{"file", "text", "basic"});
142 assertTrue(opt.createRenderer() instanceof TextRenderer);
143 opt = new CommandLineOptions(new String[]{"file", "emacs", "basic"});
144 assertTrue(opt.createRenderer() instanceof EmacsRenderer);
145 opt = new CommandLineOptions(new String[]{"file", "csv", "basic"});
146 assertTrue(opt.createRenderer() instanceof CSVRenderer);
147 opt = new CommandLineOptions(new String[]{"file", "vbhtml", "basic"});
148 assertTrue(opt.createRenderer() instanceof VBHTMLRenderer);
149 opt = new CommandLineOptions(new String[]{"file", "ideaj", "basic"});
150 assertTrue(opt.createRenderer() instanceof IDEAJRenderer);
151
152 try {
153 opt = new CommandLineOptions(new String[]{"file", "fiddlefaddle", "basic"});
154 opt.createRenderer();
155 } catch (IllegalArgumentException iae) {
156
157 }
158
159 try {
160 opt = new CommandLineOptions(new String[]{"file", "", "basic"});
161 opt.createRenderer();
162 } catch (IllegalArgumentException iae) {
163
164 }
165 }
166
167 public void testOptionsFirst(){
168 CommandLineOptions opt = new CommandLineOptions(new String[] { "-cpus", "2", "-debug", "file", "format", "basic" });
169 assertEquals(2, opt.getCpus());
170 assertEquals("file", opt.getInputPath());
171 assertEquals("format", opt.getReportFormat());
172 assertEquals("rulesets/basic.xml", opt.getRulesets());
173 assertTrue(opt.debugEnabled());
174 }
175 }