From 824a1c63f89e7e611f15d007faf55f2e0d0a8558 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 15 Mar 2016 10:14:45 +0100 Subject: SONAR-7458 new component org.sonar.api.SonarQubeVersion --- .../core/platform/SonarQubeVersionProvider.java | 49 +++++++++++++++++ .../platform/SonarQubeVersionProviderTest.java | 64 ++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 sonar-core/src/main/java/org/sonar/core/platform/SonarQubeVersionProvider.java create mode 100644 sonar-core/src/test/java/org/sonar/core/platform/SonarQubeVersionProviderTest.java (limited to 'sonar-core') 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); + } +} -- cgit v1.2.3