From 07cb91a63aff68ff0253f389b0b1eafa991e4cb6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 25 Mar 2014 11:20:50 +0100 Subject: [PATCH] Improve org.sonar.api.utils.System2 : * add to picocontainer * new method isJavaAtLeast17() * fix Javadoc --- .../batch/bootstrap/BootstrapContainer.java | 4 +++- .../java/org/sonar/api/utils/System2.java | 19 +++++++++++++++---- .../java/org/sonar/api/utils/System2Test.java | 11 ++++++++++- .../server/platform/ServerComponents.java | 4 +++- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java index c6bd4a1f93e..704548f5991 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java @@ -26,6 +26,7 @@ import org.sonar.api.platform.ComponentContainer; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.utils.Durations; import org.sonar.api.utils.HttpDownloader; +import org.sonar.api.utils.System2; import org.sonar.api.utils.UriReader; import org.sonar.api.utils.internal.TempFolderCleaner; import org.sonar.batch.components.*; @@ -83,7 +84,8 @@ public class BootstrapContainer extends ComponentContainer { TempFolderCleaner.class, HttpDownloader.class, UriReader.class, - new FileCacheProvider()); + new FileCacheProvider(), + System2.INSTANCE); } private void addDatabaseComponents() { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/System2.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/System2.java index 44c90924939..bae39cb0974 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/System2.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/System2.java @@ -20,6 +20,8 @@ package org.sonar.api.utils; import org.apache.commons.lang.SystemUtils; +import org.sonar.api.BatchComponent; +import org.sonar.api.ServerComponent; import javax.annotation.CheckForNull; import java.util.Map; @@ -28,7 +30,7 @@ import java.util.Properties; /** * Proxy over {@link java.lang.System}. It aims to improve testability of classes * that interact with low-level system methods, for example : - * + *

*

  * public class MyClass {
  *   private final System2 system;
@@ -42,7 +44,7 @@ import java.util.Properties;
  *   }
  * }
  *
- * @Test
+ * {@literal @}Test
  * public void should_return_xxx() {
  *   // using Mockito
  *   System2 system = mock(System2.class);
@@ -51,13 +53,14 @@ import java.util.Properties;
  *   assertThat(new MyClass(system).xxx()).isEqualTo(now);
  * }
  * 
- * *

* Note that the name System2 was chosen to not conflict with {@link java.lang.System}. + *

+ * An instance is available in IoC container since 4.3. * * @since 4.2 */ -public class System2 { +public class System2 implements BatchComponent, ServerComponent { public static final System2 INSTANCE = new System2(); @@ -105,6 +108,14 @@ public class System2 { return SystemUtils.IS_OS_WINDOWS; } + /** + * True if Java 7 or Java 8 runtime environment + * @since 4.3 + */ + public boolean isJavaAtLeast17() { + return SystemUtils.isJavaVersionAtLeast(1.7f); + } + public void println(String obj) { System.out.print(obj); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/System2Test.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/System2Test.java index 6157218cf2e..d60c869c547 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/System2Test.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/System2Test.java @@ -51,7 +51,7 @@ public class System2Test { @Test public void testEnvVariables() throws Exception { - Map expected = System.getenv(); + Map expected = System.getenv(); assertThat(System2.INSTANCE.envVariables()).isNotNull().isEqualTo(expected); } @@ -72,6 +72,15 @@ public class System2Test { assertThat(System2.INSTANCE.isOsWindows()).isEqualTo(SystemUtils.IS_OS_WINDOWS); } + @Test + public void testIsJavaAtLeast17() throws Exception { + if (SystemUtils.IS_JAVA_1_6) { + assertThat(System2.INSTANCE.isJavaAtLeast17()).isFalse(); + } else { + assertThat(System2.INSTANCE.isJavaAtLeast17()).isTrue(); + } + } + @Test public void testPrintln() throws Exception { // well, how to assert that ? Adding a System3 dependency to System2 ? :-) diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index fb4f1ff16c4..31b4a0234d8 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -34,6 +34,7 @@ import org.sonar.api.rules.XMLRuleParser; import org.sonar.api.server.rule.RulesDefinitionXmlLoader; import org.sonar.api.utils.Durations; import org.sonar.api.utils.HttpDownloader; +import org.sonar.api.utils.System2; import org.sonar.api.utils.UriReader; import org.sonar.api.utils.internal.TempFolderCleaner; import org.sonar.core.component.SnapshotPerspectives; @@ -161,7 +162,8 @@ class ServerComponents { SemaphoreUpdater.class, SemaphoresImpl.class, TempFolderCleaner.class, - new TempFolderProvider() + new TempFolderProvider(), + System2.INSTANCE )); components.addAll(DatabaseMigrations.CLASSES); components.addAll(DaoUtils.getDaoClasses()); -- 2.39.5