use of com.vaadin.flow.di.InstantiatorFactory in project flow by vaadin.
the class VaadinServiceTest method loadInstantiators_twoFactoriesInLookup_throws.
@Test(expected = ServiceException.class)
public void loadInstantiators_twoFactoriesInLookup_throws() throws ServiceException {
VaadinService service = createService();
Lookup lookup = Mockito.mock(Lookup.class);
service.getContext().setAttribute(Lookup.class, lookup);
InstantiatorFactory factory1 = createInstantiatorFactory(lookup);
InstantiatorFactory factory2 = createInstantiatorFactory(lookup);
Mockito.when(lookup.lookupAll(InstantiatorFactory.class)).thenReturn(Arrays.asList(factory1, factory2));
service.loadInstantiators();
}
use of com.vaadin.flow.di.InstantiatorFactory in project flow by vaadin.
the class VaadinServiceTest method createInstantiatorFactory.
private InstantiatorFactory createInstantiatorFactory(Lookup lookup) {
InstantiatorFactory factory = Mockito.mock(InstantiatorFactory.class);
Instantiator instantiator = Mockito.mock(Instantiator.class);
Mockito.when((factory.createInstantitor(Mockito.any()))).thenReturn(instantiator);
Mockito.when(instantiator.init(Mockito.any())).thenReturn(true);
return factory;
}
use of com.vaadin.flow.di.InstantiatorFactory in project flow by vaadin.
the class VaadinServiceTest method loadInstantiators_instantiatorIsLoadedUsingFactoryFromLookup.
@Test
public void loadInstantiators_instantiatorIsLoadedUsingFactoryFromLookup() throws ServiceException {
VaadinService service = createService();
Lookup lookup = Mockito.mock(Lookup.class);
service.getContext().setAttribute(Lookup.class, lookup);
InstantiatorFactory factory = createInstantiatorFactory(lookup);
Mockito.when(lookup.lookupAll(InstantiatorFactory.class)).thenReturn(Collections.singletonList(factory));
Optional<Instantiator> loadedInstantiator = service.loadInstantiators();
Instantiator instantiator = factory.createInstantitor(null);
Assert.assertSame(instantiator, loadedInstantiator.get());
}
use of com.vaadin.flow.di.InstantiatorFactory in project flow by vaadin.
the class VaadinService method loadInstantiators.
/**
* Loads and initializes instantiators.
* <p>
* A custom Vaadin service implementation can override this method to pick
* an instantiator in some other way instead of the default implementation
* that uses {@link ServiceLoader}.
* <p>
* There may be only one applicable instantiator. Otherwise
* {@link ServiceException} will be thrown.
*
* @return an optional instantator, or an empty optional if no instantiator
* found
* @throws ServiceException
* if there are multiple applicable instantiators
* @see #createInstantiator()
* @see Instantiator
*/
protected Optional<Instantiator> loadInstantiators() throws ServiceException {
Lookup lookup = getContext().getAttribute(Lookup.class);
List<Instantiator> instantiators = null;
if (lookup != null) {
// lookup may be null in tests
Collection<InstantiatorFactory> factories = lookup.lookupAll(InstantiatorFactory.class);
instantiators = new ArrayList<>(factories.size());
for (InstantiatorFactory factory : factories) {
Instantiator instantiator = factory.createInstantitor(this);
// let's respect its deprecated method
if (instantiator != null && instantiator.init(this)) {
instantiators.add(instantiator);
}
}
}
if (instantiators == null) {
instantiators = new ArrayList<>();
}
// the code to support previous way of loading instantiators
StreamSupport.stream(ServiceLoader.load(Instantiator.class, getClassLoader()).spliterator(), false).filter(iterator -> iterator.init(this)).forEach(instantiators::add);
if (instantiators.size() > 1) {
throw new ServiceException("Cannot init VaadinService because there are multiple eligible instantiator implementations: " + instantiators);
}
return instantiators.stream().findFirst();
}
Aggregations