ItUtils.assertIssuesInJsonReport(result, 3, 0, 17);
}
- @Test
- public void non_associated_mode() throws IOException {
- restoreProfile("one-issue-per-line.xml");
- setDefaultQualityProfile("xoo", "one-issue-per-line");
- SonarRunner runner = configureRunnerIssues("shared/xoo-sample-non-associated", null);
- BuildResult result = orchestrator.executeBuild(runner);
-
- assertThat(result.getLogs()).contains("Local analysis");
- assertThat(result.getLogs()).contains("Cache not found, synchronizing data");
- assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(17);
-
- result = orchestrator.executeBuild(runner);
- assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(17);
- assertThat(result.getLogs()).contains("Found cache");
- }
-
// SONAR-5715
@Test
public void test_issues_mode_on_project_with_space_in_filename() throws IOException {
assertThat(ItUtils.countIssuesInJsonReport(result, false)).isEqualTo(16);
}
- // SONAR-7100
- @Test
- public void enable_issues_cache() throws Exception {
- File homeDir = runFirstAnalysisAndFlagIssueAsWontFix();
-
- // Second issues mode using same cache dir and enable cache
- SonarRunner runner = configureRunnerIssues("shared/xoo-sample", homeDir, "sonar.useWsCache", "true");
- BuildResult result = orchestrator.executeBuild(runner);
-
- // False positive is still visible since we are using cached issues
- assertThat(ItUtils.countIssuesInJsonReport(result, false)).isEqualTo(17);
- }
-
private File runFirstAnalysisAndFlagIssueAsWontFix() throws IOException {
restoreProfile("one-issue-per-line.xml");
orchestrator.getServer().provisionProject("sample", "xoo-sample");
+++ /dev/null
-/*
- * 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.home.cache;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.DirectoryStream;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardCopyOption;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-
-import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
-
-import static java.nio.file.StandardOpenOption.CREATE;
-import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
-import static java.nio.file.StandardOpenOption.WRITE;
-
-public class PersistentCache {
- private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
- private static final Charset ENCODING = StandardCharsets.UTF_8;
- private static final String DIGEST_ALGO = "MD5";
-
- private final PersistentCacheInvalidation invalidation;
- private final Logger logger;
- private final Path dir;
- private DirectoryLock lock;
-
- public PersistentCache(Path dir, PersistentCacheInvalidation invalidation, Logger logger, DirectoryLock lock) {
- this.dir = dir;
- this.invalidation = invalidation;
- this.logger = logger;
- this.lock = lock;
-
- reconfigure();
- logger.debug("cache: " + dir);
- }
-
- public synchronized void reconfigure() {
- try {
- Files.createDirectories(dir);
- } catch (IOException e) {
- throw new IllegalStateException("failed to create cache dir", e);
- }
- }
-
- public Path getDirectory() {
- return dir;
- }
-
- @CheckForNull
- public synchronized String getString(@Nonnull String obj) throws IOException {
- byte[] cached = get(obj);
-
- if (cached == null) {
- return null;
- }
-
- return new String(cached, ENCODING);
- }
-
- @CheckForNull
- public synchronized InputStream getStream(@Nonnull String obj) throws IOException {
- String key = getKey(obj);
-
- try {
- lock();
- Path path = getCacheCopy(key);
- if (path == null) {
- return null;
- }
- return new DeleteFileOnCloseInputStream(new FileInputStream(path.toFile()), path);
-
- } finally {
- unlock();
- }
- }
-
- @CheckForNull
- public synchronized byte[] get(@Nonnull String obj) throws IOException {
- String key = getKey(obj);
-
- try {
- lock();
-
- byte[] cached = getCache(key);
-
- if (cached != null) {
- logger.debug("cache hit for " + obj + " -> " + key);
- return cached;
- }
-
- logger.debug("cache miss for " + obj + " -> " + key);
- } finally {
- unlock();
- }
-
- return null;
- }
-
- public synchronized void put(@Nonnull String obj, @Nonnull InputStream stream) throws IOException {
- String key = getKey(obj);
- try {
- lock();
- putCache(key, stream);
- } finally {
- unlock();
- }
- }
-
- public synchronized void put(@Nonnull String obj, @Nonnull byte[] value) throws IOException {
- String key = getKey(obj);
- try {
- lock();
- putCache(key, value);
- } finally {
- unlock();
- }
- }
-
- /**
- * Deletes all cache entries
- */
- public synchronized void clear() {
- logger.info("cache: clearing");
- try {
- lock();
- deleteCacheEntries(new DirectoryClearFilter());
- } catch (IOException e) {
- logger.error("Error clearing cache", e);
- } finally {
- unlock();
- }
- }
-
- /**
- * Deletes cache entries that are no longer valid according to the default expiration time period.
- */
- public synchronized void clean() {
- logger.info("cache: cleaning");
- try {
- lock();
- deleteCacheEntries(new DirectoryCleanFilter());
- } catch (IOException e) {
- logger.error("Error cleaning cache", e);
- } finally {
- unlock();
- }
- }
-
- private void lock() throws IOException {
- lock.lock();
- }
-
- private void unlock() {
- lock.unlock();
- }
-
- private static String getKey(String uri) {
- try {
- String key = uri;
- MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGO);
- digest.update(key.getBytes(StandardCharsets.UTF_8));
- return byteArrayToHex(digest.digest());
- } catch (NoSuchAlgorithmException e) {
- throw new IllegalStateException("Couldn't create hash", e);
- }
- }
-
- private void deleteCacheEntries(DirectoryStream.Filter<Path> filter) throws IOException {
- try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
- for (Path p : stream) {
- try {
- Files.delete(p);
- } catch (Exception e) {
- logger.error("Error deleting " + p, e);
- }
- }
- }
- }
-
- private class DirectoryClearFilter implements DirectoryStream.Filter<Path> {
- @Override
- public boolean accept(Path entry) throws IOException {
- return !lock.getFileLockName().equals(entry.getFileName().toString());
- }
- }
-
- private class DirectoryCleanFilter implements DirectoryStream.Filter<Path> {
- @Override
- public boolean accept(Path entry) throws IOException {
- if (lock.getFileLockName().equals(entry.getFileName().toString())) {
- return false;
- }
-
- return invalidation.test(entry);
- }
- }
-
- private void putCache(String key, byte[] value) throws IOException {
- Path cachePath = getCacheEntryPath(key);
- Files.write(cachePath, value, CREATE, WRITE, TRUNCATE_EXISTING);
- }
-
- private void putCache(String key, InputStream stream) throws IOException {
- Path cachePath = getCacheEntryPath(key);
- Files.copy(stream, cachePath, StandardCopyOption.REPLACE_EXISTING);
- }
-
- private byte[] getCache(String key) throws IOException {
- Path cachePath = getCacheEntryPath(key);
-
- if (!validateCacheEntry(cachePath)) {
- return null;
- }
-
- return Files.readAllBytes(cachePath);
- }
-
- private Path getCacheCopy(String key) throws IOException {
- Path cachePath = getCacheEntryPath(key);
-
- if (!validateCacheEntry(cachePath)) {
- return null;
- }
-
- Path temp = Files.createTempFile("sonar_cache", null);
- Files.copy(cachePath, temp, StandardCopyOption.REPLACE_EXISTING);
- return temp;
- }
-
- private boolean validateCacheEntry(Path cacheEntryPath) throws IOException {
- if (!Files.exists(cacheEntryPath)) {
- return false;
- }
-
- if (invalidation.test(cacheEntryPath)) {
- logger.debug("cache: evicting entry");
- Files.delete(cacheEntryPath);
- return false;
- }
-
- return true;
- }
-
- private Path getCacheEntryPath(String key) {
- return dir.resolve(key);
- }
-
- public static String byteArrayToHex(byte[] bytes) {
- char[] hexChars = new char[bytes.length * 2];
- for (int j = 0; j < bytes.length; j++) {
- int v = bytes[j] & 0xFF;
- hexChars[j * 2] = hexArray[v >>> 4];
- hexChars[j * 2 + 1] = hexArray[v & 0x0F];
- }
- return new String(hexChars);
- }
-}
+++ /dev/null
-/*
- * 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.home.cache;
-
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.concurrent.TimeUnit;
-
-import javax.annotation.Nullable;
-
-/**
- * Cache files will be placed in 3 areas:
- * <pre>
- * <sonar_home>/ws_cache/<server_url>-<version>/projects/<project>/
- * <sonar_home>/ws_cache/<server_url>-<version>/global/
- * <sonar_home>/ws_cache/<server_url>-<version>/local/
- * </pre>
- */
-public class PersistentCacheBuilder {
- private static final long DEFAULT_EXPIRE_DURATION = TimeUnit.MILLISECONDS.convert(9999L, TimeUnit.DAYS);
- private static final String DIR_NAME = "ws_cache";
-
- private Path cacheBasePath;
- private Path relativePath;
- private final Logger logger;
-
- public PersistentCacheBuilder(Logger logger) {
- this.logger = logger;
- }
-
- public PersistentCacheBuilder setAreaForProject(String serverUrl, String serverVersion, String projectKey) {
- relativePath = Paths.get(sanitizeFilename(serverUrl))
- .resolve(sanitizeFilename(serverVersion))
- .resolve("projects")
- .resolve(sanitizeFilename(projectKey));
- return this;
- }
-
- public PersistentCacheBuilder setAreaForGlobal(String serverUrl) {
- relativePath = Paths.get(sanitizeFilename(serverUrl))
- .resolve("global");
- return this;
- }
-
- public PersistentCacheBuilder setAreaForLocalProject(String serverUrl, String serverVersion) {
- relativePath = Paths.get(sanitizeFilename(serverUrl))
- .resolve(sanitizeFilename(serverVersion))
- .resolve("local");
- return this;
- }
-
- public PersistentCacheBuilder setSonarHome(@Nullable Path p) {
- if (p != null) {
- this.cacheBasePath = p.resolve(DIR_NAME);
- }
- return this;
- }
-
- public PersistentCache build() {
- if (relativePath == null) {
- throw new IllegalStateException("area must be set before building");
- }
- if (cacheBasePath == null) {
- setSonarHome(findHome());
- }
- Path cachePath = cacheBasePath.resolve(relativePath);
- DirectoryLock lock = new DirectoryLock(cacheBasePath, logger);
- PersistentCacheInvalidation criteria = new TTLCacheInvalidation(DEFAULT_EXPIRE_DURATION);
- return new PersistentCache(cachePath, criteria, logger, lock);
- }
-
- private static Path findHome() {
- String home = System.getenv("SONAR_USER_HOME");
-
- if (home != null) {
- return Paths.get(home);
- }
-
- home = System.getProperty("user.home");
- return Paths.get(home, ".sonar");
- }
-
- private static String sanitizeFilename(String name) {
- try {
- return URLEncoder.encode(name, StandardCharsets.UTF_8.name());
- } catch (UnsupportedEncodingException e) {
- throw new IllegalStateException("Couldn't sanitize filename: " + name, e);
- }
- }
-}
+++ /dev/null
-/*
- * 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.home.cache;
-
-import java.io.IOException;
-import java.nio.file.Path;
-
-public interface PersistentCacheInvalidation {
- boolean test(Path cacheEntryPath) throws IOException;
-}
+++ /dev/null
-/*
- * 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.home.cache;
-
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.attribute.BasicFileAttributes;
-
-public class TTLCacheInvalidation implements PersistentCacheInvalidation {
- private final long durationToExpireMs;
-
- public TTLCacheInvalidation(long durationToExpireMs) {
- this.durationToExpireMs = durationToExpireMs;
- }
-
- @Override
- public boolean test(Path cacheEntryPath) throws IOException {
- BasicFileAttributes attr = Files.readAttributes(cacheEntryPath, BasicFileAttributes.class);
- long modTime = attr.lastModifiedTime().toMillis();
-
- long age = System.currentTimeMillis() - modTime;
-
- if (age > durationToExpireMs) {
- return true;
- }
-
- return false;
- }
-
-}
+++ /dev/null
-/*
- * 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.home.cache;
-
-import java.nio.file.Files;
-import java.nio.file.Paths;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assume.assumeTrue;
-import static org.mockito.Mockito.mock;
-
-public class PersistentCacheBuilderTest {
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Test
- public void user_home_property_can_be_null() {
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(null).setAreaForGlobal("url").build();
- assertTrue(Files.isDirectory(cache.getDirectory()));
- assertThat(cache.getDirectory()).endsWith(Paths.get("url", "global"));
- }
-
- @Test
- public void set_user_home() {
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(temp.getRoot().toPath()).setAreaForGlobal("url").build();
-
- assertThat(cache.getDirectory()).isDirectory();
- assertThat(cache.getDirectory()).startsWith(temp.getRoot().toPath());
- assertTrue(Files.isDirectory(cache.getDirectory()));
- }
-
- @Test
- public void read_system_env() {
- assumeTrue(System.getenv("SONAR_USER_HOME") == null);
-
- System.setProperty("user.home", temp.getRoot().getAbsolutePath());
-
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForGlobal("url").build();
- assertTrue(Files.isDirectory(cache.getDirectory()));
- assertThat(cache.getDirectory()).startsWith(temp.getRoot().toPath());
- }
-
- @Test
- public void directories() {
- System.setProperty("user.home", temp.getRoot().getAbsolutePath());
-
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForProject("url", "0", "proj").build();
- assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url", "0", "projects", "proj"));
-
- cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForLocalProject("url", "0").build();
- assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url", "0", "local"));
-
- cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForGlobal("url").build();
- assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url", "global"));
- }
-}
+++ /dev/null
-/*
- * 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.home.cache;
-
-import org.apache.commons.io.IOUtils;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
-
-import static org.mockito.Matchers.any;
-
-import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.atLeast;
-import static org.mockito.Mockito.verify;
-import org.apache.commons.io.FileUtils;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-
-public class PersistentCacheTest {
- private final static String URI = "key1";
- private final static String VALUE = "cache content";
- private PersistentCache cache = null;
- private DirectoryLock lock = null;
- private PersistentCacheInvalidation invalidation = null;
-
- @Rule
- public TemporaryFolder tmp = new TemporaryFolder();
-
- @Before
- public void setUp() throws IOException {
- invalidation = mock(PersistentCacheInvalidation.class);
- when(invalidation.test(any(Path.class))).thenReturn(false);
- lock = mock(DirectoryLock.class);
- when(lock.getFileLockName()).thenReturn("lock");
- cache = new PersistentCache(tmp.getRoot().toPath(), invalidation, mock(Logger.class), lock);
- }
-
- @Test
- public void testCacheMiss() throws Exception {
- assertCacheHit(false);
- }
-
- @Test
- public void testClean() throws Exception {
- Path lockFile = cache.getDirectory().resolve("lock");
- // puts entry
- cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
- Files.write(lockFile, "test".getBytes(StandardCharsets.UTF_8));
- assertCacheHit(true);
- when(invalidation.test(any(Path.class))).thenReturn(true);
- cache.clean();
- when(invalidation.test(any(Path.class))).thenReturn(false);
- assertCacheHit(false);
- // lock file should not get deleted
- assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test");
- }
-
- @Test
- public void testStream() throws IOException {
- cache.put("id", "test".getBytes());
- InputStream stream = cache.getStream("id");
- assertThat(IOUtils.toString(stream)).isEqualTo("test");
-
- assertThat(cache.getStream("non existing")).isNull();
- }
-
- @Test
- public void testClear() throws Exception {
- Path lockFile = cache.getDirectory().resolve("lock");
- cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
- Files.write(lockFile, "test".getBytes(StandardCharsets.UTF_8));
- assertCacheHit(true);
- cache.clear();
- assertCacheHit(false);
- // lock file should not get deleted
- assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test");
- }
-
- @Test
- public void testCacheHit() throws Exception {
- cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
- assertCacheHit(true);
- }
-
- @Test
- public void testReconfigure() throws Exception {
- assertCacheHit(false);
- cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
- assertCacheHit(true);
-
- File root = tmp.getRoot();
- FileUtils.deleteQuietly(root);
-
- // should re-create cache directory and start using the cache
- cache.reconfigure();
- assertThat(root).exists();
-
- assertCacheHit(false);
- cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
- assertCacheHit(true);
- }
-
- @Test
- public void testExpiration() throws Exception {
- when(invalidation.test(any(Path.class))).thenReturn(true);
- cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
- assertCacheHit(false);
- }
-
- private void assertCacheHit(boolean hit) throws Exception {
- assertCacheHit(cache, hit);
- }
-
- private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception {
- String expected = hit ? VALUE : null;
- assertThat(pCache.getString(URI)).isEqualTo(expected);
- verify(lock, atLeast(1)).unlock();
- }
-
-}
+++ /dev/null
-/*
- * 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.home.cache;
-
-import org.junit.Before;
-
-import java.io.IOException;
-import java.nio.file.Path;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import org.junit.Test;
-import org.junit.Rule;
-import org.junit.rules.TemporaryFolder;
-
-public class TTLCacheInvalidationTest {
- private Path testFile;
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Before
- public void setUp() throws IOException {
- testFile = temp.newFile().toPath();
- }
-
- @Test
- public void testExpired() throws IOException {
- TTLCacheInvalidation invalidation = new TTLCacheInvalidation(-100);
- assertThat(invalidation.test(testFile)).isEqualTo(true);
- }
-
- @Test
- public void testValid() throws IOException {
- TTLCacheInvalidation invalidation = new TTLCacheInvalidation(100_000);
- assertThat(invalidation.test(testFile)).isEqualTo(false);
- }
-}
+++ /dev/null
-/*
- * 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.batch.analysis;
-
-import org.picocontainer.injectors.ProviderAdapter;
-import org.sonar.api.batch.AnalysisMode;
-import org.sonar.batch.bootstrap.BatchWsClient;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
-import org.sonar.home.cache.PersistentCache;
-
-public class AnalysisWSLoaderProvider extends ProviderAdapter {
- static final String SONAR_USE_WS_CACHE = "sonar.useWsCache";
- private WSLoader wsLoader;
-
- public WSLoader provide(AnalysisMode mode, PersistentCache cache, BatchWsClient client, AnalysisProperties props) {
- if (wsLoader == null) {
- // recreate cache directory if needed for this analysis
- cache.reconfigure();
- wsLoader = new WSLoader(getStrategy(mode, props), cache, client);
- }
- return wsLoader;
- }
-
- private static LoadStrategy getStrategy(AnalysisMode mode, AnalysisProperties props) {
- if (mode.isIssues() && "true".equals(props.property(SONAR_USE_WS_CACHE))) {
- return LoadStrategy.CACHE_ONLY;
- }
-
- return LoadStrategy.SERVER_ONLY;
- }
-}
private static final String KEY_SCAN_ALL = "sonar.scanAllFiles";
private boolean mediumTestMode;
- private boolean notAssociated;
private boolean scanAllFiles;
public DefaultAnalysisMode(GlobalProperties globalProps, AnalysisProperties props) {
return mediumTestMode;
}
- public boolean isNotAssociated() {
- return notAssociated;
- }
-
public boolean scanAllFiles() {
return scanAllFiles;
}
validate(mode);
issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode);
mediumTestMode = "true".equals(getPropertyWithFallback(analysisProps, globalProps, FakePluginInstaller.MEDIUM_TEST_ENABLED));
- notAssociated = issues && rootProjectKeyMissing(analysisProps);
String scanAllStr = getPropertyWithFallback(analysisProps, globalProps, KEY_SCAN_ALL);
scanAllFiles = !issues || "true".equals(scanAllStr);
}
if (mediumTestMode) {
LOG.info("Medium test mode");
}
- if (notAssociated) {
- LOG.info("Local analysis");
- }
if (!scanAllFiles) {
LOG.info("Scanning only changed files");
}
return CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode);
}
-
- private static boolean rootProjectKeyMissing(Map<String, String> props) {
- // ProjectReactorBuilder depends on this class, so it will only create this property later
- return !props.containsKey(CoreProperties.PROJECT_KEY_PROPERTY);
- }
-
}
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.Reader;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.Plugin;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoaderResult;
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.RemotePlugin;
import org.sonar.core.platform.RemotePluginFile;
private static final Logger LOG = Loggers.get(BatchPluginInstaller.class);
private static final String PLUGINS_INDEX_URL = "/deploy/plugins/index.txt";
- private final WSLoader wsLoader;
private final FileCache fileCache;
private final BatchPluginPredicate pluginPredicate;
private final BatchWsClient wsClient;
- public BatchPluginInstaller(WSLoader wsLoader, BatchWsClient wsClient, FileCache fileCache, BatchPluginPredicate pluginPredicate) {
- this.wsLoader = wsLoader;
+ public BatchPluginInstaller(BatchWsClient wsClient, FileCache fileCache, BatchPluginPredicate pluginPredicate) {
this.fileCache = fileCache;
this.pluginPredicate = pluginPredicate;
this.wsClient = wsClient;
private String loadPluginIndex() {
Profiler profiler = Profiler.create(LOG).startInfo("Load plugins index");
- WSLoaderResult<String> wsResult = wsLoader.loadString(PLUGINS_INDEX_URL);
- profiler.stopInfo(wsResult.isFromCache());
- return wsResult.get();
+ GetRequest getRequest = new GetRequest(PLUGINS_INDEX_URL);
+ String str;
+ try (Reader reader = wsClient.call(getRequest).contentReader()) {
+ str = IOUtils.toString(reader);
+ } catch(IOException e) {
+ throw new IllegalStateException(e);
+ }
+
+ profiler.stopInfo();
+ return str;
}
private class FileDownloader implements FileCache.Downloader {
import org.sonar.api.internal.SonarQubeVersionFactory;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.UriReader;
-import org.sonar.batch.cache.GlobalPersistentCacheProvider;
-import org.sonar.batch.cache.ProjectSyncContainer;
-import org.sonar.batch.cache.StrategyWSLoaderProvider;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
import org.sonar.batch.index.CachesManager;
import org.sonar.batch.platform.DefaultServer;
import org.sonar.batch.repository.DefaultGlobalRepositoriesLoader;
public class GlobalContainer extends ComponentContainer {
private final Map<String, String> bootstrapProperties;
- private boolean preferCache;
- private GlobalContainer(Map<String, String> bootstrapProperties, boolean preferCache) {
+ private GlobalContainer(Map<String, String> bootstrapProperties) {
super();
this.bootstrapProperties = bootstrapProperties;
- this.preferCache = preferCache;
}
- public static GlobalContainer create(Map<String, String> bootstrapProperties, List<?> extensions, boolean preferCache) {
- GlobalContainer container = new GlobalContainer(bootstrapProperties, preferCache);
+ public static GlobalContainer create(Map<String, String> bootstrapProperties, List<?> extensions) {
+ GlobalContainer container = new GlobalContainer(bootstrapProperties);
container.add(extensions);
return container;
}
protected void doBeforeStart() {
GlobalProperties bootstrapProps = new GlobalProperties(bootstrapProperties);
GlobalMode globalMode = new GlobalMode(bootstrapProps);
- LoadStrategy strategy = getDataLoadingStrategy(globalMode, preferCache);
- StrategyWSLoaderProvider wsLoaderProvider = new StrategyWSLoaderProvider(strategy);
- add(wsLoaderProvider);
add(bootstrapProps);
add(globalMode);
addBootstrapComponents();
}
- private static LoadStrategy getDataLoadingStrategy(GlobalMode mode, boolean preferCache) {
- if (!mode.isIssues()) {
- return LoadStrategy.SERVER_ONLY;
- }
-
- return preferCache ? LoadStrategy.CACHE_FIRST : LoadStrategy.SERVER_FIRST;
- }
-
private void addBootstrapComponents() {
add(
// plugins
DefaultHttpDownloader.class,
UriReader.class,
new FileCacheProvider(),
- new GlobalPersistentCacheProvider(),
System2.INSTANCE,
new GlobalRepositoriesProvider(),
UuidFactoryImpl.INSTANCE);
new TaskContainer(this, taskProperties, components).execute();
}
- public void syncProject(String projectKey, boolean force) {
- new ProjectSyncContainer(this, projectKey, force).execute();
- }
-
}
configureLogging();
try {
- bootstrapContainer = GlobalContainer.create(bootstrapProperties, components, preferCache);
+ bootstrapContainer = GlobalContainer.create(bootstrapProperties, components);
bootstrapContainer.startComponents();
} catch (RuntimeException e) {
throw handleException(e);
/**
* @since 5.2
+ * @deprecated since 5.6
*/
public Batch syncProject(String projectKey) {
checkStarted();
- bootstrapContainer.syncProject(projectKey, true);
return this;
}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.Date;
-import org.sonar.home.cache.PersistentCache;
-
-import static org.sonar.core.util.FileUtils.deleteQuietly;
-
-public class DefaultProjectCacheStatus implements ProjectCacheStatus {
- private static final String STATUS_FILENAME = "cache-sync-status";
- private PersistentCache cache;
-
- public DefaultProjectCacheStatus(PersistentCache cache) {
- this.cache = cache;
- }
-
- @Override
- public void save() {
- Date now = new Date();
-
- try {
- try (ObjectOutputStream objOutput = new ObjectOutputStream(new FileOutputStream(getStatusFilePath().toFile()))) {
- objOutput.writeObject(now);
- }
- } catch (IOException e) {
- throw new IllegalStateException("Failed to write cache sync status", e);
- }
- }
-
- @Override
- public void delete() {
- cache.clear();
- deleteQuietly(getStatusFilePath().toFile());
- }
-
- @Override
- public Date getSyncStatus() {
- Path p = getStatusFilePath();
- try {
- if (!Files.isRegularFile(p)) {
- return null;
- }
- try (ObjectInputStream objInput = new ObjectInputStream(new FileInputStream(p.toFile()))) {
- return (Date) objInput.readObject();
- }
- } catch (IOException | ClassNotFoundException e) {
- deleteQuietly(p.toFile());
- throw new IllegalStateException("Failed to read cache sync status", e);
- }
- }
-
- private Path getStatusFilePath() {
- return cache.getDirectory().resolve(STATUS_FILENAME);
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import org.apache.commons.lang.StringUtils;
-import org.sonar.batch.bootstrap.Slf4jLogger;
-import org.sonar.home.cache.PersistentCacheBuilder;
-
-import java.nio.file.Paths;
-
-import org.sonar.batch.bootstrap.GlobalProperties;
-import org.sonar.home.cache.PersistentCache;
-import org.picocontainer.injectors.ProviderAdapter;
-
-public class GlobalPersistentCacheProvider extends ProviderAdapter {
- private PersistentCache cache;
-
- public PersistentCache provide(GlobalProperties props) {
- if (cache == null) {
- PersistentCacheBuilder builder = new PersistentCacheBuilder(new Slf4jLogger());
- String home = props.property("sonar.userHome");
- String serverUrl = getServerUrl(props);
-
- if (home != null) {
- builder.setSonarHome(Paths.get(home));
- }
-
- builder.setAreaForGlobal(serverUrl);
- cache = builder.build();
- }
-
- return cache;
- }
-
- private static String getServerUrl(GlobalProperties props) {
- return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.utils.log.Loggers;
-import org.sonar.api.utils.log.Profiler;
-import org.sonar.batch.repository.QualityProfileLoader;
-import org.sonar.batch.rule.ActiveRulesLoader;
-import org.sonar.batch.rule.RulesLoader;
-import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
-
-public class NonAssociatedCacheSynchronizer {
- private static final Logger LOG = LoggerFactory.getLogger(NonAssociatedCacheSynchronizer.class);
-
- private final ProjectCacheStatus cacheStatus;
- private final QualityProfileLoader qualityProfileLoader;
- private final ActiveRulesLoader activeRulesLoader;
- private final RulesLoader rulesLoader;
-
- public NonAssociatedCacheSynchronizer(RulesLoader rulesLoader, QualityProfileLoader qualityProfileLoader, ActiveRulesLoader activeRulesLoader, ProjectCacheStatus cacheStatus) {
- this.rulesLoader = rulesLoader;
- this.qualityProfileLoader = qualityProfileLoader;
- this.activeRulesLoader = activeRulesLoader;
- this.cacheStatus = cacheStatus;
- }
-
- public void execute(boolean force) {
- Date lastSync = cacheStatus.getSyncStatus();
-
- if (lastSync != null) {
- if (!force) {
- LOG.info("Found cache [{}]", lastSync);
- return;
- } else {
- LOG.info("-- Found cache [{}], synchronizing data..", lastSync);
- }
- } else {
- LOG.info("-- Cache not found, synchronizing data..");
- }
-
- loadData();
- cacheStatus.save();
- LOG.info("-- Succesfully synchronized cache");
- }
-
- private static Collection<String> getKeys(Collection<QualityProfile> qProfiles) {
- List<String> list = new ArrayList<>(qProfiles.size());
- for (QualityProfile qp : qProfiles) {
- list.add(qp.getKey());
- }
-
- return list;
- }
-
- private void loadData() {
- Profiler profiler = Profiler.create(Loggers.get(ProjectCacheSynchronizer.class));
-
- profiler.startInfo("Load rules");
- rulesLoader.load(null);
- profiler.stopInfo();
-
- profiler.startInfo("Load default quality profiles");
- Collection<QualityProfile> qProfiles = qualityProfileLoader.loadDefault(null, null);
- profiler.stopInfo();
-
- profiler.startInfo("Load default active rules");
- Collection<String> keys = getKeys(qProfiles);
- for (String k : keys) {
- activeRulesLoader.load(k, null);
- }
- profiler.stopInfo();
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import java.util.Date;
-
-public interface ProjectCacheStatus {
- void save();
-
- void delete();
-
- Date getSyncStatus();
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import com.google.common.base.Function;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.utils.log.Loggers;
-import org.sonar.api.utils.log.Profiler;
-import org.sonar.batch.repository.ProjectRepositories;
-import org.sonar.batch.repository.ProjectRepositoriesLoader;
-import org.sonar.batch.repository.QualityProfileLoader;
-import org.sonar.batch.repository.ServerIssuesLoader;
-import org.sonar.batch.repository.user.UserRepositoryLoader;
-import org.sonar.batch.rule.ActiveRulesLoader;
-import org.sonar.batch.rule.RulesLoader;
-import org.sonar.scanner.protocol.input.ScannerInput.ServerIssue;
-import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
-
-public class ProjectCacheSynchronizer {
- private static final Logger LOG = LoggerFactory.getLogger(ProjectCacheSynchronizer.class);
-
- private final ServerIssuesLoader issuesLoader;
- private final UserRepositoryLoader userRepository;
- private final ProjectCacheStatus cacheStatus;
- private final QualityProfileLoader qualityProfileLoader;
- private final ProjectRepositoriesLoader projectRepositoriesLoader;
- private final ActiveRulesLoader activeRulesLoader;
- private final RulesLoader rulesLoader;
-
- public ProjectCacheSynchronizer(RulesLoader rulesLoader, QualityProfileLoader qualityProfileLoader, ProjectRepositoriesLoader projectSettingsLoader,
- ActiveRulesLoader activeRulesLoader, ServerIssuesLoader issuesLoader,
- UserRepositoryLoader userRepository, ProjectCacheStatus cacheStatus) {
- this.rulesLoader = rulesLoader;
- this.qualityProfileLoader = qualityProfileLoader;
- this.projectRepositoriesLoader = projectSettingsLoader;
- this.activeRulesLoader = activeRulesLoader;
- this.issuesLoader = issuesLoader;
- this.userRepository = userRepository;
- this.cacheStatus = cacheStatus;
- }
-
- private static boolean isToday(Date d) {
- Calendar c1 = Calendar.getInstance();
- Calendar c2 = Calendar.getInstance();
- c2.setTime(d);
-
- return c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR) &&
- c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR);
- }
-
- private static boolean shouldUpdate(Date lastUpdate) {
- return !isToday(lastUpdate);
- }
-
- public void load(String projectKey, boolean force) {
- Date lastSync = cacheStatus.getSyncStatus();
- boolean failOnError = true;
-
- if (lastSync != null) {
- if (force) {
- LOG.info("-- Found project [{}] cache [{}], synchronizing data (forced)..", projectKey, lastSync);
- } else if (shouldUpdate(lastSync)) {
- LOG.info("-- Found project [{}] cache [{}], synchronizing data..", projectKey, lastSync);
- failOnError = false;
- } else {
- LOG.info("Found project [{}] cache [{}]", projectKey, lastSync);
- return;
- }
- } else {
- LOG.info("-- Cache for project [{}] not found, synchronizing data..", projectKey);
- }
-
- try {
- loadData(projectKey);
- } catch (Exception e) {
- if (failOnError) {
- throw e;
- }
-
- LOG.warn("-- Cache update for project [{}] failed, continuing from cache..", projectKey, e);
- return;
- }
-
- saveStatus();
- }
-
- private void saveStatus() {
- cacheStatus.save();
- LOG.info("-- Successfully synchronized project cache");
- }
-
- private void loadData(String projectKey) {
- Profiler profiler = Profiler.create(Loggers.get(ProjectCacheSynchronizer.class));
-
- profiler.startInfo("Load rules");
- rulesLoader.load(null);
- profiler.stopInfo();
-
- profiler.startInfo("Load project settings");
- ProjectRepositories projectRepo = projectRepositoriesLoader.load(projectKey, true, null);
-
- if (!projectRepo.exists()) {
- LOG.debug("Project doesn't exist in the server");
- } else if (projectRepo.lastAnalysisDate() == null) {
- LOG.debug("No previous analysis found");
- }
- profiler.stopInfo();
-
- profiler.startInfo("Load project quality profiles");
- Collection<QualityProfile> qProfiles;
- if (projectRepo.exists()) {
- qProfiles = qualityProfileLoader.load(projectKey, null, null);
- } else {
- qProfiles = qualityProfileLoader.loadDefault(null, null);
- }
- profiler.stopInfo();
-
- profiler.startInfo("Load project active rules");
- Collection<String> keys = getKeys(qProfiles);
- for (String k : keys) {
- activeRulesLoader.load(k, null);
- }
- profiler.stopInfo();
-
- if (projectRepo.lastAnalysisDate() != null) {
- profiler.startInfo("Load server issues");
- UserLoginAccumulator consumer = new UserLoginAccumulator();
- issuesLoader.load(projectKey, consumer);
- profiler.stopInfo();
-
- profiler.startInfo("Load user information");
- for (String login : consumer.loginSet) {
- userRepository.load(login, null);
- }
- profiler.stopInfo("Load user information");
- }
- }
-
- private static Collection<String> getKeys(Collection<QualityProfile> qProfiles) {
- List<String> list = new ArrayList<>(qProfiles.size());
- for (QualityProfile qp : qProfiles) {
- list.add(qp.getKey());
- }
-
- return list;
- }
-
- private static class UserLoginAccumulator implements Function<ServerIssue, Void> {
- Set<String> loginSet = new HashSet<>();
-
- @Override
- public Void apply(ServerIssue input) {
- if (!StringUtils.isEmpty(input.getAssigneeLogin())) {
- loginSet.add(input.getAssigneeLogin());
- }
- return null;
- }
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import org.sonar.api.batch.bootstrap.ProjectKey;
-
-public class ProjectKeySupplier implements ProjectKey {
- private final String key;
-
- ProjectKeySupplier(String key) {
- this.key = key;
- }
-
- @Override
- public String get() {
- return key;
- }
-
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import org.sonar.api.batch.bootstrap.ProjectKey;
-
-import org.sonar.batch.util.BatchUtils;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.batch.bootstrap.GlobalProperties;
-import com.google.common.base.Preconditions;
-import org.sonar.batch.analysis.DefaultAnalysisMode;
-import org.sonar.batch.bootstrap.Slf4jLogger;
-
-import java.nio.file.Paths;
-
-import org.picocontainer.injectors.ProviderAdapter;
-import org.sonar.home.cache.PersistentCache;
-import org.sonar.home.cache.PersistentCacheBuilder;
-
-public class ProjectPersistentCacheProvider extends ProviderAdapter {
- private PersistentCache cache;
-
- public PersistentCache provide(GlobalProperties props, DefaultAnalysisMode mode, ProjectKey key) {
- if (cache == null) {
- PersistentCacheBuilder builder = new PersistentCacheBuilder(new Slf4jLogger());
- String projectKey = key.get();
- String home = props.property("sonar.userHome");
- String serverUrl = getServerUrl(props);
-
- if (home != null) {
- builder.setSonarHome(Paths.get(home));
- }
-
- if (mode.isNotAssociated()) {
- builder.setAreaForLocalProject(serverUrl, BatchUtils.getServerVersion());
- } else {
- Preconditions.checkNotNull(projectKey);
- builder.setAreaForProject(serverUrl, BatchUtils.getServerVersion(), projectKey);
- }
-
- cache = builder.build();
- }
-
- return cache;
- }
-
- private static String getServerUrl(GlobalProperties props) {
- return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import java.util.HashMap;
-import java.util.Map;
-import javax.annotation.Nullable;
-import org.sonar.api.CoreProperties;
-import org.sonar.batch.analysis.AnalysisProperties;
-import org.sonar.batch.analysis.DefaultAnalysisMode;
-import org.sonar.batch.bootstrap.GlobalProperties;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
-import org.sonar.batch.repository.DefaultProjectRepositoriesLoader;
-import org.sonar.batch.repository.DefaultQualityProfileLoader;
-import org.sonar.batch.repository.DefaultServerIssuesLoader;
-import org.sonar.batch.repository.ProjectRepositoriesLoader;
-import org.sonar.batch.repository.QualityProfileLoader;
-import org.sonar.batch.repository.ServerIssuesLoader;
-import org.sonar.batch.repository.user.UserRepositoryLoader;
-import org.sonar.batch.rule.ActiveRulesLoader;
-import org.sonar.batch.rule.DefaultActiveRulesLoader;
-import org.sonar.batch.rule.DefaultRulesLoader;
-import org.sonar.batch.rule.RulesLoader;
-import org.sonar.core.platform.ComponentContainer;
-
-public class ProjectSyncContainer extends ComponentContainer {
- private final boolean force;
- private final String projectKey;
-
- public ProjectSyncContainer(ComponentContainer globalContainer, @Nullable String projectKey, boolean force) {
- super(globalContainer);
- this.projectKey = projectKey;
- this.force = force;
- }
-
- @Override
- public void doBeforeStart() {
- addComponents();
- }
-
- @Override
- public void doAfterStart() {
- if (projectKey != null) {
- getComponentByType(ProjectCacheSynchronizer.class).load(projectKey, force);
- } else {
- getComponentByType(NonAssociatedCacheSynchronizer.class).execute(force);
- }
- }
-
- private static DefaultAnalysisMode createIssuesAnalysisMode(@Nullable String projectKey) {
- Map<String, String> props = new HashMap<>();
- props.put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES);
- if (projectKey != null) {
- props.put(CoreProperties.PROJECT_KEY_PROPERTY, projectKey);
- }
- GlobalProperties globalProps = new GlobalProperties(props);
- AnalysisProperties analysisProps = new AnalysisProperties(props);
- return new DefaultAnalysisMode(globalProps, analysisProps);
- }
-
- private void addComponents() {
- add(new StrategyWSLoaderProvider(LoadStrategy.SERVER_ONLY),
- new ProjectKeySupplier(projectKey),
- projectKey != null ? ProjectCacheSynchronizer.class : NonAssociatedCacheSynchronizer.class,
- UserRepositoryLoader.class,
- new ProjectPersistentCacheProvider(),
- createIssuesAnalysisMode(projectKey));
-
- addIfMissing(DefaultProjectCacheStatus.class, ProjectCacheStatus.class);
- addIfMissing(DefaultProjectRepositoriesLoader.class, ProjectRepositoriesLoader.class);
- addIfMissing(DefaultServerIssuesLoader.class, ServerIssuesLoader.class);
- addIfMissing(DefaultQualityProfileLoader.class, QualityProfileLoader.class);
- addIfMissing(DefaultRulesLoader.class, RulesLoader.class);
- addIfMissing(DefaultActiveRulesLoader.class, ActiveRulesLoader.class);
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import org.picocontainer.injectors.ProviderAdapter;
-import org.sonar.batch.bootstrap.BatchWsClient;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
-import org.sonar.home.cache.PersistentCache;
-
-public class StrategyWSLoaderProvider extends ProviderAdapter {
- private final LoadStrategy strategy;
- private WSLoader wsLoader;
-
- public StrategyWSLoaderProvider(LoadStrategy strategy) {
- this.strategy = strategy;
- }
-
- public WSLoader provide(PersistentCache cache, BatchWsClient client) {
- if (wsLoader == null) {
- wsLoader = new WSLoader(strategy, cache, client);
- }
- return wsLoader;
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Reader;
-import java.nio.charset.StandardCharsets;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-import org.apache.commons.io.IOUtils;
-import org.sonar.api.utils.MessageException;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-import org.sonar.batch.bootstrap.BatchWsClient;
-import org.sonar.home.cache.PersistentCache;
-import org.sonarqube.ws.client.GetRequest;
-import org.sonarqube.ws.client.HttpException;
-
-import static org.sonar.batch.cache.WSLoader.ServerStatus.ACCESSIBLE;
-import static org.sonar.batch.cache.WSLoader.ServerStatus.NOT_ACCESSIBLE;
-import static org.sonar.batch.cache.WSLoader.ServerStatus.UNKNOWN;
-
-public class WSLoader {
- private static final Logger LOG = Loggers.get(WSLoader.class);
- private static final String FAIL_MSG = "Server is not accessible and data is not cached";
-
- public enum ServerStatus {
- UNKNOWN, ACCESSIBLE, NOT_ACCESSIBLE
- }
-
- public enum LoadStrategy {
- SERVER_FIRST, CACHE_FIRST, SERVER_ONLY, CACHE_ONLY
- }
-
- private final LoadStrategy defautLoadStrategy;
- private final BatchWsClient wsClient;
- private final PersistentCache cache;
- private ServerStatus serverStatus;
-
- private DataLoader<String> stringServerLoader = new DataLoader<String>() {
- @Override
- public String load(String id) throws IOException {
- GetRequest getRequest = new GetRequest(id);
- try (Reader reader = wsClient.call(getRequest).contentReader()) {
- String str = IOUtils.toString(reader);
- try {
- cache.put(id, str.getBytes(StandardCharsets.UTF_8));
- } catch (IOException e) {
- throw new IllegalStateException("Error saving to WS cache", e);
- }
- return str;
- }
- }
- };
-
- private DataLoader<String> stringCacheLoader = new DataLoader<String>() {
- @Override
- public String load(String id) throws IOException {
- return cache.getString(id);
- }
- };
-
- private DataLoader<InputStream> streamServerLoader = new DataLoader<InputStream>() {
- @Override
- public InputStream load(String id) throws IOException {
- GetRequest getRequest = new GetRequest(id);
- try (InputStream is = wsClient.call(getRequest).contentStream()) {
- try {
- cache.put(id, is);
- } catch (IOException e) {
- throw new IllegalStateException("Error saving to WS cache", e);
- }
- }
- return cache.getStream(id);
- }
- };
-
- private DataLoader<InputStream> streamCacheLoader = new DataLoader<InputStream>() {
- @Override
- public InputStream load(String id) throws IOException {
- return cache.getStream(id);
- }
- };
-
- private static class NotAvailableException extends Exception {
- private static final long serialVersionUID = 1L;
-
- public NotAvailableException(String message) {
- super(message);
- }
-
- public NotAvailableException(Throwable cause) {
- super(cause);
- }
- }
-
- public WSLoader(LoadStrategy strategy, PersistentCache cache, BatchWsClient wsClient) {
- this.defautLoadStrategy = strategy;
- this.serverStatus = UNKNOWN;
- this.cache = cache;
- this.wsClient = wsClient;
- }
-
- @Nonnull
- public WSLoaderResult<InputStream> loadStream(String id) {
- return load(id, defautLoadStrategy, streamServerLoader, streamCacheLoader);
- }
-
- @Nonnull
- public WSLoaderResult<String> loadString(String id) {
- return loadString(id, defautLoadStrategy);
- }
-
- @Nonnull
- public WSLoaderResult<String> loadString(String id, WSLoader.LoadStrategy strategy) {
- return load(id, strategy, stringServerLoader, stringCacheLoader);
- }
-
- @Nonnull
- private <T> WSLoaderResult<T> load(String id, WSLoader.LoadStrategy strategy, DataLoader<T> serverLoader, DataLoader<T> cacheLoader) {
- switch (strategy) {
- case CACHE_FIRST:
- return loadFromCacheFirst(id, cacheLoader, serverLoader);
- case CACHE_ONLY:
- return loadFromCacheFirst(id, cacheLoader, null);
- case SERVER_FIRST:
- return loadFromServerFirst(id, serverLoader, cacheLoader);
- case SERVER_ONLY:
- default:
- return loadFromServerFirst(id, serverLoader, null);
- }
- }
-
- public LoadStrategy getDefaultStrategy() {
- return this.defautLoadStrategy;
- }
-
- private void switchToOffline() {
- LOG.debug("server not available - switching to offline mode");
- serverStatus = NOT_ACCESSIBLE;
- }
-
- private void switchToOnline() {
- serverStatus = ACCESSIBLE;
- }
-
- private boolean isOffline() {
- return serverStatus == NOT_ACCESSIBLE;
- }
-
- @Nonnull
- private <T> WSLoaderResult<T> loadFromCacheFirst(String id, DataLoader<T> cacheLoader, @Nullable DataLoader<T> serverLoader) {
- try {
- return loadFromCache(id, cacheLoader);
- } catch (NotAvailableException cacheNotAvailable) {
- if (serverLoader != null) {
- try {
- return loadFromServer(id, serverLoader);
- } catch (NotAvailableException serverNotAvailable) {
- throw new IllegalStateException(FAIL_MSG, serverNotAvailable.getCause());
- }
- }
- throw new IllegalStateException("Data is not cached", cacheNotAvailable.getCause());
- }
- }
-
- @Nonnull
- private <T> WSLoaderResult<T> loadFromServerFirst(String id, DataLoader<T> serverLoader, @Nullable DataLoader<T> cacheLoader) {
- try {
- return loadFromServer(id, serverLoader);
- } catch (NotAvailableException serverNotAvailable) {
- if (cacheLoader != null) {
- try {
- return loadFromCache(id, cacheLoader);
- } catch (NotAvailableException cacheNotAvailable) {
- throw new IllegalStateException(FAIL_MSG, serverNotAvailable.getCause());
- }
- }
- throw new IllegalStateException("Server is not available: " + wsClient.baseUrl(), serverNotAvailable.getCause());
- }
- }
-
- interface DataLoader<T> {
- T load(String id) throws IOException;
- }
-
- private <T> WSLoaderResult<T> loadFromCache(String id, DataLoader<T> loader) throws NotAvailableException {
- T result;
-
- try {
- result = loader.load(id);
- } catch (IOException e) {
- // any exception on the cache should fail fast
- throw new IllegalStateException(e);
- }
- if (result == null) {
- throw new NotAvailableException("resource not cached");
- }
- return new WSLoaderResult<>(result, true);
- }
-
- private <T> WSLoaderResult<T> loadFromServer(String id, DataLoader<T> loader) throws NotAvailableException {
- if (isOffline()) {
- throw new NotAvailableException("Server not available");
- }
- try {
- T t = loader.load(id);
- switchToOnline();
- return new WSLoaderResult<>(t, false);
- } catch (HttpException | MessageException e) {
- // fail fast if it could connect but there was a application-level error
- throw e;
- } catch (IllegalStateException e) {
- switchToOffline();
- throw new NotAvailableException(e);
- } catch (Exception e) {
- // fail fast
- throw new IllegalStateException(e);
- }
- }
-
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import javax.annotation.Nonnull;
-
-public class WSLoaderResult<T> {
- private T result;
- private boolean fromCache;
-
- public WSLoaderResult(T result, boolean fromCache) {
- this.result = result;
- this.fromCache = fromCache;
- }
-
- @Nonnull
- public T get() {
- return result;
- }
-
- public boolean isFromCache() {
- return fromCache;
- }
-}
+++ /dev/null
-/*
- * 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.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.batch.cache;
-
-import javax.annotation.ParametersAreNonnullByDefault;
*/
package org.sonar.batch.issue.tracking;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
-
-import org.sonar.batch.cache.WSLoaderResult;
-import org.sonar.batch.cache.WSLoader;
-import org.apache.commons.lang.mutable.MutableBoolean;
+import org.sonar.batch.bootstrap.BatchWsClient;
+import org.sonar.batch.util.BatchUtils;
+import org.sonarqube.ws.client.GetRequest;
-import javax.annotation.Nullable;
+import java.io.IOException;
+import java.io.Reader;
-import org.sonar.batch.util.BatchUtils;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterators;
+import org.apache.commons.io.IOUtils;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler;
public class DefaultServerLineHashesLoader implements ServerLineHashesLoader {
+ private BatchWsClient wsClient;
- private final WSLoader wsLoader;
-
- public DefaultServerLineHashesLoader(WSLoader wsLoader) {
- this.wsLoader = wsLoader;
+ public DefaultServerLineHashesLoader(BatchWsClient wsClient) {
+ this.wsClient = wsClient;
}
@Override
- public String[] getLineHashes(String fileKey, @Nullable MutableBoolean fromCache) {
- String hashesFromWs = loadHashesFromWs(fileKey, fromCache);
+ public String[] getLineHashes(String fileKey) {
+ String hashesFromWs = loadHashesFromWs(fileKey);
return Iterators.toArray(Splitter.on('\n').split(hashesFromWs).iterator(), String.class);
}
- private String loadHashesFromWs(String fileKey, @Nullable MutableBoolean fromCache) {
+ private String loadHashesFromWs(String fileKey) {
Profiler profiler = Profiler.createIfDebug(Loggers.get(getClass()))
.addContext("file", fileKey)
.startDebug("Load line hashes");
- WSLoaderResult<String> result = wsLoader.loadString("/api/sources/hash?key=" + BatchUtils.encodeForUrl(fileKey), LoadStrategy.CACHE_FIRST);
+
+ GetRequest getRequest = new GetRequest("/api/sources/hash?key=" + BatchUtils.encodeForUrl(fileKey));
+ Reader reader = wsClient.call(getRequest).contentReader();
try {
- if (fromCache != null) {
- fromCache.setValue(result.isFromCache());
- }
- return result.get();
+ return IOUtils.toString(reader);
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
} finally {
- if (result.isFromCache()) {
- profiler.stopDebug("Load line hashes (done from cache)");
- } else {
- profiler.stopDebug();
- }
+ profiler.stopDebug();
}
}
}
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
this.issuesCache = caches.createCache("previousIssues");
caches.registerValueCoder(ServerIssue.class, new ServerIssueValueCoder());
- boolean fromCache = previousIssuesLoader.load(reactor.getRoot().getKeyWithBranch(), new SaveIssueConsumer());
- profiler.stopInfo(fromCache);
+ previousIssuesLoader.load(reactor.getRoot().getKeyWithBranch(), new SaveIssueConsumer());
+ profiler.stopInfo();
}
public Iterable<ServerIssue> byComponent(BatchComponent component) {
return null;
}
}
-
+
public Iterable<ServerIssue> issuesOnMissingComponents() {
return issuesCache.values(0);
}
*/
package org.sonar.batch.issue.tracking;
-import org.apache.commons.lang.mutable.MutableBoolean;
-
-import javax.annotation.Nullable;
-
import org.sonar.api.batch.BatchSide;
@BatchSide
public interface ServerLineHashesLoader {
- String[] getLineHashes(String fileKey, @Nullable MutableBoolean fromCache);
+ String[] getLineHashes(String fileKey);
}
} else if (status == Status.SAME) {
hashedReference = hashedSource;
} else {
- String[] lineHashes = lastSnapshots.getLineHashes(inputFile.key(), null);
+ String[] lineHashes = lastSnapshots.getLineHashes(inputFile.key());
hashedReference = lineHashes != null ? FileHashes.create(lineHashes) : null;
}
}
*/
package org.sonar.batch.repository;
-import org.sonar.batch.cache.WSLoaderResult;
import org.sonar.scanner.protocol.input.GlobalRepositories;
-import org.sonar.batch.cache.WSLoader;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonar.batch.bootstrap.BatchWsClient;
-import javax.annotation.Nullable;
+import java.io.IOException;
+import java.io.Reader;
-import org.apache.commons.lang.mutable.MutableBoolean;
+import org.apache.commons.io.IOUtils;
public class DefaultGlobalRepositoriesLoader implements GlobalRepositoriesLoader {
private static final String BATCH_GLOBAL_URL = "/batch/global";
+ private BatchWsClient wsClient;
- private final WSLoader wsLoader;
-
- public DefaultGlobalRepositoriesLoader(WSLoader wsLoader) {
- this.wsLoader = wsLoader;
+ public DefaultGlobalRepositoriesLoader(BatchWsClient wsClient) {
+ this.wsClient = wsClient;
}
@Override
- public GlobalRepositories load(@Nullable MutableBoolean fromCache) {
- WSLoaderResult<String> result = wsLoader.loadString(BATCH_GLOBAL_URL);
- if (fromCache != null) {
- fromCache.setValue(result.isFromCache());
+ public GlobalRepositories load() {
+ GetRequest getRequest = new GetRequest(BATCH_GLOBAL_URL);
+ Reader reader = wsClient.call(getRequest).contentReader();
+ String str;
+ try {
+ str = IOUtils.toString(reader);
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
}
- return GlobalRepositories.fromJson(result.get());
+ return GlobalRepositories.fromJson(str);
}
}
import java.util.Date;
import java.util.Map;
-import javax.annotation.Nullable;
-
import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.MessageException;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonar.batch.util.BatchUtils;
import org.sonarqube.ws.WsBatch.WsProjectResponse;
import org.sonarqube.ws.WsBatch.WsProjectResponse.FileDataByPath;
import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings;
+import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.HttpException;
public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {
private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class);
private static final String BATCH_PROJECT_URL = "/batch/project.protobuf";
- private final WSLoader loader;
+ private BatchWsClient wsClient;
- public DefaultProjectRepositoriesLoader(WSLoader loader) {
- this.loader = loader;
+ public DefaultProjectRepositoriesLoader(BatchWsClient wsClient) {
+ this.wsClient = wsClient;
}
@Override
- public ProjectRepositories load(String projectKey, boolean issuesMode, @Nullable MutableBoolean fromCache) {
+ public ProjectRepositories load(String projectKey, boolean issuesMode) {
try {
- WSLoaderResult<InputStream> result = loader.loadStream(getUrl(projectKey, issuesMode));
- if (fromCache != null) {
- fromCache.setValue(result.isFromCache());
- }
- return processStream(result.get(), projectKey);
+ GetRequest request = new GetRequest(getUrl(projectKey, issuesMode));
+ InputStream is = wsClient.call(request).contentStream();
+ return processStream(is, projectKey);
} catch (RuntimeException e) {
if (shouldThrow(e)) {
throw e;
import org.sonar.batch.util.BatchUtils;
import org.apache.commons.io.IOUtils;
import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
-import org.apache.commons.lang.mutable.MutableBoolean;
-import org.sonar.batch.cache.WSLoaderResult;
-import org.sonar.batch.cache.WSLoader;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonar.batch.bootstrap.BatchWsClient;
import javax.annotation.Nullable;
public class DefaultQualityProfileLoader implements QualityProfileLoader {
private static final String WS_URL = "/api/qualityprofiles/search.protobuf";
- private WSLoader wsLoader;
+ private BatchWsClient wsClient;
- public DefaultQualityProfileLoader(WSLoader wsLoader) {
- this.wsLoader = wsLoader;
+ public DefaultQualityProfileLoader(BatchWsClient wsClient) {
+ this.wsClient = wsClient;
}
@Override
- public List<QualityProfile> loadDefault(@Nullable String profileName, @Nullable MutableBoolean fromCache) {
+ public List<QualityProfile> loadDefault(@Nullable String profileName) {
String url = WS_URL + "?defaults=true";
if (profileName != null) {
url += "&profileName=" + BatchUtils.encodeForUrl(profileName);
}
- return loadResource(url, fromCache);
+ return loadResource(url);
}
@Override
- public List<QualityProfile> load(String projectKey, @Nullable String profileName, @Nullable MutableBoolean fromCache) {
+ public List<QualityProfile> load(String projectKey, @Nullable String profileName) {
String url = WS_URL + "?projectKey=" + BatchUtils.encodeForUrl(projectKey);
if (profileName != null) {
url += "&profileName=" + BatchUtils.encodeForUrl(profileName);
}
- return loadResource(url, fromCache);
+ return loadResource(url);
}
- private List<QualityProfile> loadResource(String url, @Nullable MutableBoolean fromCache) {
- WSLoaderResult<InputStream> result = wsLoader.loadStream(url);
- if (fromCache != null) {
- fromCache.setValue(result.isFromCache());
- }
- InputStream is = result.get();
+ private List<QualityProfile> loadResource(String url) {
+ GetRequest getRequest = new GetRequest(url);
+ InputStream is = wsClient.call(getRequest).contentStream();
SearchWsResponse profiles = null;
try {
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonar.batch.util.BatchUtils;
import org.sonar.scanner.protocol.input.ScannerInput.ServerIssue;
+import org.sonarqube.ws.client.GetRequest;
public class DefaultServerIssuesLoader implements ServerIssuesLoader {
- private final WSLoader wsLoader;
+ private final BatchWsClient wsClient;
- public DefaultServerIssuesLoader(WSLoader wsLoader) {
- this.wsLoader = wsLoader;
+ public DefaultServerIssuesLoader(BatchWsClient wsClient) {
+ this.wsClient = wsClient;
}
@Override
- public boolean load(String componentKey, Function<ServerIssue, Void> consumer) {
- WSLoaderResult<InputStream> result = wsLoader.loadStream("/batch/issues.protobuf?key=" + BatchUtils.encodeForUrl(componentKey));
- parseIssues(result.get(), consumer);
- return result.isFromCache();
+ public void load(String componentKey, Function<ServerIssue, Void> consumer) {
+ GetRequest getRequest = new GetRequest("/batch/issues.protobuf?key=" + BatchUtils.encodeForUrl(componentKey));
+ InputStream is = wsClient.call(getRequest).contentStream();
+ parseIssues(is, consumer);
}
private static void parseIssues(InputStream is, Function<ServerIssue, Void> consumer) {
*/
package org.sonar.batch.repository;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.sonar.scanner.protocol.input.GlobalRepositories;
-import javax.annotation.Nullable;
public interface GlobalRepositoriesLoader {
-
- GlobalRepositories load(@Nullable MutableBoolean fromCache);
-
+ GlobalRepositories load();
}
*/
package org.sonar.batch.repository;
-import org.apache.commons.lang.mutable.MutableBoolean;
-
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
public GlobalRepositories provide(GlobalRepositoriesLoader loader) {
if (globalReferentials == null) {
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- MutableBoolean fromCache = new MutableBoolean();
- globalReferentials = loader.load(fromCache);
- profiler.stopInfo(fromCache.booleanValue());
+ globalReferentials = loader.load();
+ profiler.stopInfo();
}
return globalReferentials;
}
*/
package org.sonar.batch.repository;
-import javax.annotation.Nullable;
-
-import org.apache.commons.lang.mutable.MutableBoolean;
public interface ProjectRepositoriesLoader {
- ProjectRepositories load(String projectKeyWithBranch, boolean issuesMode, @Nullable MutableBoolean fromCache);
+ ProjectRepositories load(String projectKeyWithBranch, boolean issuesMode);
}
import org.sonar.api.utils.log.Loggers;
import org.sonar.batch.analysis.DefaultAnalysisMode;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.picocontainer.injectors.ProviderAdapter;
public class ProjectRepositoriesProvider extends ProviderAdapter {
public ProjectRepositories provide(ProjectRepositoriesLoader loader, ProjectKey projectKey, DefaultAnalysisMode mode) {
if (project == null) {
- MutableBoolean fromCache = new MutableBoolean(false);
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- if (mode.isNotAssociated()) {
- project = createNonAssociatedProjectRepositories();
- profiler.stopInfo();
- } else {
- project = loader.load(projectKey.get(), mode.isIssues(), fromCache);
- checkProject(mode);
- profiler.stopInfo(fromCache.booleanValue());
- }
-
+ project = loader.load(projectKey.get(), mode.isIssues());
+ checkProject(mode);
+ profiler.stopInfo();
}
return project;
if (mode.isIssues()) {
if (!project.exists()) {
LOG.warn("Project doesn't exist on the server. All issues will be marked as 'new'.");
- } else if (project.lastAnalysisDate() == null && !mode.isNotAssociated()) {
+ } else if (project.lastAnalysisDate() == null) {
LOG.warn("No analysis has been found on the server for this project. All issues will be marked as 'new'.");
}
}
}
-
- private static ProjectRepositories createNonAssociatedProjectRepositories() {
- return new ProjectRepositories();
- }
}
*/
package org.sonar.batch.repository;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
import javax.annotation.Nullable;
import java.util.List;
public interface QualityProfileLoader {
- List<QualityProfile> load(String projectKey, @Nullable String profileName, @Nullable MutableBoolean fromCache);
-
- List<QualityProfile> loadDefault(@Nullable String profileName, @Nullable MutableBoolean fromCache);
+ List<QualityProfile> load(String projectKey, @Nullable String profileName);
+
+ List<QualityProfile> loadDefault(@Nullable String profileName);
}
import java.util.List;
import javax.annotation.CheckForNull;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.batch.bootstrap.ProjectKey;
import org.sonar.api.utils.log.Logger;
public ModuleQProfiles provide(ProjectKey projectKey, QualityProfileLoader loader, ProjectRepositories projectRepositories, AnalysisProperties props, DefaultAnalysisMode mode) {
if (this.profiles == null) {
List<QualityProfile> profileList;
- MutableBoolean fromCache = new MutableBoolean();
-
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- if (mode.isNotAssociated() || !projectRepositories.exists()) {
- profileList = loader.loadDefault(getSonarProfile(props, mode), fromCache);
+ if (!projectRepositories.exists()) {
+ profileList = loader.loadDefault(getSonarProfile(props, mode));
} else {
- profileList = loader.load(projectKey.get(), getSonarProfile(props, mode), fromCache);
+ profileList = loader.load(projectKey.get(), getSonarProfile(props, mode));
}
- profiler.stopInfo(fromCache.booleanValue());
+ profiler.stopInfo();
profiles = new ModuleQProfiles(profileList);
}
public interface ServerIssuesLoader {
- boolean load(String componentKey, Function<ServerIssue, Void> consumer);
+ void load(String componentKey, Function<ServerIssue, Void> consumer);
}
import org.apache.commons.io.IOUtils;
-import org.sonar.batch.cache.WSLoaderResult;
-import org.sonar.batch.cache.WSLoader;
+import org.sonar.batch.bootstrap.BatchWsClient;
-import javax.annotation.Nullable;
-
-import org.apache.commons.lang.mutable.MutableBoolean;
import com.google.common.collect.Lists;
import com.google.common.base.Joiner;
import org.sonar.batch.util.BatchUtils;
import org.sonar.scanner.protocol.input.ScannerInput;
+import org.sonarqube.ws.client.GetRequest;
import com.google.common.base.Function;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UserRepositoryLoader {
- private final WSLoader wsLoader;
+ private final BatchWsClient wsClient;
- public UserRepositoryLoader(WSLoader wsLoader) {
- this.wsLoader = wsLoader;
+ public UserRepositoryLoader(BatchWsClient wsClient) {
+ this.wsClient = wsClient;
}
public ScannerInput.User load(String userLogin) {
- return load(userLogin, null);
- }
-
- public ScannerInput.User load(String userLogin, @Nullable MutableBoolean fromCache) {
- InputStream is = loadQuery(new UserEncodingFunction().apply(userLogin), fromCache);
+ InputStream is = loadQuery(new UserEncodingFunction().apply(userLogin));
return parseUser(is);
}
public Collection<ScannerInput.User> load(List<String> userLogins) {
- return load(userLogins, null);
- }
-
- /**
- * Not cache friendly. Should not be used if a cache hit is expected.
- */
- public Collection<ScannerInput.User> load(List<String> userLogins, @Nullable MutableBoolean fromCache) {
if (userLogins.isEmpty()) {
return Collections.emptyList();
}
- InputStream is = loadQuery(Joiner.on(',').join(Lists.transform(userLogins, new UserEncodingFunction())), fromCache);
+ InputStream is = loadQuery(Joiner.on(',').join(Lists.transform(userLogins, new UserEncodingFunction())));
return parseUsers(is);
}
- private InputStream loadQuery(String loginsQuery, @Nullable MutableBoolean fromCache) {
- WSLoaderResult<InputStream> result = wsLoader.loadStream("/batch/users?logins=" + loginsQuery);
- if (fromCache != null) {
- fromCache.setValue(result.isFromCache());
- }
- return result.get();
+ private InputStream loadQuery(String loginsQuery) {
+ GetRequest getRequest = new GetRequest("/batch/users?logins=" + loginsQuery);
+ return wsClient.call(getRequest).contentStream();
}
private static class UserEncodingFunction implements Function<String, String> {
*/
package org.sonar.batch.rule;
-import org.apache.commons.lang.mutable.MutableBoolean;
-
-import javax.annotation.Nullable;
-
import java.util.List;
public interface ActiveRulesLoader {
- List<LoadedActiveRule> load(String qualityProfileKey, @Nullable MutableBoolean fromCache);
+ List<LoadedActiveRule> load(String qualityProfileKey);
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
public ActiveRules provide(ActiveRulesLoader loader, ModuleQProfiles qProfiles) {
if (singleton == null) {
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- MutableBoolean fromCache = new MutableBoolean();
- singleton = load(loader, qProfiles, fromCache);
- profiler.stopInfo(fromCache.booleanValue());
+ singleton = load(loader, qProfiles);
+ profiler.stopInfo();
}
return singleton;
}
- private static ActiveRules load(ActiveRulesLoader loader, ModuleQProfiles qProfiles, MutableBoolean fromCache) {
+ private static ActiveRules load(ActiveRulesLoader loader, ModuleQProfiles qProfiles) {
Collection<String> qProfileKeys = getKeys(qProfiles);
Map<RuleKey, LoadedActiveRule> loadedRulesByKey = new HashMap<>();
try {
for (String qProfileKey : qProfileKeys) {
Collection<LoadedActiveRule> qProfileRules;
- qProfileRules = load(loader, qProfileKey, fromCache);
+ qProfileRules = load(loader, qProfileKey);
for (LoadedActiveRule r : qProfileRules) {
if (!loadedRulesByKey.containsKey(r.getRuleKey())) {
return builder.build();
}
- private static List<LoadedActiveRule> load(ActiveRulesLoader loader, String qProfileKey, MutableBoolean fromCache) throws IOException {
- return loader.load(qProfileKey, fromCache);
+ private static List<LoadedActiveRule> load(ActiveRulesLoader loader, String qProfileKey) throws IOException {
+ return loader.load(qProfileKey);
}
private static Collection<String> getKeys(ModuleQProfiles qProfiles) {
import java.util.List;
import java.util.Map;
-import javax.annotation.Nullable;
-
import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.sonar.api.rule.RuleKey;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonarqube.ws.Rules.Active;
import org.sonarqube.ws.Rules.Active.Param;
+import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.Rules.ActiveList;
import org.sonarqube.ws.Rules.Rule;
import org.sonarqube.ws.Rules.SearchResponse;
public class DefaultActiveRulesLoader implements ActiveRulesLoader {
private static final String RULES_SEARCH_URL = "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives&activation=true";
- private final WSLoader wsLoader;
+ private final BatchWsClient wsClient;
- public DefaultActiveRulesLoader(WSLoader wsLoader) {
- this.wsLoader = wsLoader;
+ public DefaultActiveRulesLoader(BatchWsClient wsClient) {
+ this.wsClient = wsClient;
}
@Override
- public List<LoadedActiveRule> load(String qualityProfileKey, @Nullable MutableBoolean fromCache) {
+ public List<LoadedActiveRule> load(String qualityProfileKey) {
List<LoadedActiveRule> ruleList = new LinkedList<>();
int page = 1;
int pageSize = 500;
int loaded = 0;
while (true) {
- WSLoaderResult<InputStream> result = wsLoader.loadStream(getUrl(qualityProfileKey, page, pageSize));
- SearchResponse response = loadFromStream(result.get());
+ GetRequest getRequest = new GetRequest(getUrl(qualityProfileKey, page, pageSize));
+ SearchResponse response = loadFromStream(wsClient.call(getRequest).contentStream());
List<LoadedActiveRule> pageRules = readPage(response);
ruleList.addAll(pageRules);
loaded += response.getPs();
break;
}
page++;
- if (fromCache != null) {
- fromCache.setValue(result.isFromCache());
- }
}
return ruleList;
import org.apache.commons.io.IOUtils;
-import org.sonar.batch.cache.WSLoaderResult;
-import org.sonar.batch.cache.WSLoader;
-
-import javax.annotation.Nullable;
-
-import org.apache.commons.lang.mutable.MutableBoolean;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonarqube.ws.Rules.ListResponse.Rule;
+import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.Rules.ListResponse;
import java.io.IOException;
public class DefaultRulesLoader implements RulesLoader {
private static final String RULES_SEARCH_URL = "/api/rules/list.protobuf";
- private final WSLoader wsLoader;
+ private final BatchWsClient wsClient;
- public DefaultRulesLoader(WSLoader wsLoader) {
- this.wsLoader = wsLoader;
+ public DefaultRulesLoader(BatchWsClient wsClient) {
+ this.wsClient = wsClient;
}
@Override
- public List<Rule> load(@Nullable MutableBoolean fromCache) {
- WSLoaderResult<InputStream> result = wsLoader.loadStream(RULES_SEARCH_URL);
- ListResponse list = loadFromStream(result.get());
- if (fromCache != null) {
- fromCache.setValue(result.isFromCache());
- }
+ public List<Rule> load() {
+ GetRequest getRequest = new GetRequest(RULES_SEARCH_URL);
+ ListResponse list = loadFromStream(wsClient.call(getRequest).contentStream());
return list.getRulesList();
}
package org.sonar.batch.rule;
import java.util.List;
-import javax.annotation.Nullable;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.sonarqube.ws.Rules.ListResponse.Rule;
public interface RulesLoader {
- List<Rule> load(@Nullable MutableBoolean fromCache);
+ List<Rule> load();
}
*/
package org.sonar.batch.rule;
-import org.apache.commons.lang.mutable.MutableBoolean;
-
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler;
private static Rules load(RulesLoader ref) {
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- MutableBoolean fromCache = new MutableBoolean();
- List<Rule> loadedRules = ref.load(fromCache);
+ List<Rule> loadedRules = ref.load();
RulesBuilder builder = new RulesBuilder();
for (Rule r : loadedRules) {
newRule.setInternalKey(r.getInternalKey());
}
- profiler.stopInfo(fromCache.booleanValue());
+ profiler.stopInfo();
return builder.build();
}
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.MessageException;
private static final List<String> NON_HERITED_PROPERTIES_FOR_CHILD = Lists.newArrayList(PROPERTY_PROJECT_BASEDIR, CoreProperties.WORKING_DIRECTORY, PROPERTY_MODULES,
CoreProperties.PROJECT_DESCRIPTION_PROPERTY);
- private static final String NON_ASSOCIATED_PROJECT_KEY = "project";
-
private final AnalysisProperties analysisProps;
- private final AnalysisMode analysisMode;
private File rootProjectWorkDir;
- public ProjectReactorBuilder(AnalysisProperties props, AnalysisMode analysisMode) {
+ public ProjectReactorBuilder(AnalysisProperties props) {
this.analysisProps = props;
- this.analysisMode = analysisMode;
}
public ProjectReactor execute() {
}
}
- private static void prepareNonAssociatedProject(Map<String, String> props, AnalysisMode mode) {
- if (mode.isIssues() && !props.containsKey(CoreProperties.PROJECT_KEY_PROPERTY)) {
- props.put(CoreProperties.PROJECT_KEY_PROPERTY, NON_ASSOCIATED_PROJECT_KEY);
- }
- }
-
protected ProjectDefinition defineRootProject(Map<String, String> rootProperties, @Nullable ProjectDefinition parent) {
- prepareNonAssociatedProject(rootProperties, analysisMode);
-
if (rootProperties.containsKey(PROPERTY_MODULES)) {
checkMandatoryProperties(rootProperties, MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT);
} else {
import org.sonar.batch.ProjectConfigurator;
import org.sonar.batch.analysis.AnalysisProperties;
import org.sonar.batch.analysis.AnalysisTempFolderProvider;
-import org.sonar.batch.analysis.AnalysisWSLoaderProvider;
import org.sonar.batch.analysis.DefaultAnalysisMode;
import org.sonar.batch.bootstrap.ExtensionInstaller;
import org.sonar.batch.bootstrap.ExtensionMatcher;
import org.sonar.batch.bootstrap.ExtensionUtils;
import org.sonar.batch.bootstrap.MetricProvider;
-import org.sonar.batch.cache.ProjectPersistentCacheProvider;
import org.sonar.batch.cpd.CpdExecutor;
import org.sonar.batch.cpd.index.SonarCpdBlockIndex;
import org.sonar.batch.events.EventBus;
ResourceTypes.class,
DefaultProjectTree.class,
ProjectReactorValidator.class,
- new AnalysisWSLoaderProvider(),
CodeColorizers.class,
MetricProvider.class,
ProjectConfigurator.class,
DefaultIssueCallback.class,
new RulesProvider(),
new ProjectRepositoriesProvider(),
- new ProjectPersistentCacheProvider(),
// temp
new AnalysisTempFolderProvider(),
}
private boolean isTherePreviousAnalysis() {
- if (getComponentByType(DefaultAnalysisMode.class).isNotAssociated()) {
- return false;
- }
-
return getComponentByType(ProjectRepositories.class).lastAnalysisDate() != null;
}
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.util.ProgressReport;
-import org.sonar.home.cache.DirectoryLock;
import java.io.File;
import java.io.IOException;
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- if (!Files.isHidden(file) && !DirectoryLock.LOCK_FILE_NAME.equals(file.getFileName().toString())) {
+ if (!Files.isHidden(file)) {
indexFile(inputFileBuilder, fileSystem, status, file, type);
}
return FileVisitResult.CONTINUE;
*/
package org.sonar.batch.task;
-import javax.annotation.CheckForNull;
import org.sonar.api.CoreProperties;
import org.sonar.api.task.Task;
import org.sonar.api.task.TaskDefinition;
import org.sonar.batch.analysis.AnalysisProperties;
-import org.sonar.batch.analysis.DefaultAnalysisMode;
-import org.sonar.batch.bootstrap.GlobalProperties;
-import org.sonar.batch.cache.ProjectSyncContainer;
import org.sonar.batch.scan.ProjectScanContainer;
import org.sonar.core.platform.ComponentContainer;
@Override
public void execute() {
AnalysisProperties props = new AnalysisProperties(taskProps.properties(), taskProps.property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH));
- if (isIssuesMode(props)) {
- String projectKey = getProjectKeyWithBranch(props);
- new ProjectSyncContainer(taskContainer, projectKey, false).execute();
- }
new ProjectScanContainer(taskContainer, props).execute();
}
-
- @CheckForNull
- private static String getProjectKeyWithBranch(AnalysisProperties props) {
- String projectKey = props.property(CoreProperties.PROJECT_KEY_PROPERTY);
- if (projectKey != null && props.property(CoreProperties.PROJECT_BRANCH_PROPERTY) != null) {
- projectKey = projectKey + ":" + props.property(CoreProperties.PROJECT_BRANCH_PROPERTY);
- }
- return projectKey;
- }
-
- private boolean isIssuesMode(AnalysisProperties props) {
- DefaultAnalysisMode mode = new DefaultAnalysisMode(taskContainer.getComponentByType(GlobalProperties.class), props);
- return mode.isIssues();
- }
-
}
--- /dev/null
+/*
+ * 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.batch;
+
+import org.apache.commons.lang.StringUtils;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.sonar.batch.bootstrap.BatchWsClient;
+import org.sonarqube.ws.client.WsRequest;
+import org.sonarqube.ws.client.WsResponse;
+
+import java.io.InputStream;
+import java.io.Reader;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class WsTestUtil {
+ public static void mockStream(BatchWsClient mock, String path, InputStream is) {
+ WsResponse response = mock(WsResponse.class);
+ when(response.contentStream()).thenReturn(is);
+ when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response);
+ }
+
+ public static void mockStream(BatchWsClient mock, InputStream is) {
+ WsResponse response = mock(WsResponse.class);
+ when(response.contentStream()).thenReturn(is);
+ when(mock.call(any(WsRequest.class))).thenReturn(response);
+ }
+
+ public static void mockReader(BatchWsClient mock, Reader reader) {
+ WsResponse response = mock(WsResponse.class);
+ when(response.contentReader()).thenReturn(reader);
+ when(mock.call(any(WsRequest.class))).thenReturn(response);
+ }
+
+ public static void mockReader(BatchWsClient mock, String path, Reader reader) {
+ WsResponse response = mock(WsResponse.class);
+ when(response.contentReader()).thenReturn(reader);
+ when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response);
+ }
+
+ public static void mockException(BatchWsClient mock, Exception e) {
+ when(mock.call(any(WsRequest.class))).thenThrow(e);
+ }
+
+ public static void mockException(BatchWsClient mock, String path, Exception e) {
+ when(mock.call(argThat(new RequestMatcher(path)))).thenThrow(e);
+ }
+
+ public static void verifyCall(BatchWsClient mock, String path) {
+ verify(mock).call(argThat(new RequestMatcher(path)));
+ }
+
+ private static class RequestMatcher extends BaseMatcher<WsRequest> {
+ private String path;
+
+ public RequestMatcher(String path) {
+ this.path = path;
+ }
+
+ @Override
+ public boolean matches(Object item) {
+ if (item == null) {
+ return false;
+ }
+ WsRequest request = (WsRequest) item;
+ return StringUtils.equals(request.getPath(), path);
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("request path (\"" + path + "\")");
+ }
+ }
+}
+++ /dev/null
-/*
- * 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.batch.analysis;
-
-import com.google.common.collect.ImmutableMap;
-import org.assertj.core.util.Maps;
-import org.junit.Test;
-import org.sonar.api.batch.AnalysisMode;
-import org.sonar.batch.bootstrap.BatchWsClient;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
-import org.sonar.home.cache.PersistentCache;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class AnalysisWSLoaderProviderTest {
-
- PersistentCache cache = mock(PersistentCache.class);
- BatchWsClient wsClient = mock(BatchWsClient.class);
- AnalysisMode mode = mock(AnalysisMode.class);
-
- AnalysisWSLoaderProvider underTest = new AnalysisWSLoaderProvider();
-
- @Test
- public void testDefault() {
- WSLoader loader = underTest.provide(mode, cache, wsClient, new AnalysisProperties(Maps.<String, String>newHashMap()));
- assertThat(loader.getDefaultStrategy()).isEqualTo(LoadStrategy.SERVER_ONLY);
- }
-
- @Test
- public void no_cache_by_default_in_issues_mode() {
- when(mode.isIssues()).thenReturn(true);
- WSLoader loader = underTest.provide(mode, cache, wsClient, new AnalysisProperties(Maps.<String, String>newHashMap()));
- assertThat(loader.getDefaultStrategy()).isEqualTo(LoadStrategy.SERVER_ONLY);
- }
-
- @Test
- public void enable_cache_in_issues_mode() {
- when(mode.isIssues()).thenReturn(true);
- WSLoader loader = underTest.provide(mode, cache, wsClient, new AnalysisProperties(ImmutableMap.of(AnalysisWSLoaderProvider.SONAR_USE_WS_CACHE, "true")));
- assertThat(loader.getDefaultStrategy()).isEqualTo(LoadStrategy.CACHE_ONLY);
- }
-}
package org.sonar.batch.bootstrap;
import java.io.File;
+import java.io.StringReader;
import java.util.List;
+
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.sonar.batch.WsTestUtil;
import org.sonar.core.platform.RemotePlugin;
import org.sonar.home.cache.FileCache;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Rule
public ExpectedException thrown = ExpectedException.none();
- FileCache fileCache = mock(FileCache.class);
- BatchWsClient wsClient = mock(BatchWsClient.class);
- BatchPluginPredicate pluginPredicate = mock(BatchPluginPredicate.class);
+ private FileCache fileCache = mock(FileCache.class);
+ private BatchWsClient wsClient;
+ private BatchPluginPredicate pluginPredicate = mock(BatchPluginPredicate.class);
+
+ @Before
+ public void setUp() {
+ wsClient = mock(BatchWsClient.class);
+ }
@Test
public void listRemotePlugins() {
-
- WSLoader wsLoader = mock(WSLoader.class);
- when(wsLoader.loadString("/deploy/plugins/index.txt")).thenReturn(new WSLoaderResult<>("checkstyle\nsqale", true));
- BatchPluginInstaller underTest = new BatchPluginInstaller(wsLoader, wsClient, fileCache, pluginPredicate);
+ WsTestUtil.mockReader(wsClient, "/deploy/plugins/index.txt", new StringReader("checkstyle\nsqale"));
+ BatchPluginInstaller underTest = new BatchPluginInstaller(wsClient, fileCache, pluginPredicate);
List<RemotePlugin> remotePlugins = underTest.listRemotePlugins();
assertThat(remotePlugins).extracting("key").containsOnly("checkstyle", "sqale");
File pluginJar = temp.newFile();
when(fileCache.get(eq("checkstyle-plugin.jar"), eq("fakemd5_1"), any(FileCache.Downloader.class))).thenReturn(pluginJar);
- WSLoader wsLoader = mock(WSLoader.class);
- BatchPluginInstaller underTest = new BatchPluginInstaller(wsLoader, wsClient, fileCache, pluginPredicate);
+ BatchPluginInstaller underTest = new BatchPluginInstaller(wsClient, fileCache, pluginPredicate);
RemotePlugin remote = new RemotePlugin("checkstyle").setFile("checkstyle-plugin.jar", "fakemd5_1");
File file = underTest.download(remote);
@Test
public void should_fail_to_get_plugin_index() {
+ WsTestUtil.mockException(wsClient, "/deploy/plugins/index.txt", new IllegalStateException());
thrown.expect(IllegalStateException.class);
- WSLoader wsLoader = mock(WSLoader.class);
- doThrow(new IllegalStateException()).when(wsLoader).loadString("/deploy/plugins/index.txt");
-
- new BatchPluginInstaller(wsLoader, wsClient, fileCache, pluginPredicate).installRemotes();
+ new BatchPluginInstaller(wsClient, fileCache, pluginPredicate).installRemotes();
}
}
Map<String, String> props = ImmutableMap.of(CoreProperties.WORKING_DIRECTORY, temp.getRoot().getAbsolutePath(),
CoreProperties.GLOBAL_WORKING_DIRECTORY, temp.getRoot().getAbsolutePath());
- GlobalContainer container = GlobalContainer.create(props, extensions, false);
+ GlobalContainer container = GlobalContainer.create(props, extensions);
container.doBeforeStart();
return container;
}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import com.google.common.io.Files;
-import java.io.File;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.util.Date;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.home.cache.PersistentCache;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class DefaultProjectCacheStatusTest {
- @Rule
- public TemporaryFolder tmp = new TemporaryFolder();
-
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
- ProjectCacheStatus cacheStatus;
- PersistentCache cache = mock(PersistentCache.class);
-
- @Before
- public void setUp() {
- when(cache.getDirectory()).thenReturn(tmp.getRoot().toPath());
- cacheStatus = new DefaultProjectCacheStatus(cache);
- }
-
- @Test
- public void errorSave() throws IOException {
- when(cache.getDirectory()).thenReturn(tmp.getRoot().toPath().resolve("unexistent_folder"));
- cacheStatus = new DefaultProjectCacheStatus(cache);
-
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Failed to write cache sync status");
- cacheStatus.save();
- }
-
- @Test
- public void errorStatus() throws IOException {
- Files.write("trash".getBytes(StandardCharsets.UTF_8), new File(tmp.getRoot(), "cache-sync-status"));
- cacheStatus = new DefaultProjectCacheStatus(cache);
-
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Failed to read cache sync status");
- cacheStatus.getSyncStatus();
- }
-
- @Test
- public void testSave() {
- cacheStatus.save();
- assertThat(cacheStatus.getSyncStatus()).isNotNull();
- assertThat(age(cacheStatus.getSyncStatus())).isLessThan(2000);
- }
-
- @Test
- public void testDelete() {
- cacheStatus.save();
- cacheStatus.delete();
- assertThat(cacheStatus.getSyncStatus()).isNull();
- }
-
- private long age(Date date) {
- return (new Date().getTime()) - date.getTime();
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import org.sonar.home.cache.PersistentCache;
-
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.HashMap;
-
-import static org.junit.Assert.*;
-import static org.assertj.core.api.Assertions.assertThat;
-import org.sonar.batch.bootstrap.GlobalProperties;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.Rule;
-import org.junit.rules.TemporaryFolder;
-
-public class GlobalPersistentCacheProviderTest {
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- private GlobalPersistentCacheProvider provider;
- private GlobalProperties globalProperties;
-
- @Before
- public void setUp() {
- HashMap<String, String> map = new HashMap<>();
- map.put("sonar.userHome", temp.getRoot().getAbsolutePath());
- globalProperties = new GlobalProperties(map);
- provider = new GlobalPersistentCacheProvider();
- }
-
- @Test
- public void test_path() {
- PersistentCache cache = provider.provide(globalProperties);
- assertThat(cache.getDirectory()).isEqualTo(temp.getRoot().toPath()
- .resolve("ws_cache")
- .resolve("http%3A%2F%2Flocalhost%3A9000")
- .resolve("global"));
- }
-
- @Test
- public void test_singleton() {
- assertTrue(provider.provide(globalProperties) == provider.provide(globalProperties));
- }
-
- @Test
- public void test_without_sonar_home() {
- globalProperties = new GlobalProperties(new HashMap<String, String>());
- PersistentCache cache = provider.provide(globalProperties);
- assertThat(cache.getDirectory().toAbsolutePath().toString()).startsWith(findHome().toAbsolutePath().toString());
-
- }
-
- private static Path findHome() {
- String home = System.getenv("SONAR_USER_HOME");
-
- if (home != null) {
- return Paths.get(home);
- }
-
- home = System.getProperty("user.home");
- return Paths.get(home, ".sonar");
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import com.google.common.collect.ImmutableList;
-import java.util.Date;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.sonar.batch.repository.QualityProfileLoader;
-import org.sonar.batch.rule.ActiveRulesLoader;
-import org.sonar.batch.rule.LoadedActiveRule;
-import org.sonar.batch.rule.RulesLoader;
-import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
-
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
-
-public class NonAssociatedCacheSynchronizerTest {
- private NonAssociatedCacheSynchronizer synchronizer;
-
- @Mock
- private RulesLoader rulesLoader;
- @Mock
- private QualityProfileLoader qualityProfileLoader;
- @Mock
- private ActiveRulesLoader activeRulesLoader;
- @Mock
- private ProjectCacheStatus cacheStatus;
-
- @Before
- public void setUp() {
- MockitoAnnotations.initMocks(this);
-
- QualityProfile pf = QualityProfile.newBuilder().setKey("profile").setName("profile").setLanguage("lang").build();
- LoadedActiveRule ar = new LoadedActiveRule();
-
- when(qualityProfileLoader.loadDefault(null, null)).thenReturn(ImmutableList.of(pf));
- when(activeRulesLoader.load("profile", null)).thenReturn(ImmutableList.of(ar));
-
- synchronizer = new NonAssociatedCacheSynchronizer(rulesLoader, qualityProfileLoader, activeRulesLoader, cacheStatus);
- }
-
- @Test
- public void dont_sync_if_exists() {
- when(cacheStatus.getSyncStatus()).thenReturn(new Date());
- synchronizer.execute(false);
- verifyZeroInteractions(rulesLoader, qualityProfileLoader, activeRulesLoader);
- }
-
- @Test
- public void always_sync_if_force() {
- when(cacheStatus.getSyncStatus()).thenReturn(new Date());
- synchronizer.execute(true);
- checkSync();
- }
-
- @Test
- public void sync_if_doesnt_exist() {
- synchronizer.execute(false);
- checkSync();
- }
-
- private void checkSync() {
- verify(cacheStatus).getSyncStatus();
- verify(cacheStatus).save();
- verify(rulesLoader).load(null);
- verify(qualityProfileLoader).loadDefault(null, null);
- verify(activeRulesLoader).load("profile", null);
-
- verifyNoMoreInteractions(qualityProfileLoader, activeRulesLoader);
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import com.google.common.base.Function;
-import com.google.common.collect.ImmutableList;
-import java.io.IOException;
-import java.util.Date;
-import java.util.HashMap;
-import org.apache.commons.lang.mutable.MutableBoolean;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.batch.analysis.AnalysisProperties;
-import org.sonar.batch.analysis.DefaultAnalysisMode;
-import org.sonar.batch.repository.DefaultProjectRepositoriesLoader;
-import org.sonar.batch.repository.DefaultQualityProfileLoader;
-import org.sonar.batch.repository.DefaultServerIssuesLoader;
-import org.sonar.batch.repository.ProjectRepositories;
-import org.sonar.batch.repository.ProjectRepositoriesLoader;
-import org.sonar.batch.repository.QualityProfileLoader;
-import org.sonar.batch.repository.ServerIssuesLoader;
-import org.sonar.batch.repository.user.UserRepositoryLoader;
-import org.sonar.batch.rule.ActiveRulesLoader;
-import org.sonar.batch.rule.DefaultActiveRulesLoader;
-import org.sonar.batch.rule.LoadedActiveRule;
-import org.sonar.batch.rule.RulesLoader;
-import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyBoolean;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
-
-public class ProjectCacheSynchronizerTest {
- private static final String PROJECT_KEY = "org.codehaus.sonar-plugins:sonar-scm-git-plugin";
-
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
- @Mock
- private ProjectDefinition project;
- @Mock
- private ProjectCacheStatus cacheStatus;
- @Mock
- private DefaultAnalysisMode analysisMode;
- @Mock
- private AnalysisProperties properties;
- @Mock
- private RulesLoader rulesLoader;
-
- private ServerIssuesLoader issuesLoader;
- private UserRepositoryLoader userRepositoryLoader;
- private QualityProfileLoader qualityProfileLoader;
- private ActiveRulesLoader activeRulesLoader;
- private ProjectRepositoriesLoader projectRepositoriesLoader;
-
- @Before
- public void setUp() throws IOException {
- MockitoAnnotations.initMocks(this);
-
- when(analysisMode.isIssues()).thenReturn(true);
- when(properties.properties()).thenReturn(new HashMap<String, String>());
- }
-
- private ProjectCacheSynchronizer createMockedLoaders(boolean projectExists, Date lastAnalysisDate) {
- issuesLoader = mock(DefaultServerIssuesLoader.class);
- userRepositoryLoader = mock(UserRepositoryLoader.class);
- qualityProfileLoader = mock(DefaultQualityProfileLoader.class);
- activeRulesLoader = mock(DefaultActiveRulesLoader.class);
- projectRepositoriesLoader = mock(DefaultProjectRepositoriesLoader.class);
-
- QualityProfile pf = QualityProfile.newBuilder().setKey("profile").setName("profile").setLanguage("lang").build();
- LoadedActiveRule ar = new LoadedActiveRule();
- ProjectRepositories repo = mock(ProjectRepositories.class);
-
- when(qualityProfileLoader.load(PROJECT_KEY, null, null)).thenReturn(ImmutableList.of(pf));
- when(qualityProfileLoader.loadDefault(null, null)).thenReturn(ImmutableList.of(pf));
- when(activeRulesLoader.load("profile", null)).thenReturn(ImmutableList.of(ar));
- when(repo.lastAnalysisDate()).thenReturn(lastAnalysisDate);
- when(repo.exists()).thenReturn(projectExists);
- when(projectRepositoriesLoader.load(anyString(), anyBoolean(), any(MutableBoolean.class))).thenReturn(repo);
-
- return new ProjectCacheSynchronizer(rulesLoader, qualityProfileLoader, projectRepositoriesLoader, activeRulesLoader, issuesLoader, userRepositoryLoader, cacheStatus);
- }
-
- @Test
- public void testLoadersUsage() {
- ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date());
- synchronizer.load(PROJECT_KEY, false);
-
- verify(issuesLoader).load(eq(PROJECT_KEY), any(Function.class));
- verify(rulesLoader).load(null);
- verify(qualityProfileLoader).load(PROJECT_KEY, null, null);
- verify(activeRulesLoader).load("profile", null);
- verify(projectRepositoriesLoader).load(eq(PROJECT_KEY), eq(true), any(MutableBoolean.class));
-
- verifyNoMoreInteractions(issuesLoader, userRepositoryLoader, qualityProfileLoader, activeRulesLoader, projectRepositoriesLoader);
- }
-
- @Test
- public void testLoadersUsage_NoLastAnalysis() {
- ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, null);
- synchronizer.load(PROJECT_KEY, false);
-
- verify(projectRepositoriesLoader).load(eq(PROJECT_KEY), eq(true), any(MutableBoolean.class));
- verify(qualityProfileLoader).load(PROJECT_KEY, null, null);
- verify(activeRulesLoader).load("profile", null);
-
- verifyNoMoreInteractions(issuesLoader, userRepositoryLoader, qualityProfileLoader, activeRulesLoader, projectRepositoriesLoader);
- }
-
- @Test
- public void testLoadersUsage_ProjectDoesntExist() {
- ProjectCacheSynchronizer synchronizer = createMockedLoaders(false, null);
- synchronizer.load(PROJECT_KEY, false);
-
- verify(projectRepositoriesLoader).load(eq(PROJECT_KEY), eq(true), any(MutableBoolean.class));
- verify(qualityProfileLoader).loadDefault(null, null);
- verify(activeRulesLoader).load("profile", null);
-
- verifyNoMoreInteractions(issuesLoader, userRepositoryLoader, qualityProfileLoader, activeRulesLoader, projectRepositoriesLoader);
- }
-
- @Test
- public void testLastAnalysisToday() {
- ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date());
-
- when(cacheStatus.getSyncStatus()).thenReturn(new Date());
- synchronizer.load(PROJECT_KEY, false);
-
- verify(cacheStatus).getSyncStatus();
- verifyNoMoreInteractions(issuesLoader, userRepositoryLoader, qualityProfileLoader, activeRulesLoader, projectRepositoriesLoader, cacheStatus);
- }
-
- @Test
- public void testLastAnalysisYesterday() {
- ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date());
-
- Date d = new Date(new Date().getTime() - 60 * 60 * 24 * 1000);
- when(cacheStatus.getSyncStatus()).thenReturn(d);
- synchronizer.load(PROJECT_KEY, false);
-
- verify(cacheStatus).save();
- verify(cacheStatus).getSyncStatus();
- }
-
- @Test
- public void testDontFailOnError() {
- ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date());
-
- Date d = new Date(new Date().getTime() - 60 * 60 * 24 * 1000);
- when(cacheStatus.getSyncStatus()).thenReturn(d);
-
- when(projectRepositoriesLoader.load(anyString(), anyBoolean(), any(MutableBoolean.class))).thenThrow(IllegalStateException.class);
- synchronizer.load(PROJECT_KEY, false);
-
- verify(cacheStatus).getSyncStatus();
- verifyNoMoreInteractions(cacheStatus);
- }
-
- @Test
- public void testForce() {
- ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date());
-
- when(cacheStatus.getSyncStatus()).thenReturn(new Date());
- synchronizer.load(PROJECT_KEY, true);
-
- verify(cacheStatus).save();
- verify(cacheStatus).getSyncStatus();
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import org.sonar.api.batch.bootstrap.ProjectKey;
-
-import org.sonar.batch.util.BatchUtils;
-import org.sonar.batch.analysis.DefaultAnalysisMode;
-import org.junit.Rule;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.batch.bootstrap.GlobalProperties;
-import org.sonar.batch.cache.ProjectPersistentCacheProvider;
-
-import java.io.File;
-import java.nio.file.Path;
-import java.util.Collections;
-
-import static org.mockito.Mockito.mock;
-import org.junit.Before;
-import static org.assertj.core.api.Assertions.assertThat;
-import org.junit.Test;
-
-public class ProjectPersistentCacheProviderTest {
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- private ProjectPersistentCacheProvider provider = null;
- private GlobalProperties props = null;
- private DefaultAnalysisMode mode = null;
- private ProjectKey key = null;
-
- @Before
- public void prepare() {
- key = new ProjectKeySupplier("proj");
- props = new GlobalProperties(Collections.<String, String>emptyMap());
- mode = mock(DefaultAnalysisMode.class);
- provider = new ProjectPersistentCacheProvider();
- }
-
- @Test
- public void test_singleton() {
- assertThat(provider.provide(props, mode, key)).isEqualTo(provider.provide(props, mode, key));
- }
-
- @Test
- public void test_cache_dir() {
- assertThat(provider.provide(props, mode, key).getDirectory().toFile()).exists().isDirectory();
- }
-
- @Test
- public void test_home() {
- File f = temp.getRoot();
- props.properties().put("sonar.userHome", f.getAbsolutePath());
- Path expected = f.toPath()
- .resolve("ws_cache")
- .resolve("http%3A%2F%2Flocalhost%3A9000")
- .resolve( BatchUtils.getServerVersion())
- .resolve("projects")
- .resolve("proj");
-
- assertThat(provider.provide(props, mode, key).getDirectory()).isEqualTo(expected);
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import java.util.HashMap;
-import org.junit.Test;
-import org.sonar.batch.bootstrap.GlobalProperties;
-import org.sonar.core.platform.ComponentContainer;
-import org.sonar.home.cache.PersistentCache;
-import org.sonar.scanner.protocol.input.ProjectRepositories;
-import org.sonarqube.ws.client.WsClient;
-
-import static org.mockito.Mockito.mock;
-
-public class ProjectSyncContainerTest {
- private ComponentContainer createParentContainer() {
- PersistentCache cache = mock(PersistentCache.class);
- WsClient server = mock(WsClient.class);
-
- GlobalProperties globalProps = new GlobalProperties(new HashMap<String, String>());
- ComponentContainer parent = new ComponentContainer();
- parent.add(cache);
- parent.add(server);
- parent.add(globalProps);
- return parent;
- }
-
- @Test
- public void testProjectRepository() {
- ProjectSyncContainer container = new ProjectSyncContainer(createParentContainer(), "my:project", true);
- container.doBeforeStart();
- container.getPicoContainer().start();
- container.getComponentByType(ProjectRepositories.class);
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.sonar.batch.bootstrap.BatchWsClient;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
-import org.sonar.home.cache.PersistentCache;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class StrategyWSLoaderProviderTest {
- @Mock
- private PersistentCache cache;
-
- @Mock
- private BatchWsClient client;
-
- @Before
- public void setUp() {
- MockitoAnnotations.initMocks(this);
- }
-
- @Test
- public void testStrategy() {
- StrategyWSLoaderProvider provider = new StrategyWSLoaderProvider(LoadStrategy.CACHE_FIRST);
- WSLoader wsLoader = provider.provide(cache, client);
-
- assertThat(wsLoader.getDefaultStrategy()).isEqualTo(LoadStrategy.CACHE_FIRST);
- }
-
- @Test
- public void testSingleton() {
- StrategyWSLoaderProvider provider = new StrategyWSLoaderProvider(LoadStrategy.CACHE_FIRST);
- WSLoader wsLoader = provider.provide(cache, client);
-
- assertThat(provider.provide(null, null)).isEqualTo(wsLoader);
- }
-}
+++ /dev/null
-/*
- * 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.batch.cache;
-
-import java.io.IOException;
-import java.io.InputStream;
-import org.apache.commons.io.IOUtils;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.mockito.InOrder;
-import org.mockito.Mockito;
-import org.sonar.batch.bootstrap.BatchWsClient;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
-import org.sonar.home.cache.PersistentCache;
-import org.sonarqube.ws.client.HttpException;
-import org.sonarqube.ws.client.MockWsResponse;
-import org.sonarqube.ws.client.WsRequest;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.inOrder;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
-
-public class WSLoaderTest {
- private final static String ID = "dummy";
- private final static String cacheValue = "cache";
- private final static String serverValue = "server";
-
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
- BatchWsClient ws = mock(BatchWsClient.class, Mockito.RETURNS_DEEP_STUBS);
- PersistentCache cache = mock(PersistentCache.class);
-
- @Test
- public void dont_retry_server_offline() throws IOException {
- turnServerOffline();
- when(cache.getString(ID)).thenReturn(cacheValue);
- WSLoader underTest = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
-
- assertResult(underTest.loadString(ID), cacheValue, true);
- assertResult(underTest.loadString(ID), cacheValue, true);
-
- assertUsedServer(1);
- assertUsedCache(2);
- }
-
- @Test
- public void get_stream_from_cache() throws IOException {
- InputStream is = IOUtils.toInputStream("is");
- when(cache.getStream(ID)).thenReturn(is);
-
- WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, ws);
- WSLoaderResult<InputStream> result = loader.loadStream(ID);
-
- assertThat(result.get()).isEqualTo(is);
- verify(cache).getStream(ID);
- verifyNoMoreInteractions(cache, ws);
- }
-
- @Test
- public void put_stream_in_cache() throws IOException {
- InputStream input = IOUtils.toInputStream("is");
-
- when(ws.call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(input));
- when(cache.getStream(ID)).thenReturn(input);
-
- // SERVER_FIRST -> load from server then put to cache
- WSLoader underTest = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
- WSLoaderResult<InputStream> result = underTest.loadStream(ID);
- assertThat(result.get()).isEqualTo(input);
-
- InOrder inOrder = inOrder(ws, cache);
- inOrder.verify(ws).call(any(WsRequest.class));
- inOrder.verify(cache).put(eq(ID), any(InputStream.class));
- inOrder.verify(cache).getStream(ID);
- verifyNoMoreInteractions(cache, ws);
- }
-
- @Test
- public void test_cache_strategy_fallback() throws IOException {
- turnCacheEmpty();
- when(ws.call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(serverValue));
- WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, ws);
-
- assertResult(loader.loadString(ID), serverValue, false);
-
- InOrder inOrder = inOrder(ws, cache);
- inOrder.verify(cache).getString(ID);
- inOrder.verify(ws).call(any(WsRequest.class));
- }
-
- @Test
- public void test_server_strategy_fallback() throws IOException {
- turnServerOffline();
- when(cache.getString(ID)).thenReturn(cacheValue);
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
-
- assertResult(loader.loadString(ID), cacheValue, true);
-
- InOrder inOrder = inOrder(ws, cache);
- inOrder.verify(ws).call(any(WsRequest.class));
- inOrder.verify(cache).getString(ID);
- }
-
- @Test
- public void test_put_cache() throws IOException {
- when(ws.call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(serverValue));
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
- loader.loadString(ID);
- verify(cache).put(ID, serverValue.getBytes());
- }
-
- @Test
- public void test_throw_cache_exception_fallback() throws IOException {
- turnServerOffline();
-
- when(cache.getString(ID)).thenThrow(new NullPointerException());
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
-
- try {
- loader.loadString(ID);
- fail("NPE expected");
- } catch (NullPointerException e) {
- assertUsedServer(1);
- assertUsedCache(1);
- }
- }
-
- @Test
- public void test_throw_cache_exception() throws IOException {
- when(cache.getString(ID)).thenThrow(new IllegalStateException());
-
- WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, ws);
-
- try {
- loader.loadString(ID);
- fail("IllegalStateException expected");
- } catch (IllegalStateException e) {
- assertUsedServer(0);
- assertUsedCache(1);
- }
- }
-
- @Test
- public void test_throw_http_exceptions() {
- when(ws.call(any(WsRequest.class))).thenThrow(new HttpException("url", 500));
-
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
-
- try {
- loader.loadString(ID);
- fail("IllegalStateException expected");
- } catch (HttpException e) {
- // cache should not be used
- verifyNoMoreInteractions(cache);
- }
- }
-
- @Test
- public void test_server_only_not_available() {
- turnServerOffline();
-
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Server is not available");
-
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, ws);
- loader.loadString(ID);
- }
-
- @Test
- public void test_server_cache_not_available() throws IOException {
- turnServerOffline();
- turnCacheEmpty();
-
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Server is not accessible and data is not cached");
-
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
- loader.loadString(ID);
- }
-
- @Test
- public void test_cache_only_available() throws IOException {
- turnCacheEmpty();
-
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Data is not cached");
-
- WSLoader loader = new WSLoader(LoadStrategy.CACHE_ONLY, cache, ws);
- loader.loadString(ID);
- }
-
- @Test
- public void test_server_strategy() throws IOException {
- when(ws.call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(serverValue));
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
- assertResult(loader.loadString(ID), serverValue, false);
-
- // should not fetch from cache
- verify(cache).put(ID, serverValue.getBytes());
- verifyNoMoreInteractions(cache);
- }
-
- @Test(expected = IllegalStateException.class)
- public void test_server_only() throws IOException {
- turnServerOffline();
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, ws);
- loader.loadString(ID);
- }
-
- @Test
- public void test_string() {
- when(ws.call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(serverValue));
- WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws);
- assertResult(loader.loadString(ID), serverValue, false);
- }
-
- private void assertUsedCache(int times) throws IOException {
- verify(cache, times(times)).getString(ID);
- }
-
- private void assertUsedServer(int times) {
- verify(ws, times(times)).call(any(WsRequest.class));
- }
-
- private void assertResult(WSLoaderResult<String> result, String expected, boolean fromCache) {
- assertThat(result).isNotNull();
- assertThat(result.get()).isEqualTo(expected);
- assertThat(result.isFromCache()).isEqualTo(fromCache);
- }
-
- private void turnServerOffline() {
- when(ws.call(any(WsRequest.class))).thenThrow(new IllegalStateException());
- }
-
- private void turnCacheEmpty() throws IOException {
- when(cache.getString(ID)).thenReturn(null);
- }
-}
*/
package org.sonar.batch.issue.tracking;
-import org.sonar.batch.cache.WSLoader.LoadStrategy;
-import org.sonar.batch.cache.WSLoaderResult;
-import org.sonar.batch.cache.WSLoader;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.HttpDownloader;
+import org.sonar.batch.WsTestUtil;
+import org.sonar.batch.bootstrap.BatchWsClient;
+import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
-import static org.mockito.Matchers.any;
-
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.verify;
-import static org.mockito.Mockito.when;
public class DefaultServerLineHashesLoaderTest {
+ private BatchWsClient wsClient;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void before() {
+ wsClient = mock(BatchWsClient.class);
}
@Test
public void should_download_source_from_ws_if_preview_mode() {
- WSLoader wsLoader = mock(WSLoader.class);
- when(wsLoader.loadString(anyString(), any(LoadStrategy.class))).thenReturn(new WSLoaderResult<>("ae12\n\n43fb", true));
-
- ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsLoader);
+ WsTestUtil.mockReader(wsClient, new StringReader("ae12\n\n43fb"));
+ ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsClient);
- String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Bar.c", null);
+ String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Bar.c");
assertThat(hashes).containsOnly("ae12", "", "43fb");
- verify(wsLoader).loadString("/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FBar.c", LoadStrategy.CACHE_FIRST);
+ WsTestUtil.verifyCall(wsClient, "/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FBar.c");
}
@Test
public void should_download_source_with_space_from_ws_if_preview_mode() {
- WSLoader server = mock(WSLoader.class);
- when(server.loadString(anyString(), any(LoadStrategy.class))).thenReturn(new WSLoaderResult<>("ae12\n\n43fb", true));
+ WsTestUtil.mockReader(wsClient, new StringReader("ae12\n\n43fb"));
+ ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsClient);
- ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(server);
-
- MutableBoolean fromCache = new MutableBoolean();
- String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Foo Bar.c", fromCache);
- assertThat(fromCache.booleanValue()).isTrue();
+ String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Foo Bar.c");
assertThat(hashes).containsOnly("ae12", "", "43fb");
- verify(server).loadString("/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FFoo+Bar.c", LoadStrategy.CACHE_FIRST);
+ WsTestUtil.verifyCall(wsClient, "/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FFoo+Bar.c");
}
@Test
public void should_fail_to_download_source_from_ws() throws URISyntaxException {
- WSLoader server = mock(WSLoader.class);
- when(server.loadString(anyString(), any(LoadStrategy.class))).thenThrow(new HttpDownloader.HttpException(new URI(""), 500));
-
- ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(server);
+ WsTestUtil.mockException(wsClient, new HttpDownloader.HttpException(new URI(""), 500));
+ ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsClient);
thrown.expect(HttpDownloader.HttpException.class);
- lastSnapshots.getLineHashes("foo", null);
+ lastSnapshots.getLineHashes("foo");
}
}
FileUtils.write(ioFile, source, StandardCharsets.UTF_8);
when(file.key()).thenReturn(key);
when(file.status()).thenReturn(InputFile.Status.CHANGED);
- when(lastSnapshots.getLineHashes(key, null)).thenReturn(new String[] {md5Hex(source)});
+ when(lastSnapshots.getLineHashes(key)).thenReturn(new String[] {md5Hex(source)});
assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source));
- verify(lastSnapshots).getLineHashes(key, null);
+ verify(lastSnapshots).getLineHashes(key);
assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source));
Mockito.verifyNoMoreInteractions(lastSnapshots);
import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
import org.sonar.batch.repository.QualityProfileLoader;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.mutable.MutableBoolean;
import javax.annotation.Nullable;
import org.sonar.scanner.protocol.input.GlobalRepositories;
import org.sonar.scanner.protocol.input.ScannerInput.ServerIssue;
import com.google.common.base.Function;
-import com.google.common.collect.HashBasedTable;
-import com.google.common.collect.Table;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
-import javax.annotation.Nullable;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.sonar.api.CoreProperties;
import org.sonar.api.Plugin;
import org.sonar.api.batch.debt.internal.DefaultDebtModel;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.server.rule.RulesDefinition;
-import org.sonar.api.server.rule.RulesDefinition.Repository;
-import org.sonar.api.utils.DateUtils;
import org.sonar.batch.bootstrapper.Batch;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
-import org.sonar.batch.bootstrapper.IssueListener;
import org.sonar.batch.bootstrapper.LogOutput;
import org.sonar.batch.issue.tracking.ServerLineHashesLoader;
import org.sonar.batch.report.ReportPublisher;
-import org.sonar.batch.repository.FileData;
import org.sonar.batch.repository.GlobalRepositoriesLoader;
-import org.sonar.batch.repository.ProjectRepositories;
import org.sonar.batch.repository.ProjectRepositoriesLoader;
-import org.sonar.batch.repository.QualityProfileLoader;
import org.sonar.batch.repository.ServerIssuesLoader;
-import org.sonar.batch.rule.ActiveRulesLoader;
-import org.sonar.batch.rule.LoadedActiveRule;
-import org.sonar.batch.rule.RulesLoader;
-import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
-import org.sonarqube.ws.Rules.ListResponse.Rule;
/**
* Main utility class for writing batch medium tests.
}
@Override
- public List<Rule> load(@Nullable MutableBoolean fromCache) {
+ public List<Rule> load() {
return rules;
}
}
}
@Override
- public List<LoadedActiveRule> load(String qualityProfileKey, MutableBoolean fromCache) {
+ public List<LoadedActiveRule> load(String qualityProfileKey) {
return activeRules;
}
}
private GlobalRepositories ref = new GlobalRepositories();
@Override
- public GlobalRepositories load(@Nullable MutableBoolean fromCache) {
+ public GlobalRepositories load() {
return ref;
}
private Date lastAnalysisDate;
@Override
- public ProjectRepositories load(String projectKey, boolean isIssuesMode, @Nullable MutableBoolean fromCache) {
+ public ProjectRepositories load(String projectKey, boolean isIssuesMode) {
Table<String, String, String> settings = HashBasedTable.create();
return new ProjectRepositories(settings, fileDataTable, lastAnalysisDate);
}
}
@Override
- public List<QualityProfile> load(String projectKey, String profileName, MutableBoolean fromCache) {
+ public List<QualityProfile> load(String projectKey, String profileName) {
return qualityProfiles;
}
@Override
- public List<QualityProfile> loadDefault(String profileName, MutableBoolean fromCache) {
+ public List<QualityProfile> loadDefault(String profileName) {
return qualityProfiles;
}
}
}
@Override
- public boolean load(String componentKey, Function<ServerIssue, Void> consumer) {
+ public void load(String componentKey, Function<ServerIssue, Void> consumer) {
for (ServerIssue serverIssue : serverIssues) {
consumer.apply(serverIssue);
}
- return true;
}
}
private Map<String, String[]> byKey = new HashMap<>();
@Override
- public String[] getLineHashes(String fileKey, @Nullable MutableBoolean fromCache) {
+ public String[] getLineHashes(String fileKey) {
if (byKey.containsKey(fileKey)) {
return byKey.get(fileKey);
} else {
+++ /dev/null
-/*
- * 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.batch.mediumtest.cache;
-
-import org.junit.rules.TemporaryFolder;
-
-import org.sonar.batch.mediumtest.TaskResult;
-import org.sonar.batch.mediumtest.BatchMediumTester.TaskBuilder;
-import org.sonar.batch.mediumtest.LogOutputRecorder;
-import org.sonar.batch.repository.FileData;
-import com.google.common.collect.ImmutableMap;
-
-import java.util.Date;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import org.junit.After;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.CoreProperties;
-import org.sonar.batch.mediumtest.BatchMediumTester;
-import org.sonar.xoo.XooPlugin;
-import org.sonar.xoo.rule.XooRulesDefinition;
-
-public class CacheSyncTest {
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- private BatchMediumTester tester;
-
- @After
- public void stop() {
- if (tester != null) {
- tester.stop();
- tester = null;
- }
- }
-
- @Test
- public void testExecuteTask() {
- LogOutputRecorder logOutput = new LogOutputRecorder();
-
- tester = BatchMediumTester.builder()
- .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES,
- "sonar.verbose", "true"))
- .registerPlugin("xoo", new XooPlugin())
- .addRules(new XooRulesDefinition())
- .addQProfile("lang", "name")
- .addActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "my/internal/key", "xoo")
- .setPreviousAnalysisDate(new Date())
- .addFileData("test-project", "file1", new FileData("hash", "123456789"))
- .setLogOutput(logOutput)
- .build();
-
- tester.start();
- executeTask(tester.newTask());
- assertThat(logOutput.getAsString()).contains("Cache for project [key] not found, synchronizing");
- }
-
- @Test
- public void testSyncFirstTime() {
- LogOutputRecorder logOutput = new LogOutputRecorder();
-
- tester = BatchMediumTester.builder()
- .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES,
- "sonar.verbose", "true"))
- .registerPlugin("xoo", new XooPlugin())
- .addRules(new XooRulesDefinition())
- .addQProfile("lang", "name")
- .addActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "my/internal/key", "xoo")
- .setPreviousAnalysisDate(new Date())
- .addFileData("test-project", "file1", new FileData("hash", "123456789"))
- .setLogOutput(logOutput)
- .build();
-
- tester.start();
- tester.syncProject("test-project");
- assertThat(logOutput.getAsString()).contains("Cache for project [test-project] not found");
- }
-
- @Test
- public void testSyncTwice() {
- LogOutputRecorder logOutput = new LogOutputRecorder();
-
- tester = BatchMediumTester.builder()
- .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES,
- "sonar.verbose", "true"))
- .registerPlugin("xoo", new XooPlugin())
- .addRules(new XooRulesDefinition())
- .addQProfile("lang", "name")
- .addActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "my/internal/key", "xoo")
- .setPreviousAnalysisDate(new Date())
- .addFileData("test-project", "file1", new FileData("hash", "123456789"))
- .setLogOutput(logOutput)
- .build();
-
- tester.start();
- tester.syncProject("test-project");
- tester.syncProject("test-project");
- assertThat(logOutput.getAsString()).contains("-- Found project [test-project]");
- assertThat(logOutput.getAsString()).contains("not found, synchronizing data");
- assertThat(logOutput.getAsString()).contains("], synchronizing data (forced)..");
- }
-
- @Test
- public void testNonAssociated() {
- LogOutputRecorder logOutput = new LogOutputRecorder();
-
- tester = BatchMediumTester.builder()
- .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES))
- .registerPlugin("xoo", new XooPlugin())
- .addRules(new XooRulesDefinition())
- .addQProfile("lang", "name")
- .addActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "my/internal/key", "xoo")
- .setPreviousAnalysisDate(new Date())
- .addFileData("test-project", "file1", new FileData("hash", "123456789"))
- .setLogOutput(logOutput)
- .build();
-
- tester.start();
- tester.syncProject(null);
-
- assertThat(logOutput.getAsString()).contains("Cache not found, synchronizing data");
- }
-
- private TaskResult executeTask(TaskBuilder builder) {
- builder.property("sonar.projectKey", "key");
- builder.property("sonar.projectVersion", "1.0");
- builder.property("sonar.projectName", "key");
- builder.property("sonar.projectBaseDir", temp.getRoot().getAbsolutePath());
- builder.property("sonar.sources", temp.getRoot().getAbsolutePath());
- return builder.start();
- }
-
-}
+++ /dev/null
-/*
- * 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.batch.mediumtest.issuesmode;
-
-import com.google.common.collect.ImmutableMap;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.filefilter.FileFilterUtils;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.utils.log.LogTester;
-import org.sonar.batch.mediumtest.BatchMediumTester;
-import org.sonar.batch.mediumtest.TaskResult;
-import org.sonar.xoo.XooPlugin;
-import org.sonar.xoo.rule.XooRulesDefinition;
-
-import java.io.File;
-import java.io.IOException;
-
-public class NonAssociatedProject {
- @org.junit.Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @org.junit.Rule
- public LogTester logTester = new LogTester();
-
- public BatchMediumTester tester;
-
- @Before
- public void prepare() throws IOException {
- tester = BatchMediumTester.builder()
- .bootstrapProperties(ImmutableMap.of(
- CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES,
- CoreProperties.GLOBAL_WORKING_DIRECTORY, temp.newFolder().getAbsolutePath()))
- .registerPlugin("xoo", new XooPlugin())
- .addQProfile("xoo", "Sonar Way")
- .addRules(new XooRulesDefinition())
- .addRule("manual:MyManualIssue", "manual", "MyManualIssue", "My manual issue")
- .addRule("manual:MyManualIssueDup", "manual", "MyManualIssue", "My manual issue")
- .addActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", null, "xoo")
- .addActiveRule("xoo", "OneIssueOnDirPerFile", null, "OneIssueOnDirPerFile", "MAJOR", null, "xoo")
- .addActiveRule("xoo", "OneIssuePerModule", null, "OneIssuePerModule", "MAJOR", null, "xoo")
- .addActiveRule("manual", "MyManualIssue", null, "My manual issue", "MAJOR", null, null)
- .setAssociated(false)
- .build();
- tester.start();
- }
-
- @After
- public void stop() {
- tester.stop();
- }
-
- private File copyProject(String path) throws Exception {
- File projectDir = temp.newFolder();
- File originalProjectDir = new File(IssueModeAndReportsMediumTest.class.getResource(path).toURI());
- FileUtils.copyDirectory(originalProjectDir, projectDir, FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter(".sonar")));
- return projectDir;
- }
-
- @Test
- public void testNonAssociated() throws Exception {
- File projectDir = copyProject("/mediumtest/xoo/multi-modules-sample-not-associated");
-
- TaskResult result = tester
- .newScanTask(new File(projectDir, "sonar-project.properties"))
- .start();
-
- }
-}
import org.junit.BeforeClass;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.api.utils.MessageException;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.sonar.batch.repository.GlobalRepositoriesLoader;
import org.sonar.scanner.protocol.input.GlobalRepositories;
import org.sonar.batch.bootstrapper.Batch;
boolean withCause = false;
@Override
- public GlobalRepositories load(MutableBoolean fromCache) {
+ public GlobalRepositories load() {
if (withCause) {
IllegalStateException cause = new IllegalStateException("Code 401");
throw MessageException.of("Error loading repository", cause);
*/
package org.sonar.batch.repository;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.junit.Before;
-import org.junit.Test;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.sonar.batch.WsTestUtil;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonar.scanner.protocol.input.GlobalRepositories;
-import static org.assertj.core.api.Assertions.assertThat;
+import java.io.StringReader;
+
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
public class DefaultGlobalRepositoriesLoaderTest {
private static final String BATCH_GLOBAL_URL = "/batch/global";
- private WSLoader wsLoader;
- private WSLoaderResult<String> result;
+ private BatchWsClient wsClient;
private DefaultGlobalRepositoriesLoader globalRepositoryLoader;
@Before
public void setUp() {
- wsLoader = mock(WSLoader.class);
- result = new WSLoaderResult<>(new GlobalRepositories().toJson(), true);
- when(wsLoader.loadString(BATCH_GLOBAL_URL)).thenReturn(result);
-
- globalRepositoryLoader = new DefaultGlobalRepositoriesLoader(wsLoader);
+ wsClient = mock(BatchWsClient.class);
+ WsTestUtil.mockReader(wsClient, BATCH_GLOBAL_URL, new StringReader(new GlobalRepositories().toJson()));
+ globalRepositoryLoader = new DefaultGlobalRepositoriesLoader(wsClient);
}
- @Test
public void test() {
- MutableBoolean fromCache = new MutableBoolean();
- globalRepositoryLoader.load(fromCache);
-
- assertThat(fromCache.booleanValue()).isTrue();
- verify(wsLoader).loadString(BATCH_GLOBAL_URL);
- verifyNoMoreInteractions(wsLoader);
- }
-
- @Test
- public void testFromServer() {
- result = new WSLoaderResult<>(new GlobalRepositories().toJson(), false);
- when(wsLoader.loadString(BATCH_GLOBAL_URL)).thenReturn(result);
- MutableBoolean fromCache = new MutableBoolean();
- globalRepositoryLoader.load(fromCache);
-
- assertThat(fromCache.booleanValue()).isFalse();
- verify(wsLoader).loadString(BATCH_GLOBAL_URL);
- verifyNoMoreInteractions(wsLoader);
- }
-
- public void testWithoutArg() {
- globalRepositoryLoader.load(null);
-
- verify(wsLoader).loadString(BATCH_GLOBAL_URL);
- verifyNoMoreInteractions(wsLoader);
+ globalRepositoryLoader.load();
+ WsTestUtil.verifyCall(wsClient, BATCH_GLOBAL_URL);
+ verifyNoMoreInteractions(wsClient);
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.MessageException;
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.sonar.batch.WsTestUtil;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonarqube.ws.WsBatch.WsProjectResponse;
import org.sonarqube.ws.client.HttpException;
+import org.sonarqube.ws.client.WsRequest;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class DefaultProjectRepositoriesLoaderTest {
public ExpectedException thrown = ExpectedException.none();
private DefaultProjectRepositoriesLoader loader;
- private WSLoader wsLoader;
+ private BatchWsClient wsClient;
@Before
public void prepare() throws IOException {
- wsLoader = mock(WSLoader.class);
+ wsClient = mock(BatchWsClient.class);
InputStream is = mockData();
- when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
- loader = new DefaultProjectRepositoriesLoader(wsLoader);
+ WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
+ loader = new DefaultProjectRepositoriesLoader(wsClient);
}
@Test
public void continueOnError() {
- when(wsLoader.loadStream(anyString())).thenThrow(IllegalStateException.class);
- ProjectRepositories proj = loader.load(PROJECT_KEY, false, null);
+ when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class);
+ ProjectRepositories proj = loader.load(PROJECT_KEY, false);
assertThat(proj.exists()).isEqualTo(false);
}
public void parsingError() throws IOException {
InputStream is = mock(InputStream.class);
when(is.read()).thenThrow(IOException.class);
-
- when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, false));
- loader.load(PROJECT_KEY, false, null);
+ WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
+ loader.load(PROJECT_KEY, false);
}
@Test(expected = IllegalStateException.class)
public void failFastHttpError() {
HttpException http = new HttpException("url", 403);
IllegalStateException e = new IllegalStateException("http error", http);
- when(wsLoader.loadStream(anyString())).thenThrow(e);
- loader.load(PROJECT_KEY, false, null);
+ WsTestUtil.mockException(wsClient, e);
+ loader.load(PROJECT_KEY, false);
}
-
+
@Test
public void failFastHttpErrorMessageException() {
thrown.expect(MessageException.class);
thrown.expectMessage("http error");
-
+
HttpException http = new HttpException("uri", 403);
MessageException e = MessageException.of("http error", http);
- when(wsLoader.loadStream(anyString())).thenThrow(e);
- loader.load(PROJECT_KEY, false, null);
+ WsTestUtil.mockException(wsClient, e);
+ loader.load(PROJECT_KEY, false);
}
@Test
public void passIssuesModeParameter() {
- loader.load(PROJECT_KEY, false, null);
- verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
+ loader.load(PROJECT_KEY, false);
+ WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F");
- loader.load(PROJECT_KEY, true, null);
- verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F&issues_mode=true");
+ loader.load(PROJECT_KEY, true);
+ WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F&issues_mode=true");
}
@Test
public void deserializeResponse() throws IOException {
- MutableBoolean fromCache = new MutableBoolean();
- loader.load(PROJECT_KEY, false, fromCache);
- assertThat(fromCache.booleanValue()).isTrue();
+ loader.load(PROJECT_KEY, false);
}
@Test
public void passAndEncodeProjectKeyParameter() {
- loader.load(PROJECT_KEY, false, null);
- verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
+ loader.load(PROJECT_KEY, false);
+ WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F");
}
private InputStream mockData() throws IOException {
@Test
public void readRealResponse() throws IOException {
InputStream is = getTestResource("project.protobuf");
- when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
+ WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=org.sonarsource.github%3Asonar-github-plugin&issues_mode=true", is);
- ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", true, null);
+ ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", true);
FileData fd = proj.fileData("org.sonarsource.github:sonar-github-plugin",
"src/test/java/org/sonar/plugins/github/PullRequestIssuePostJobTest.java");
import org.sonarqube.ws.QualityProfiles;
import com.google.common.io.Resources;
import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
-import org.sonar.batch.cache.WSLoaderResult;
-import org.sonar.batch.cache.WSLoader;
+import org.sonar.batch.WsTestUtil;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.Before;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public ExpectedException exception = ExpectedException.none();
private DefaultQualityProfileLoader qpLoader;
- private WSLoader ws;
+ private BatchWsClient wsClient;
private InputStream is;
@Before
public void setUp() throws IOException {
- ws = mock(WSLoader.class);
+ wsClient = mock(BatchWsClient.class);
is = mock(InputStream.class);
when(is.read()).thenReturn(-1);
- WSLoaderResult<InputStream> result = new WSLoaderResult<>(is, false);
- when(ws.loadStream(anyString())).thenReturn(result);
- qpLoader = new DefaultQualityProfileLoader(ws);
+ WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232", is);
+ qpLoader = new DefaultQualityProfileLoader(wsClient);
}
@Test
public void testEncoding() throws IOException {
- WSLoaderResult<InputStream> result = new WSLoaderResult<>(createEncodedQP("qp"), false);
- when(ws.loadStream(anyString())).thenReturn(result);
+ InputStream is = createEncodedQP("qp");
+ WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232", is);
- List<QualityProfile> loaded = qpLoader.load("foo#2", "my-profile#2", null);
- verify(ws).loadStream("/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232");
- verifyNoMoreInteractions(ws);
+ List<QualityProfile> loaded = qpLoader.load("foo#2", "my-profile#2");
+ WsTestUtil.verifyCall(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232");
+ verifyNoMoreInteractions(wsClient);
assertThat(loaded).hasSize(1);
}
@Test
public void testNoProfile() throws IOException {
InputStream is = createEncodedQP();
- when(ws.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, false));
+ WsTestUtil.mockStream(wsClient, is);
exception.expect(MessageException.class);
exception.expectMessage("No quality profiles");
- qpLoader.load("project", null, null);
- verifyNoMoreInteractions(ws);
+ qpLoader.load("project", null);
+ verifyNoMoreInteractions(wsClient);
}
@Test
public void use_real_response() throws IOException {
InputStream is = getTestResource("quality_profile_search_default");
- when(ws.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, false));
+ WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true", is);
- List<QualityProfile> loaded = qpLoader.loadDefault(null, null);
- verify(ws).loadStream("/api/qualityprofiles/search.protobuf?defaults=true");
- verifyNoMoreInteractions(ws);
+ List<QualityProfile> loaded = qpLoader.loadDefault(null);
+ WsTestUtil.verifyCall(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true");
+ verifyNoMoreInteractions(wsClient);
assertThat(loaded).hasSize(1);
}
*/
package org.sonar.batch.repository;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.sonar.batch.WsTestUtil;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonar.scanner.protocol.input.ScannerInput;
import org.sonar.scanner.protocol.input.ScannerInput.ServerIssue;
-import org.sonar.batch.cache.WSLoader;
import com.google.common.base.Function;
import org.junit.Before;
import org.junit.Test;
public class DefaultServerIssuesLoaderTest {
private DefaultServerIssuesLoader loader;
- private WSLoader wsLoader;
+ private BatchWsClient wsClient;
@Before
public void prepare() {
- wsLoader = mock(WSLoader.class);
- loader = new DefaultServerIssuesLoader(wsLoader);
+ wsClient = mock(BatchWsClient.class);
+ loader = new DefaultServerIssuesLoader(wsClient);
}
@Test
.writeDelimitedTo(bos);
InputStream is = new ByteArrayInputStream(bos.toByteArray());
- when(wsLoader.loadStream("/batch/issues.protobuf?key=foo")).thenReturn(new WSLoaderResult<>(is, true));
+ WsTestUtil.mockStream(wsClient, "/batch/issues.protobuf?key=foo", is);
final List<ServerIssue> result = new ArrayList<>();
loader.load("foo", new Function<ScannerInput.ServerIssue, Void>() {
public void testError() throws IOException {
InputStream is = mock(InputStream.class);
when(is.read()).thenThrow(IOException.class);
- when(wsLoader.loadStream("/batch/issues.protobuf?key=foo")).thenReturn(new WSLoaderResult<>(is, true));
+ WsTestUtil.mockStream(wsClient, "/batch/issues.protobuf?key=foo", is);
loader.load("foo", mock(Function.class));
}
}
import org.sonar.batch.repository.FileData;
import com.google.common.collect.Table;
import com.google.common.collect.HashBasedTable;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.sonar.api.batch.bootstrap.ProjectKey;
import org.sonar.batch.analysis.DefaultAnalysisMode;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
when(projectKey.get()).thenReturn("key");
}
- @Test
- public void testNonAssociated() {
- when(mode.isNotAssociated()).thenReturn(true);
- ProjectRepositories repo = provider.provide(loader, projectKey, mode);
-
- assertThat(repo.exists()).isEqualTo(false);
- verify(mode).isNotAssociated();
- verifyNoMoreInteractions(loader, projectKey, mode);
- }
-
- @Test
- public void singleton() {
- when(mode.isNotAssociated()).thenReturn(true);
- ProjectRepositories repo = provider.provide(loader, projectKey, mode);
-
- assertThat(repo.exists()).isEqualTo(false);
- verify(mode).isNotAssociated();
- verifyNoMoreInteractions(loader, projectKey, mode);
-
- repo = provider.provide(loader, projectKey, mode);
- verifyNoMoreInteractions(loader, projectKey, mode);
- }
-
@Test
public void testValidation() {
- when(mode.isNotAssociated()).thenReturn(false);
when(mode.isIssues()).thenReturn(true);
- when(loader.load(eq("key"), eq(true), any(MutableBoolean.class))).thenReturn(project);
+ when(loader.load(eq("key"), eq(true))).thenReturn(project);
provider.provide(loader, projectKey, mode);
}
@Test
public void testAssociated() {
- when(mode.isNotAssociated()).thenReturn(false);
when(mode.isIssues()).thenReturn(false);
- when(loader.load(eq("key"), eq(false), any(MutableBoolean.class))).thenReturn(project);
+ when(loader.load(eq("key"), eq(false))).thenReturn(project);
ProjectRepositories repo = provider.provide(loader, projectKey, mode);
assertThat(repo.exists()).isEqualTo(true);
assertThat(repo.lastAnalysisDate()).isNotNull();
- verify(mode).isNotAssociated();
verify(mode, times(2)).isIssues();
verify(projectKey).get();
- verify(loader).load(eq("key"), eq(false), any(MutableBoolean.class));
+ verify(loader).load(eq("key"), eq(false));
verifyNoMoreInteractions(loader, projectKey, mode);
}
}
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.lang.mutable.MutableBoolean;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
@Test
public void testProvide() {
- when(mode.isNotAssociated()).thenReturn(false);
- when(loader.load(eq("project"), isNull(String.class), any(MutableBoolean.class))).thenReturn(response);
+ when(loader.load(eq("project"), isNull(String.class))).thenReturn(response);
ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode);
assertResponse(qps);
- verify(loader).load(eq("project"), isNull(String.class), any(MutableBoolean.class));
- verifyNoMoreInteractions(loader);
- }
-
- @Test
- public void testNonAssociated() {
- when(mode.isNotAssociated()).thenReturn(true);
- when(loader.loadDefault(anyString(), any(MutableBoolean.class))).thenReturn(response);
- ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode);
- assertResponse(qps);
-
- verify(loader).loadDefault(anyString(), any(MutableBoolean.class));
+ verify(loader).load(eq("project"), isNull(String.class));
verifyNoMoreInteractions(loader);
}
@Test
public void testProjectDoesntExist() {
- when(mode.isNotAssociated()).thenReturn(false);
when(projectRepo.exists()).thenReturn(false);
- when(loader.loadDefault(anyString(), any(MutableBoolean.class))).thenReturn(response);
+ when(loader.loadDefault(anyString())).thenReturn(response);
ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode);
assertResponse(qps);
- verify(loader).loadDefault(anyString(), any(MutableBoolean.class));
+ verify(loader).loadDefault(anyString());
verifyNoMoreInteractions(loader);
}
@Test
public void testProfileProp() {
- when(mode.isNotAssociated()).thenReturn(false);
- when(loader.load(eq("project"), eq("custom"), any(MutableBoolean.class))).thenReturn(response);
+ when(loader.load(eq("project"), eq("custom"))).thenReturn(response);
when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("custom");
when(props.properties()).thenReturn(ImmutableMap.of(ModuleQProfiles.SONAR_PROFILE_PROP, "custom"));
ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode);
assertResponse(qps);
- verify(loader).load(eq("project"), eq("custom"), any(MutableBoolean.class));
+ verify(loader).load(eq("project"), eq("custom"));
verifyNoMoreInteractions(loader);
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP
+ "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server.");
@Test
public void testIgnoreSonarProfileIssuesMode() {
- when(mode.isNotAssociated()).thenReturn(false);
when(mode.isIssues()).thenReturn(true);
- when(loader.load(eq("project"), (String) eq(null), any(MutableBoolean.class))).thenReturn(response);
+ when(loader.load(eq("project"), (String) eq(null))).thenReturn(response);
when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("custom");
ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode);
assertResponse(qps);
- verify(loader).load(eq("project"), (String) eq(null), any(MutableBoolean.class));
+ verify(loader).load(eq("project"), (String) eq(null));
verifyNoMoreInteractions(loader);
}
@Test
public void testProfilePropDefault() {
- when(mode.isNotAssociated()).thenReturn(true);
- when(loader.loadDefault(eq("custom"), any(MutableBoolean.class))).thenReturn(response);
+ when(projectRepo.exists()).thenReturn(false);
+ when(loader.loadDefault(eq("custom"))).thenReturn(response);
when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("custom");
when(props.properties()).thenReturn(ImmutableMap.of(ModuleQProfiles.SONAR_PROFILE_PROP, "custom"));
ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode);
assertResponse(qps);
- verify(loader).loadDefault(eq("custom"), any(MutableBoolean.class));
+ verify(loader).loadDefault(eq("custom"));
verifyNoMoreInteractions(loader);
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP
+ "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server.");
import org.assertj.core.util.Lists;
-import org.sonar.batch.cache.WSLoaderResult;
import org.sonar.scanner.protocol.input.ScannerInput;
-import org.sonar.batch.cache.WSLoader;
+import org.sonar.batch.WsTestUtil;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.junit.Before;
-import com.google.common.collect.ImmutableList;
-import org.apache.commons.lang.mutable.MutableBoolean;
import com.google.common.collect.ImmutableMap;
import org.junit.rules.ExpectedException;
import org.junit.Rule;
import java.util.Arrays;
import java.util.Map;
-import static org.mockito.Matchers.anyString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class UserRepositoryLoaderTest {
@Rule
public final ExpectedException exception = ExpectedException.none();
- private WSLoader wsLoader;
+ private BatchWsClient wsClient;
private UserRepositoryLoader userRepo;
@Before
public void setUp() {
- wsLoader = mock(WSLoader.class);
- userRepo = new UserRepositoryLoader(wsLoader);
+ wsClient = mock(BatchWsClient.class);
+ userRepo = new UserRepositoryLoader(wsClient);
}
@Test
@Test
public void testLoad() throws IOException {
Map<String, String> userMap = ImmutableMap.of("fmallet", "Freddy Mallet", "sbrandhof", "Simon");
- WSLoaderResult<InputStream> res = new WSLoaderResult<>(createUsersMock(userMap), true);
- when(wsLoader.loadStream("/batch/users?logins=fmallet,sbrandhof")).thenReturn(res);
-
+ InputStream is = createUsersMock(userMap);
+ WsTestUtil.mockStream(wsClient, "/batch/users?logins=fmallet,sbrandhof", is);
assertThat(userRepo.load(Arrays.asList("fmallet", "sbrandhof"))).extracting("login", "name").containsOnly(tuple("fmallet", "Freddy Mallet"), tuple("sbrandhof", "Simon"));
}
- @Test
- public void testFromCache() throws IOException {
- WSLoaderResult<InputStream> res = new WSLoaderResult<>(createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet")), true);
- when(wsLoader.loadStream(anyString())).thenReturn(res);
- MutableBoolean fromCache = new MutableBoolean();
- userRepo.load("", fromCache);
- assertThat(fromCache.booleanValue()).isTrue();
-
- fromCache.setValue(false);
- userRepo.load(ImmutableList.of("user"), fromCache);
- assertThat(fromCache.booleanValue()).isTrue();
- }
-
@Test
public void testLoadSingleUser() throws IOException {
- WSLoaderResult<InputStream> res = new WSLoaderResult<>(createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet")), true);
- when(wsLoader.loadStream("/batch/users?logins=fmallet")).thenReturn(res);
-
+ InputStream is = createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet"));
+ WsTestUtil.mockStream(wsClient, "/batch/users?logins=fmallet", is);
assertThat(userRepo.load("fmallet").getName()).isEqualTo("Freddy Mallet");
}
public void testInputStreamError() throws IOException {
InputStream is = mock(InputStream.class);
Mockito.doThrow(IOException.class).when(is).read();
- WSLoaderResult<InputStream> res = new WSLoaderResult<>(is, true);
-
- when(wsLoader.loadStream("/batch/users?logins=fmallet,sbrandhof")).thenReturn(res);
+ WsTestUtil.mockStream(wsClient, "/batch/users?logins=fmallet,sbrandhof", is);
exception.expect(IllegalStateException.class);
exception.expectMessage("Unable to get user details from server");
List<LoadedActiveRule> qp2Rules = ImmutableList.of(r2, r3);
List<LoadedActiveRule> qp3Rules = ImmutableList.of(r1, r3);
- when(loader.load(eq("qp1"), any(MutableBoolean.class))).thenReturn(qp1Rules);
- when(loader.load(eq("qp2"), any(MutableBoolean.class))).thenReturn(qp2Rules);
- when(loader.load(eq("qp3"), any(MutableBoolean.class))).thenReturn(qp3Rules);
+ when(loader.load(eq("qp1"))).thenReturn(qp1Rules);
+ when(loader.load(eq("qp2"))).thenReturn(qp2Rules);
+ when(loader.load(eq("qp3"))).thenReturn(qp3Rules);
ModuleQProfiles profiles = mockProfiles("qp1", "qp2", "qp3");
ActiveRules activeRules = provider.provide(loader, profiles);
assertThat(activeRules.findAll()).extracting("ruleKey").containsOnly(
RuleKey.of("rule1", "rule1"), RuleKey.of("rule2", "rule2"), RuleKey.of("rule3", "rule3"));
- verify(loader).load(eq("qp1"), any(MutableBoolean.class));
- verify(loader).load(eq("qp2"), any(MutableBoolean.class));
- verify(loader).load(eq("qp3"), any(MutableBoolean.class));
+ verify(loader).load(eq("qp1"));
+ verify(loader).load(eq("qp2"));
+ verify(loader).load(eq("qp3"));
verifyNoMoreInteractions(loader);
}
package org.sonar.batch.rule;
import org.sonar.api.rule.RuleKey;
-import org.sonar.batch.cache.WSLoaderResult;
-import org.sonar.batch.cache.WSLoader;
+import org.sonar.batch.WsTestUtil;
+import org.sonar.batch.bootstrap.BatchWsClient;
import com.google.common.io.Resources;
import org.junit.Test;
import java.io.InputStream;
import java.util.Collection;
-import static org.mockito.Mockito.verify;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
import org.junit.Before;
public class DefaultActiveRulesLoaderTest {
private DefaultActiveRulesLoader loader;
- private WSLoader ws;
+ private BatchWsClient wsClient;
@Before
public void setUp() {
- ws = mock(WSLoader.class);
- loader = new DefaultActiveRulesLoader(ws);
+ wsClient = mock(BatchWsClient.class);
+ loader = new DefaultActiveRulesLoader(wsClient);
}
@Test
String req1 = "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives&activation=true&qprofile=c%2B-test_c%2B-values-17445&p=1&ps=500";
String req2 = "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives&activation=true&qprofile=c%2B-test_c%2B-values-17445&p=2&ps=500";
- when(ws.loadStream(req1)).thenReturn(new WSLoaderResult<>(response1, false));
- when(ws.loadStream(req2)).thenReturn(new WSLoaderResult<>(response2, false));
+ WsTestUtil.mockStream(wsClient, req1, response1);
+ WsTestUtil.mockStream(wsClient, req2, response2);
- Collection<LoadedActiveRule> activeRules = loader.load("c+-test_c+-values-17445", null);
+ Collection<LoadedActiveRule> activeRules = loader.load("c+-test_c+-values-17445");
assertThat(activeRules).hasSize(226);
assertActiveRule(activeRules);
-
- verify(ws).loadStream(req1);
- verify(ws).loadStream(req2);
- verifyNoMoreInteractions(ws);
+
+ WsTestUtil.verifyCall(wsClient, req1);
+ WsTestUtil.verifyCall(wsClient, req2);
+
+ verifyNoMoreInteractions(wsClient);
}
-
+
private static void assertActiveRule(Collection<LoadedActiveRule> activeRules) {
RuleKey key = RuleKey.of("squid", "S3008");
for (LoadedActiveRule r : activeRules) {
package org.sonar.batch.rule;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
import org.junit.rules.ExpectedException;
-import org.sonar.batch.cache.WSLoaderResult;
-import org.sonar.batch.cache.WSLoader;
-import org.apache.commons.lang.mutable.MutableBoolean;
+import org.sonar.batch.WsTestUtil;
+import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonarqube.ws.Rules.ListResponse.Rule;
import com.google.common.io.ByteSource;
import com.google.common.io.Resources;
import java.io.InputStream;
import java.util.List;
-import static org.mockito.Matchers.anyString;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
@Test
public void testParseServerResponse() throws IOException {
- WSLoader wsLoader = mock(WSLoader.class);
+ BatchWsClient wsClient = mock(BatchWsClient.class);
InputStream is = Resources.asByteSource(this.getClass().getResource("DefaultRulesLoader/response.protobuf")).openBufferedStream();
- when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
- DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader);
- List<Rule> ruleList = loader.load(null);
+ WsTestUtil.mockStream(wsClient, is);
+ DefaultRulesLoader loader = new DefaultRulesLoader(wsClient);
+ List<Rule> ruleList = loader.load();
assertThat(ruleList).hasSize(318);
}
- @Test
- public void testLoadedFromCache() throws IOException {
- WSLoader wsLoader = mock(WSLoader.class);
- InputStream is = Resources.asByteSource(this.getClass().getResource("DefaultRulesLoader/response.protobuf")).openBufferedStream();
- when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
- DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader);
- MutableBoolean fromCache = new MutableBoolean();
- loader.load(fromCache);
-
- assertThat(fromCache.booleanValue()).isTrue();
- }
-
@Test
public void testError() throws IOException {
- WSLoader wsLoader = mock(WSLoader.class);
+ BatchWsClient wsClient = mock(BatchWsClient.class);
InputStream is = ByteSource.wrap(new String("trash").getBytes()).openBufferedStream();
- when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
- DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader);
+ WsTestUtil.mockStream(wsClient, is);
+ DefaultRulesLoader loader = new DefaultRulesLoader(wsClient);
exception.expect(IllegalStateException.class);
exception.expectMessage("Unable to get rules");
- loader.load(null);
+ loader.load();
}
-
}
*/
package org.sonar.batch.rule;
-import static org.mockito.Matchers.any;
-
-import org.apache.commons.lang.mutable.MutableBoolean;
-
import com.google.common.collect.Lists;
import org.sonar.api.batch.rule.Rules;
import static org.assertj.core.api.Assertions.assertThat;
@Test
public void testRuleTranslation() {
RulesLoader loader = mock(RulesLoader.class);
- when(loader.load(any(MutableBoolean.class))).thenReturn(Lists.newArrayList(getTestRule()));
+ when(loader.load()).thenReturn(Lists.newArrayList(getTestRule()));
RulesProvider provider = new RulesProvider();
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.analysis.AnalysisProperties;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class ProjectReactorBuilderTest {
@Rule
public LogTester logTester = new LogTester();
- private AnalysisMode mode;
-
- @Before
- public void setUp() {
- mode = mock(AnalysisMode.class);
- }
-
@Test
public void shouldDefineSimpleProject() {
ProjectDefinition projectDefinition = loadProjectDefinition("simple-project");
AnalysisProperties taskProperties = new AnalysisProperties(props, null);
assertThat(taskProperties.property("module1.module11.property")).isEqualTo("My module11 property");
- new ProjectReactorBuilder(taskProperties, mode).execute();
+ new ProjectReactorBuilder(taskProperties).execute();
assertThat(taskProperties.property("module1.module11.property")).isNull();
}
@Test
public void shouldInitRootWorkDir() {
- ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(Maps.<String, String>newHashMap(), null), mode);
+ ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(Maps.<String, String>newHashMap(), null));
File baseDir = new File("target/tmp/baseDir");
File workDir = builder.initRootProjectWorkDir(baseDir, Maps.<String, String>newHashMap());
assertThat(workDir).isEqualTo(new File(baseDir, ".sonar"));
}
- @Test
- public void nonAssociatedMode() {
- when(mode.isIssues()).thenReturn(true);
- ProjectDefinition project = loadProjectDefinition("multi-module-with-basedir-not-associated");
-
- assertThat(project.getKey()).isEqualTo("project");
- }
-
@Test
public void shouldInitWorkDirWithCustomRelativeFolder() {
Map<String, String> props = Maps.<String, String>newHashMap();
props.put("sonar.working.directory", ".foo");
- ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(props, null), mode);
+ ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(props, null));
File baseDir = new File("target/tmp/baseDir");
File workDir = builder.initRootProjectWorkDir(baseDir, props);
public void shouldInitRootWorkDirWithCustomAbsoluteFolder() {
Map<String, String> props = Maps.<String, String>newHashMap();
props.put("sonar.working.directory", new File("src").getAbsolutePath());
- ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(props, null), mode);
+ ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(props, null));
File baseDir = new File("target/tmp/baseDir");
File workDir = builder.initRootProjectWorkDir(baseDir, props);
private ProjectDefinition loadProjectDefinition(String projectFolder) {
Map<String, String> props = loadProps(projectFolder);
AnalysisProperties bootstrapProps = new AnalysisProperties(props, null);
- ProjectReactor projectReactor = new ProjectReactorBuilder(bootstrapProps, mode).execute();
+ ProjectReactor projectReactor = new ProjectReactorBuilder(bootstrapProps).execute();
return projectReactor.getRoot();
}
Map<String, String> props = loadProps("simple-project");
props.put("sonar.qualitygate", "somevalue");
AnalysisProperties bootstrapProps = new AnalysisProperties(props, null);
- new ProjectReactorBuilder(bootstrapProps, mode).execute();
+ new ProjectReactorBuilder(bootstrapProps).execute();
assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Property 'sonar.qualitygate' is not supported any more. It will be ignored.");
}
+++ /dev/null
-package com.sonar.it.samples.modules.a1;
-
-public class HelloA1 {
- private int i;
- private HelloA1() {
-
- }
-
- public void hello() {
- System.out.println("hello" + " xoo");
- }
-
- protected String getHello() {
- return "hello";
- }
-}
\ No newline at end of file
+++ /dev/null
-package com.sonar.it.samples.modules.a2;
-
-public class HelloA2 {
- private int i;
- private HelloA2() {
-
- }
-
- public void hello() {
- System.out.println("hello" + " xoo");
- }
-}
\ No newline at end of file
+++ /dev/null
-package com.sonar.it.samples.modules.b1;
-
-public class HelloB1 {
- private int i;
- private HelloB1() {
-
- }
-
- public void hello() {
- System.out.println("hello" + " world");
- }
-}
\ No newline at end of file
+++ /dev/null
-package com.sonar.it.samples.modules.b2;
-
-public class HelloB2 {
- private int i;
- private HelloB2() {
-
- }
-
- public void hello() {
- System.out.println("hello" + " world");
- }
-}
\ No newline at end of file
+++ /dev/null
-# Root project information
-#sonar.projectKey=com.sonarsource.it.samples:multi-modules-sample
-sonar.projectName=Sonar :: Integration Tests :: Multi-modules Sample
-sonar.projectVersion=1.0-SNAPSHOT
-
-sonar.language=xoo
-
-# Some properties that will be inherited by the modules
-sonar.sources=src/main/xoo
-
-# List of the module identifiers
-sonar.modules=module_a,module_b
-
-module_a.sonar.projectKey=module_a
-module_a.sonar.projectName=Module A
-
-module_a.sonar.modules=module_a1,module_a2
-
-module_a.module_a1.sonar.projectName=Sub-module A1
-
-module_a.module_a2.sonar.projectName=Sub-module A2
-
-
-module_b.sonar.projectKey=module_b
-module_b.sonar.projectName=Module B
-
-module_b.sonar.modules=module_b1,module_b2
-
-module_b.module_b1.sonar.projectName=Sub-module B1
-
-module_b.module_b2.sonar.projectName=Sub-module B2
+++ /dev/null
-{
- "timestamp": 0,
- "qprofilesByLanguage": {
- "java": {
- "key": "java-sonar-way-72608",
- "name": "Sonar way",
- "language": "java",
- "rulesUpdatedAt": "2015-08-10T12:06:53+0200"
- }
- },
- "activeRules": [
- {
- "repositoryKey": "common-java",
- "ruleKey": "DuplicatedBlocks",
- "name": "Source files should not have any duplicated blocks",
- "severity": "MAJOR",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "common-java",
- "ruleKey": "InsufficientBranchCoverage",
- "name": "Branches should have sufficient coverage by unit tests",
- "severity": "MAJOR",
- "language": "java",
- "params": {
- "minimumBranchCoverageRatio": "65.0"
- }
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "RightCurlyBraceStartLineCheck",
- "name": "A close curly brace should be located at the beginning of a line",
- "severity": "MINOR",
- "internalKey": "RightCurlyBraceStartLineCheck",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "UselessParenthesesCheck",
- "name": "Useless parentheses around expressions should be removed to prevent any misunderstanding",
- "severity": "MAJOR",
- "internalKey": "UselessParenthesesCheck",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "ObjectFinalizeCheck",
- "name": "The Object.finalize() method should not be called",
- "severity": "CRITICAL",
- "internalKey": "ObjectFinalizeCheck",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "ObjectFinalizeOverridenCheck",
- "name": "The Object.finalize() method should not be overriden",
- "severity": "CRITICAL",
- "internalKey": "ObjectFinalizeOverridenCheck",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "ObjectFinalizeOverridenCallsSuperFinalizeCheck",
- "name": "super.finalize() should be called at the end of Object.finalize() implementations",
- "severity": "BLOCKER",
- "internalKey": "ObjectFinalizeOverridenCallsSuperFinalizeCheck",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "ClassVariableVisibilityCheck",
- "name": "Class variable fields should not have public accessibility",
- "severity": "MAJOR",
- "internalKey": "ClassVariableVisibilityCheck",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "S2188",
- "name": "JUnit test cases should call super methods",
- "severity": "CRITICAL",
- "internalKey": "S2188",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "S2186",
- "name": "JUnit assertions should not be used in \"run\" methods",
- "severity": "CRITICAL",
- "internalKey": "S2186",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "S2187",
- "name": "TestCases should contain tests",
- "severity": "MAJOR",
- "internalKey": "S2187",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "S2391",
- "name": "JUnit framework methods should be declared properly",
- "severity": "CRITICAL",
- "internalKey": "S2391",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "S2325",
- "name": "\"private\" methods that don\u0027t access instance data should be \"static\"",
- "severity": "MINOR",
- "internalKey": "S2325",
- "language": "java",
- "params": {}
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "S1166",
- "name": "Exception handlers should preserve the original exception",
- "severity": "CRITICAL",
- "internalKey": "S1166",
- "language": "java",
- "params": {
- "exceptions": "java.lang.InterruptedException, java.lang.NumberFormatException, java.text.ParseException, java.net.MalformedURLException"
- }
- },
- {
- "repositoryKey": "squid",
- "ruleKey": "S2970",
- "name": "Assertions should be complete",
- "severity": "CRITICAL",
- "internalKey": "S2970",
- "language": "java",
- "params": {}
- }
-
- ],
- "settingsByModule": {},
- "fileDataByModuleAndPath": {
- "org.codehaus.sonar-plugins:sonar-scm-git-plugin": {
- "src/test/java/org/sonar/plugins/scm/git/JGitBlameCommandTest.java": {
- "needBlame": true
- },
- "src/main/java/org/sonar/plugins/scm/git/GitScmProvider.java": {
- "hash": "90082117d0dc0f1189ab7e4990a20667",
- "needBlame": true
- }
- }
- },
- "lastAnalysisDate": "2015-08-10T13:20:09+0200"
-}
\ No newline at end of file