*/
package org.sonar.core.preview;
-import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import javax.annotation.Nullable;
import java.io.File;
-import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
this.previewDatabaseFactory = previewDatabaseFactory;
}
- public byte[] getDatabaseForPreview(@Nullable Long projectId) {
+ public String getPathToDatabaseFile(@Nullable Long projectId) {
long notNullProjectId = projectId != null ? projectId.longValue() : 0L;
ReadWriteLock rwl = getLock(notNullProjectId);
try {
rwl.writeLock().unlock();
}
File dbFile = new File(getCacheLocation(projectId), lastTimestampPerProject.get(notNullProjectId) + PreviewDatabaseFactory.H2_FILE_SUFFIX);
- return fileToByte(dbFile);
+ return dbFile.getAbsolutePath();
} finally {
rwl.readLock().unlock();
}
lastTimestampPerProject.put(notNullProjectId, newTimestamp);
}
- private byte[] fileToByte(File dbFile) {
- try {
- return Files.toByteArray(dbFile);
- } catch (IOException e) {
- throw new SonarException("Unable to create h2 database file", e);
- }
- }
-
private synchronized ReadWriteLock getLock(long notNullProjectId) {
if (!lockPerProject.containsKey(notNullProjectId)) {
lockPerProject.put(notNullProjectId, new ReentrantReadWriteLock(true));
when(dryRunDatabaseFactory.createNewDatabaseForDryRun(isNull(Long.class), any(File.class), anyString())).thenAnswer(new Answer<File>() {
public File answer(InvocationOnMock invocation) throws IOException {
Object[] args = invocation.getArguments();
- File dbFile = new File(new File(dryRunCacheLocation, "default"), (String) args[2] + ".h2.db");
+ File dbFile = new File(new File(dryRunCacheLocation, "default"), args[2] + ".h2.db");
FileUtils.write(dbFile, "fake db content");
return dbFile;
}
});
- byte[] dbContent = dryRunCache.getDatabaseForPreview(null);
- assertThat(new String(dbContent)).isEqualTo("fake db content");
+ String path = dryRunCache.getPathToDatabaseFile(null);
+ assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content");
- dbContent = dryRunCache.getDatabaseForPreview(null);
- assertThat(new String(dbContent)).isEqualTo("fake db content");
+ path = dryRunCache.getPathToDatabaseFile(null);
+ assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content");
verify(dryRunDatabaseFactory, times(1)).createNewDatabaseForDryRun(anyLong(), any(File.class), anyString());
}
}
});
when(resourceDao.getRootProjectByComponentId(123L)).thenReturn(new ResourceDto().setId(123L));
- byte[] dbContent = dryRunCache.getDatabaseForPreview(123L);
- assertThat(new String(dbContent)).isEqualTo("fake db content");
+ String path = dryRunCache.getPathToDatabaseFile(123L);
+ assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content");
- dbContent = dryRunCache.getDatabaseForPreview(123L);
- assertThat(new String(dbContent)).isEqualTo("fake db content");
+ path = dryRunCache.getPathToDatabaseFile(123L);
+ assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content");
verify(dryRunDatabaseFactory, times(1)).createNewDatabaseForDryRun(anyLong(), any(File.class), anyString());
}
return dbFile;
}
});
- byte[] dbContent = dryRunCache.getDatabaseForPreview(null);
- assertThat(new String(dbContent)).isEqualTo("fake db content 1");
+ String path = dryRunCache.getPathToDatabaseFile(null);
+ assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content 1");
// Emulate invalidation of cache
Thread.sleep(100);
when(propertiesDao.selectGlobalProperty(PreviewCache.SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY)).thenReturn(new PropertyDto().setValue("" + System.currentTimeMillis()));
- dbContent = dryRunCache.getDatabaseForPreview(null);
- assertThat(new String(dbContent)).isEqualTo("fake db content 2");
+ path = dryRunCache.getPathToDatabaseFile(null);
+ assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content 2");
verify(dryRunDatabaseFactory, times(2)).createNewDatabaseForDryRun(anyLong(), any(File.class), anyString());
}
});
when(resourceDao.getRootProjectByComponentId(123L)).thenReturn(new ResourceDto().setId(123L));
- byte[] dbContent = dryRunCache.getDatabaseForPreview(123L);
- assertThat(new String(dbContent)).isEqualTo("fake db content 1");
+ String path = dryRunCache.getPathToDatabaseFile(123L);
+ assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content 1");
// Emulate invalidation of cache
Thread.sleep(100);
when(propertiesDao.selectProjectProperty(123L, PreviewCache.SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY)).thenReturn(new PropertyDto().setValue("" + System.currentTimeMillis()));
- dbContent = dryRunCache.getDatabaseForPreview(123L);
- assertThat(new String(dbContent)).isEqualTo("fake db content 2");
+ path = dryRunCache.getPathToDatabaseFile(123L);
+ assertThat(FileUtils.readFileToString(new File(path))).isEqualTo("fake db content 2");
verify(dryRunDatabaseFactory, times(2)).createNewDatabaseForDryRun(anyLong(), any(File.class), anyString());
}