FacetRegistry { @SuppressWarnings("unchecked") <T> List<T> getFacets(@Nullable Class<T> facet) { if (facet != null && Plugin.class.isAssignableFrom(facet)) { List<T> ts = new ArrayList<>(); Plugin plugin = pluginsByClass.get(facet); if (plugin != null) { ts.add((T) plugin); } return ts; } return (List<T>) pluginsByFacet.get(facet); } FacetRegistry(final Collection<Plugin> plugins);   }
@Test public void getFacets_never_return_null() { List<Plugin> plugins = new ArrayList<>(); OneFacetPlugin oneFacetPlugin = new OneFacetPlugin(); plugins.add(oneFacetPlugin); FacetRegistry registry = new FacetRegistry(plugins); List<Facet2> facet2s = registry.getFacets(Facet2.class); Assertions.assertThat(facet2s).isNotNull(); Assertions.assertThat(facet2s).isEmpty(); }
@Test public void get_multiple_facet_implementations() { List<Plugin> plugins = new ArrayList<>(); OneFacetPlugin oneFacetPlugin = new OneFacetPlugin(); TwoFacetPlugin twoFacetPlugin = new TwoFacetPlugin(); plugins.add(oneFacetPlugin); plugins.add(twoFacetPlugin); FacetRegistry registry = new FacetRegistry(plugins); List<Facet1> facet1s = registry.getFacets(Facet1.class); Assertions.assertThat(facet1s).hasSize(2); Assertions.assertThat(facet1s.get(0)).isEqualTo(oneFacetPlugin); Assertions.assertThat(facet1s.get(0)).isInstanceOf(OneFacetPlugin.class); Assertions.assertThat(facet1s.get(1)).isEqualTo(twoFacetPlugin); Assertions.assertThat(facet1s.get(1)).isInstanceOf(TwoFacetPlugin.class); List<Facet2> facet2s = registry.getFacets(Facet2.class); Assertions.assertThat(facet2s).hasSize(1); }
@Test public void get_plugin_implementation() { List<Plugin> plugins = new ArrayList<>(); LegacyPlugin legacyPlugin = new LegacyPlugin(); plugins.add(legacyPlugin); FacetRegistry registry = new FacetRegistry(plugins); List<LegacyPlugin> legacyPlugins = registry.getFacets(LegacyPlugin.class); Assertions.assertThat(legacyPlugins).hasSize(1); Assertions.assertThat(legacyPlugins.get(0)).isEqualTo(legacyPlugin); Assertions.assertThat(legacyPlugins.get(0)).isInstanceOf(LegacyPlugin.class); }
@Test public void get_missing_plugin_implementations() { List<Plugin> plugins = new ArrayList<>(); FacetRegistry registry = new FacetRegistry(plugins); List<LegacyPlugin> legacyPlugins = registry.getFacets(LegacyPlugin.class); Assertions.assertThat(legacyPlugins).hasSize(0); }