Class System2

  • Direct Known Subclasses:
    AlwaysIncreasingSystem2, TestSystem2

    @ScannerSide
    @ServerSide
    @ComputeEngineSide
    public class System2
    extends java.lang.Object
    Proxy over System. It aims to improve testability of classes that interact with low-level system methods, for example :
     public class MyClass {
       private final System2 system;
    
       public MyClass(System2 s) {
         this.system = s;
       }
    
       public long xxx() {
         return system.now();
       }
     }
    
     @Test
     public void should_return_xxx() {
       // using Mockito
       System2 system = mock(System2.class);
       long now = 123456789L;
       doReturn(now).when(system).now();
       assertThat(new MyClass(system).xxx()).isEqualTo(now);
     }
     

    Note that the name System2 was chosen to not conflict with System.
    An instance is available in IoC container since 4.3. Since 6.4 you can also inject Clock instead of System2 if you are only interested by date/time operations
    Since:
    4.2
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static System2 INSTANCE  
    • Constructor Summary

      Constructors 
      Constructor Description
      System2()  
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void close​(java.lang.AutoCloseable closeable)
      Closes the object and throws an IllegalStateException on error.
      java.lang.String envVariable​(java.lang.String key)
      Shortcut for System.getenv(String)
      java.util.Map<java.lang.String,​java.lang.String> envVariables()
      Shortcut for System.getenv()
      java.util.TimeZone getDefaultTimeZone()  
      java.net.URL getResource​(java.lang.String name)  
      boolean isJavaAtLeast17()
      Deprecated.
      in 6.4.
      boolean isOsWindows()
      True if this is MS Windows.
      long now()
      Shortcut for System.currentTimeMillis() Since 6.4 you can also inject Clock instead of System2
      void println​(java.lang.String obj)  
      java.util.Properties properties()
      Shortcut for System.getProperties()
      java.lang.String property​(java.lang.String key)
      Shortcut for System.getProperty(String)
      System2 setProperty​(java.lang.String key, java.lang.String value)
      Shortcut for System{@link #setProperty(String, String)}
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INSTANCE

        public static final System2 INSTANCE
    • Constructor Detail

      • System2

        public System2()
    • Method Detail

      • now

        public long now()
        Shortcut for System.currentTimeMillis() Since 6.4 you can also inject Clock instead of System2
      • properties

        public java.util.Properties properties()
        Shortcut for System.getProperties()
      • property

        @CheckForNull
        public java.lang.String property​(java.lang.String key)
        Shortcut for System.getProperty(String)
      • setProperty

        public System2 setProperty​(java.lang.String key,
                                   java.lang.String value)
        Shortcut for System{@link #setProperty(String, String)}
        Since:
        6.4
      • envVariables

        public java.util.Map<java.lang.String,​java.lang.String> envVariables()
        Shortcut for System.getenv()
      • envVariable

        @CheckForNull
        public java.lang.String envVariable​(java.lang.String key)
        Shortcut for System.getenv(String)
      • isOsWindows

        public boolean isOsWindows()
        True if this is MS Windows.
      • isJavaAtLeast17

        @Deprecated
        public boolean isJavaAtLeast17()
        Deprecated.
        in 6.4. Java 8+ is required, so this method always returns true.
        True if Java 7 or Java 8 runtime environment
        Since:
        4.3
      • println

        public void println​(java.lang.String obj)
      • getDefaultTimeZone

        public java.util.TimeZone getDefaultTimeZone()
        Returns:
        the JVM's default time zone
        Since:
        5.1
      • getResource

        public java.net.URL getResource​(java.lang.String name)
        Since:
        5.5
        See Also:
        Class.getResource(String)
      • close

        public void close​(java.lang.AutoCloseable closeable)
        Closes the object and throws an IllegalStateException on error.
        Since:
        5.1