Subversion Repositories DevTools

Rev

Rev 6914 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 *  A collection of utilities used in testing
 */
package com.erggroup.buildtool.utf;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Utilities
{
    
    private Utilities() {
        throw new IllegalStateException("Utilities class");
      }

    /**
     * Check build file against golden image
     * Golden images are held in the 'expected' subdirectory
     * 
     * @param content
     *            - Generated build file
     * @param baseName
     *            - Base name used to save buildfile and for golden image
     * @return boolean - True if the build file matches the golden image
     */
    static public boolean checkBuildfile(String content, String baseName)
    {
        String buildFileName;
        String testFilename;
        String expected = "";
        String user = "";
        boolean rv = false;

        // persist the generated build file content to daemon2.xml for visibility
        buildFileName = new String(baseName + ".xml");
        testFilename = new String("work/expected/" + baseName + ".xml");

        try
        {
            File buildFile = new File("build", buildFileName);
            FileWriter buildFileWriter = new FileWriter(buildFile);
            buildFileWriter.write(content);
            buildFileWriter.close();

            File expectedBuildFile = new File(testFilename);
            FileReader expectedBuildFileReader = new FileReader(expectedBuildFile);
            char[] expectedBuildFileContent = new char[(int) expectedBuildFile.length()];
            expectedBuildFileReader.read(expectedBuildFileContent);
            expectedBuildFileReader.close();

            expected = new String(expectedBuildFileContent);
            expected = expected.replace("\r", "");

            user = content.replace("\r", "");
            
            rv = user.compareTo(expected) == 0;
        }
        catch (IOException e)
        {
            System.out.println("checkBuildFile caught IOException:" + baseName);
            rv = false;
        }
        
        return rv;
    }

    /** Compare a named file with its expected value
     *  Golden images are held in the 'expected' subdirectory 
     * 
     * @param file1             - test file
     * @return                  - True if the files match
     */
    public static boolean checkBuildfiles(String file1)
    {
        boolean rv = false;
        String fileG = new String("work/expected/" + file1);
        
        try
        {
             
            File expected1 = new File(fileG);
            FileReader expected1Reader = new FileReader(expected1);
            char[] expected1Content = new char[(int) expected1.length()];
            expected1Reader.read(expected1Content);
            expected1Reader.close();
            String readExpected1Content = new String(expected1Content);
            
            File buildFile1 = new File("build",file1);
            FileReader buildFile1Reader = new FileReader(buildFile1);
            char[] buildFile1Content = new char[(int) buildFile1.length()];
            buildFile1Reader.read(buildFile1Content);
            buildFile1Reader.close();
            String readBuildFile1Content = new String(buildFile1Content);
            
            readExpected1Content = readExpected1Content.replace("\r", "");
            readBuildFile1Content = readBuildFile1Content.replace("\r", "");
            
            rv = readExpected1Content.compareTo(readBuildFile1Content) == 0;
        }
        catch (IOException e)
        {
            System.out.println("checkBuildFiles caught IOException:" + fileG + ":" + file1);
            rv = false;
        }
        
        return rv;
       
    }


}