OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / sdkmanager / app / tests / com / android / sdkmanager / SdkCommandLineTest.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.sdkmanager;
18
19 import com.android.sdklib.ISdkLog;
20
21 import junit.framework.TestCase;
22
23 public class SdkCommandLineTest extends TestCase {
24
25     private MockStdLogger mLog;
26     
27     /**
28      * A mock version of the {@link SdkCommandLine} class that does not
29      * exits and discards its stdout/stderr output.
30      */
31     public static class MockSdkCommandLine extends SdkCommandLine {
32         private boolean mExitCalled;
33         private boolean mHelpCalled;
34         
35         public MockSdkCommandLine(ISdkLog logger) {
36             super(logger);
37         }
38
39         @Override
40         public void printHelpAndExitForAction(String verb, String directObject,
41                 String errorFormat, Object... args) {
42             mHelpCalled = true;
43             super.printHelpAndExitForAction(verb, directObject, errorFormat, args);
44         }
45
46         @Override
47         protected void exit() {
48             mExitCalled = true;
49         }
50         
51         @Override
52         protected void stdout(String format, Object... args) {
53             // discard
54         }
55         
56         @Override
57         protected void stderr(String format, Object... args) {
58             // discard
59         }
60
61         public boolean wasExitCalled() {
62             return mExitCalled;
63         }
64         
65         public boolean wasHelpCalled() {
66             return mHelpCalled;
67         }
68     }
69
70     @Override
71     protected void setUp() throws Exception {
72         mLog = new MockStdLogger();
73         super.setUp();
74     }
75
76     @Override
77     protected void tearDown() throws Exception {
78         super.tearDown();
79     }
80
81     /** Test list */
82     public final void testList_Avd_Verbose() {
83         MockSdkCommandLine c = new MockSdkCommandLine(mLog);
84         c.parseArgs(new String[] { "-v", "list", "avd" });
85         assertFalse(c.wasHelpCalled());
86         assertFalse(c.wasExitCalled());
87         assertEquals("list", c.getVerb());
88         assertEquals("avd", c.getDirectObject());
89         assertTrue(c.isVerbose());
90     }
91
92     public final void testList_Target() {
93         MockSdkCommandLine c = new MockSdkCommandLine(mLog);
94         c.parseArgs(new String[] { "list", "target" });
95         assertFalse(c.wasHelpCalled());
96         assertFalse(c.wasExitCalled());
97         assertEquals("list", c.getVerb());
98         assertEquals("target", c.getDirectObject());
99         assertFalse(c.isVerbose());
100     }
101
102     public final void testList_None() {
103         MockSdkCommandLine c = new MockSdkCommandLine(mLog);
104         c.parseArgs(new String[] { "list" });
105         assertFalse(c.wasHelpCalled());
106         assertFalse(c.wasExitCalled());
107         assertEquals("list", c.getVerb());
108         assertEquals("", c.getDirectObject());
109         assertFalse(c.isVerbose());
110     }
111
112     public final void testList_Invalid() {
113         MockSdkCommandLine c = new MockSdkCommandLine(mLog);
114         c.parseArgs(new String[] { "list", "unknown" });
115         assertTrue(c.wasHelpCalled());
116         assertTrue(c.wasExitCalled());
117         assertEquals(null, c.getVerb());
118         assertEquals(null, c.getDirectObject());
119         assertFalse(c.isVerbose());
120     }
121
122     public final void testList_Plural() {
123         MockSdkCommandLine c = new MockSdkCommandLine(mLog);
124         c.parseArgs(new String[] { "list", "avds" });
125         assertFalse(c.wasHelpCalled());
126         assertFalse(c.wasExitCalled());
127         assertEquals("list", c.getVerb());
128         // we get the non-plural form
129         assertEquals("avd", c.getDirectObject());
130         assertFalse(c.isVerbose());
131
132         c = new MockSdkCommandLine(mLog);
133         c.parseArgs(new String[] { "list", "targets" });
134         assertFalse(c.wasHelpCalled());
135         assertFalse(c.wasExitCalled());
136         assertEquals("list", c.getVerb());
137         // we get the non-plural form
138         assertEquals("target", c.getDirectObject());
139         assertFalse(c.isVerbose());
140     }
141 }