1 /* 2 * Copyright (C) 2020 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.build.config; 18 19 import org.junit.Assert; 20 import org.junit.Test; 21 22 import java.util.HashMap; 23 24 public class OptionsTest { 25 parse(Errors errors, String[] args)26 private Options parse(Errors errors, String[] args) { 27 final HashMap<String, String> env = new HashMap(); 28 env.put("TARGET_PRODUCT", "test_product"); 29 env.put("TARGET_BUILD_VARIANT", "user"); 30 final Options.Parser parser = new Options.Parser(errors, args, env); 31 parser.setSkipRequiredArgValidation(); 32 return parser.parse(); 33 } 34 35 @Test testErrorMissingLast()36 public void testErrorMissingLast() { 37 final Errors errors = new Errors(); 38 39 final Options options = parse(errors, new String[] { 40 "--error" 41 }); 42 43 Assert.assertNotEquals("", TestErrors.getErrorMessages(errors)); 44 Assert.assertEquals(Options.Action.DEFAULT, options.getAction()); 45 TestErrors.assertHasEntry(errors.ERROR_COMMAND_LINE, errors); 46 } 47 48 @Test testErrorMissingNotLast()49 public void testErrorMissingNotLast() { 50 final Errors errors = new Errors(); 51 52 final Options options = parse(errors, new String[] { 53 "--error", "--warning", "2" 54 }); 55 56 Assert.assertNotEquals("", TestErrors.getErrorMessages(errors)); 57 Assert.assertEquals(Options.Action.DEFAULT, options.getAction()); 58 TestErrors.assertHasEntry(errors.ERROR_COMMAND_LINE, errors); 59 } 60 61 @Test testErrorNotNumeric()62 public void testErrorNotNumeric() { 63 final Errors errors = new Errors(); 64 65 final Options options = parse(errors, new String[] { 66 "--error", "notgood" 67 }); 68 69 Assert.assertNotEquals("", TestErrors.getErrorMessages(errors)); 70 Assert.assertEquals(Options.Action.DEFAULT, options.getAction()); 71 TestErrors.assertHasEntry(errors.ERROR_COMMAND_LINE, errors); 72 } 73 74 @Test testErrorInvalidError()75 public void testErrorInvalidError() { 76 final Errors errors = new Errors(); 77 78 final Options options = parse(errors, new String[] { 79 "--error", "50000" 80 }); 81 82 Assert.assertEquals("", TestErrors.getErrorMessages(errors)); 83 Assert.assertEquals(Options.Action.DEFAULT, options.getAction()); 84 TestErrors.assertHasEntry(errors.WARNING_UNKNOWN_COMMAND_LINE_ERROR, errors); 85 } 86 87 @Test testErrorOne()88 public void testErrorOne() { 89 final Errors errors = new Errors(); 90 91 final Options options = parse(errors, new String[] { 92 "--error", "2" 93 }); 94 95 Assert.assertEquals("", TestErrors.getErrorMessages(errors)); 96 Assert.assertEquals(Options.Action.DEFAULT, options.getAction()); 97 Assert.assertFalse(errors.hadWarningOrError()); 98 } 99 100 @Test testWarningOne()101 public void testWarningOne() { 102 final Errors errors = new Errors(); 103 104 final Options options = parse(errors, new String[] { 105 "--warning", "2" 106 }); 107 108 Assert.assertEquals("", TestErrors.getErrorMessages(errors)); 109 Assert.assertEquals(Options.Action.DEFAULT, options.getAction()); 110 Assert.assertFalse(errors.hadWarningOrError()); 111 } 112 113 @Test testHideOne()114 public void testHideOne() { 115 final Errors errors = new Errors(); 116 117 final Options options = parse(errors, new String[] { 118 "--hide", "2" 119 }); 120 121 Assert.assertEquals("", TestErrors.getErrorMessages(errors)); 122 Assert.assertEquals(Options.Action.DEFAULT, options.getAction()); 123 Assert.assertFalse(errors.hadWarningOrError()); 124 } 125 126 @Test testEnv()127 public void testEnv() { 128 final Errors errors = new Errors(); 129 130 final Options options = parse(errors, new String[0]); 131 132 Assert.assertEquals("test_product", options.getProduct()); 133 Assert.assertEquals("user", options.getVariant()); 134 Assert.assertFalse(errors.hadWarningOrError()); 135 } 136 } 137 138