Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.launcher;
0019 
0020 import java.util.Arrays;
0021 import java.util.Collections;
0022 import java.util.List;
0023 
0024 import org.junit.Before;
0025 import org.junit.Test;
0026 import static org.mockito.ArgumentMatchers.isNull;
0027 import static org.mockito.Mockito.*;
0028 
0029 public class SparkSubmitOptionParserSuite extends BaseSuite {
0030 
0031   private SparkSubmitOptionParser parser;
0032 
0033   @Before
0034   public void setUp() {
0035     parser = spy(new DummyParser());
0036   }
0037 
0038   @Test
0039   public void testAllOptions() {
0040     int count = 0;
0041     for (String[] optNames : parser.opts) {
0042       for (String optName : optNames) {
0043         String value = optName + "-value";
0044         parser.parse(Arrays.asList(optName, value));
0045         count++;
0046         verify(parser).handle(eq(optNames[0]), eq(value));
0047         verify(parser, times(count)).handle(anyString(), anyString());
0048         verify(parser, times(count)).handleExtraArgs(eq(Collections.emptyList()));
0049       }
0050     }
0051 
0052     int nullCount = 0;
0053     for (String[] switchNames : parser.switches) {
0054       int switchCount = 0;
0055       for (String name : switchNames) {
0056         parser.parse(Arrays.asList(name));
0057         count++;
0058         nullCount++;
0059         switchCount++;
0060         verify(parser, times(switchCount)).handle(eq(switchNames[0]), same(null));
0061         verify(parser, times(nullCount)).handle(anyString(), isNull());
0062         verify(parser, times(count - nullCount)).handle(anyString(), any(String.class));
0063         verify(parser, times(count)).handleExtraArgs(eq(Collections.emptyList()));
0064       }
0065     }
0066   }
0067 
0068   @Test
0069   public void testExtraOptions() {
0070     List<String> args = Arrays.asList(parser.MASTER, parser.MASTER, "foo", "bar");
0071     parser.parse(args);
0072     verify(parser).handle(eq(parser.MASTER), eq(parser.MASTER));
0073     verify(parser).handleUnknown(eq("foo"));
0074     verify(parser).handleExtraArgs(eq(Arrays.asList("bar")));
0075   }
0076 
0077   @Test(expected=IllegalArgumentException.class)
0078   public void testMissingArg() {
0079     parser.parse(Arrays.asList(parser.MASTER));
0080   }
0081 
0082   @Test
0083   public void testEqualSeparatedOption() {
0084     List<String> args = Arrays.asList(parser.MASTER + "=" + parser.MASTER);
0085     parser.parse(args);
0086     verify(parser).handle(eq(parser.MASTER), eq(parser.MASTER));
0087     verify(parser).handleExtraArgs(eq(Collections.emptyList()));
0088   }
0089 
0090   private static class DummyParser extends SparkSubmitOptionParser {
0091 
0092     @Override
0093     protected boolean handle(String opt, String value) {
0094       return true;
0095     }
0096 
0097     @Override
0098     protected boolean handleUnknown(String opt) {
0099       return false;
0100     }
0101 
0102     @Override
0103     protected void handleExtraArgs(List<String> extra) {
0104 
0105     }
0106 
0107   }
0108 
0109 }