SessionHelpers { static SessionRepository repository(ServletContext servletContext, SessionConfiguration conf) { String repositoryFactoryId = conf.getRepositoryFactory(); if (repositoryFactoryId == null) { repositoryFactoryId = DEFAULT_REPOSITORY_FACTORY; } @SuppressWarnings("unchecked") Map<String, String> providers = (Map<String, String>)servletContext.getAttribute(Attributes.PROVIDERS); String repositoryFactory; if (providers != null && providers.containsKey(repositoryFactoryId)) { repositoryFactory = providers.get(repositoryFactoryId); } else { repositoryFactory = repositoryFactoryId; } try { return repositoryOrDefault(repositoryFactory, servletContext, conf); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new IllegalArgumentException("Unable to load or instantiate SessionRepositoryFactory. Id=" + repositoryFactoryId + ", Implementation=" + repositoryFactory, e); } }  ServletRequest prepareRequest(ServletRequest request, ServletResponse response,
      ServletContext originalServletContext); ServletResponse prepareResponse(ServletRequest request, ServletResponse response); MethodHandle[] initSessionManagement(ServletContext servletContext); MethodHandle[] initSessionManagement(ServletContext servletContext, boolean reset); void commitRequest(ServletRequest request, ServletRequest oldRequest); @SuppressWarnings("unchecked") void interceptHttpListener(EventListener caller, HttpSessionEvent event); void onAddListener(ServletContext servletContext, Object listener);  }
@Test public void testGetRepositoryDefault() { when(servletContext.getClassLoader()).thenReturn(this.getClass().getClassLoader()); SessionConfiguration sessionConfiguration = new SessionConfiguration(); SessionRepository repository = SessionHelpers.repository(servletContext, sessionConfiguration); assertEquals("com.amadeus.session.repository.inmemory.InMemoryRepository", repository.getClass().getName()); }
@Test public void testGetRepositoryByKeyExisting() { when(servletContext.getClassLoader()).thenReturn(this.getClass().getClassLoader()); Map<String, String> map = new HashMap<>(); map.put("IN", InMemoryRepositoryFactory.class.getName()); map.put("BAD", "com.not.existing.class.Bad"); when(servletContext.getAttribute(Attributes.PROVIDERS)).thenReturn(map); SessionConfiguration sessionConfiguration = new SessionConfiguration(); sessionConfiguration.setRepositoryFactory("IN"); SessionRepository repository = SessionHelpers.repository(servletContext, sessionConfiguration); assertEquals("com.amadeus.session.repository.inmemory.InMemoryRepository", repository.getClass().getName()); }
@Test(expected=IllegalArgumentException.class) public void testGetRepositoryByKeyBadClass() { when(servletContext.getClassLoader()).thenReturn(this.getClass().getClassLoader()); Map<String, String> map = new HashMap<>(); map.put("IN", InMemoryRepositoryFactory.class.getName()); map.put("BAD", "com.not.existing.class.Bad"); when(servletContext.getAttribute(Attributes.PROVIDERS)).thenReturn(map); SessionConfiguration sessionConfiguration = new SessionConfiguration(); sessionConfiguration.setRepositoryFactory("BAD"); SessionHelpers.repository(servletContext, sessionConfiguration); }
@Test(expected=IllegalArgumentException.class) public void testGetRepositoryByKeyBadId() { when(servletContext.getClassLoader()).thenReturn(this.getClass().getClassLoader()); Map<String, String> map = new HashMap<>(); map.put("IN", InMemoryRepositoryFactory.class.getName()); map.put("BAD", "com.not.existing.class.Bad"); when(servletContext.getAttribute(Attributes.PROVIDERS)).thenReturn(map); SessionConfiguration sessionConfiguration = new SessionConfiguration(); sessionConfiguration.setRepositoryFactory("BAD_ID"); SessionHelpers.repository(servletContext, sessionConfiguration); }