/*
 * Copyright 2018, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.io;

import com.google.common.testing.NullPointerTester;
import io.spine.testing.TestValues;
import io.spine.testing.Tests;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import static io.spine.io.Files2.ensureFile;
import static io.spine.io.Files2.existsNonEmpty;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class Files2Should {

    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();

    @Test
    public void have_utility_ctor() {
        Tests.assertHasPrivateParameterlessCtor(Files2.class);
    }

    @Test
    public void ensure_file() {
        File expected = new File(testFolder.getRoot()
                                           .getAbsolutePath(), "with/sub/dirs/file.txt");

        boolean result = ensureFile(expected);

        // Check that the file was created.
        assertTrue(result);
        assertTrue(expected.exists());

        // When the file exists, the returned value is false.
        assertFalse(ensureFile(expected));
    }

    @Test
    public void verify_non_empty_file() throws IOException {
        File empty = testFolder.newFile();
        assertFalse(existsNonEmpty(empty));

        File doesNotExist = new File(TestValues.randomString());
        assertFalse(existsNonEmpty(doesNotExist));

        File nonEmpty = testFolder.newFile();
        try (PrintWriter out = new PrintWriter(nonEmpty.getAbsolutePath())) {
            out.println(TestValues.randomString());
        }
        assertTrue(existsNonEmpty(nonEmpty));
    }

    @Test
    public void pass_null_tolerance_test() {
        new NullPointerTester().testAllPublicConstructors(Files2.class);
    }
}

@@ -1406,16 +1406,71 @@
 nEmpty;%0A
+import static java.nio.charset.Charset.defaultCharset;%0A
 import s
@@ -2543,32 +2543,136 @@
 lder.newFile();%0A
+        String path = nonEmpty.getAbsolutePath();%0A        String charsetName = defaultCharset().name();%0A
         try (Pri
@@ -2706,34 +2706,25 @@
 ter(
-nonEmpty.getAbsolutePath()
+path, charsetName
 )) %7B

Fix an error-prone warning.