001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program 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 * This program 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.batch.BatchSide; 024import org.sonar.api.server.ServerSide; 025 026import javax.annotation.CheckForNull; 027 028import java.util.Date; 029import java.util.Map; 030import java.util.Properties; 031import java.util.TimeZone; 032 033/** 034 * Proxy over {@link java.lang.System}. It aims to improve testability of classes 035 * that interact with low-level system methods, for example : 036 * <p/> 037 * <pre> 038 * public class MyClass { 039 * private final System2 system; 040 * 041 * public MyClass(System2 s) { 042 * this.system = s; 043 * } 044 * 045 * public long xxx() { 046 * return system.now(); 047 * } 048 * } 049 * 050 * {@literal @}Test 051 * public void should_return_xxx() { 052 * // using Mockito 053 * System2 system = mock(System2.class); 054 * long now = 123456789L; 055 * doReturn(now).when(system).now(); 056 * assertThat(new MyClass(system).xxx()).isEqualTo(now); 057 * } 058 * </pre> 059 * <p/> 060 * Note that the name System2 was chosen to not conflict with {@link java.lang.System}. 061 * <p/> 062 * An instance is available in IoC container since 4.3. 063 * 064 * @since 4.2 065 */ 066@BatchSide 067@ServerSide 068public class System2 { 069 070 public static final System2 INSTANCE = new System2(); 071 072 /** 073 * Shortcut for {@link System#currentTimeMillis()} 074 */ 075 public long now() { 076 return System.currentTimeMillis(); 077 } 078 079 /** 080 * Shortcut for {@link System#getProperties()} 081 */ 082 public Properties properties() { 083 return System.getProperties(); 084 } 085 086 /** 087 * Shortcut for {@link System#getProperty(String)} 088 */ 089 @CheckForNull 090 public String property(String key) { 091 return System.getProperty(key); 092 } 093 094 /** 095 * Shortcut for {@link System#getenv()} 096 */ 097 public Map<String, String> envVariables() { 098 return System.getenv(); 099 } 100 101 /** 102 * Shortcut for {@link System#getenv(String)} 103 */ 104 @CheckForNull 105 public String envVariable(String key) { 106 return System.getenv(key); 107 } 108 109 /** 110 * True if this is MS Windows. 111 */ 112 public boolean isOsWindows() { 113 return SystemUtils.IS_OS_WINDOWS; 114 } 115 116 /** 117 * True if Java 7 or Java 8 runtime environment 118 * @since 4.3 119 */ 120 public boolean isJavaAtLeast17() { 121 return SystemUtils.isJavaVersionAtLeast(1.7f); 122 } 123 124 public void println(String obj) { 125 System.out.print(obj); 126 } 127 128 /** 129 * @deprecated in 5.2. Please use {@link #now()} 130 */ 131 @Deprecated 132 public Date newDate() { 133 return new Date(); 134 } 135 136 /** 137 * @since 5.1 138 * @return the JVM's default time zone 139 */ 140 public TimeZone getDefaultTimeZone() { 141 return TimeZone.getDefault(); 142 } 143 144 /** 145 * Closes the object and throws an {@link java.lang.IllegalStateException} on error. 146 * @since 5.1 147 */ 148 public void close(AutoCloseable closeable) { 149 try { 150 closeable.close(); 151 } catch (Exception e) { 152 throw new IllegalStateException("Fail to close " + closeable, e); 153 } 154 } 155}