aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-03-15 10:14:45 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-03-16 19:42:59 +0100
commit824a1c63f89e7e611f15d007faf55f2e0d0a8558 (patch)
treecd524c642c3acfcd7a06e70c0c7f555a68de50d5 /sonar-core
parent13f9918a47199fdbd35261e49e6d15b5c8bfdc36 (diff)
downloadsonarqube-824a1c63f89e7e611f15d007faf55f2e0d0a8558.tar.gz
sonarqube-824a1c63f89e7e611f15d007faf55f2e0d0a8558.zip
SONAR-7458 new component org.sonar.api.SonarQubeVersion
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/platform/SonarQubeVersionProvider.java49
-rw-r--r--sonar-core/src/test/java/org/sonar/core/platform/SonarQubeVersionProviderTest.java64
2 files changed, 113 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/platform/SonarQubeVersionProvider.java b/sonar-core/src/main/java/org/sonar/core/platform/SonarQubeVersionProvider.java
new file mode 100644
index 00000000000..a0f54de7490
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/platform/SonarQubeVersionProvider.java
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.platform;
+
+import com.google.common.io.Resources;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import org.picocontainer.injectors.ProviderAdapter;
+import org.sonar.api.SonarQubeVersion;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.Version;
+
+public class SonarQubeVersionProvider extends ProviderAdapter {
+
+ private static final String FILE_PATH = "/sq-version.txt";
+
+ private SonarQubeVersion version = null;
+
+ public SonarQubeVersion provide(System2 system) {
+ if (version == null) {
+ try {
+ URL url = system.getResource(FILE_PATH);
+ String versionInFile = Resources.toString(url, StandardCharsets.UTF_8);
+ version = new SonarQubeVersion(Version.parse(versionInFile));
+ } catch (IOException e) {
+ throw new IllegalStateException("Can not load " + FILE_PATH + " from classpath", e);
+ }
+ }
+ return version;
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/platform/SonarQubeVersionProviderTest.java b/sonar-core/src/test/java/org/sonar/core/platform/SonarQubeVersionProviderTest.java
new file mode 100644
index 00000000000..79d9baddc27
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/platform/SonarQubeVersionProviderTest.java
@@ -0,0 +1,64 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.platform;
+
+import java.io.File;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.SonarQubeVersion;
+import org.sonar.api.utils.System2;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SonarQubeVersionProviderTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ SonarQubeVersionProvider underTest = new SonarQubeVersionProvider();
+
+ @Test
+ public void create() {
+ SonarQubeVersion version = underTest.provide(System2.INSTANCE);
+ assertThat(version).isNotNull();
+ assertThat(version.get().major()).isGreaterThanOrEqualTo(5);
+ }
+
+ @Test
+ public void cache_version() {
+ SonarQubeVersion version1 = underTest.provide(System2.INSTANCE);
+ SonarQubeVersion version2 = underTest.provide(System2.INSTANCE);
+ assertThat(version1).isSameAs(version2);
+ }
+
+ @Test
+ public void throw_ISE_if_fail_to_load_version() throws Exception {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Can not load /sq-version.txt from classpath");
+
+ System2 system = mock(System2.class);
+ when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURL());
+ underTest.provide(system);
+ }
+}