@Test public void convert() throws IOException, OfficeException, OfficeConverterException { OfficeConverterException officeConverterException = assertThrows(OfficeConverterException.class, () -> this.defaultOfficeConverter.convert(Collections.emptyMap(), "myFile", "myOutputFile")); assertEquals("No input stream specified for main input file [myFile].", officeConverterException.getMessage()); String content = "my content"; Map<String, InputStream> inputStreamMap = Collections.singletonMap("myFile", new ByteArrayInputStream(content.getBytes())); ConversionJobWithOptionalSourceFormatUnspecified jobSource = mock(ConversionJobWithOptionalSourceFormatUnspecified.class); when(this.localConverter.convert(any(File.class))).thenReturn(jobSource); ConversionJobWithOptionalTargetFormatUnspecified jobTarget = mock(ConversionJobWithOptionalTargetFormatUnspecified.class); when(jobSource.to(any(File.class))).thenReturn(jobTarget); Map<String, byte[]> obtainedResult = this.defaultOfficeConverter.convert(inputStreamMap, "myFile", "myOutputFile"); assertTrue(obtainedResult.isEmpty()); ArgumentCaptor<File> argument = ArgumentCaptor.forClass(File.class); verify(this.localConverter).convert(argument.capture()); assertEquals("myFile", argument.getValue().getName()); argument = ArgumentCaptor.forClass(File.class); verify(jobSource).to(argument.capture()); assertEquals("myOutputFile", argument.getValue().getName()); verify(jobTarget).execute(); }
@Override public Map<String, byte[]> convert(Map<String, InputStream> inputStreams, String inputFileName, String outputFileName) throws OfficeConverterException { if (null == inputStreams.get(inputFileName)) { String message = "No input stream specified for main input file [%s]."; throw new OfficeConverterException(String.format(message, inputFileName)); } OfficeConverterFileStorage storage = null; try { storage = new OfficeConverterFileStorage(this.workDir, inputFileName, outputFileName); FileOutputStream fos = null; for (Map.Entry<String, InputStream> entry : inputStreams.entrySet()) { try { File temp = new File(storage.getInputDir(), entry.getKey()); fos = new FileOutputStream(temp); IOUtils.copy(entry.getValue(), fos); } finally { IOUtils.closeQuietly(fos); } } this.converter.convert(storage.getInputFile()) .to(storage.getOutputFile()) .execute(); Map<String, byte[]> result = new HashMap<String, byte[]>(); FileInputStream fis = null; for (File file : storage.getOutputDir().listFiles()) { try { fis = new FileInputStream(file); result.put(file.getName(), IOUtils.toByteArray(fis)); } finally { IOUtils.closeQuietly(fis); } } return result; } catch (Exception ex) { throw new OfficeConverterException("Error while performing conversion.", ex); } finally { if (!storage.cleanUp()) { LOGGER.error("Could not cleanup temporary storage after conversion."); } } }
DefaultOfficeConverter implements OfficeConverter { @Override public Map<String, byte[]> convert(Map<String, InputStream> inputStreams, String inputFileName, String outputFileName) throws OfficeConverterException { if (null == inputStreams.get(inputFileName)) { String message = "No input stream specified for main input file [%s]."; throw new OfficeConverterException(String.format(message, inputFileName)); } OfficeConverterFileStorage storage = null; try { storage = new OfficeConverterFileStorage(this.workDir, inputFileName, outputFileName); FileOutputStream fos = null; for (Map.Entry<String, InputStream> entry : inputStreams.entrySet()) { try { File temp = new File(storage.getInputDir(), entry.getKey()); fos = new FileOutputStream(temp); IOUtils.copy(entry.getValue(), fos); } finally { IOUtils.closeQuietly(fos); } } this.converter.convert(storage.getInputFile()) .to(storage.getOutputFile()) .execute(); Map<String, byte[]> result = new HashMap<String, byte[]>(); FileInputStream fis = null; for (File file : storage.getOutputDir().listFiles()) { try { fis = new FileInputStream(file); result.put(file.getName(), IOUtils.toByteArray(fis)); } finally { IOUtils.closeQuietly(fis); } } return result; } catch (Exception ex) { throw new OfficeConverterException("Error while performing conversion.", ex); } finally { if (!storage.cleanUp()) { LOGGER.error("Could not cleanup temporary storage after conversion."); } } } }
DefaultOfficeConverter implements OfficeConverter { @Override public Map<String, byte[]> convert(Map<String, InputStream> inputStreams, String inputFileName, String outputFileName) throws OfficeConverterException { if (null == inputStreams.get(inputFileName)) { String message = "No input stream specified for main input file [%s]."; throw new OfficeConverterException(String.format(message, inputFileName)); } OfficeConverterFileStorage storage = null; try { storage = new OfficeConverterFileStorage(this.workDir, inputFileName, outputFileName); FileOutputStream fos = null; for (Map.Entry<String, InputStream> entry : inputStreams.entrySet()) { try { File temp = new File(storage.getInputDir(), entry.getKey()); fos = new FileOutputStream(temp); IOUtils.copy(entry.getValue(), fos); } finally { IOUtils.closeQuietly(fos); } } this.converter.convert(storage.getInputFile()) .to(storage.getOutputFile()) .execute(); Map<String, byte[]> result = new HashMap<String, byte[]>(); FileInputStream fis = null; for (File file : storage.getOutputDir().listFiles()) { try { fis = new FileInputStream(file); result.put(file.getName(), IOUtils.toByteArray(fis)); } finally { IOUtils.closeQuietly(fis); } } return result; } catch (Exception ex) { throw new OfficeConverterException("Error while performing conversion.", ex); } finally { if (!storage.cleanUp()) { LOGGER.error("Could not cleanup temporary storage after conversion."); } } } DefaultOfficeConverter(LocalConverter converter, File workDir); }
DefaultOfficeConverter implements OfficeConverter { @Override public Map<String, byte[]> convert(Map<String, InputStream> inputStreams, String inputFileName, String outputFileName) throws OfficeConverterException { if (null == inputStreams.get(inputFileName)) { String message = "No input stream specified for main input file [%s]."; throw new OfficeConverterException(String.format(message, inputFileName)); } OfficeConverterFileStorage storage = null; try { storage = new OfficeConverterFileStorage(this.workDir, inputFileName, outputFileName); FileOutputStream fos = null; for (Map.Entry<String, InputStream> entry : inputStreams.entrySet()) { try { File temp = new File(storage.getInputDir(), entry.getKey()); fos = new FileOutputStream(temp); IOUtils.copy(entry.getValue(), fos); } finally { IOUtils.closeQuietly(fos); } } this.converter.convert(storage.getInputFile()) .to(storage.getOutputFile()) .execute(); Map<String, byte[]> result = new HashMap<String, byte[]>(); FileInputStream fis = null; for (File file : storage.getOutputDir().listFiles()) { try { fis = new FileInputStream(file); result.put(file.getName(), IOUtils.toByteArray(fis)); } finally { IOUtils.closeQuietly(fis); } } return result; } catch (Exception ex) { throw new OfficeConverterException("Error while performing conversion.", ex); } finally { if (!storage.cleanUp()) { LOGGER.error("Could not cleanup temporary storage after conversion."); } } } DefaultOfficeConverter(LocalConverter converter, File workDir); @Override Map<String, byte[]> convert(Map<String, InputStream> inputStreams, String inputFileName,
        String outputFileName); @Override DocumentFormatRegistry getFormatRegistry(); }
