Browse Source

Improve org.sonar.api.utils.System2 :

* add to picocontainer
* new method isJavaAtLeast17()
* fix Javadoc
tags/4.3
Simon Brandhof 10 years ago
parent
commit
07cb91a63a

+ 3
- 1
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java View File

@@ -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() {

+ 15
- 4
sonar-plugin-api/src/main/java/org/sonar/api/utils/System2.java View File

@@ -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 :
*
* <p/>
* <pre>
* 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);
* }
* </pre>
*
* <p/>
* Note that the name System2 was chosen to not conflict with {@link java.lang.System}.
* <p/>
* 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);
}

+ 10
- 1
sonar-plugin-api/src/test/java/org/sonar/api/utils/System2Test.java View File

@@ -51,7 +51,7 @@ public class System2Test {

@Test
public void testEnvVariables() throws Exception {
Map<String,String> expected = System.getenv();
Map<String, String> 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 ? :-)

+ 3
- 1
sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java View File

@@ -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());

Loading…
Cancel
Save