Пример #1
0
  @Test
  public void test_getDatabaseForDryRun_global_invalidation() 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 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, "default"), (String) args[2] + ".h2.db");
                FileUtils.write(dbFile, "fake db content 2");
                return dbFile;
              }
            });
    byte[] dbContent = dryRunCache.getDatabaseForPreview(null);
    assertThat(new String(dbContent)).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");

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