WebComponentConfigurationRegistryInitializer implements ClassLoaderAwareServletContainerInitializer { @Override @SuppressWarnings("rawtypes") public void process(Set<Class<?>> set, ServletContext servletContext) throws ServletException { WebComponentConfigurationRegistry instance = WebComponentConfigurationRegistry .getInstance(new VaadinServletContext(servletContext)); if (set == null || set.isEmpty()) { instance.setConfigurations(Collections.emptySet()); return; } try { Set<WebComponentExporterFactory> factories = WebComponentExporterUtils .getFactories(set); Set<WebComponentConfiguration<? extends Component>> configurations = constructConfigurations( factories); validateTagNames(configurations); validateDistinctTagNames(configurations); instance.setConfigurations(configurations); } catch (Exception e) { throw new ServletException( String.format("%s failed to collect %s implementations!", WebComponentConfigurationRegistryInitializer.class .getSimpleName(), WebComponentExporter.class.getSimpleName()), e); } }  @Override @SuppressWarnings("rawtypes") void process(Set<Class<?>> set, ServletContext servletContext);  }
@Test public void process() throws ServletException { initializer.process( Stream.of(MyComponentExporter.class, UserBoxExporter.class, ExporterFactory.class).collect(Collectors.toSet()), servletContext); ArgumentCaptor<Set> captor = ArgumentCaptor.forClass(Set.class); Mockito.verify(registry).setConfigurations(captor.capture()); Set<?> set = captor.getValue(); Assert.assertEquals(3, set.size()); Set<Class> componentClasses = set.stream() .map(WebComponentConfiguration.class::cast) .map(WebComponentConfiguration::getComponentClass) .collect(Collectors.toSet()); Assert.assertTrue(componentClasses.contains(MyComponent.class)); Assert.assertTrue(componentClasses.contains(UserBox.class)); Assert.assertTrue(componentClasses.contains(InvalidName.class)); }
@Test public void process_noExceptionWithNullArguments() { try { initializer.process(null, servletContext); } catch (Exception e) { Assert.fail( "WebComponentRegistryInitializer.process should not throw with null argument"); } Mockito.verify(registry).setConfigurations(Collections.emptySet()); }
@Test public void process_noExceptionForMultipleCorrectExportsOfTheSameComponent() { try { initializer.process( Stream.of(MyComponentExporter.class, SiblingExporter.class) .collect(Collectors.toSet()), servletContext); } catch (Exception e) { Assert.fail("WebComponentRegistryInitializer.process should not " + "throw with 'sibling' exporters"); } }
@Test public void emptySet_noExceptionAndWebComponentsSet() { try { initializer.process(Collections.emptySet(), servletContext); } catch (Exception e) { Assert.fail( "WebComponentRegistryInitializer.process should not throw with empty set"); } Mockito.verify(registry).setConfigurations(Collections.emptySet()); }
@Test public void duplicateNamesFoundprocess_exceptionIsThrown() throws ServletException { expectedEx.expect(ServletException.class); expectedEx.expectCause(CauseMatcher.ex(IllegalArgumentException.class) .msgStartsWith("Found two WebComponentExporter classes")); initializer.process( Stream.of(MyComponentExporter.class, DuplicateTagExporter.class) .collect(Collectors.toSet()), servletContext); }
@Test public void invalidCustomElementName_initializerThrowsException() throws ServletException { expectedEx.expect(ServletException.class); expectedEx.expectCause(CauseMatcher .ex(InvalidCustomElementNameException.class) .msgEquals(String.format( "Tag name '%s' given by '%s' is not a valid custom element " + "name.", "invalid", InvalidNameExporter.class.getCanonicalName()))); initializer.process(Collections.singleton(InvalidNameExporter.class), servletContext); }
@Test public void duplicatePropertyRegistration_doesNotCauseIssues() throws ServletException { initializer.process( Collections.singleton(DuplicatePropertyExporter.class), servletContext); }
@Test public void duplicatePropertyRegistrationBetweenParentAndChild_doesNotCauseIssues() throws ServletException { initializer.process(Collections.singleton(ExtendingExporter.class), servletContext); }