1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */1718package org.apache.any23.cli;
1920import com.beust.jcommander.Parameters;
21import org.apache.any23.Any23OnlineTestBase;
2223import java.util.Arrays;
24import java.util.Locale;
25import java.util.Objects;
2627importstatic java.lang.String.format;
28importstatic org.junit.Assert.assertEquals;
2930/**31 * Base class for <i>CLI</i> related tests.32 *33 * @author Michele Mostarda (mostarda@fbk.eu)34 */35publicabstractclassToolTestBaseextendsAny23OnlineTestBase {
3637publicstaticfinal String TOOL_RUN_METHOD = "run";
3839privatefinal Class<? extends Tool> toolClazz;
4041protectedToolTestBase(Class<? extends Tool> tool) {
42 toolClazz = Objects.requireNonNull(tool, "Tool class cannot be null.");
43 }
4445/**46 * Runs the underlying tool.47 *48 * @param args49 * tool arguments.50 * 51 * @return the tool exit code.52 * 53 * @throws Exception54 * if there is an error asserting the test data.55 */56protectedint runTool(String... args) throws Exception {
57final String commandName = toolClazz.getAnnotation(Parameters.class).commandNames()[0];
5859final String[] enhancedArgs = new String[args.length + 1];
60 enhancedArgs[0] = commandName;
61 System.arraycopy(args, 0, enhancedArgs, 1, args.length);
6263returnnew ToolRunner().execute(true, enhancedArgs);
64 }
6566/**67 * Runs the underlying tool.68 *69 * @param args70 * args tool arguments.71 * 72 * @return the tool exit code.73 * 74 * @throws Exception75 * if there is an error asserting the test data.76 */77protectedint runTool(String args) throws Exception {
78return runTool(args.split(" "));
79 }
8081/**82 * Runs the underlying tool and verify the exit code to <code>0</code>.83 *84 * @param args85 * tool arguments.86 * 87 * @throws Exception88 * if there is an error asserting the test data.89 */90protectedvoid runToolCheckExit0(String... args) throws Exception {
91 assertEquals(format(Locale.ROOT, "Unexpected exit code for tool [%s] invoked with %s",
92 toolClazz.getSimpleName(), Arrays.asList(args)), 0, runTool(args));
93 }
9495 }