Class Utils


  • public class Utils
    extends Object
    Collection of some utility methods and constants
    Author:
    jkuhnert, PikeTec GmbH
    • Constructor Detail

      • Utils

        public Utils()
    • Method Detail

      • getGeneratedTestDataDir

        public static String getGeneratedTestDataDir​(JenkinsConfiguration ec)
        Creates a String with a path to create for the test data dir. This will be relative to the build workspace
        Parameters:
        ec - through the jenkins configuration we get the id of the configuration, which is used to create a unique path.
        Returns:
        a String with the folders for the test data dir.
      • getGeneratedReportDir

        public static String getGeneratedReportDir​(JenkinsConfiguration ec)
        Creates a String with a path to create for the report dir. This will be relative to the build workspace
        Parameters:
        ec - through the jenkins configuration we get the id of the configuration, which is used to create a unique path.
        Returns:
        a String with the folders for the report dir.
      • getTptPluginRootDir

        public static File getTptPluginRootDir()
                                        throws IOException
        Returns:
        the rootdir from the "piketec-tpt" plugin, used for knowing where to get html files, json or some data stored there.
        Throws:
        IOException - If no Jenkins isntance could be found
      • getAbsolutePath

        public static File getAbsolutePath​(File workspaceDir,
                                           File path)
        Builds a absolute path from the workspace directory and the given path.
        • If both are null, the current working directory will returned - hopefully it is inside the workspace.
        • If the workspace is null, the path will returned.
        • If the path is null, the workspace will returned.
        • If the path is absolute, the path will returned.
        • If the path is relative, the path will append to the workspace and returned.
        Parameters:
        workspaceDir - Current workspace for the build.
        path - Relative or absolute path.
        Returns:
        A absolute path, but it can be a nonexisting file system object or not a directory.
      • toString

        public static <T> String toString​(Collection<T> list,
                                          String delimeter)
        Transforms the list into a String by calling toString() on every item, seperating them by the given delimeter
        Type Parameters:
        T - The types of items contained in the list
        Parameters:
        list - The list to tranform to String
        delimeter - The sting seperating the items
        Returns:
        The list represneted as a string
      • deleteFiles

        public static void deleteFiles​(FilePath path)
                                throws IOException,
                                       InterruptedException
        TPT changes its workind directory during execution, fails to set it back correctly after multicore execution and prevents the deletion of test data directory. This method only deletes the files in a directory recursively if FilePath.deleteContents() fails. Also TPT may keep tptbin-Files open for some Time in a cache, so we will retry more often to delete the files than the standard implementation.
        Parameters:
        path - The paths to delete
        Throws:
        IOException - If path could not be deleted
        InterruptedException - If thread was interrupted
      • copyRecursive

        public static void copyRecursive​(FilePath from,
                                         FilePath to,
                                         TptLogger logger)
                                  throws IOException,
                                         InterruptedException
        Copies all files from a remote location to another remote location. FilePath.copyRecursiveTo is not able to do that. See https://issues.jenkins-ci.org/browse/JENKINS-2126
        Parameters:
        from - Target path
        to - Source path
        logger - for dumping messages
        Throws:
        IOException - If an IO error occurs
        InterruptedException - If thread was interrupted
      • isValidWidnowsFileName

        public static boolean isValidWidnowsFileName​(String name)
        Checks if name does not contain a lien break, <, >, :, \, ", /, \, |, ?, * nor matches any of the reserved names CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
        Parameters:
        name - The name to check
        Returns:
        true if the name can be used as a windows file name
      • getEnvironment

        public static EnvVars getEnvironment​(AbstractBuild<?,​?> build,
                                             Launcher launcher,
                                             TptLogger logger)
                                      throws InterruptedException
        Get the environment variables for a build. In pipeline all strings should be handled as litereals and replacing is dones by Groovy so we do not accept Run here.
        Parameters:
        build - The build.
        launcher - The launcher
        logger - for dumping messages
        Returns:
        the environment variables from the agent machine the code is running on
        Throws:
        InterruptedException - If thread was interrupted
      • getInvisibleActionTPTExecutions

        public static List<InvisibleActionTPTExecution> getInvisibleActionTPTExecutions​(Run<?,​?> build)
        Get all InvisibleActionTPTExecution of a build.
        Parameters:
        build - The build to get the InvisibleActionTPTExecution list from
        Returns:
        The list of InvisibleActionTPTExecution
      • checkIdAndAddInvisibleActionTPTExecution

        public static boolean checkIdAndAddInvisibleActionTPTExecution​(JenkinsConfiguration resolvedConfig,
                                                                       Run<?,​?> build,
                                                                       TptLogger logger)
        Check for duplicate ID of already finished JenkinsConfiguration. Adds an InvisibleActionTPTExecution if ID is valid. Is synchronized to ensure that parallel parts of a pipeline using the same ID leads to a failure. Method is not heavily used, so staic synchronized is feasible.
        Parameters:
        resolvedConfig - JenkinsConfiguration with ID
        build - Build that executes the JenkinsConfiguration and may already have executed other JenkinsConfiguration wich must have a different ID.
        logger - to write an error message to
        Returns:
        true if the ID is unique and a valid windows file name
      • checkId

        public static boolean checkId​(JenkinsConfiguration ec,
                                      Run<?,​?> build,
                                      TptLogger logger)
        Check for duplicate ID of already finished JenkinsConfiguration.
        Parameters:
        ec - JenkinsConfiguration with ID
        build - Build that executes the JenkinsConfiguration and may already have executed other JenkinsConfiguration wich must have a different ID.
        logger - to write an error message to
        Returns:
        true if the ID is unique and a valid windows file name
      • parseCommandLine

        public static List<String> parseCommandLine​(String toProcess)
        Translates a command line String to seperate arguments using Commandline.translateCommandline(String).
        Returns:
        a fixed-size list backed by the command line array