view src/org/thermostat/qa2/framework/utils/FileUtilities.java @ 185:3ec1dcfecb16

Adds new test class ShellCommandsTest, which tests commands in thermostat shell.
author Zdenek Zambersky <zzambers@redhat.com>
date Fri, 15 May 2015 19:12:02 +0200
parents 6b6ac47a811f
children 2af53c61efc6
line wrap: on
line source

/*
 ThermostatQA - test framework for Thermostat Monitoring Tool

 Copyright 2015 Red Hat, Inc.

 This file is part of ThermostatQA

 ThermostatQA is distributed under the GNU General Public License,
 version 2 or any later version (with a special exception described
 below, commonly known as the "Classpath Exception").

 A copy of GNU General Public License (GPL) is included in this
 distribution, in the file COPYING.

 Linking ThermostatQA code with other modules is making a combined work
 based on ThermostatQA.  Thus, the terms and conditions of the GPL
 cover the whole combination.

 As a special exception, the copyright holders of ThermostatQA give you
 permission to link this code with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on ThermostatQA code.  If you modify ThermostatQA, you may
 extend this exception to your version of the software, but you are
 not obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
 */
package org.thermostat.qa2.framework.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.thermostat.qa2.framework.ThermostatQAConfig;

/**
 *
 * @author Zdeněk Žamberský
 */
public class FileUtilities {

    public static String getFileName(String s) {
        int i = s.lastIndexOf(File.separator);
        return i >= 0 ? s.substring(i + 1) : s;
    }

    public static String getUniqueFile(String file) {
        String name = file;
        int counter = 1;
        while (new File(name).exists()) {
            name = file + counter++;
        }
        return name;
    }

    public static String getUniqueTempFile(String name) {
        return getUniqueFile(ThermostatQAConfig.backupDir + File.separator + name);
    }

    public static void copyFile(String src, String dest) throws Exception {
        ProcessUtilities.run("cp", "--", src, dest);
    }

    public static void removeFile(String file) throws Exception {
        ProcessUtilities.run("rm", "-rf", "--", file);
    }

    public static List<String> getLineListFromFile(String file) throws FileNotFoundException, IOException {
        ArrayList<String> list = new ArrayList();
        return addLinesFromFileToList(list, file);
    }

    public static List<String> addLinesFromFileToList(List<String> list, String file) throws FileNotFoundException, IOException {
        FileReader fis = null;
        BufferedReader br = null;
        try {
            fis = new FileReader(file);
            br = new BufferedReader(fis);
            return CommonUtilities.addLinesToList(list, br);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ex) {
                    Logger.getLogger(FileUtilities.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(FileUtilities.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void printLineListToFile(String file, List<String> list) throws FileNotFoundException {
        FileOutputStream fos = null;
        PrintStream ps = null;

        try {
            fos = new FileOutputStream(file);
            ps = new PrintStream(fos);
            for (String s : list) {
                ps.println(s);
            }
            ps.flush();
        } finally {
            if (ps != null) {
                ps.close();
            }
        }
    }

}