JobReports { void init(String reportingDirectory) throws JobReportsException { this.reportingDirectory = reportingDirectory; this.writers = new HashMap<>(); Path directoryPath = Paths.get(this.reportingDirectory); if (!Files.exists(directoryPath)) { try { Files.createDirectory(directoryPath); } catch (IOException exception) { LOGGER.log(Level.SEVERE, "Unable to create directory '" + this.reportingDirectory + "'", exception); throw new JobReportsException("Unable to create directory '" + this.reportingDirectory + "'", exception); } } try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directoryPath, "calvalus-reporting*.json")) { LOGGER.info("Initializing job reports files inside '" + this.reportingDirectory + "'."); for (Path reportFile : directoryStream) { String reportFileName = reportFile.getFileName().toString(); String reportKey; try { reportKey = parseReportKey(reportFileName); } catch (JobReportsFileException exception) { LOGGER.log(Level.WARNING, exception.getMessage()); continue; } Stream<String> stream = Files.lines(reportFile); Gson gson = new Gson(); stream.forEach((line) -> { String formattedEntry = formatToJson(line); JobDetailType jobDetail = gson.fromJson(formattedEntry, JobDetailType.class); this.knownJobIdSet.add(jobDetail.getJobId()); }); stream.close(); } } catch (IOException exception) { LOGGER.log(Level.SEVERE, "Unable to initiate job reports file inside '" + this.reportingDirectory + "'", exception); throw new JobReportsException("Unable to initiate job reports file inside '" + this.reportingDirectory + "'", exception); } }  void add(String jobId, long finishTime, String jobDetailJson);  }
@Test public void canCatchIOException() throws Exception { thrownException.expect(JobReportsException.class); thrownException.expectMessage("Unable to create directory 'imaginary/unknown'"); jobReports.init("imaginary/unknown"); }