|
| 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.hadoop.ozone.local; |
| 19 | + |
| 20 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | + |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.OutputStreamWriter; |
| 28 | +import java.io.PrintWriter; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import picocli.CommandLine; |
| 31 | +import picocli.CommandLine.Command; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link OzoneLocal}. |
| 35 | + */ |
| 36 | +class TestOzoneLocal { |
| 37 | + |
| 38 | + @Test |
| 39 | + void localCommandMetadataIsPresentAndHidden() { |
| 40 | + Command command = OzoneLocal.class.getAnnotation(Command.class); |
| 41 | + |
| 42 | + assertNotNull(command); |
| 43 | + assertEquals("ozone local", command.name()); |
| 44 | + assertTrue(command.hidden()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void runCommandMetadataIsPresentAndHidden() { |
| 49 | + Command command = OzoneLocal.RunCommand.class.getAnnotation(Command.class); |
| 50 | + |
| 51 | + assertNotNull(command); |
| 52 | + assertEquals("run", command.name()); |
| 53 | + assertTrue(command.hidden()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void genericCliRegistersRunPlaceholder() { |
| 58 | + OzoneLocal local = new OzoneLocal(); |
| 59 | + |
| 60 | + assertTrue(local.getCmd().getSubcommands().containsKey("run")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void rootHelpHidesRunPlaceholder() throws Exception { |
| 65 | + OzoneLocal local = new OzoneLocal(); |
| 66 | + CommandLine commandLine = local.getCmd(); |
| 67 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 68 | + ByteArrayOutputStream err = new ByteArrayOutputStream(); |
| 69 | + commandLine.setOut(new PrintWriter(new OutputStreamWriter(out, UTF_8), |
| 70 | + true)); |
| 71 | + commandLine.setErr(new PrintWriter(new OutputStreamWriter(err, UTF_8), |
| 72 | + true)); |
| 73 | + |
| 74 | + int exitCode = local.execute(new String[] {"--help"}); |
| 75 | + |
| 76 | + String help = out.toString(UTF_8.name()); |
| 77 | + assertEquals(0, exitCode); |
| 78 | + assertTrue(help.contains("Usage: ozone local")); |
| 79 | + assertFalse(help.contains("run")); |
| 80 | + assertEquals("", err.toString(UTF_8.name())); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void runCommandIsQuietNoOpPlaceholder() throws Exception { |
| 85 | + OzoneLocal local = new OzoneLocal(); |
| 86 | + CommandLine commandLine = local.getCmd(); |
| 87 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 88 | + ByteArrayOutputStream err = new ByteArrayOutputStream(); |
| 89 | + commandLine.setOut(new PrintWriter(new OutputStreamWriter(out, UTF_8), |
| 90 | + true)); |
| 91 | + commandLine.setErr(new PrintWriter(new OutputStreamWriter(err, UTF_8), |
| 92 | + true)); |
| 93 | + |
| 94 | + int exitCode = local.execute(new String[] {"run"}); |
| 95 | + |
| 96 | + assertEquals(0, exitCode); |
| 97 | + assertEquals("", out.toString(UTF_8.name())); |
| 98 | + assertEquals("", err.toString(UTF_8.name())); |
| 99 | + } |
| 100 | +} |
0 commit comments