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