### Question:
AbnormalAnalyzerAppModule extends AbstractModule { @Provides @Singleton EventRepository provideEventRepository() { EventRepository eventRepository; Configuration configuration = getConfiguration(); String eventRepositoryType = configuration.getString(CONFKEY_EVENTS_REPOSITORY_TYPE); try { if ("csv".equalsIgnoreCase(eventRepositoryType)) { String csvFileName = configuration.getString(CONFKEY_EVENTS_CSV_FILE); eventRepository = new CsvEventRepository(Files.newOutputStream(Paths.get(csvFileName), StandardOpenOption.CREATE_NEW), false); } else if ("h2".equalsIgnoreCase(eventRepositoryType)) { SessionFactory sessionFactory = JpaSessionFactoryFactory.newH2SessionFactory(new File(configuration.getString(CONFKEY_EVENTS_H2_FILE))); eventRepository = new JpaEventRepository(sessionFactory, false); } else if ("pgsql".equalsIgnoreCase(eventRepositoryType)) { SessionFactory sessionFactory = JpaSessionFactoryFactory.newPostgresSessionFactory( configuration.getString(CONFKEY_EVENTS_PGSQL_HOST), configuration.getInt(CONFKEY_EVENTS_PGSQL_PORT, 8432), configuration.getString(CONFKEY_EVENTS_PGSQL_NAME), configuration.getString(CONFKEY_EVENTS_PGSQL_USERNAME), configuration.getString(CONFKEY_EVENTS_PGSQL_PASSWORD) ); eventRepository = new JpaEventRepository(sessionFactory, false); } else { throw new IllegalArgumentException("eventRepositoryType: " + eventRepositoryType); } } catch (HibernateException e) { LOG.error(e.getMessage(), e); throw e; } catch (IOException e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } return eventRepository; } AbnormalAnalyzerAppModule(Path configFile); @Override void configure(); final static long STARTUP_TIMESTAMP; }### Answer:
@Test(expected = IllegalArgumentException.class) public void cannotProvideUnknownEventRepositoryH2() throws IOException { configuration.addProperty(CONFKEY_EVENTS_REPOSITORY_TYPE, "unknown"); sut.provideEventRepository().getClass(); }
@Test public void canProvideCsvEventRepository() throws IOException { Files.deleteIfExists(Paths.get("events.csv")); configuration.addProperty(CONFKEY_EVENTS_REPOSITORY_TYPE, "csv"); configuration.addProperty(CONFKEY_EVENTS_CSV_FILE, "events.csv"); assertEquals(CsvEventRepository.class, sut.provideEventRepository().getClass()); }
@Test public void canProvideJpaEventRepositoryH2() throws IOException { configuration.addProperty(CONFKEY_EVENTS_REPOSITORY_TYPE, "h2"); configuration.addProperty(CONFKEY_EVENTS_H2_FILE, "h2file"); assertEquals(JpaEventRepository.class, sut.provideEventRepository().getClass()); }
@Test(expected = NullPointerException.class) public void canProvideJpaEventRepositoryPgsql() throws IOException { configuration.addProperty(CONFKEY_EVENTS_REPOSITORY_TYPE, "pgsql"); assertEquals(JpaEventRepository.class, sut.provideEventRepository().getClass()); }