@Test
public void testRefreshLoadedJobCache() throws Exception {

  HistoryFileManager historyManager = mock(HistoryFileManager.class);
  jobHistory = spy(new JobHistory());
  doReturn(historyManager).when(jobHistory).createHistoryFileManager();
  Configuration conf = new Configuration();
  // Set the cache size to 2
  conf.setInt(JHAdminConfig.MR_HISTORY_LOADED_JOB_CACHE_SIZE, 2);
  jobHistory.init(conf);
  jobHistory.start();
  CachedHistoryStorage storage = spy((CachedHistoryStorage) jobHistory
      .getHistoryStorage());
  assertFalse(storage.getUseLoadedTasksCache());
  Job[] jobs = new Job[3];
  JobId[] jobIds = new JobId[3];
  for (int i = 0; i < 3; i++) {
    jobs[i] = mock(Job.class);
    jobIds[i] = mock(JobId.class);
    when(jobs[i].getID()).thenReturn(jobIds[i]);
  }
  HistoryFileInfo fileInfo = mock(HistoryFileInfo.class);
  when(historyManager.getFileInfo(any(JobId.class))).thenReturn(fileInfo);
  when(fileInfo.loadJob()).thenReturn(jobs[0]).thenReturn(jobs[1])
      .thenReturn(jobs[2]);
  // getFullJob will put the job in the cache if it isn't there
  for (int i = 0; i < 3; i++) {
    storage.getFullJob(jobs[i].getID());
  }
  Cache<JobId, Job> jobCache = storage.getLoadedJobCache();
  // Verify some jobs are stored in the cache.  Hard to predict eviction
  // in Guava version.
  assertTrue(jobCache.size() > 0);
  // Setting cache size to 3
  conf.setInt(JHAdminConfig.MR_HISTORY_LOADED_JOB_CACHE_SIZE, 3);
  doReturn(conf).when(storage).createConf();
  when(fileInfo.loadJob()).thenReturn(jobs[0]).thenReturn(jobs[1])
      .thenReturn(jobs[2]);
  jobHistory.refreshLoadedJobCache();
  for (int i = 0; i < 3; i++) {
    storage.getFullJob(jobs[i].getID());
  }
  jobCache = storage.getLoadedJobCache();
  // Verify some jobs are stored in the cache.  Hard to predict eviction
  // in Guava version.
  assertTrue(jobCache.size() > 0);
}