auto import from //depot/cupcake/@135843
This commit is contained in:
@@ -1,186 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.sdkmanager;
|
||||
|
||||
import com.android.sdklib.ISdkLog;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
||||
public class CommandLineProcessorTest extends TestCase {
|
||||
|
||||
private MockStdLogger mLog;
|
||||
|
||||
/**
|
||||
* A mock version of the {@link CommandLineProcessor} class that does not
|
||||
* exits and captures its stdout/stderr output.
|
||||
*/
|
||||
public static class MockCommandLineProcessor extends CommandLineProcessor {
|
||||
private boolean mExitCalled;
|
||||
private boolean mHelpCalled;
|
||||
private String mStdOut = "";
|
||||
private String mStdErr = "";
|
||||
|
||||
public MockCommandLineProcessor(ISdkLog logger) {
|
||||
super(logger,
|
||||
new String[][] {
|
||||
{ "verb1", "action1", "Some action" },
|
||||
{ "verb1", "action2", "Another action" },
|
||||
});
|
||||
define(MODE.STRING, false /*mandatory*/,
|
||||
"verb1", "action1", "1", "first", "non-mandatory flag", null);
|
||||
define(MODE.STRING, true /*mandatory*/,
|
||||
"verb1", "action1", "2", "second", "mandatory flag", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printHelpAndExitForAction(String verb, String directObject,
|
||||
String errorFormat, Object... args) {
|
||||
mHelpCalled = true;
|
||||
super.printHelpAndExitForAction(verb, directObject, errorFormat, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void exit() {
|
||||
mExitCalled = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void stdout(String format, Object... args) {
|
||||
String s = String.format(format, args);
|
||||
mStdOut += s + "\n";
|
||||
// don't call super to avoid printing stuff
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void stderr(String format, Object... args) {
|
||||
String s = String.format(format, args);
|
||||
mStdErr += s + "\n";
|
||||
// don't call super to avoid printing stuff
|
||||
}
|
||||
|
||||
public boolean wasHelpCalled() {
|
||||
return mHelpCalled;
|
||||
}
|
||||
|
||||
public boolean wasExitCalled() {
|
||||
return mExitCalled;
|
||||
}
|
||||
|
||||
public String getStdOut() {
|
||||
return mStdOut;
|
||||
}
|
||||
|
||||
public String getStdErr() {
|
||||
return mStdErr;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
mLog = new MockStdLogger();
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public final void testPrintHelpAndExit() {
|
||||
MockCommandLineProcessor c = new MockCommandLineProcessor(mLog);
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertFalse(c.wasHelpCalled());
|
||||
assertTrue(c.getStdOut().equals(""));
|
||||
assertTrue(c.getStdErr().equals(""));
|
||||
c.printHelpAndExit(null);
|
||||
assertTrue(c.getStdOut().indexOf("-v") != -1);
|
||||
assertTrue(c.getStdOut().indexOf("--verbose") != -1);
|
||||
assertTrue(c.getStdErr().equals(""));
|
||||
assertTrue(c.wasExitCalled());
|
||||
|
||||
c = new MockCommandLineProcessor(mLog);
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertTrue(c.getStdOut().equals(""));
|
||||
assertTrue(c.getStdErr().indexOf("Missing parameter") == -1);
|
||||
|
||||
c.printHelpAndExit("Missing %s", "parameter");
|
||||
assertTrue(c.wasExitCalled());
|
||||
assertFalse(c.getStdOut().equals(""));
|
||||
assertTrue(c.getStdErr().indexOf("Missing parameter") != -1);
|
||||
}
|
||||
|
||||
public final void testVerbose() {
|
||||
MockCommandLineProcessor c = new MockCommandLineProcessor(mLog);
|
||||
|
||||
assertFalse(c.isVerbose());
|
||||
c.parseArgs(new String[] { "-v" });
|
||||
assertTrue(c.isVerbose());
|
||||
assertTrue(c.wasExitCalled());
|
||||
assertTrue(c.wasHelpCalled());
|
||||
assertTrue(c.getStdErr().indexOf("Missing verb name.") != -1);
|
||||
|
||||
c = new MockCommandLineProcessor(mLog);
|
||||
c.parseArgs(new String[] { "--verbose" });
|
||||
assertTrue(c.isVerbose());
|
||||
assertTrue(c.wasExitCalled());
|
||||
assertTrue(c.wasHelpCalled());
|
||||
assertTrue(c.getStdErr().indexOf("Missing verb name.") != -1);
|
||||
}
|
||||
|
||||
public final void testHelp() {
|
||||
MockCommandLineProcessor c = new MockCommandLineProcessor(mLog);
|
||||
|
||||
c.parseArgs(new String[] { "-h" });
|
||||
assertTrue(c.wasExitCalled());
|
||||
assertTrue(c.wasHelpCalled());
|
||||
assertTrue(c.getStdErr().indexOf("Missing verb name.") == -1);
|
||||
|
||||
c = new MockCommandLineProcessor(mLog);
|
||||
c.parseArgs(new String[] { "--help" });
|
||||
assertTrue(c.wasExitCalled());
|
||||
assertTrue(c.wasHelpCalled());
|
||||
assertTrue(c.getStdErr().indexOf("Missing verb name.") == -1);
|
||||
}
|
||||
|
||||
public final void testMandatory() {
|
||||
MockCommandLineProcessor c = new MockCommandLineProcessor(mLog);
|
||||
|
||||
c.parseArgs(new String[] { "verb1", "action1", "-1", "value1", "-2", "value2" });
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertFalse(c.wasHelpCalled());
|
||||
assertEquals("", c.getStdErr());
|
||||
assertEquals("value1", c.getValue("verb1", "action1", "first"));
|
||||
assertEquals("value2", c.getValue("verb1", "action1", "second"));
|
||||
|
||||
c = new MockCommandLineProcessor(mLog);
|
||||
c.parseArgs(new String[] { "verb1", "action1", "-2", "value2" });
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertFalse(c.wasHelpCalled());
|
||||
assertEquals("", c.getStdErr());
|
||||
assertEquals(null, c.getValue("verb1", "action1", "first"));
|
||||
assertEquals("value2", c.getValue("verb1", "action1", "second"));
|
||||
|
||||
c = new MockCommandLineProcessor(mLog);
|
||||
c.parseArgs(new String[] { "verb1", "action1" });
|
||||
assertTrue(c.wasExitCalled());
|
||||
assertTrue(c.wasHelpCalled());
|
||||
assertTrue(c.getStdErr().indexOf("must be defined") != -1);
|
||||
assertEquals(null, c.getValue("verb1", "action1", "first"));
|
||||
assertEquals(null, c.getValue("verb1", "action1", "second"));
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Eclipse Public License, Version 1.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.eclipse.org/org/documents/epl-v10.php
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.sdkmanager;
|
||||
|
||||
import com.android.sdklib.ISdkLog;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class MockStdLogger implements ISdkLog {
|
||||
|
||||
public void error(Throwable t, String errorFormat, Object... args) {
|
||||
if (errorFormat != null) {
|
||||
System.err.printf("Error: " + errorFormat, args);
|
||||
if (!errorFormat.endsWith("\n")) {
|
||||
System.err.printf("\n");
|
||||
}
|
||||
}
|
||||
if (t != null) {
|
||||
System.err.printf("Error: %s\n", t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void warning(String warningFormat, Object... args) {
|
||||
System.out.printf("Warning: " + warningFormat, args);
|
||||
if (!warningFormat.endsWith("\n")) {
|
||||
System.out.printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
public void printf(String msgFormat, Object... args) {
|
||||
System.out.printf(msgFormat, args);
|
||||
}
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.sdkmanager;
|
||||
|
||||
import com.android.sdklib.ISdkLog;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SdkCommandLineTest extends TestCase {
|
||||
|
||||
private MockStdLogger mLog;
|
||||
|
||||
/**
|
||||
* A mock version of the {@link SdkCommandLine} class that does not
|
||||
* exits and discards its stdout/stderr output.
|
||||
*/
|
||||
public static class MockSdkCommandLine extends SdkCommandLine {
|
||||
private boolean mExitCalled;
|
||||
private boolean mHelpCalled;
|
||||
|
||||
public MockSdkCommandLine(ISdkLog logger) {
|
||||
super(logger);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printHelpAndExitForAction(String verb, String directObject,
|
||||
String errorFormat, Object... args) {
|
||||
mHelpCalled = true;
|
||||
super.printHelpAndExitForAction(verb, directObject, errorFormat, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void exit() {
|
||||
mExitCalled = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void stdout(String format, Object... args) {
|
||||
// discard
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void stderr(String format, Object... args) {
|
||||
// discard
|
||||
}
|
||||
|
||||
public boolean wasExitCalled() {
|
||||
return mExitCalled;
|
||||
}
|
||||
|
||||
public boolean wasHelpCalled() {
|
||||
return mHelpCalled;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
mLog = new MockStdLogger();
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
/** Test list */
|
||||
public final void testList_Avd_Verbose() {
|
||||
MockSdkCommandLine c = new MockSdkCommandLine(mLog);
|
||||
c.parseArgs(new String[] { "-v", "list", "avd" });
|
||||
assertFalse(c.wasHelpCalled());
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertEquals("list", c.getVerb());
|
||||
assertEquals("avd", c.getDirectObject());
|
||||
assertTrue(c.isVerbose());
|
||||
}
|
||||
|
||||
public final void testList_Target() {
|
||||
MockSdkCommandLine c = new MockSdkCommandLine(mLog);
|
||||
c.parseArgs(new String[] { "list", "target" });
|
||||
assertFalse(c.wasHelpCalled());
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertEquals("list", c.getVerb());
|
||||
assertEquals("target", c.getDirectObject());
|
||||
assertFalse(c.isVerbose());
|
||||
}
|
||||
|
||||
public final void testList_None() {
|
||||
MockSdkCommandLine c = new MockSdkCommandLine(mLog);
|
||||
c.parseArgs(new String[] { "list" });
|
||||
assertFalse(c.wasHelpCalled());
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertEquals("list", c.getVerb());
|
||||
assertEquals("", c.getDirectObject());
|
||||
assertFalse(c.isVerbose());
|
||||
}
|
||||
|
||||
public final void testList_Invalid() {
|
||||
MockSdkCommandLine c = new MockSdkCommandLine(mLog);
|
||||
c.parseArgs(new String[] { "list", "unknown" });
|
||||
assertTrue(c.wasHelpCalled());
|
||||
assertTrue(c.wasExitCalled());
|
||||
assertEquals(null, c.getVerb());
|
||||
assertEquals(null, c.getDirectObject());
|
||||
assertFalse(c.isVerbose());
|
||||
}
|
||||
|
||||
public final void testList_Plural() {
|
||||
MockSdkCommandLine c = new MockSdkCommandLine(mLog);
|
||||
c.parseArgs(new String[] { "list", "avds" });
|
||||
assertFalse(c.wasHelpCalled());
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertEquals("list", c.getVerb());
|
||||
// we get the non-plural form
|
||||
assertEquals("avd", c.getDirectObject());
|
||||
assertFalse(c.isVerbose());
|
||||
|
||||
c = new MockSdkCommandLine(mLog);
|
||||
c.parseArgs(new String[] { "list", "targets" });
|
||||
assertFalse(c.wasHelpCalled());
|
||||
assertFalse(c.wasExitCalled());
|
||||
assertEquals("list", c.getVerb());
|
||||
// we get the non-plural form
|
||||
assertEquals("target", c.getDirectObject());
|
||||
assertFalse(c.isVerbose());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user