Package org.sonar.api.utils
Class System2
- java.lang.Object
-
- org.sonar.api.utils.System2
-
@ScannerSide @ServerSide @ComputeEngineSide public class System2 extends java.lang.Object
Proxy overSystem
. 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 withSystem
.
An instance is available in IoC container since 4.3. Since 6.4 you can also injectClock
instead ofSystem2
if you are only interested by date/time operations- Since:
- 4.2
-
-
Constructor Summary
Constructors Constructor Description System2()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close(java.lang.AutoCloseable closeable)
Closes the object and throws anIllegalStateException
on error.java.lang.String
envVariable(java.lang.String key)
Shortcut forSystem.getenv(String)
java.util.Map<java.lang.String,java.lang.String>
envVariables()
Shortcut forSystem.getenv()
java.util.TimeZone
getDefaultTimeZone()
java.net.URL
getResource(java.lang.String name)
boolean
isOsMac()
True if this is Mac system.boolean
isOsWindows()
True if this is MS Windows.long
now()
void
println(java.lang.String obj)
java.util.Properties
properties()
Shortcut forSystem.getProperties()
java.lang.String
property(java.lang.String key)
Shortcut forSystem.getProperty(String)
System2
setProperty(java.lang.String key, java.lang.String value)
Shortcut forSystem{@link #setProperty(String, String)}
-
-
-
Field Detail
-
INSTANCE
public static final System2 INSTANCE
-
-
Method Detail
-
now
public long now()
-
properties
public java.util.Properties properties()
Shortcut forSystem.getProperties()
-
property
@CheckForNull public java.lang.String property(java.lang.String key)
Shortcut forSystem.getProperty(String)
-
setProperty
public System2 setProperty(java.lang.String key, java.lang.String value)
Shortcut forSystem{@link #setProperty(String, String)}
- Since:
- 6.4
-
envVariables
public java.util.Map<java.lang.String,java.lang.String> envVariables()
Shortcut forSystem.getenv()
-
envVariable
@CheckForNull public java.lang.String envVariable(java.lang.String key)
Shortcut forSystem.getenv(String)
-
isOsWindows
public boolean isOsWindows()
True if this is MS Windows.
-
isOsMac
public boolean isOsMac()
True if this is Mac system.
-
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 anIllegalStateException
on error.- Since:
- 5.1
-
-