### Question:
CacheStorageReader { Optional<DescriptorDigest> select(DescriptorDigest selector) throws CacheCorruptedException, IOException { Path selectorFile = cacheStorageFiles.getSelectorFile(selector); if (!Files.exists(selectorFile)) { return Optional.empty(); } String selectorFileContents = new String(Files.readAllBytes(selectorFile), StandardCharsets.UTF_8); try { return Optional.of(DescriptorDigest.fromHash(selectorFileContents)); } catch (DigestException ex) { throw new CacheCorruptedException( cacheStorageFiles.getCacheDirectory(), "Expected valid layer digest as contents of selector file `" + selectorFile + "` for selector `" + selector.getHash() + "`, but got: " + selectorFileContents); } } CacheStorageReader(CacheStorageFiles cacheStorageFiles);   }### Answer:
@Test public void testSelect_invalidLayerDigest() throws IOException { DescriptorDigest selector = layerDigest1; Path selectorFile = cacheStorageFiles.getSelectorFile(selector); Files.createDirectories(selectorFile.getParent()); Files.write(selectorFile, "not a valid layer digest".getBytes(StandardCharsets.UTF_8)); try { cacheStorageReader.select(selector); Assert.fail("Should have thrown CacheCorruptedException"); } catch (CacheCorruptedException ex) { MatcherAssert.assertThat( ex.getMessage(), CoreMatchers.startsWith( "Expected valid layer digest as contents of selector file `" + selectorFile + "` for selector `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`, but got: not a valid layer digest")); } }
@Test public void testSelect() throws IOException, CacheCorruptedException { DescriptorDigest selector = layerDigest1; Path selectorFile = cacheStorageFiles.getSelectorFile(selector); Files.createDirectories(selectorFile.getParent()); Files.write(selectorFile, layerDigest2.getHash().getBytes(StandardCharsets.UTF_8)); Optional<DescriptorDigest> selectedLayerDigest = cacheStorageReader.select(selector); Assert.assertTrue(selectedLayerDigest.isPresent()); Assert.assertEquals(layerDigest2, selectedLayerDigest.get()); }