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.ArrayList;
0021 import java.util.Arrays;
0022 import java.util.List;
0023 
0024 import org.junit.Test;
0025 import static org.junit.Assert.*;
0026 
0027 import static org.apache.spark.launcher.CommandBuilderUtils.*;
0028 
0029 public class CommandBuilderUtilsSuite {
0030 
0031   @Test
0032   public void testValidOptionStrings() {
0033     testOpt("a b c d e", Arrays.asList("a", "b", "c", "d", "e"));
0034     testOpt("a 'b c' \"d\" e", Arrays.asList("a", "b c", "d", "e"));
0035     testOpt("a 'b\\\"c' \"'d'\" e", Arrays.asList("a", "b\\\"c", "'d'", "e"));
0036     testOpt("a 'b\"c' \"\\\"d\\\"\" e", Arrays.asList("a", "b\"c", "\"d\"", "e"));
0037     testOpt(" a b c \\\\ ", Arrays.asList("a", "b", "c", "\\"));
0038 
0039     // Following tests ported from UtilsSuite.scala.
0040     testOpt("", new ArrayList<>());
0041     testOpt("a", Arrays.asList("a"));
0042     testOpt("aaa", Arrays.asList("aaa"));
0043     testOpt("a b c", Arrays.asList("a", "b", "c"));
0044     testOpt("  a   b\t c ", Arrays.asList("a", "b", "c"));
0045     testOpt("a 'b c'", Arrays.asList("a", "b c"));
0046     testOpt("a 'b c' d", Arrays.asList("a", "b c", "d"));
0047     testOpt("'b c'", Arrays.asList("b c"));
0048     testOpt("a \"b c\"", Arrays.asList("a", "b c"));
0049     testOpt("a \"b c\" d", Arrays.asList("a", "b c", "d"));
0050     testOpt("\"b c\"", Arrays.asList("b c"));
0051     testOpt("a 'b\" c' \"d' e\"", Arrays.asList("a", "b\" c", "d' e"));
0052     testOpt("a\t'b\nc'\nd", Arrays.asList("a", "b\nc", "d"));
0053     testOpt("a \"b\\\\c\"", Arrays.asList("a", "b\\c"));
0054     testOpt("a \"b\\\"c\"", Arrays.asList("a", "b\"c"));
0055     testOpt("a 'b\\\"c'", Arrays.asList("a", "b\\\"c"));
0056     testOpt("'a'b", Arrays.asList("ab"));
0057     testOpt("'a''b'", Arrays.asList("ab"));
0058     testOpt("\"a\"b", Arrays.asList("ab"));
0059     testOpt("\"a\"\"b\"", Arrays.asList("ab"));
0060     testOpt("''", Arrays.asList(""));
0061     testOpt("\"\"", Arrays.asList(""));
0062   }
0063 
0064   @Test
0065   public void testInvalidOptionStrings() {
0066     testInvalidOpt("\\");
0067     testInvalidOpt("\"abcde");
0068     testInvalidOpt("'abcde");
0069   }
0070 
0071   @Test
0072   public void testWindowsBatchQuoting() {
0073     assertEquals("abc", quoteForBatchScript("abc"));
0074     assertEquals("\"a b c\"", quoteForBatchScript("a b c"));
0075     assertEquals("\"a \"\"b\"\" c\"", quoteForBatchScript("a \"b\" c"));
0076     assertEquals("\"a\"\"b\"\"c\"", quoteForBatchScript("a\"b\"c"));
0077     assertEquals("\"ab=\"\"cd\"\"\"", quoteForBatchScript("ab=\"cd\""));
0078     assertEquals("\"a,b,c\"", quoteForBatchScript("a,b,c"));
0079     assertEquals("\"a;b;c\"", quoteForBatchScript("a;b;c"));
0080     assertEquals("\"a,b,c\\\\\"", quoteForBatchScript("a,b,c\\"));
0081   }
0082 
0083   @Test
0084   public void testPythonArgQuoting() {
0085     assertEquals("\"abc\"", quoteForCommandString("abc"));
0086     assertEquals("\"a b c\"", quoteForCommandString("a b c"));
0087     assertEquals("\"a \\\"b\\\" c\"", quoteForCommandString("a \"b\" c"));
0088   }
0089 
0090   @Test
0091   public void testJavaMajorVersion() {
0092     assertEquals(6, javaMajorVersion("1.6.0_50"));
0093     assertEquals(7, javaMajorVersion("1.7.0_79"));
0094     assertEquals(8, javaMajorVersion("1.8.0_66"));
0095     assertEquals(9, javaMajorVersion("9-ea"));
0096     assertEquals(9, javaMajorVersion("9+100"));
0097     assertEquals(9, javaMajorVersion("9"));
0098     assertEquals(9, javaMajorVersion("9.1.0"));
0099     assertEquals(10, javaMajorVersion("10"));
0100   }
0101 
0102   private static void testOpt(String opts, List<String> expected) {
0103     assertEquals(String.format("test string failed to parse: [[ %s ]]", opts),
0104         expected, parseOptionString(opts));
0105   }
0106 
0107   private static void testInvalidOpt(String opts) {
0108     try {
0109       parseOptionString(opts);
0110       fail("Expected exception for invalid option string.");
0111     } catch (IllegalArgumentException e) {
0112       // pass.
0113     }
0114   }
0115 
0116 }