Class SystemReader


  • public abstract class SystemReader
    extends java.lang.Object
    Interface to read values from the system.

    When writing unit tests, extending this interface with a custom class permits to simulate an access to a system variable or property and permits to control the user's global configuration.

    • Constructor Detail

      • SystemReader

        public SystemReader()
    • Method Detail

      • getInstance

        public static SystemReader getInstance()
        Returns:
        the live instance to read system properties.
      • setInstance

        public static void setInstance​(SystemReader newReader)
        Parameters:
        newReader - the new instance to use when accessing properties, or null for the default instance.
      • setPlatformChecker

        protected final void setPlatformChecker()
        Should be used in tests when the platform is explicitly changed.
        Since:
        3.6
      • getHostname

        public abstract java.lang.String getHostname()
        Gets the hostname of the local host. If no hostname can be found, the hostname is set to the default value "localhost".
        Returns:
        the canonical hostname
      • getenv

        public abstract java.lang.String getenv​(java.lang.String variable)
        Parameters:
        variable - system variable to read
        Returns:
        value of the system variable
      • getProperty

        public abstract java.lang.String getProperty​(java.lang.String key)
        Parameters:
        key - of the system property to read
        Returns:
        value of the system property
      • openUserConfig

        public abstract FileBasedConfig openUserConfig​(Config parent,
                                                       FS fs)
        Parameters:
        parent - a config with values not found directly in the returned config
        fs - the file system abstraction which will be necessary to perform certain file system operations.
        Returns:
        the git configuration found in the user home
      • openSystemConfig

        public abstract FileBasedConfig openSystemConfig​(Config parent,
                                                         FS fs)
        Parameters:
        parent - a config with values not found directly in the returned config. Null is a reasonable value here.
        fs - the file system abstraction which will be necessary to perform certain file system operations.
        Returns:
        the gitonfig configuration found in the system-wide "etc" directory
      • getCurrentTime

        public abstract long getCurrentTime()
        Returns:
        the current system time
      • getTimezone

        public abstract int getTimezone​(long when)
        Parameters:
        when - TODO
        Returns:
        the local time zone
      • getTimeZone

        public java.util.TimeZone getTimeZone()
        Returns:
        system time zone, possibly mocked for testing
        Since:
        1.2
      • getLocale

        public java.util.Locale getLocale()
        Returns:
        the locale to use
        Since:
        1.2
      • getSimpleDateFormat

        public java.text.SimpleDateFormat getSimpleDateFormat​(java.lang.String pattern)
        Returns a simple date format instance as specified by the given pattern.
        Parameters:
        pattern - the pattern as defined in SimpleDateFormat(String)
        Returns:
        the simple date format
        Since:
        2.0
      • getSimpleDateFormat

        public java.text.SimpleDateFormat getSimpleDateFormat​(java.lang.String pattern,
                                                              java.util.Locale locale)
        Returns a simple date format instance as specified by the given pattern.
        Parameters:
        pattern - the pattern as defined in SimpleDateFormat(String)
        locale - locale to be used for the SimpleDateFormat
        Returns:
        the simple date format
        Since:
        3.2
      • getDateTimeInstance

        public java.text.DateFormat getDateTimeInstance​(int dateStyle,
                                                        int timeStyle)
        Returns a date/time format instance for the given styles.
        Parameters:
        dateStyle - the date style as specified in DateFormat.getDateTimeInstance(int, int)
        timeStyle - the time style as specified in DateFormat.getDateTimeInstance(int, int)
        Returns:
        the date format
        Since:
        2.0
      • isWindows

        public boolean isWindows()
        Returns:
        true if we are running on a Windows.
      • isMacOS

        public boolean isMacOS()
        Returns:
        true if we are running on Mac OS X
      • checkPath

        public void checkPath​(java.lang.String path)
                       throws CorruptObjectException
        Check tree path entry for validity.

        Scans a multi-directory path string such as "src/main.c".

        Parameters:
        path - path string to scan.
        Throws:
        CorruptObjectException - path is invalid.
        Since:
        3.6