@Override
public void dispose(PicoContainer container) {
+ //nothing to do
}
@Override
import org.sonar.api.batch.AnalysisMode;
public abstract class AbstractAnalysisMode implements AnalysisMode {
- protected final static String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES};
+ private static final String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES};
protected boolean preview;
protected boolean issues;
@Override
public void dispose(PicoContainer container) {
+ //nothing to do
}
@Override
return reactor;
}
- private void cleanDirectory(File dir) {
+ private static void cleanDirectory(File dir) {
try {
FileUtils.deleteDirectory(dir);
Files.createDirectories(dir.toPath());
lockFile = lockChannel.tryLock(0, 1024, false);
if (lockFile == null) {
- failAlreadyInProgress();
+ failAlreadyInProgress(null);
}
} catch (OverlappingFileLockException e) {
- failAlreadyInProgress();
+ failAlreadyInProgress(e);
} catch (IOException e) {
throw new IllegalStateException("Failed to create project lock in " + lockFilePath.toString(), e);
}
}
- private static void failAlreadyInProgress() {
- throw new IllegalStateException("Another SonarQube analysis is already in progress for this project");
+ private static void failAlreadyInProgress(Exception e) {
+ throw new IllegalStateException("Another SonarQube analysis is already in progress for this project", e);
}
+ @Override
public void stop() {
if (lockFile != null) {
try {
// Sort module by reverse lexicographic order to avoid issue when one module id is a prefix of another one
Collections.sort(moduleIds);
Collections.reverse(moduleIds);
-
+
Map<String, Map<String, String>> result = new HashMap<>();
result.put(currentModuleId, currentModuleProperties);
-
+
for (String moduleId : moduleIds) {
Map<String, Map<String, String>> subModuleProps = extractPropertiesByModule(moduleId, currentModuleProperties);
checkRepeatedModuleNames(result.keySet(), subModuleProps.keySet());
union.addAll(currentModules);
union.retainAll(modulesToMerge);
if (!union.isEmpty()) {
- if(union.size() > 1) {
- throw new IllegalStateException(String.format("Modules have the following repeated names: %s. Each module must have a unique name.", union));
+ if (union.size() > 1) {
+ throw new IllegalStateException(String.format("Modules have the following repeated names: %s. Each module must have a unique name.", union));
} else {
throw new IllegalStateException(String.format("Two modules have the same name: %s. Each module must have a unique name.", union.iterator().next()));
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.sonar.home.cache.PersistentCacheLoader;
+
+import org.junit.internal.runners.statements.ExpectException;
+import org.junit.rules.ExpectedException;
+
+import java.io.IOException;
+import java.util.Date;
+
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.any;
+import org.junit.Test;
+import org.sonar.home.cache.Logger;
+import org.junit.rules.TemporaryFolder;
+import org.junit.Rule;
+import org.junit.Before;
+import org.sonar.batch.bootstrap.ServerClient;
+import org.sonar.home.cache.PersistentCache;
+
+public class DefaultProjectCacheStatusTest {
+ private static final String PROJ_KEY = "project1";
+ @Rule
+ public TemporaryFolder tmp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ ProjectCacheStatus cacheStatus;
+ PersistentCache cache;
+ ServerClient client;
+
+ @Before
+ public void setUp() {
+ cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
+ client = mock(ServerClient.class);
+ when(client.getURL()).thenReturn("localhost");
+ cacheStatus = new DefaultProjectCacheStatus(cache, client);
+ }
+
+ @Test
+ public void errorDelete() throws IOException {
+ cache = mock(PersistentCache.class);
+ doThrow(IOException.class).when(cache).put(anyString(), any(byte[].class));
+ cacheStatus = new DefaultProjectCacheStatus(cache, client);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Failed to delete cache sync status");
+ cacheStatus.delete(PROJ_KEY);
+ }
+
+ @Test
+ public void errorSave() throws IOException {
+ cache = mock(PersistentCache.class);
+ doThrow(IOException.class).when(cache).put(anyString(), any(byte[].class));
+ cacheStatus = new DefaultProjectCacheStatus(cache, client);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Failed to write cache sync status");
+ cacheStatus.save(PROJ_KEY);
+ }
+
+ @Test
+ public void errorStatus() throws IOException {
+ cache = mock(PersistentCache.class);
+ doThrow(IOException.class).when(cache).get(anyString(), any(PersistentCacheLoader.class));
+ cacheStatus = new DefaultProjectCacheStatus(cache, client);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Failed to read cache sync status");
+ cacheStatus.getSyncStatus(PROJ_KEY);
+ }
+
+ @Test
+ public void testSave() {
+ cacheStatus.save(PROJ_KEY);
+ assertThat(cacheStatus.getSyncStatus(PROJ_KEY)).isNotNull();
+ assertThat(age(cacheStatus.getSyncStatus(PROJ_KEY))).isLessThan(2000);
+ assertThat(cacheStatus.getSyncStatus(PROJ_KEY + "1")).isNull();
+ }
+
+ @Test
+ public void testDelete() {
+ cacheStatus.save(PROJ_KEY);
+ cacheStatus.delete(PROJ_KEY);
+ assertThat(cacheStatus.getSyncStatus(PROJ_KEY)).isNull();
+ }
+
+ private long age(Date date) {
+ return (new Date().getTime()) - date.getTime();
+ }
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.assertj.core.api.Assertions.assertThat;
-
-import java.util.Date;
-
-import org.junit.Test;
-import org.sonar.home.cache.Logger;
-import org.junit.rules.TemporaryFolder;
-import org.junit.Rule;
-import org.junit.Before;
-import org.sonar.batch.bootstrap.ServerClient;
-import org.sonar.home.cache.PersistentCache;
-
-public class ProjectCacheStatusTest {
- private static final String PROJ_KEY = "project1";
- @Rule
- public TemporaryFolder tmp = new TemporaryFolder();
-
- ProjectCacheStatus cacheStatus;
- PersistentCache cache;
- ServerClient client;
-
- @Before
- public void setUp() {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
- client = mock(ServerClient.class);
- when(client.getURL()).thenReturn("localhost");
- cacheStatus = new DefaultProjectCacheStatus(cache, client);
- }
-
- @Test
- public void testSave() {
- cacheStatus.save(PROJ_KEY);
- assertThat(cacheStatus.getSyncStatus(PROJ_KEY)).isNotNull();
- assertThat(age(cacheStatus.getSyncStatus(PROJ_KEY))).isLessThan(2000);
- assertThat(cacheStatus.getSyncStatus(PROJ_KEY + "1")).isNull();
- }
-
- @Test
- public void testDelete() {
- cacheStatus.save(PROJ_KEY);
- cacheStatus.delete(PROJ_KEY);
- assertThat(cacheStatus.getSyncStatus(PROJ_KEY)).isNull();
- }
-
- private long age(Date date) {
- return (new Date().getTime()) - date.getTime();
- }
-}
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);
package org.sonar.batch.scan;
import org.junit.rules.ExpectedException;
-
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
+import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Before
public void setUp() {
+ lock = setUpTest(tempFolder.getRoot());
+ }
+
+ private ProjectLock setUpTest(File file) {
ProjectReactor projectReactor = mock(ProjectReactor.class);
ProjectDefinition projectDefinition = mock(ProjectDefinition.class);
when(projectReactor.getRoot()).thenReturn(projectDefinition);
- when(projectDefinition.getBaseDir()).thenReturn(tempFolder.getRoot());
+ when(projectDefinition.getBaseDir()).thenReturn(file);
- lock = new ProjectLock(projectReactor);
+ return new ProjectLock(projectReactor);
}
@Test
lock.tryLock();
lock.stop();
}
+
+ @Test
+ public void errorLock() {
+ lock = setUpTest(Paths.get("path", "that", "wont", "exist", "ever").toFile());
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Failed to create project lock in");
+ lock.tryLock();
+ }
+
+ @Test
+ public void errorDeleteLock() {
+ lock = setUpTest(Paths.get("path", "that", "wont", "exist", "ever").toFile());
+ lock.stop();
+ }
}
return baseDir;
}
- public ProjectDefinition setWorkDir(@Nullable File workDir) {
+ public ProjectDefinition setWorkDir(File workDir) {
this.workDir = workDir;
return this;
}
- @CheckForNull
public File getWorkDir() {
return workDir;
}
- public ProjectDefinition setBuildDir(@Nullable File d) {
+ public ProjectDefinition setBuildDir( File d) {
this.buildDir = d;
return this;
}
- @CheckForNull
public File getBuildDir() {
return buildDir;
}