1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.spark.launcher;
19 
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 
24 import org.junit.Before;
25 import org.junit.Test;
26 import static org.junit.Assert.*;
27 import static org.mockito.Mockito.*;
28 
29 import static org.apache.spark.launcher.SparkSubmitOptionParser.*;
30 
31 public class SparkSubmitOptionParserSuite extends BaseSuite {
32 
33   private SparkSubmitOptionParser parser;
34 
35   @Before
setUp()36   public void setUp() {
37     parser = spy(new DummyParser());
38   }
39 
40   @Test
testAllOptions()41   public void testAllOptions() {
42     int count = 0;
43     for (String[] optNames : parser.opts) {
44       for (String optName : optNames) {
45         String value = optName + "-value";
46         parser.parse(Arrays.asList(optName, value));
47         count++;
48         verify(parser).handle(eq(optNames[0]), eq(value));
49         verify(parser, times(count)).handle(anyString(), anyString());
50         verify(parser, times(count)).handleExtraArgs(eq(Collections.<String>emptyList()));
51       }
52     }
53 
54     for (String[] switchNames : parser.switches) {
55       int switchCount = 0;
56       for (String name : switchNames) {
57         parser.parse(Arrays.asList(name));
58         count++;
59         switchCount++;
60         verify(parser, times(switchCount)).handle(eq(switchNames[0]), same((String) null));
61         verify(parser, times(count)).handle(anyString(), any(String.class));
62         verify(parser, times(count)).handleExtraArgs(eq(Collections.<String>emptyList()));
63       }
64     }
65   }
66 
67   @Test
testExtraOptions()68   public void testExtraOptions() {
69     List<String> args = Arrays.asList(parser.MASTER, parser.MASTER, "foo", "bar");
70     parser.parse(args);
71     verify(parser).handle(eq(parser.MASTER), eq(parser.MASTER));
72     verify(parser).handleUnknown(eq("foo"));
73     verify(parser).handleExtraArgs(eq(Arrays.asList("bar")));
74   }
75 
76   @Test(expected=IllegalArgumentException.class)
testMissingArg()77   public void testMissingArg() {
78     parser.parse(Arrays.asList(parser.MASTER));
79   }
80 
81   @Test
testEqualSeparatedOption()82   public void testEqualSeparatedOption() {
83     List<String> args = Arrays.asList(parser.MASTER + "=" + parser.MASTER);
84     parser.parse(args);
85     verify(parser).handle(eq(parser.MASTER), eq(parser.MASTER));
86     verify(parser).handleExtraArgs(eq(Collections.<String>emptyList()));
87   }
88 
89   private static class DummyParser extends SparkSubmitOptionParser {
90 
91     @Override
handle(String opt, String value)92     protected boolean handle(String opt, String value) {
93       return true;
94     }
95 
96     @Override
handleUnknown(String opt)97     protected boolean handleUnknown(String opt) {
98       return false;
99     }
100 
101     @Override
handleExtraArgs(List<String> extra)102     protected void handleExtraArgs(List<String> extra) {
103 
104     }
105 
106   }
107 
108 }
109