BasicConfiguration { public ContainerProvider getProvider() throws ServletException { Class<? extends ContainerProvider> providerType = getProviderType(); logger.info("Using {} as Container Provider", providerType); try { return providerType.getDeclaredConstructor().newInstance(); } catch (InvocationTargetException e) { throw new ServletException(e.getCause()); } catch (Exception e) { throw new ServletException(e); } } BasicConfiguration(ServletContext servletContext); ContainerProvider getProvider(); boolean hasBasePackages(); String[] getBasePackages(); String getEncoding(); String getWebinfClassesDirectory(); ServletContext getServletContext(); boolean isClasspathScanningEnabled(); static final String CONTAINER_PROVIDER; static final String ENCODING; static final String BASE_PACKAGES_PARAMETER_NAME; static final String SCANNING_PARAM; }
@Test public void shouldUseSpringContainerAsDefaultProvider() throws ServletException { when(context.getInitParameter(BasicConfiguration.CONTAINER_PROVIDER)).thenReturn(null); assertThat(config.getProvider().getClass(), is(typeCompatibleWith(SpringProvider.class))); }
@Test public void shouldThrowExceptionWhenProviderClassWasNotFound() throws ServletException { when(context.getInitParameter(BasicConfiguration.CONTAINER_PROVIDER)).thenReturn("UnknowClass"); try { config.getProvider(); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), startsWith("You must configure")); } }
@Test public void shouldAllowProviderOverriding() throws ServletException { when(context.getInitParameter(BasicConfiguration.CONTAINER_PROVIDER)) .thenReturn(MyCustomProvider.class.getName()); assertThat(config.getProvider().getClass(), Matchers.is(Matchers .typeCompatibleWith(MyCustomProvider.class))); }
@Test public void shouldThrowInstantiationExceptionCauseIfThereIsSuchException() { when(context.getInitParameter(BasicConfiguration.CONTAINER_PROVIDER)) .thenReturn(DogProvider.class.getName()); try { config.getProvider(); fail(); } catch (ServletException e) { assertNotNull("Should have a cause", e.getRootCause()); assertEquals(IOException.class, e.getRootCause().getClass()); } }