FacetRegistry { <T> T getFacet(@Nullable Class<T> facet) throws IllegalStateException { T plugin = null; if (facet != null) { List<T> plugins = getFacets(facet); if (plugins.size() > 1) { throw new IllegalStateException(String.format("One implementation was expected for the %s facet, but found: %s", facet.getSimpleName(), plugins.toString())); } else if (plugins.size() == 1) { plugin = plugins.get(0); } } return plugin; } FacetRegistry(final Collection<Plugin> plugins);   }
@Test public void get_null_is_accepted() { FacetRegistry registry = new FacetRegistry(new ArrayList<>()); Assertions.assertThat((Object)registry.getFacet(null)).isNull(); }
@Test public void scan_a_plugin_facet() { List<Plugin> plugins = new ArrayList<>(); OneFacetPlugin oneFacetPlugin = new OneFacetPlugin(); plugins.add(oneFacetPlugin); Facet1 facet1 = new FacetRegistry(plugins).getFacet(Facet1.class); Assertions.assertThat(facet1).isNotNull(); Assertions.assertThat(facet1).isEqualTo(oneFacetPlugin); }
@Test public void scan_multiple_facet_in_a_plugin() { List<Plugin> plugins = new ArrayList<>(); TwoFacetPlugin twoFacetPlugin = new TwoFacetPlugin(); plugins.add(twoFacetPlugin); FacetRegistry registry = new FacetRegistry(plugins); Facet1 facet1 = registry.getFacet(Facet1.class); Facet2 facet2 = registry.getFacet(Facet2.class); Assertions.assertThat(facet1).isNotNull(); Assertions.assertThat(facet1).isEqualTo(twoFacetPlugin); Assertions.assertThat(facet2).isNotNull(); Assertions.assertThat(facet2).isEqualTo(twoFacetPlugin); }
@Test(expected = IllegalStateException.class) public void getFacet_fails_for_multiple_facet_implementation() { List<Plugin> plugins = new ArrayList<>(); OneFacetPlugin oneFacetPlugin = new OneFacetPlugin(); TwoFacetPlugin twoFacetPlugin = new TwoFacetPlugin(); plugins.add(oneFacetPlugin); plugins.add(twoFacetPlugin); FacetRegistry registry = new FacetRegistry(plugins); registry.getFacet(Facet1.class); }