### Question:
NamespaceId extends AbstractId<NamespaceId> { public static NamespaceId of(final String canonical) { try { return cache.get(canonical, () -> new NamespaceId(canonical)); } catch (ExecutionException e) { throw new AssertionError( "This should never happen: ID constructor should never return null."); } } private  NamespaceId(String canonical); static NamespaceId of(final String canonical);  }### Answer:
@Test public void testCacheNoDuplicates() { assertNotSame(Namespace.ACCUMULO.id(), Namespace.DEFAULT.id()); String namespaceString = "namespace-" + name.getMethodName(); long initialSize = NamespaceId.cache.asMap().entrySet().stream().count(); NamespaceId nsId = NamespaceId.of(namespaceString); assertEquals(initialSize + 1, NamespaceId.cache.asMap().entrySet().stream().count()); assertEquals(namespaceString, nsId.canonical()); NamespaceId builtInNamespaceId = NamespaceId.of("+accumulo"); assertSame(Namespace.ACCUMULO.id(), builtInNamespaceId); builtInNamespaceId = NamespaceId.of("+default"); assertSame(Namespace.DEFAULT.id(), builtInNamespaceId); nsId = NamespaceId.of(namespaceString); assertEquals(initialSize + 1, NamespaceId.cache.asMap().entrySet().stream().count()); assertEquals(namespaceString, nsId.canonical()); NamespaceId nsId2 = NamespaceId.of(namespaceString); assertEquals(initialSize + 1, NamespaceId.cache.asMap().entrySet().stream().count()); assertSame(nsId, nsId2); }
@Test(timeout = 30_000) public void testCacheIncreasesAndDecreasesAfterGC() { long initialSize = NamespaceId.cache.asMap().entrySet().stream().count(); assertTrue(initialSize < 20); LOG.info("Initial cache size: {}", initialSize); LOG.info(NamespaceId.cache.asMap().toString()); String namespaceString = "namespace-" + name.getMethodName(); NamespaceId nsId = NamespaceId.of(namespaceString); assertEquals(initialSize + 1, NamespaceId.cache.asMap().entrySet().stream().count()); assertEquals(namespaceString, nsId.canonical()); for (int i = 0; i < 999; i++) { NamespaceId.of(new String("namespace" + i)); } long preGCSize = NamespaceId.cache.asMap().entrySet().stream().count(); LOG.info("Entries before System.gc(): {}", preGCSize); assertTrue(preGCSize > 500); long postGCSize = preGCSize; while (postGCSize >= preGCSize) { TableIdTest.tryToGc(); postGCSize = NamespaceId.cache.asMap().entrySet().stream().count(); LOG.info("Entries after System.gc(): {}", postGCSize); } }