Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | package com.android.tradefed.config; |
| 17 | |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 18 | import java.util.List; |
| 19 | |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 20 | import junit.framework.TestCase; |
| 21 | |
| 22 | /** |
| 23 | * Unit tests for {@link ArgsOptionParser}. |
| 24 | */ |
| 25 | public class ArgsOptionParserTest extends TestCase { |
| 26 | |
| 27 | /** |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 28 | * An option source with one {@link Option} specified. |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 29 | */ |
| 30 | private static class OneOptionSource { |
| 31 | |
| 32 | private static final String DEFAULT_VALUE = "default"; |
| 33 | |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 34 | @Option(name="my_option", shortName='o') |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 35 | private String mMyOption = DEFAULT_VALUE; |
| 36 | } |
| 37 | |
| 38 | /** |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 39 | * An option source with boolean {@link Option} specified. |
| 40 | */ |
| 41 | private static class BooleanOptionSource { |
| 42 | |
| 43 | private static final boolean DEFAULT_BOOL = false; |
| 44 | private static final String DEFAULT_VALUE = "default"; |
| 45 | |
| 46 | @Option(name="my_boolean", shortName='b') |
| 47 | private boolean mMyBool = DEFAULT_BOOL; |
| 48 | |
| 49 | @Option(name="my_option", shortName='o') |
| 50 | protected String mMyOption = DEFAULT_VALUE; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * An option source with boolean {@link Option} specified with default = true. |
| 55 | */ |
| 56 | private static class BooleanTrueOptionSource { |
| 57 | |
| 58 | private static final boolean DEFAULT_BOOL = true; |
| 59 | |
| 60 | @Option(name="my_boolean", shortName='b') |
| 61 | private boolean mMyBool = DEFAULT_BOOL; |
| 62 | } |
| 63 | |
| 64 | /** |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 65 | * Test passing an empty argument list for an object that has one option specified. |
| 66 | * <p/> |
| 67 | * Expected that the option field should retain its default value. |
| 68 | */ |
| 69 | public void testParse_noArg() throws ConfigurationException { |
| 70 | OneOptionSource object = new OneOptionSource(); |
| 71 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 72 | parser.parse(new String[] {}); |
| 73 | assertEquals(OneOptionSource.DEFAULT_VALUE, object.mMyOption); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Test passing an single argument for an object that has one option specified. |
| 78 | */ |
| 79 | public void testParse_oneArg() throws ConfigurationException { |
| 80 | OneOptionSource object = new OneOptionSource(); |
| 81 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 82 | final String expectedValue = "set"; |
| 83 | parser.parse(new String[] {"--my_option", expectedValue}); |
| 84 | assertEquals(expectedValue, object.mMyOption); |
| 85 | } |
| 86 | |
| 87 | /** |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 88 | * Test passing an single argument for an object that has one option specified, using the |
| 89 | * option=value notation. |
| 90 | */ |
| 91 | public void testParse_oneArgEquals() throws ConfigurationException { |
| 92 | OneOptionSource object = new OneOptionSource(); |
| 93 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 94 | final String expectedValue = "set"; |
| 95 | parser.parse(new String[] {String.format("--my_option=%s", expectedValue)}); |
| 96 | assertEquals(expectedValue, object.mMyOption); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Test passing a single argument for an object that has one option specified, using the |
| 101 | * short option notation. |
| 102 | */ |
| 103 | public void testParse_oneShortArg() throws ConfigurationException { |
| 104 | OneOptionSource object = new OneOptionSource(); |
| 105 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 106 | final String expectedValue = "set"; |
| 107 | parser.parse(new String[] {"-o", expectedValue}); |
| 108 | assertEquals(expectedValue, object.mMyOption); |
| 109 | } |
| 110 | |
| 111 | /** |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 112 | * Test that "--" marks the beginning of positional arguments |
| 113 | */ |
| 114 | public void testParse_posArgs() throws ConfigurationException { |
| 115 | OneOptionSource object = new OneOptionSource(); |
| 116 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 117 | final String expectedValue = "set"; |
| 118 | // have a position argument with a long option prefix, to try to confuse the parser |
| 119 | final String posArg = "--unused"; |
| 120 | List<String> leftOver = parser.parse(new String[] {"-o", expectedValue, "--", posArg}); |
| 121 | assertEquals(expectedValue, object.mMyOption); |
| 122 | assertTrue(leftOver.contains(posArg)); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Test passing a single boolean argument. |
| 127 | */ |
| 128 | public void testParse_boolArg() throws ConfigurationException { |
| 129 | BooleanOptionSource object = new BooleanOptionSource(); |
| 130 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 131 | parser.parse(new String[] {"-b"}); |
| 132 | assertTrue(object.mMyBool); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Test passing a boolean argument with another short argument. |
| 137 | */ |
| 138 | public void testParse_boolTwoArg() throws ConfigurationException { |
| 139 | BooleanOptionSource object = new BooleanOptionSource(); |
| 140 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 141 | final String expectedValue = "set"; |
| 142 | parser.parse(new String[] {"-bo", expectedValue}); |
| 143 | assertTrue(object.mMyBool); |
| 144 | assertEquals(expectedValue, object.mMyOption); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Test passing a boolean argument with another short argument, with value concatenated. |
| 149 | * e.g -bovalue |
| 150 | */ |
| 151 | public void testParse_boolTwoArgValue() throws ConfigurationException { |
| 152 | BooleanOptionSource object = new BooleanOptionSource(); |
| 153 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 154 | final String expectedValue = "set"; |
| 155 | parser.parse(new String[] {String.format("-bo%s", expectedValue)}); |
| 156 | assertTrue(object.mMyBool); |
| 157 | assertEquals(expectedValue, object.mMyOption); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Test the "--no-<bool option>" syntax |
| 162 | */ |
| 163 | public void testParse_boolFalse() throws ConfigurationException { |
| 164 | BooleanTrueOptionSource object = new BooleanTrueOptionSource(); |
| 165 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 166 | parser.parse(new String[] {"--no-my_boolean"}); |
| 167 | assertFalse(object.mMyBool); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Test the boolean long option syntax |
| 172 | */ |
| 173 | public void testParse_boolLong() throws ConfigurationException { |
| 174 | BooleanOptionSource object = new BooleanOptionSource(); |
| 175 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 176 | parser.parse(new String[] {"--my_boolean"}); |
| 177 | assertTrue(object.mMyBool); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Test passing arg string where value is missing |
| 182 | */ |
| 183 | public void testParse_missingValue() throws ConfigurationException { |
| 184 | OneOptionSource object = new OneOptionSource(); |
| 185 | ArgsOptionParser parser = new ArgsOptionParser(object); |
| 186 | try { |
| 187 | parser.parse(new String[] {"--my_option"}); |
| 188 | fail("ConfigurationException not thrown"); |
| 189 | } catch (ConfigurationException e) { |
| 190 | // expected |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 195 | * Test parsing args for an option that does not exist. |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 196 | */ |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 197 | public void testParse_optionNotPresent() throws ConfigurationException { |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 198 | OneOptionSource object = new OneOptionSource(); |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 199 | ArgsOptionParser parser = new ArgsOptionParser(object); |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 200 | try { |
| 201 | parser.parse(new String[] {"--my_option", "set", "--not_here", "value"}); |
| 202 | fail("ConfigurationException not thrown"); |
| 203 | } catch (ConfigurationException e) { |
| 204 | // expected |
| 205 | } |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 206 | } |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 207 | } |