Content of file BaseTestHelper.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>BaseTestHelper.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">TheseFoolishThings :: Examples :: Finder :: In-memory Finder</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-util-test</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.util.test</a> &gt; <span class="el_source">BaseTestHelper.java</span></div><h1>BaseTestHelper.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * TheseFoolishThings: Miscellaneous utilities
 * http://tidalwave.it/projects/thesefoolishthings
 *
 * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); 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 &quot;AS IS&quot; 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.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
 * git clone https://github.com/tidalwave-it/thesefoolishthings-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.util.test;

import javax.annotation.Nonnull;
import java.util.List;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static java.nio.charset.StandardCharsets.UTF_8;
import static it.tidalwave.util.test.FileComparisonUtils.assertSameContents;
import static lombok.AccessLevel.PRIVATE;

/***********************************************************************************************************************
 *
 * A facility that provides some common tasks for testing, such as manipulating test files.
 *
 * @author  Fabrizio Giudici
 * @since 3.2-ALPHA-18
 *
 **********************************************************************************************************************/
<span class="nc bnc" id="L50" title="All 2 branches missed.">@RequiredArgsConstructor @Slf4j</span>
public class BaseTestHelper
  {
    @Nonnull
    protected final Object test;

    /*******************************************************************************************************************
     *
     * Returns a {@link Path} for a resource file. The resource should be placed under
     * {@code src/test/resources/test-class-simple-name/test-resources/resource-name}. Note that the file actually
     * loaded is the one under {@code target/test-classes} copied there (and eventually filtered) by Maven.
     *
     * @param   resourceName    the resource name
     * @return the {@code Path}
     *
     ******************************************************************************************************************/
    @Nonnull
    public Path resourceFileFor (@Nonnull final String resourceName)
      {
<span class="nc" id="L69">        final var testName = test.getClass().getSimpleName();</span>
<span class="nc" id="L70">        return Paths.get(&quot;target/test-classes&quot;, testName, &quot;test-resources&quot;, resourceName);</span>
      }

    /*******************************************************************************************************************
     *
     * Reads the content from the resource file as a single string. See {@link #resourceFileFor(String)} for
     * further info.
     *
     * @param   resourceName    the resource name
     * @return the string
     * @throws IOException     in case of error
     *
     ******************************************************************************************************************/
    @Nonnull
    public String readStringFromResource (@Nonnull final String resourceName)
            throws IOException
      {
<span class="nc" id="L87">        final var file = resourceFileFor(resourceName);</span>
<span class="nc" id="L88">        final var buffer = new StringBuilder();</span>
<span class="nc" id="L89">        var separator = &quot;&quot;;</span>

<span class="nc bnc" id="L91" title="All 2 branches missed.">        for (final var string : Files.readAllLines(file, UTF_8))</span>
          {
<span class="nc" id="L93">            buffer.append(separator).append(string);</span>
<span class="nc" id="L94">            separator = &quot;\n&quot;;</span>
<span class="nc" id="L95">          }</span>

<span class="nc" id="L97">        return buffer.toString();</span>
//            return String.join(&quot;\n&quot;, Files.readAllLines(path, UTF_8)); TODO JDK 8
JDK 8
} /******************************************************************************************************************* * * Create a {@link TestResource} for the given name. The actual file will be created under * {@code target/test-artifacts/test-class-simple-name/resourceName}. The expected file should be * placed in {@code src/test/resources/test-class-simple-name/expected-results/resource-name}. Note that the file * actually loaded is the one under {@code target/test-classes} copied there (and eventually filtered) by Maven. * The {@code test-class-simple-name} is tried first with the current test, and then with its eventual * super-classes; this allows to extend existing test suites. Note that if the resource files for a super class are * not in the current project module, they should be explicitly copied here (for instance, by means of the * Maven dependency plugin). * * @param resourceName the name * @return the {@code TestResource} * @throws IOException in case of error * ******************************************************************************************************************/ @Nonnull public TestResource testResourceFor (@Nonnull final String resourceName) throws IOException { <span class="nc" id="L121"> final var testName = test.getClass().getSimpleName();</span> <span class="nc" id="L122"> final var expectedFile = findExpectedFilePath(resourceName);</span> <span class="nc" id="L123"> final var actualFile = Paths.get(&quot;target/test-artifacts&quot;, testName, resourceName);</span> <span class="nc" id="L124"> Files.createDirectories(actualFile.getParent());</span> <span class="nc" id="L125"> return new TestResource(resourceName, actualFile, expectedFile);</span> } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private Path findExpectedFilePath (@Nonnull final String resourceName) throws IOException { <span class="nc bnc" id="L135" title="All 2 branches missed."> for (var testClass = test.getClass(); testClass != null; testClass = testClass.getSuperclass())</span> { <span class="nc" id="L137"> final var expectedFile =</span> <span class="nc" id="L138"> Paths.get(&quot;target/test-classes&quot;, testClass.getSimpleName(), &quot;expected-results&quot;, resourceName);</span> <span class="nc bnc" id="L140" title="All 2 branches missed."> if (Files.exists(expectedFile))</span> { <span class="nc" id="L142"> return expectedFile;</span> } } <span class="nc" id="L146"> throw new FileNotFoundException(&quot;Expected file for test &quot; + resourceName);</span> } /******************************************************************************************************************* * * A manipulator of a pair of (actual file, expected file). * ******************************************************************************************************************/ <span class="nc bnc" id="L154" title="All 6 branches missed."> @RequiredArgsConstructor(access = PRIVATE)</span> public final class TestResource { @Nonnull private final String name; @Nonnull private final Path actualFile; @Nonnull private final Path expectedFile; /*************************************************************************************************************** * * Assert that the content of the actual file are the same as the expected file. * * @throws IOException in case of error * **************************************************************************************************************/ public void assertActualFileContentSameAsExpected () throws IOException { <span class="nc" id="L176"> assertSameContents(expectedFile.toFile(), actualFile.toFile());</span> <span class="nc" id="L177"> }</span> /*************************************************************************************************************** * * Writes the given strings to the actual file. * * @param strings the strings * @throws IOException in case of error * **************************************************************************************************************/ public void writeToActualFile (@Nonnull final String... strings) throws IOException { <span class="nc" id="L190"> writeToActualFile(List.of(strings));</span> <span class="nc" id="L191"> }</span> /*************************************************************************************************************** * * Writes the given strings to the actual file. * * @param strings the strings * @throws IOException in case of error * **************************************************************************************************************/ public void writeToActualFile (@Nonnull final Iterable&lt;String&gt; strings) throws IOException { <span class="nc" id="L204"> Files.write(actualFile, strings, UTF_8);</span> <span class="nc" id="L205"> }</span> /*************************************************************************************************************** * * Writes the given bytes to the actual file. * * @param bytes the bytes * @throws IOException in case of error * **************************************************************************************************************/ public void writeToActualFile (@Nonnull final byte[] bytes) throws IOException { <span class="nc" id="L218"> Files.write(actualFile, bytes);</span> <span class="nc" id="L219"> }</span> /*************************************************************************************************************** * * Reads the content from the resource file as a single string. * * @return the string * @throws IOException in case of error * **************************************************************************************************************/ @Nonnull public String readStringFromResource () throws IOException { <span class="nc" id="L233"> return BaseTestHelper.this.readStringFromResource(name);</span> } } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>