@Test
public void testUpgradeFileName() throws Exception {

  String key = "abc%def";
  int id = index.assignIdForKey(key);
  File v3file = createTestFile(id + ".0.1.v3.exo");
  File v2file = createTestFile("abc%25def.1.2.v2.exo"); // %25 is '%' after escaping
  File wrongEscapedV2file = createTestFile("abc%2Gdef.3.4.v2.exo"); // 2G is invalid hex
  File v1File = createTestFile("abc%def.5.6.v1.exo"); // V1 did not escape
  for (File file : cacheDir.listFiles()) {
    SimpleCacheSpan cacheEntry = SimpleCacheSpan.createCacheEntry(file, file.length(), index);
    if (file.equals(wrongEscapedV2file)) {
      assertThat(cacheEntry).isNull();
    } else {
      assertThat(cacheEntry).isNotNull();
    }
  }
  assertThat(v3file.exists()).isTrue();
  assertThat(v2file.exists()).isFalse();
  assertThat(wrongEscapedV2file.exists()).isTrue();
  assertThat(v1File.exists()).isFalse();
  File[] files = cacheDir.listFiles();
  assertThat(files).hasLength(4);
  Set<String> keys = index.getKeys();
  assertWithMessage("There should be only one key for all files.").that(keys).hasSize(1);
  assertThat(keys).contains(key);
  TreeSet<SimpleCacheSpan> spans = index.get(key).getSpans();
  assertWithMessage("upgradeOldFiles() shouldn't add any spans.").that(spans.isEmpty()).isTrue();
  LongSparseArray<Long> cachedPositions = new LongSparseArray<>();
  for (File file : files) {
    SimpleCacheSpan cacheSpan = SimpleCacheSpan.createCacheEntry(file, file.length(), index);
    if (cacheSpan != null) {
      assertThat(cacheSpan.key).isEqualTo(key);
      cachedPositions.put(cacheSpan.position, cacheSpan.lastTouchTimestamp);
    }
  }
  assertThat(cachedPositions.get(0)).isEqualTo(1);
  assertThat(cachedPositions.get(1)).isEqualTo(2);
  assertThat(cachedPositions.get(5)).isEqualTo(6);
}