Class FileUtils

java.lang.Object
org.citrusframework.util.FileUtils

public abstract class FileUtils extends Object
Class to provide general file utilities, such as listing all XML files in a directory, or finding certain tests in a directory.
Since:
2007
  • Field Details

  • Method Details

    • setSimulationMode

      public static void setSimulationMode(boolean mode)
      Sets the simulation mode.
    • readToString

      public static String readToString(Resource resource) throws IOException
      Read file resource to string value with default charset settings.
      Parameters:
      resource -
      Returns:
      Throws:
      IOException
    • readToString

      public static String readToString(InputStream inputStream) throws IOException
      Read file input stream to string value with default charset settings.
      Parameters:
      inputStream -
      Returns:
      Throws:
      IOException
    • readToString

      public static String readToString(File file) throws IOException
      Read file content to string value with default charset settings.
      Parameters:
      file -
      Returns:
      Throws:
      IOException
    • readToString

      public static String readToString(Resource resource, Charset charset) throws IOException
      Read file resource to string value.
      Parameters:
      resource -
      charset -
      Returns:
      Throws:
      IOException
    • readToString

      public static String readToString(InputStream inputStream, Charset charset) throws IOException
      Read file input stream to string value.
      Parameters:
      inputStream -
      charset -
      Returns:
      Throws:
      IOException
    • writeToFile

      public static void writeToFile(InputStream inputStream, File file)
      Writes inputStream content to file. Uses default charset encoding.
      Parameters:
      inputStream -
      file -
    • writeToFile

      public static void writeToFile(String content, File file)
      Writes String content to file. Uses default charset encoding.
      Parameters:
      content -
      file -
    • writeToFile

      public static void writeToFile(String content, File file, Charset charset)
      Writes String content to file with given charset encoding. Automatically closes file output streams when done.
      Parameters:
      content -
      file -
    • findFiles

      public static List<File> findFiles(String startDir, Set<String> fileNamePatterns)
      Method to retrieve all files with given file name pattern in given directory. Hierarchy of folders is supported.
      Parameters:
      startDir - the directory to hold the files
      fileNamePatterns - the file names to include
      Returns:
      list of test files as filename paths
    • getFileResource

      public static Resource getFileResource(String filePath, TestContext context)
      Reads file resource from path with variable replacement support.
      Parameters:
      filePath -
      context -
      Returns:
    • getFileResource

      public static Resource getFileResource(String filePath)
      Reads file resource from path with variable replacement support.
      Parameters:
      filePath -
      Returns:
    • getDefaultCharset

      public static Charset getDefaultCharset()
      Gets the default charset. If set by Citrus system property (citrus.file.encoding) use this one otherwise use system default.
      Returns:
    • getCharset

      public static Charset getCharset(String path)
      Extract charset information from file path. If not set return default charset. Charset is read as path parameter at the end of the file path
      Parameters:
      path -
      Returns:
    • getFileExtension

      public static String getFileExtension(String path)
      Extract file extension from given path.
      Parameters:
      path -
      Returns:
    • loadAsProperties

      public static Properties loadAsProperties(Resource resource)
      Load properties from file. Supports XML and key-value format.
      Parameters:
      resource -
      Returns:
    • getFileName

      public static String getFileName(String path)
      Gets the file name from given file path.
      Parameters:
      path -
      Returns:
    • getBaseName

      public static String getBaseName(String fileName)
      Remove file extension from file name.
      Parameters:
      fileName -
      Returns:
    • getBasePath

      public static String getBasePath(String filePath)
      Gets the base path of given file path removing any file name if present.
      Parameters:
      filePath -
      Returns:
    • copyToByteArray

      public static byte[] copyToByteArray(File file)
    • copyToByteArray

      public static byte[] copyToByteArray(Resource resource)
    • copyToByteArray

      public static byte[] copyToByteArray(InputStream inputStream)
    • constructContentType

      public static String constructContentType(String contentType, Charset charset)
      Construct content type information with given charset parameter.
      Parameters:
      contentType -
      charset -
      Returns:
    • getTestSource

      public static TestSource getTestSource(String sourceFile)
      Read String representation and construct proper test source instance. Extract source type from give file extension and try to set proper test source name from given file name.
      Parameters:
      sourceFile -
      Returns: