001: /**
002: * <copyright>
003: * Copyright 1997-2002 InfoEther, LLC
004: * under sponsorship of the Defense Advanced Research Projects Agency
005: (DARPA).
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the Cougaar Open Source License as published
009: by
010: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
013: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
014: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
015: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
016: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
017: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
018: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
019: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
020: * PERFORMANCE OF THE COUGAAR SOFTWARE.
021: * </copyright>
022: */package test.net.sourceforge.pmd;
023:
024: import static org.junit.Assert.assertEquals;
025: import static org.junit.Assert.assertSame;
026: import static org.junit.Assert.assertTrue;
027: import net.sourceforge.pmd.CommandLineOptions;
028: import net.sourceforge.pmd.renderers.CSVRenderer;
029: import net.sourceforge.pmd.renderers.EmacsRenderer;
030: import net.sourceforge.pmd.renderers.HTMLRenderer;
031: import net.sourceforge.pmd.renderers.IDEAJRenderer;
032: import net.sourceforge.pmd.renderers.TextRenderer;
033: import net.sourceforge.pmd.renderers.VBHTMLRenderer;
034: import net.sourceforge.pmd.renderers.XMLRenderer;
035:
036: import org.junit.Test;
037:
038: import java.io.InputStreamReader;
039:
040: import junit.framework.JUnit4TestAdapter;
041:
042: public class CommandLineOptionsTest {
043:
044: @Test
045: public void testTargetJDKVersion() {
046: CommandLineOptions opt = new CommandLineOptions(new String[] {
047: "file", "format", "basic" });
048: assertEquals("1.5", opt.getTargetJDK());
049: opt = new CommandLineOptions(new String[] { "file", "format",
050: "ruleset", "-targetjdk", "1.3" });
051: assertEquals("1.3", opt.getTargetJDK());
052: opt = new CommandLineOptions(new String[] { "file", "format",
053: "ruleset", "-targetjdk", "1.5" });
054: assertEquals("1.5", opt.getTargetJDK());
055: opt = new CommandLineOptions(new String[] { "file", "format",
056: "ruleset", "-targetjdk", "1.6" });
057: assertEquals("1.6", opt.getTargetJDK());
058: opt = new CommandLineOptions(new String[] { "-targetjdk",
059: "1.6", "file", "format", "ruleset" });
060: assertEquals("1.6", opt.getTargetJDK());
061: }
062:
063: @Test
064: public void testDebug() {
065: CommandLineOptions opt = new CommandLineOptions(new String[] {
066: "file", "format", "basic", "-debug" });
067: assertTrue(opt.debugEnabled());
068: opt = new CommandLineOptions(new String[] { "-debug", "file",
069: "format", "basic" });
070: assertTrue(opt.debugEnabled());
071: }
072:
073: @Test
074: public void testExcludeMarker() {
075: CommandLineOptions opt = new CommandLineOptions(new String[] {
076: "file", "format", "basic", "-excludemarker", "FOOBAR" });
077: assertEquals("FOOBAR", opt.getExcludeMarker());
078: opt = new CommandLineOptions(new String[] { "-excludemarker",
079: "FOOBAR", "file", "format", "basic" });
080: assertEquals("FOOBAR", opt.getExcludeMarker());
081: }
082:
083: @Test
084: public void testShortNames() {
085: CommandLineOptions opt = new CommandLineOptions(new String[] {
086: "file", "format", "basic", "-shortnames" });
087: assertTrue(opt.shortNamesEnabled());
088: opt = new CommandLineOptions(new String[] { "-shortnames",
089: "file", "format", "basic" });
090: assertTrue(opt.shortNamesEnabled());
091: }
092:
093: @Test
094: public void testEncoding() {
095: CommandLineOptions opt = new CommandLineOptions(new String[] {
096: "file", "format", "basic" });
097: assertTrue(opt.getEncoding().equals(
098: (new InputStreamReader(System.in)).getEncoding()));
099: opt = new CommandLineOptions(new String[] { "file", "format",
100: "ruleset", "-encoding", "UTF-8" });
101: assertTrue(opt.getEncoding().equals("UTF-8"));
102: opt = new CommandLineOptions(new String[] { "-encoding",
103: "UTF-8", "file", "format", "ruleset" });
104: assertTrue(opt.getEncoding().equals("UTF-8"));
105: }
106:
107: @Test
108: public void testInputFileName() {
109: CommandLineOptions opt = new CommandLineOptions(new String[] {
110: "file", "format", "basic" });
111: assertEquals("file", opt.getInputPath());
112: }
113:
114: @Test
115: public void testReportFormat() {
116: CommandLineOptions opt = new CommandLineOptions(new String[] {
117: "file", "format", "basic" });
118: assertEquals("format", opt.getReportFormat());
119: }
120:
121: @Test
122: public void testRulesets() {
123: CommandLineOptions opt = new CommandLineOptions(new String[] {
124: "file", "format", "basic" });
125: assertEquals("rulesets/basic.xml", opt.getRulesets());
126: }
127:
128: @Test
129: public void testCommaSeparatedFiles() {
130: CommandLineOptions opt = new CommandLineOptions(new String[] {
131: "file1,file2,file3", "format", "basic" });
132: assertTrue(opt.containsCommaSeparatedFileList());
133: }
134:
135: @Test(expected=RuntimeException.class)
136: public void testNotEnoughArgs() {
137: new CommandLineOptions(new String[] { "file1", "format" });
138: }
139:
140: @Test(expected=RuntimeException.class)
141: public void testNullArgs() {
142: new CommandLineOptions(null);
143: }
144:
145: @Test
146: public void testReportFile() {
147:
148: CommandLineOptions opt = new CommandLineOptions(new String[] {
149: "file", "format", "basic", "-reportfile", "foo.txt" });
150: assertSame("foo.txt", opt.getReportFile());
151: opt = new CommandLineOptions(new String[] { "-reportfile",
152: "foo.txt", "file", "format", "basic" });
153: assertSame("foo.txt", opt.getReportFile());
154: }
155:
156: @Test
157: public void testCpus() {
158:
159: CommandLineOptions opt = new CommandLineOptions(new String[] {
160: "file", "format", "basic", "-cpus", "2" });
161: assertEquals(2, opt.getCpus());
162: opt = new CommandLineOptions(new String[] { "-cpus", "2",
163: "file", "format", "basic" });
164: assertEquals(2, opt.getCpus());
165: }
166:
167: @Test
168: public void testRenderer() {
169: CommandLineOptions opt = new CommandLineOptions(new String[] {
170: "file", "xml", "basic" });
171: assertTrue(opt.createRenderer() instanceof XMLRenderer);
172: opt = new CommandLineOptions(new String[] { "file", "html",
173: "basic" });
174: assertTrue(opt.createRenderer() instanceof HTMLRenderer);
175: opt = new CommandLineOptions(new String[] { "file", "text",
176: "basic" });
177: assertTrue(opt.createRenderer() instanceof TextRenderer);
178: opt = new CommandLineOptions(new String[] { "file", "emacs",
179: "basic" });
180: assertTrue(opt.createRenderer() instanceof EmacsRenderer);
181: opt = new CommandLineOptions(new String[] { "file", "csv",
182: "basic" });
183: assertTrue(opt.createRenderer() instanceof CSVRenderer);
184: opt = new CommandLineOptions(new String[] { "file", "vbhtml",
185: "basic" });
186: assertTrue(opt.createRenderer() instanceof VBHTMLRenderer);
187: opt = new CommandLineOptions(new String[] { "file", "ideaj",
188: "basic" });
189: assertTrue(opt.createRenderer() instanceof IDEAJRenderer);
190: }
191:
192: @Test(expected=IllegalArgumentException.class)
193: public void illegalArgument1() {
194: CommandLineOptions opt = new CommandLineOptions(new String[] {
195: "file", "", "basic" });
196: opt.createRenderer();
197: }
198:
199: @Test(expected=IllegalArgumentException.class)
200: public void illegalArgument2() {
201: CommandLineOptions opt = new CommandLineOptions(new String[] {
202: "file", "fiddlefaddle", "basic" });
203: opt.createRenderer();
204: }
205:
206: @Test
207: public void testOptionsFirst() {
208: CommandLineOptions opt = new CommandLineOptions(new String[] {
209: "-cpus", "2", "-debug", "file", "format", "basic" });
210: assertEquals(2, opt.getCpus());
211: assertEquals("file", opt.getInputPath());
212: assertEquals("format", opt.getReportFormat());
213: assertEquals("rulesets/basic.xml", opt.getRulesets());
214: assertTrue(opt.debugEnabled());
215: }
216:
217: public static junit.framework.Test suite() {
218: return new JUnit4TestAdapter(CommandLineOptionsTest.class);
219: }
220: }
|