Skip to content

Commit

Permalink
Prevent NPE when group has no default command
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jan 19, 2020
1 parent d1c423b commit cc79a8f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/io/airlift/airline/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
Expand Down Expand Up @@ -86,7 +87,9 @@ private Cli(String name,
.map(group -> loadCommandGroup(
group.name,
group.description,
loadCommand(group.defaultCommand),
Optional.ofNullable(group.defaultCommand)
.map(MetadataLoader::loadCommand)
.orElse(null),
loadCommands(group.commands)))
.collect(toImmutableList());

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/airlift/airline/model/MetadataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Streams.stream;
import static java.util.Objects.requireNonNull;

public final class MetadataLoader
{
Expand Down Expand Up @@ -71,6 +72,7 @@ public static <T> ImmutableList<CommandMetadata> loadCommands(Iterable<Class<? e

public static CommandMetadata loadCommand(Class<?> commandType)
{
requireNonNull(commandType, "commandType is null");
Command command = null;
for (Class<?> cls = commandType; command == null && !Object.class.equals(cls); cls = cls.getSuperclass()) {
command = cls.getAnnotation(Command.class);
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/io/airlift/airline/TestCli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed 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 io.airlift.airline;

import io.airlift.airline.Cli.CliBuilder;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestCli
{
@Test
public void testGroupWithoutDefaultCommand()
{
CliBuilder<Runnable> builder = Cli.builder("command");
builder.withGroup("subcommand")
.withCommand(Help.class);

Cli<Runnable> cli = builder.build();

assertThatThrownBy(cli::parse)
.isInstanceOf(ParseCommandMissingException.class)
.hasMessage("No command specified");

assertThatThrownBy(() -> cli.parse("subcommand"))
.isInstanceOf(ParseCommandMissingException.class)
.hasMessage("No command specified");

assertThat(cli.parse("subcommand", "help")).isInstanceOf(Help.class);
}
}

0 comments on commit cc79a8f

Please sign in to comment.