View Javadoc

    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.any23.cli;
    19
    20  import com.beust.jcommander.Parameters;
    21  import org.apache.any23.Any23OnlineTestBase;
    22
    23  import java.util.Arrays;
    24  import java.util.Locale;
    25  import java.util.Objects;
    26
    27  import static java.lang.String.format;
    28  import static org.junit.Assert.assertEquals;
    29
    30  /**
    31   * Base class for <i>CLI</i> related tests.
    32   *
    33   * @author Michele Mostarda (mostarda@fbk.eu)
    34   */
    35  public abstract class ToolTestBase extends Any23OnlineTestBase {
    36
    37      public static final String TOOL_RUN_METHOD = "run";
    38
    39      private final Class<? extends Tool> toolClazz;
    40
    41      protected ToolTestBase(Class<? extends Tool> tool) {
    42          toolClazz = Objects.requireNonNull(tool, "Tool class cannot be null.");
    43      }
    44
    45      /**
    46       * Runs the underlying tool.
    47       *
    48       * @param args
    49       *            tool arguments.
    50       *
    51       * @return the tool exit code.
    52       *
    53       * @throws Exception
    54       *             if there is an error asserting the test data.
    55       */
    56      protected int runTool(String... args) throws Exception {
    57          final String commandName = toolClazz.getAnnotation(Parameters.class).commandNames()[0];
    58
    59          final String[] enhancedArgs = new String[args.length + 1];
    60          enhancedArgs[0] = commandName;
    61          System.arraycopy(args, 0, enhancedArgs, 1, args.length);
    62
    63          return new ToolRunner().execute(true, enhancedArgs);
    64      }
    65
    66      /**
    67       * Runs the underlying tool.
    68       *
    69       * @param args
    70       *            args tool arguments.
    71       *
    72       * @return the tool exit code.
    73       *
    74       * @throws Exception
    75       *             if there is an error asserting the test data.
    76       */
    77      protected int runTool(String args) throws Exception {
    78          return runTool(args.split(" "));
    79      }
    80
    81      /**
    82       * Runs the underlying tool and verify the exit code to <code>0</code>.
    83       *
    84       * @param args
    85       *            tool arguments.
    86       *
    87       * @throws Exception
    88       *             if there is an error asserting the test data.
    89       */
    90      protected void 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      }
    94
    95  }