@Test public void noHistoryFileGivesMemoryHistory() throws Exception { File historyFile = Paths.get(temp.newFolder().getAbsolutePath(), "asfasd", "zxvses", "fanjtaacf").toFile(); assertFalse(historyFile.getParentFile().isDirectory()); assertFalse(historyFile.getParentFile().getParentFile().isDirectory()); Historian historian = FileHistorian.setupHistory(reader, logger, historyFile); assertNotNull(historian); verify(logger).printError(contains("Could not load history file. Falling back to session-based history.\n" + "Failed to create directory for history")); }
@Nonnull public static Historian setupHistory(@Nonnull final ConsoleReader reader, @Nonnull final Logger logger, @Nonnull final File historyFile) throws IOException { try { File dir = historyFile.getParentFile(); if (!dir.isDirectory() && !dir.mkdir()) { throw new IOException("Failed to create directory for history: " + dir.getAbsolutePath()); } final FileHistory history = new FileHistory(historyFile); reader.setHistory(history); addShutdownHookToFlushHistory(logger, history); return new FileHistorian(history); } catch (IOException e) { logger.printError("Could not load history file. Falling back to session-based history.\n" + e.getMessage()); MemoryHistory history = new MemoryHistory(); reader.setHistory(history); return new FileHistorian(history); } }
FileHistorian implements Historian { @Nonnull public static Historian setupHistory(@Nonnull final ConsoleReader reader, @Nonnull final Logger logger, @Nonnull final File historyFile) throws IOException { try { File dir = historyFile.getParentFile(); if (!dir.isDirectory() && !dir.mkdir()) { throw new IOException("Failed to create directory for history: " + dir.getAbsolutePath()); } final FileHistory history = new FileHistory(historyFile); reader.setHistory(history); addShutdownHookToFlushHistory(logger, history); return new FileHistorian(history); } catch (IOException e) { logger.printError("Could not load history file. Falling back to session-based history.\n" + e.getMessage()); MemoryHistory history = new MemoryHistory(); reader.setHistory(history); return new FileHistorian(history); } } }
FileHistorian implements Historian { @Nonnull public static Historian setupHistory(@Nonnull final ConsoleReader reader, @Nonnull final Logger logger, @Nonnull final File historyFile) throws IOException { try { File dir = historyFile.getParentFile(); if (!dir.isDirectory() && !dir.mkdir()) { throw new IOException("Failed to create directory for history: " + dir.getAbsolutePath()); } final FileHistory history = new FileHistory(historyFile); reader.setHistory(history); addShutdownHookToFlushHistory(logger, history); return new FileHistorian(history); } catch (IOException e) { logger.printError("Could not load history file. Falling back to session-based history.\n" + e.getMessage()); MemoryHistory history = new MemoryHistory(); reader.setHistory(history); return new FileHistorian(history); } } private  FileHistorian(MemoryHistory history); }
FileHistorian implements Historian { @Nonnull public static Historian setupHistory(@Nonnull final ConsoleReader reader, @Nonnull final Logger logger, @Nonnull final File historyFile) throws IOException { try { File dir = historyFile.getParentFile(); if (!dir.isDirectory() && !dir.mkdir()) { throw new IOException("Failed to create directory for history: " + dir.getAbsolutePath()); } final FileHistory history = new FileHistory(historyFile); reader.setHistory(history); addShutdownHookToFlushHistory(logger, history); return new FileHistorian(history); } catch (IOException e) { logger.printError("Could not load history file. Falling back to session-based history.\n" + e.getMessage()); MemoryHistory history = new MemoryHistory(); reader.setHistory(history); return new FileHistorian(history); } } private  FileHistorian(MemoryHistory history); @Nonnull static Historian setupHistory(@Nonnull final ConsoleReader reader,
                                  @Nonnull final Logger logger,
                                  @Nonnull final File historyFile); @Nonnull static File getDefaultHistoryFile(); @Nonnull @Override List<String> getHistory(); @Override void flushHistory(); }
FileHistorian implements Historian { @Nonnull public static Historian setupHistory(@Nonnull final ConsoleReader reader, @Nonnull final Logger logger, @Nonnull final File historyFile) throws IOException { try { File dir = historyFile.getParentFile(); if (!dir.isDirectory() && !dir.mkdir()) { throw new IOException("Failed to create directory for history: " + dir.getAbsolutePath()); } final FileHistory history = new FileHistory(historyFile); reader.setHistory(history); addShutdownHookToFlushHistory(logger, history); return new FileHistorian(history); } catch (IOException e) { logger.printError("Could not load history file. Falling back to session-based history.\n" + e.getMessage()); MemoryHistory history = new MemoryHistory(); reader.setHistory(history); return new FileHistorian(history); } } private  FileHistorian(MemoryHistory history); @Nonnull static Historian setupHistory(@Nonnull final ConsoleReader reader,
                                  @Nonnull final Logger logger,
                                  @Nonnull final File historyFile); @Nonnull static File getDefaultHistoryFile(); @Nonnull @Override List<String> getHistory(); @Override void flushHistory();  }