org.sonar.api.utils
Class System2

java.lang.Object
  extended by org.sonar.api.utils.System2
All Implemented Interfaces:
BatchComponent, ServerComponent

public class System2
extends Object
implements BatchComponent, ServerComponent

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:
4.2

Field Summary
static System2 INSTANCE
           
 
Constructor Summary
System2()
           
 
Method Summary
 String envVariable(String key)
          Shortcut for System.getenv(String)
 Map<String,String> envVariables()
          Shortcut for System.getenv()
 boolean isJavaAtLeast17()
          True if Java 7 or Java 8 runtime environment
 boolean isOsWindows()
          True if this is MS Windows.
 long now()
          Shortcut for System.currentTimeMillis()
 void println(String obj)
           
 Properties properties()
          Shortcut for System.getProperties()
 String property(String key)
          Shortcut for System.getProperty(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()


properties

public Properties properties()
Shortcut for System.getProperties()


property

@CheckForNull
public String property(String key)
Shortcut for System.getProperty(String)


envVariables

public Map<String,String> envVariables()
Shortcut for System.getenv()


envVariable

@CheckForNull
public String envVariable(String key)
Shortcut for System.getenv(String)


isOsWindows

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


isJavaAtLeast17

public boolean isJavaAtLeast17()
True if Java 7 or Java 8 runtime environment

Since:
4.3

println

public void println(String obj)


Copyright © 2009–2014 SonarSource. All rights reserved.