diff --git a/bin/zeppelin.sh b/bin/zeppelin.sh index d767891db85..244f7e61735 100755 --- a/bin/zeppelin.sh +++ b/bin/zeppelin.sh @@ -43,10 +43,11 @@ if [ -f /proc/self/cgroup ] && [ -n "$(command -v getent)" ]; then fi function usage() { - echo "Usage: bin/zeppelin.sh [--config ] [--run ]" + echo "Usage: bin/zeppelin.sh [--config ] [--run ] [--version|-v]" } POSITIONAL=() +VERSION_ONLY=false while [[ $# -gt 0 ]] do key="$1" @@ -61,6 +62,10 @@ do shift # past argument shift # past value ;; + -v|--version) + VERSION_ONLY=true + shift + ;; -h|--help) usage exit 0 @@ -81,7 +86,7 @@ bin="$(cd "${bin}">/dev/null; pwd)" check_java_version -if [ "$1" == "--version" ] || [ "$1" == "-v" ]; then +if [[ "${VERSION_ONLY}" == true ]]; then getZeppelinVersion fi diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/CommandLineUtils.java b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/CommandLineUtils.java new file mode 100644 index 00000000000..e1804dca033 --- /dev/null +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/CommandLineUtils.java @@ -0,0 +1,41 @@ +/* + * 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.zeppelin.utils; + +import java.util.Locale; + +import org.apache.zeppelin.util.Util; + +/** + * Command-line entry point invoked by getZeppelinVersion() in bin/common.sh. + */ +public class CommandLineUtils { + public static void main(String[] args) { + if (args.length == 0) { + return; + } + + String usage = args[0].toLowerCase(Locale.US); + switch (usage) { + case "--version": + case "-v": + System.out.println(Util.getVersion()); + break; + default: + } + } +} diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/utils/CommandLineUtilsTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/utils/CommandLineUtilsTest.java new file mode 100644 index 00000000000..5fc07191ab1 --- /dev/null +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/utils/CommandLineUtilsTest.java @@ -0,0 +1,91 @@ +/* + * 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.zeppelin.utils; + +import org.apache.zeppelin.util.Util; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit tests for {@link CommandLineUtils} written in Given-When-Then style. + */ +class CommandLineUtilsTest { + + /** + * Captures standard output during test execution. + */ + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + + /** + * Original System.out kept for restoration. + */ + private final PrintStream originalOut = System.out; + + @AfterEach + void restoreStdout() { + // Restore the original System.out after each test + System.setOut(originalOut); + } + + @Test + @DisplayName("Given --version flag, when main is executed, then version is printed") + void givenVersionFlag_whenMain_thenPrintsVersion() { + // ----- Given ----- + System.setOut(new PrintStream(outContent)); + String expected = Util.getVersion(); // whatever the project returns + + // ----- When ----- + CommandLineUtils.main(new String[]{"--version"}); + + // ----- Then ----- + assertEquals(expected, outContent.toString().trim()); + } + + @Test + @DisplayName("Given -v flag, when main is executed, then version is printed") + void givenShortVersionFlag_whenMain_thenPrintsVersion() { + // ----- Given ----- + System.setOut(new PrintStream(outContent)); + String expected = Util.getVersion(); + + // ----- When ----- + CommandLineUtils.main(new String[]{"-v"}); + + // ----- Then ----- + assertEquals(expected, outContent.toString().trim()); + } + + @Test + @DisplayName("Given no arguments, when main is executed, then nothing is printed") + void givenNoArgs_whenMain_thenPrintsNothing() { + // ----- Given ----- + System.setOut(new PrintStream(outContent)); + + // ----- When ----- + CommandLineUtils.main(new String[]{}); + + // ----- Then ----- + assertEquals("", outContent.toString().trim()); + } +}