2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.sdkmanager;
19 import com.android.sdklib.ISdkLog;
21 import junit.framework.TestCase;
24 public class CommandLineProcessorTest extends TestCase {
26 private MockStdLogger mLog;
29 * A mock version of the {@link CommandLineProcessor} class that does not
30 * exits and captures its stdout/stderr output.
32 public static class MockCommandLineProcessor extends CommandLineProcessor {
33 private boolean mExitCalled;
34 private boolean mHelpCalled;
35 private String mStdOut = "";
36 private String mStdErr = "";
38 public MockCommandLineProcessor(ISdkLog logger) {
41 { "verb1", "action1", "Some action" },
42 { "verb1", "action2", "Another action" },
44 define(Mode.STRING, false /*mandatory*/,
45 "verb1", "action1", "1", "first", "non-mandatory flag", null);
46 define(Mode.STRING, true /*mandatory*/,
47 "verb1", "action1", "2", "second", "mandatory flag", null);
51 public void printHelpAndExitForAction(String verb, String directObject,
52 String errorFormat, Object... args) {
54 super.printHelpAndExitForAction(verb, directObject, errorFormat, args);
58 protected void exit() {
63 protected void stdout(String format, Object... args) {
64 String s = String.format(format, args);
66 // don't call super to avoid printing stuff
70 protected void stderr(String format, Object... args) {
71 String s = String.format(format, args);
73 // don't call super to avoid printing stuff
76 public boolean wasHelpCalled() {
80 public boolean wasExitCalled() {
84 public String getStdOut() {
88 public String getStdErr() {
94 protected void setUp() throws Exception {
95 mLog = new MockStdLogger();
100 protected void tearDown() throws Exception {
104 public final void testPrintHelpAndExit() {
105 MockCommandLineProcessor c = new MockCommandLineProcessor(mLog);
106 assertFalse(c.wasExitCalled());
107 assertFalse(c.wasHelpCalled());
108 assertTrue(c.getStdOut().equals(""));
109 assertTrue(c.getStdErr().equals(""));
110 c.printHelpAndExit(null);
111 assertTrue(c.getStdOut().indexOf("-v") != -1);
112 assertTrue(c.getStdOut().indexOf("--verbose") != -1);
113 assertTrue(c.getStdErr().equals(""));
114 assertTrue(c.wasExitCalled());
116 c = new MockCommandLineProcessor(mLog);
117 assertFalse(c.wasExitCalled());
118 assertTrue(c.getStdOut().equals(""));
119 assertTrue(c.getStdErr().indexOf("Missing parameter") == -1);
121 c.printHelpAndExit("Missing %s", "parameter");
122 assertTrue(c.wasExitCalled());
123 assertFalse(c.getStdOut().equals(""));
124 assertTrue(c.getStdErr().indexOf("Missing parameter") != -1);
127 public final void testVerbose() {
128 MockCommandLineProcessor c = new MockCommandLineProcessor(mLog);
130 assertFalse(c.isVerbose());
131 c.parseArgs(new String[] { "-v" });
132 assertTrue(c.isVerbose());
133 assertTrue(c.wasExitCalled());
134 assertTrue(c.wasHelpCalled());
135 assertTrue(c.getStdErr().indexOf("Missing verb name.") != -1);
137 c = new MockCommandLineProcessor(mLog);
138 c.parseArgs(new String[] { "--verbose" });
139 assertTrue(c.isVerbose());
140 assertTrue(c.wasExitCalled());
141 assertTrue(c.wasHelpCalled());
142 assertTrue(c.getStdErr().indexOf("Missing verb name.") != -1);
145 public final void testHelp() {
146 MockCommandLineProcessor c = new MockCommandLineProcessor(mLog);
148 c.parseArgs(new String[] { "-h" });
149 assertTrue(c.wasExitCalled());
150 assertTrue(c.wasHelpCalled());
151 assertTrue(c.getStdErr().indexOf("Missing verb name.") == -1);
153 c = new MockCommandLineProcessor(mLog);
154 c.parseArgs(new String[] { "--help" });
155 assertTrue(c.wasExitCalled());
156 assertTrue(c.wasHelpCalled());
157 assertTrue(c.getStdErr().indexOf("Missing verb name.") == -1);
160 public final void testMandatory() {
161 MockCommandLineProcessor c = new MockCommandLineProcessor(mLog);
163 c.parseArgs(new String[] { "verb1", "action1", "-1", "value1", "-2", "value2" });
164 assertFalse(c.wasExitCalled());
165 assertFalse(c.wasHelpCalled());
166 assertEquals("", c.getStdErr());
167 assertEquals("value1", c.getValue("verb1", "action1", "first"));
168 assertEquals("value2", c.getValue("verb1", "action1", "second"));
170 c = new MockCommandLineProcessor(mLog);
171 c.parseArgs(new String[] { "verb1", "action1", "-2", "value2" });
172 assertFalse(c.wasExitCalled());
173 assertFalse(c.wasHelpCalled());
174 assertEquals("", c.getStdErr());
175 assertEquals(null, c.getValue("verb1", "action1", "first"));
176 assertEquals("value2", c.getValue("verb1", "action1", "second"));
178 c = new MockCommandLineProcessor(mLog);
179 c.parseArgs(new String[] { "verb1", "action1" });
180 assertTrue(c.wasExitCalled());
181 assertTrue(c.wasHelpCalled());
182 assertTrue(c.getStdErr().indexOf("must be defined") != -1);
183 assertEquals(null, c.getValue("verb1", "action1", "first"));
184 assertEquals(null, c.getValue("verb1", "action1", "second"));