aboutsummaryrefslogtreecommitdiff
path: root/launcher/src/test/java/org/apache/spark/launcher/SparkSubmitOptionParserSuite.java
blob: 3ee5b8cf9689d7e6caaefa59523bfba66d5afc3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.spark.launcher;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import static org.apache.spark.launcher.SparkSubmitOptionParser.*;

public class SparkSubmitOptionParserSuite extends BaseSuite {

  private SparkSubmitOptionParser parser;

  @Before
  public void setUp() {
    parser = spy(new DummyParser());
  }

  @Test
  public void testAllOptions() {
    int count = 0;
    for (String[] optNames : parser.opts) {
      for (String optName : optNames) {
        String value = optName + "-value";
        parser.parse(Arrays.asList(optName, value));
        count++;
        verify(parser).handle(eq(optNames[0]), eq(value));
        verify(parser, times(count)).handle(anyString(), anyString());
        verify(parser, times(count)).handleExtraArgs(eq(Collections.<String>emptyList()));
      }
    }

    for (String[] switchNames : parser.switches) {
      int switchCount = 0;
      for (String name : switchNames) {
        parser.parse(Arrays.asList(name));
        count++;
        switchCount++;
        verify(parser, times(switchCount)).handle(eq(switchNames[0]), same((String) null));
        verify(parser, times(count)).handle(anyString(), any(String.class));
        verify(parser, times(count)).handleExtraArgs(eq(Collections.<String>emptyList()));
      }
    }
  }

  @Test
  public void testExtraOptions() {
    List<String> args = Arrays.asList(parser.MASTER, parser.MASTER, "foo", "bar");
    parser.parse(args);
    verify(parser).handle(eq(parser.MASTER), eq(parser.MASTER));
    verify(parser).handleUnknown(eq("foo"));
    verify(parser).handleExtraArgs(eq(Arrays.asList("bar")));
  }

  @Test(expected=IllegalArgumentException.class)
  public void testMissingArg() {
    parser.parse(Arrays.asList(parser.MASTER));
  }

  @Test
  public void testEqualSeparatedOption() {
    List<String> args = Arrays.asList(parser.MASTER + "=" + parser.MASTER);
    parser.parse(args);
    verify(parser).handle(eq(parser.MASTER), eq(parser.MASTER));
    verify(parser).handleExtraArgs(eq(Collections.<String>emptyList()));
  }

  private static class DummyParser extends SparkSubmitOptionParser {

    @Override
    protected boolean handle(String opt, String value) {
      return true;
    }

    @Override
    protected boolean handleUnknown(String opt) {
      return false;
    }

    @Override
    protected void handleExtraArgs(List<String> extra) {

    }

  }

}