DefaultOfficeConverter implements OfficeConverter { @Override public Map<String, byte[]> convert(Map<String, InputStream> inputStreams, String inputFileName, String outputFileName) throws OfficeConverterException { if (null == inputStreams.get(inputFileName)) { String message = "No input stream specified for main input file [%s]."; throw new OfficeConverterException(String.format(message, inputFileName)); } OfficeConverterFileStorage storage = null; try { storage = new OfficeConverterFileStorage(this.workDir, inputFileName, outputFileName); FileOutputStream fos = null; for (Map.Entry<String, InputStream> entry : inputStreams.entrySet()) { try { File temp = new File(storage.getInputDir(), entry.getKey()); fos = new FileOutputStream(temp); IOUtils.copy(entry.getValue(), fos); } finally { IOUtils.closeQuietly(fos); } } this.converter.convert(storage.getInputFile()) .to(storage.getOutputFile()) .execute(); Map<String, byte[]> result = new HashMap<String, byte[]>(); FileInputStream fis = null; for (File file : storage.getOutputDir().listFiles()) { try { fis = new FileInputStream(file); result.put(file.getName(), IOUtils.toByteArray(fis)); } finally { IOUtils.closeQuietly(fis); } } return result; } catch (Exception ex) { throw new OfficeConverterException("Error while performing conversion.", ex); } finally { if (!storage.cleanUp()) { LOGGER.error("Could not cleanup temporary storage after conversion."); } } } DefaultOfficeConverter(LocalConverter converter, File workDir); @Override Map<String, byte[]> convert(Map<String, InputStream> inputStreams, String inputFileName,
        String outputFileName); @Override DocumentFormatRegistry getFormatRegistry();  }