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  package org.apache.any23.cli;
    18
    19  import java.io.File;
    20  import java.lang.invoke.MethodHandles;
    21  import java.nio.charset.StandardCharsets;
    22  import java.util.Arrays;
    23  import java.util.Collection;
    24  import java.util.Locale;
    25
    26  import org.apache.commons.io.FileUtils;
    27  import org.junit.Assert;
    28  import org.junit.Test;
    29  import org.junit.runner.RunWith;
    30  import org.junit.runners.Parameterized;
    31  import org.slf4j.Logger;
    32  import org.slf4j.LoggerFactory;
    33
    34  /**
    35   * Unit test for issue ANY23-310
    36   *
    37   * @author Jacek Grzebyta (grzebyta.dev [at] gmail.com)
    38   */
    39  @RunWith(Parameterized.class)
    40  public class SimpleRoverTest extends ToolTestBase {
    41
    42      private static final String baseUri = "urn:test";
    43      private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    44
    45      private String filePath;
    46
    47      @Parameterized.Parameters
    48      public static Collection<String[]> litsFiles() throws Exception {
    49          return Arrays.asList(new String[][] { { "/org/apache/any23/extractor/yaml/simple-load.yml" },
    50                  { "/org/apache/any23/extractor/csv/test-comma.csv" } });
    51      }
    52
    53      public SimpleRoverTest(String filePath) {
    54          super(Rover.class);
    55          this.filePath = filePath;
    56      }
    57
    58      /**
    59       * Ref <a href="https://issues.apache.org/jira/browse/ANY23-310">ANY23-310</a> unit test.
    60       *
    61       * @throws Exception
    62       *             if there is an error asserting the test data.
    63       */
    64      @Test
    65      public void ref310Test() throws Exception {
    66          File outputFile = File.createTempFile("rover-test", ".ttl", tempDirectory);
    67          File logfile = File.createTempFile("test-log", ".txt", tempDirectory);
    68
    69          int exitCode = runTool(
    70                  String.format(Locale.ROOT, "-l %s -o %s -f turtle -e yaml,csv -d %s %s", logfile.getAbsolutePath(),
    71                          outputFile.getAbsolutePath(), baseUri, copyResourceToTempFile(filePath).getAbsolutePath()));
    72
    73          Assert.assertTrue(logfile.exists());
    74          Assert.assertTrue(outputFile.exists());
    75          // check if output file is longer than 10 chracters
    76          String outputFileContent = FileUtils.readFileToString(outputFile, StandardCharsets.UTF_8);
    77          Assert.assertTrue(outputFileContent.length() > 10);
    78
    79          String[] logFileContent = FileUtils.readLines(logfile, StandardCharsets.UTF_8).toArray(new String[0]);
    80          Assert.assertEquals(2, logFileContent.length);
    81          // Assert.assertTrue(logFileContent[1].split("\\W*")[1] == );
    82          int contentSize = Integer.valueOf(logFileContent[1].split("\\t")[1]);
    83          String extractors = logFileContent[1].split("\\t")[4].replaceAll("[\\[\\]\\s:\\d]", "");
    84
    85          if (log.isDebugEnabled()) {
    86              log.debug("Content: '{}'", contentSize);
    87              log.debug("Extractors: '{}'", extractors);
    88              log.debug("Log file location: {}", logfile.getAbsolutePath());
    89          }
    90          if (log.isTraceEnabled()) {
    91              log.trace("Log file content: \n{}\n", FileUtils.readFileToString(logfile, StandardCharsets.UTF_8));
    92          }
    93
    94          Assert.assertTrue("Content size should be greated than 0", contentSize > 0);
    95          Assert.assertFalse(extractors.isEmpty());
    96          Assert.assertEquals("Unexpected exit code.", 0, exitCode);
    97      }
    98
    99      /**
    100      * Ref <a href="https://issues.apache.org/jira/browse/ANY23-310">ANY23-310</a> unit test.
    101      *
    102      * Example without the logging file.
    103      *
    104      * By default that test is not active. It might be useful for debugging.
    105      *
    106      * @throws Exception
    107      *             if there is an error asserting the test data.
    108      */
    109     @Test
    110     public void ref310ExtendedTest() throws Exception {
    111         File outputFile = File.createTempFile("rover-test", ".ttl", tempDirectory);
    112
    113         int exitCode = runTool(String.format(Locale.ROOT, "-o %s -f turtle -e yaml,csv -d %s %s",
    114                 outputFile.getAbsolutePath(), baseUri, copyResourceToTempFile(filePath).getAbsolutePath()));
    115
    116         Assert.assertTrue(outputFile.exists());
    117         // check if output file is longer than 10 chracters
    118         String outputFileContent = FileUtils.readFileToString(outputFile, StandardCharsets.UTF_8);
    119         Assert.assertTrue(outputFileContent.length() > 10);
    120
    121         Assert.assertEquals("Unexpected exit code.", 0, exitCode);
    122     }
    123
    124 }