コード例 #1
0
  @Test
  public void test_getDatabaseForDryRun_project_invalidation() throws Exception {
    when(dryRunDatabaseFactory.createNewDatabaseForDryRun(eq(123L), 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, "123"), (String) args[2] + ".h2.db");
                FileUtils.write(dbFile, "fake db content 1");
                return dbFile;
              }
            })
        .thenAnswer(
            new Answer<File>() {
              public File answer(InvocationOnMock invocation) throws IOException {
                Object[] args = invocation.getArguments();
                File dbFile =
                    new File(new File(dryRunCacheLocation, "123"), (String) args[2] + ".h2.db");
                FileUtils.write(dbFile, "fake db content 2");
                return dbFile;
              }
            });
    when(resourceDao.getRootProjectByComponentId(123L)).thenReturn(new ResourceDto().setId(123L));

    byte[] dbContent = dryRunCache.getDatabaseForPreview(123L);
    assertThat(new String(dbContent)).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");

    verify(dryRunDatabaseFactory, times(2))
        .createNewDatabaseForDryRun(anyLong(), any(File.class), anyString());
  }
コード例 #2
0
  @Test
  public void test_getDatabaseForDryRun_on_new_project() throws Exception {
    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");
                FileUtils.write(dbFile, "fake db content");
                return dbFile;
              }
            });
    byte[] dbContent = dryRunCache.getDatabaseForPreview(null);
    assertThat(new String(dbContent)).isEqualTo("fake db content");

    dbContent = dryRunCache.getDatabaseForPreview(null);
    assertThat(new String(dbContent)).isEqualTo("fake db content");

    verify(dryRunDatabaseFactory, times(1))
        .createNewDatabaseForDryRun(anyLong(), any(File.class), anyString());
  }
コード例 #3
0
  @Test
  public void test_getDatabaseForDryRun_on_existing_project() throws Exception {
    when(dryRunDatabaseFactory.createNewDatabaseForDryRun(eq(123L), 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, "123"), (String) args[2] + ".h2.db");
                FileUtils.write(dbFile, "fake db content");
                return dbFile;
              }
            });
    when(resourceDao.getRootProjectByComponentId(123L)).thenReturn(new ResourceDto().setId(123L));
    byte[] dbContent = dryRunCache.getDatabaseForPreview(123L);
    assertThat(new String(dbContent)).isEqualTo("fake db content");

    dbContent = dryRunCache.getDatabaseForPreview(123L);
    assertThat(new String(dbContent)).isEqualTo("fake db content");

    verify(dryRunDatabaseFactory, times(1))
        .createNewDatabaseForDryRun(anyLong(), any(File.class), anyString());
  }