/*
 * Copyright 2015.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 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 "AS IS" 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.
 */
package com.tascape.qa.th;

import java.awt.AWTException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author linsong wang
 */
public abstract class AbstractTestResource {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractTestResource.class);

    protected final SystemConfiguration sysConfig = SystemConfiguration.getInstance();

    public abstract Path getLogPath();

    public File saveAsTextFile(String filePrefix, CharSequence data) throws IOException {
        Path path = this.getLogPath();
        if (!path.toFile().mkdirs()) {
            throw new IOException("Cannot create test log directory " + path);
        }
        File f = File.createTempFile(filePrefix, ".txt", path.toFile());
        FileUtils.write(f, data);
        LOG.debug("Save data into file {}", f.getAbsolutePath());
        return f;
    }

    public File saveAsTempTextFile(String filePrefix, CharSequence data) throws IOException {
        Path path = this.getLogPath();
        File f = File.createTempFile(SystemConfiguration.CONSTANT_LOG_KEEP_ALIVE_PREFIX + filePrefix, ".txt",
            path.toFile());
        FileUtils.write(f, data);
        LOG.debug("Save data into file {}", f.getAbsolutePath());
        return f;
    }

    /**
     * @return png file
     */
    public File captureScreen() {
        Path path = this.getLogPath();
        File png = path.resolve("screen-" + System.currentTimeMillis() + ".png").toFile();
        png = Utils.getKeepAliveFile(png);
        try {
            Utils.captureScreen(png);
        } catch (AWTException | IOException ex) {
            LOG.warn("Cannot take screenshot", ex);
        }
        return png;
    }

    protected File createDataFile(String prefix) throws IOException {
        return this.createDataFile(prefix, "txt");
    }

    protected File createDataFile(String prefix, String extension) throws IOException {
        File f = File.createTempFile(prefix + "-", "." + extension, this.getLogPath().toFile());
        return f;
    }

    protected File createKeepAliveLogFile(String prefix, String extension) throws IOException {
        File f = File.createTempFile(prefix + "-", "." + extension, this.getLogPath().toFile());
        return Utils.getKeepAliveFile(f);
    }
}

@@ -1289,13 +1289,17 @@
     
-if (!
+File p = 
 path
@@ -1307,16 +1307,48 @@
 toFile()
+;%0D%0A        if (!p.exists() && !p
 .mkdirs(
@@ -1426,19 +1426,16 @@
 ry %22 + p
-ath
 );%0D%0A    
@@ -1499,28 +1499,16 @@
 .txt%22, p
-ath.toFile()
 );%0D%0A    

fix a problem of creating log dir