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.io.IOException;
    21  import java.nio.charset.StandardCharsets;
    22  import java.util.Locale;
    23
    24  import org.apache.commons.io.FileUtils;
    25  import org.junit.Assert;
    26  import org.junit.Test;
    27  import org.slf4j.Logger;
    28  import org.slf4j.LoggerFactory;
    29
    30  /**
    31   * Unit test for issue ANY23-308
    32   *
    33   * @author Jacek Grzebyta (grzebyta.dev [at] gmail.com)
    34   */
    35  public class YAMLRoverTest extends ToolTestBase {
    36
    37      private static final String file1 = "/org/apache/any23/extractor/yaml/simple-load.yml";
    38
    39      private static final String baseUri = "urn:test";
    40
    41      private final Logger log = LoggerFactory.getLogger(getClass());
    42
    43      public YAMLRoverTest() {
    44          super(Rover.class);
    45      }
    46
    47      @Test
    48      public void simpleTest() throws Exception {
    49          File outputFile = File.createTempFile("rover-test", ".ttl", tempDirectory);
    50          File logfile = File.createTempFile("test-log", ".txt", tempDirectory);
    51
    52          int exitCode = runTool(
    53                  String.format(Locale.ROOT, "-l %s -o %s -f turtle -e yaml,csv -d %s %s", logfile.getAbsolutePath(),
    54                          outputFile.getAbsolutePath(), baseUri, copyResourceToTempFile(file1).getAbsolutePath()));
    55
    56          Assert.assertTrue(logfile.exists());
    57          log.debug("Log file location: {}", logfile.getAbsolutePath());
    58          log.debug("Log file content: \n{}\n", FileUtils.readFileToString(logfile, StandardCharsets.UTF_8));
    59
    60          Assert.assertEquals("Unexpected exit code.", 0, exitCode);
    61          assertFileContainsString(outputFile, baseUri);
    62      }
    63
    64      /**
    65       * Asserts if file contains wanted string.
    66       *
    67       * If logging level is <code>trace</code> than additionally displays file content.
    68       *
    69       * @param f
    70       *            input file
    71       * @param s
    72       *            Expected string in the file
    73       *
    74       * @throws IOException
    75       *             if there is an error reading the input data.
    76       */
    77      public void assertFileContainsString(File f, String s) throws IOException {
    78          String fileContent = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
    79          log.trace("File content: \n{}\n", fileContent);
    80          Assert.assertTrue(fileContent.contains(s));
    81      }
    82
    83  }