@Test public void processAndBackupFileFileShouldCatchExceptionStream() { try { new Expectations(FileInputStream.class) { { new FileInputStream((File) any); result = new IOException("testException123"); } }; } catch (FileNotFoundException e) { fail("Unexpected exception in expectations block: " + e); } testImporterProcessor.processAndBackupFile(mockFile, injectableBackupDir, injectableFailureDir); }
public void processAndBackupFile(Path filePath, Path backupDir, Path failureDir) { boolean success = true; InputStream inputStream = null; BufferedInputStream bis = null; try { inputStream = new FileInputStream(filePath.toFile()); String probeContentType = Files.probeContentType(filePath); if ((probeContentType != null && gZipPattern.matcher(probeContentType).matches()) || filePath.toString().toLowerCase().endsWith("gz")) { logger.info("Treating as gzip file"); inputStream = new GZIPInputStream(inputStream); bis = publishFile(filePath, inputStream); } else if ((probeContentType != null && zipPattern.matcher(probeContentType).matches()) || filePath.toString().endsWith("zip")) { logger.info("Treating as zip file"); inputStream = new ZipInputStream(inputStream); ZipInputStream zis = (ZipInputStream)inputStream; while (zis.getNextEntry() != null) { bis = publishFile(filePath, inputStream); } } else { logger.info("Treating as unknown file"); bis = publishFile(filePath, inputStream); } } catch (Exception e) { success = false; logger.error("Failed to open or process file: " + filePath, e); EventLogger.logger.error("Failed to open or process file: " + filePath, e); } finally { try { if (bis != null) { bis.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { logger.error("Failed to close file stream: {}", e); } } try { if (success) { OdeFileUtils.backupFile(filePath, backupDir); logger.info("File moved to backup: {}", backupDir); EventLogger.logger.info("File moved to backup: {}", backupDir); } else { OdeFileUtils.moveFile(filePath, failureDir); logger.info("File moved to failure directory: {}", failureDir); EventLogger.logger.info("File moved to failure directory: {}", failureDir); } } catch (IOException e) { logger.error("Unable to backup file: " + filePath, e); } }
ImporterProcessor { public void processAndBackupFile(Path filePath, Path backupDir, Path failureDir) { boolean success = true; InputStream inputStream = null; BufferedInputStream bis = null; try { inputStream = new FileInputStream(filePath.toFile()); String probeContentType = Files.probeContentType(filePath); if ((probeContentType != null && gZipPattern.matcher(probeContentType).matches()) || filePath.toString().toLowerCase().endsWith("gz")) { logger.info("Treating as gzip file"); inputStream = new GZIPInputStream(inputStream); bis = publishFile(filePath, inputStream); } else if ((probeContentType != null && zipPattern.matcher(probeContentType).matches()) || filePath.toString().endsWith("zip")) { logger.info("Treating as zip file"); inputStream = new ZipInputStream(inputStream); ZipInputStream zis = (ZipInputStream)inputStream; while (zis.getNextEntry() != null) { bis = publishFile(filePath, inputStream); } } else { logger.info("Treating as unknown file"); bis = publishFile(filePath, inputStream); } } catch (Exception e) { success = false; logger.error("Failed to open or process file: " + filePath, e); EventLogger.logger.error("Failed to open or process file: " + filePath, e); } finally { try { if (bis != null) { bis.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { logger.error("Failed to close file stream: {}", e); } } try { if (success) { OdeFileUtils.backupFile(filePath, backupDir); logger.info("File moved to backup: {}", backupDir); EventLogger.logger.info("File moved to backup: {}", backupDir); } else { OdeFileUtils.moveFile(filePath, failureDir); logger.info("File moved to failure directory: {}", failureDir); EventLogger.logger.info("File moved to failure directory: {}", failureDir); } } catch (IOException e) { logger.error("Unable to backup file: " + filePath, e); } } }
ImporterProcessor { public void processAndBackupFile(Path filePath, Path backupDir, Path failureDir) { boolean success = true; InputStream inputStream = null; BufferedInputStream bis = null; try { inputStream = new FileInputStream(filePath.toFile()); String probeContentType = Files.probeContentType(filePath); if ((probeContentType != null && gZipPattern.matcher(probeContentType).matches()) || filePath.toString().toLowerCase().endsWith("gz")) { logger.info("Treating as gzip file"); inputStream = new GZIPInputStream(inputStream); bis = publishFile(filePath, inputStream); } else if ((probeContentType != null && zipPattern.matcher(probeContentType).matches()) || filePath.toString().endsWith("zip")) { logger.info("Treating as zip file"); inputStream = new ZipInputStream(inputStream); ZipInputStream zis = (ZipInputStream)inputStream; while (zis.getNextEntry() != null) { bis = publishFile(filePath, inputStream); } } else { logger.info("Treating as unknown file"); bis = publishFile(filePath, inputStream); } } catch (Exception e) { success = false; logger.error("Failed to open or process file: " + filePath, e); EventLogger.logger.error("Failed to open or process file: " + filePath, e); } finally { try { if (bis != null) { bis.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { logger.error("Failed to close file stream: {}", e); } } try { if (success) { OdeFileUtils.backupFile(filePath, backupDir); logger.info("File moved to backup: {}", backupDir); EventLogger.logger.info("File moved to backup: {}", backupDir); } else { OdeFileUtils.moveFile(filePath, failureDir); logger.info("File moved to failure directory: {}", failureDir); EventLogger.logger.info("File moved to failure directory: {}", failureDir); } } catch (IOException e) { logger.error("Unable to backup file: " + filePath, e); } } ImporterProcessor(OdeProperties odeProperties, ImporterFileType fileType); }
ImporterProcessor { public void processAndBackupFile(Path filePath, Path backupDir, Path failureDir) { boolean success = true; InputStream inputStream = null; BufferedInputStream bis = null; try { inputStream = new FileInputStream(filePath.toFile()); String probeContentType = Files.probeContentType(filePath); if ((probeContentType != null && gZipPattern.matcher(probeContentType).matches()) || filePath.toString().toLowerCase().endsWith("gz")) { logger.info("Treating as gzip file"); inputStream = new GZIPInputStream(inputStream); bis = publishFile(filePath, inputStream); } else if ((probeContentType != null && zipPattern.matcher(probeContentType).matches()) || filePath.toString().endsWith("zip")) { logger.info("Treating as zip file"); inputStream = new ZipInputStream(inputStream); ZipInputStream zis = (ZipInputStream)inputStream; while (zis.getNextEntry() != null) { bis = publishFile(filePath, inputStream); } } else { logger.info("Treating as unknown file"); bis = publishFile(filePath, inputStream); } } catch (Exception e) { success = false; logger.error("Failed to open or process file: " + filePath, e); EventLogger.logger.error("Failed to open or process file: " + filePath, e); } finally { try { if (bis != null) { bis.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { logger.error("Failed to close file stream: {}", e); } } try { if (success) { OdeFileUtils.backupFile(filePath, backupDir); logger.info("File moved to backup: {}", backupDir); EventLogger.logger.info("File moved to backup: {}", backupDir); } else { OdeFileUtils.moveFile(filePath, failureDir); logger.info("File moved to failure directory: {}", failureDir); EventLogger.logger.info("File moved to failure directory: {}", failureDir); } } catch (IOException e) { logger.error("Unable to backup file: " + filePath, e); } } ImporterProcessor(OdeProperties odeProperties, ImporterFileType fileType); int processDirectory(Path dir, Path backupDir, Path failureDir); void processAndBackupFile(Path filePath, Path backupDir, Path failureDir); }
ImporterProcessor { public void processAndBackupFile(Path filePath, Path backupDir, Path failureDir) { boolean success = true; InputStream inputStream = null; BufferedInputStream bis = null; try { inputStream = new FileInputStream(filePath.toFile()); String probeContentType = Files.probeContentType(filePath); if ((probeContentType != null && gZipPattern.matcher(probeContentType).matches()) || filePath.toString().toLowerCase().endsWith("gz")) { logger.info("Treating as gzip file"); inputStream = new GZIPInputStream(inputStream); bis = publishFile(filePath, inputStream); } else if ((probeContentType != null && zipPattern.matcher(probeContentType).matches()) || filePath.toString().endsWith("zip")) { logger.info("Treating as zip file"); inputStream = new ZipInputStream(inputStream); ZipInputStream zis = (ZipInputStream)inputStream; while (zis.getNextEntry() != null) { bis = publishFile(filePath, inputStream); } } else { logger.info("Treating as unknown file"); bis = publishFile(filePath, inputStream); } } catch (Exception e) { success = false; logger.error("Failed to open or process file: " + filePath, e); EventLogger.logger.error("Failed to open or process file: " + filePath, e); } finally { try { if (bis != null) { bis.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { logger.error("Failed to close file stream: {}", e); } } try { if (success) { OdeFileUtils.backupFile(filePath, backupDir); logger.info("File moved to backup: {}", backupDir); EventLogger.logger.info("File moved to backup: {}", backupDir); } else { OdeFileUtils.moveFile(filePath, failureDir); logger.info("File moved to failure directory: {}", failureDir); EventLogger.logger.info("File moved to failure directory: {}", failureDir); } } catch (IOException e) { logger.error("Unable to backup file: " + filePath, e); } } ImporterProcessor(OdeProperties odeProperties, ImporterFileType fileType); int processDirectory(Path dir, Path backupDir, Path failureDir); void processAndBackupFile(Path filePath, Path backupDir, Path failureDir);  }