001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 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; 023import org.sonar.api.BatchComponent; 024import org.sonar.api.ServerComponent; 025 026import javax.annotation.CheckForNull; 027import java.util.Map; 028import java.util.Properties; 029 030/** 031 * Proxy over {@link java.lang.System}. It aims to improve testability of classes 032 * that interact with low-level system methods, for example : 033 * <p/> 034 * <pre> 035 * public class MyClass { 036 * private final System2 system; 037 * 038 * public MyClass(System2 s) { 039 * this.system = s; 040 * } 041 * 042 * public long xxx() { 043 * return system.now(); 044 * } 045 * } 046 * 047 * {@literal @}Test 048 * public void should_return_xxx() { 049 * // using Mockito 050 * System2 system = mock(System2.class); 051 * long now = 123456789L; 052 * doReturn(now).when(system).now(); 053 * assertThat(new MyClass(system).xxx()).isEqualTo(now); 054 * } 055 * </pre> 056 * <p/> 057 * Note that the name System2 was chosen to not conflict with {@link java.lang.System}. 058 * <p/> 059 * An instance is available in IoC container since 4.3. 060 * 061 * @since 4.2 062 */ 063public class System2 implements BatchComponent, ServerComponent { 064 065 public static final System2 INSTANCE = new System2(); 066 067 /** 068 * Shortcut for {@link System#currentTimeMillis()} 069 */ 070 public long now() { 071 return System.currentTimeMillis(); 072 } 073 074 /** 075 * Shortcut for {@link System#getProperties()} 076 */ 077 public Properties properties() { 078 return System.getProperties(); 079 } 080 081 /** 082 * Shortcut for {@link System#getProperty(String)} 083 */ 084 @CheckForNull 085 public String property(String key) { 086 return System.getProperty(key); 087 } 088 089 /** 090 * Shortcut for {@link System#getenv()} 091 */ 092 public Map<String, String> envVariables() { 093 return System.getenv(); 094 } 095 096 /** 097 * Shortcut for {@link System#getenv(String)} 098 */ 099 @CheckForNull 100 public String envVariable(String key) { 101 return System.getenv(key); 102 } 103 104 /** 105 * True if this is MS Windows. 106 */ 107 public boolean isOsWindows() { 108 return SystemUtils.IS_OS_WINDOWS; 109 } 110 111 /** 112 * True if Java 7 or Java 8 runtime environment 113 * @since 4.3 114 */ 115 public boolean isJavaAtLeast17() { 116 return SystemUtils.isJavaVersionAtLeast(1.7f); 117 } 118 119 public void println(String obj) { 120 System.out.print(obj); 121 } 122}