001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2013 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * SonarQube is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.utils; 021 022import org.apache.commons.lang.SystemUtils; 023 024import javax.annotation.CheckForNull; 025import java.util.Map; 026import java.util.Properties; 027 028/** 029 * Proxy over {@link java.lang.System}. It aims to improve testability of classes 030 * that interact with low-level system methods, for example : 031 * 032 * <pre> 033 * public class MyClass { 034 * private final System2 system; 035 * 036 * public MyClass(System2 s) { 037 * this.system = s; 038 * } 039 * 040 * public long xxx() { 041 * return system.now(); 042 * } 043 * } 044 * 045 * @Test 046 * public void should_return_xxx() { 047 * // using Mockito 048 * System2 system = mock(System2.class); 049 * long now = 123456789L; 050 * doReturn(now).when(system).now(); 051 * assertThat(new MyClass(system).xxx()).isEqualTo(now); 052 * } 053 * </pre> 054 * 055 * <p/> 056 * Note that the name System2 was chosen to not conflict with {@link java.lang.System}. 057 * 058 * @since 4.2 059 */ 060public class System2 { 061 062 public static final System2 INSTANCE = new System2(); 063 064 /** 065 * Shortcut for {@link System#currentTimeMillis()} 066 */ 067 public long now() { 068 return System.currentTimeMillis(); 069 } 070 071 /** 072 * Shortcut for {@link System#getProperties()} 073 */ 074 public Properties properties() { 075 return System.getProperties(); 076 } 077 078 /** 079 * Shortcut for {@link System#getProperty(String)} 080 */ 081 @CheckForNull 082 public String property(String key) { 083 return System.getProperty(key); 084 } 085 086 /** 087 * Shortcut for {@link System#getenv()} 088 */ 089 public Map<String, String> envVariables() { 090 return System.getenv(); 091 } 092 093 /** 094 * Shortcut for {@link System#getenv(String)} 095 */ 096 @CheckForNull 097 public String envVariable(String key) { 098 return System.getenv(key); 099 } 100 101 /** 102 * True if this is MS Windows. 103 */ 104 public boolean isOsWindows() { 105 return SystemUtils.IS_OS_WINDOWS; 106 } 107 108 public void println(String obj) { 109 System.out.print(obj); 110 } 111}