PageRenderTime 7199ms CodeModel.GetById 35ms RepoModel.GetById 6ms app.codeStats 0ms

/poi/src/test/java/com/qmetric/utilities/poi/xssf/XSSFWorkBookProcessorTest.java

https://github.com/qmetric/qmg-utilities
Java | 84 lines | 66 code | 17 blank | 1 comment | 0 complexity | f8038c52cbb1ace5de7b05a9aa05e7bd MD5 | raw file
  1. package com.qmetric.utilities.poi.xssf;
  2. import com.qmetric.utilities.file.FileUtils;
  3. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  4. import org.apache.poi.xssf.eventusermodel.XSSFReader;
  5. import org.apache.poi.xssf.model.SharedStringsTable;
  6. import org.junit.Test;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import static org.mockito.Matchers.any;
  10. import static org.mockito.Matchers.eq;
  11. import static org.mockito.Mockito.mock;
  12. import static org.mockito.Mockito.stub;
  13. import static org.mockito.Mockito.verify;
  14. import static org.mockito.Mockito.when;
  15. //TODO Processor?
  16. public class XSSFWorkBookProcessorTest
  17. {
  18. @Test
  19. public void shouldProcessWorkBookWithProvidedRowProcessor() throws IOException, InvalidFormatException
  20. {
  21. final String location = "location";
  22. final String sheetName = "sheetName";
  23. final InputStream inputStream = mock(InputStream.class);
  24. final RowProcessor rowProcessor = mock(RowProcessor.class);
  25. final XSSFReaderFactory xssfReaderFactory = readerFactory(inputStream, sheetName);
  26. final XSSFWorksheetParserFactory xssfWorksheetParserFactory = xssfWorksheetParserFactory(location, inputStream, rowProcessor);
  27. final SheetProcessor sheetProcessor = rowProcessorFactory(sheetName, rowProcessor);
  28. final XSSFWorkBookProcessor xssfWorkBookProcessor = new XSSFWorkBookProcessor(xssfWorksheetParserFactory, fileUtils(location, inputStream), xssfReaderFactory);
  29. xssfWorkBookProcessor.process(location, sheetProcessor);
  30. verify(sheetProcessor).getNewRowProcessor(sheetName);
  31. }
  32. private SheetProcessor rowProcessorFactory(final String sheetName, final RowProcessor rowProcessor)
  33. {
  34. final SheetProcessor sheetProcessor = mock(SheetProcessor.class);
  35. when(sheetProcessor.getNewRowProcessor(sheetName)).thenReturn(rowProcessor);
  36. when(sheetProcessor.getNewRowProcessor(sheetName)).thenReturn(rowProcessor);
  37. return sheetProcessor;
  38. }
  39. private XSSFReaderFactory readerFactory(final InputStream inputStream, final String sheetName) throws IOException, InvalidFormatException
  40. {
  41. final XSSFReader.SheetIterator sheetIterator = mock(XSSFReader.SheetIterator.class);
  42. stub(sheetIterator.hasNext()).toReturn(true).toReturn(false);
  43. when(sheetIterator.next()).thenReturn(inputStream);
  44. when(sheetIterator.getSheetName()).thenReturn(sheetName);
  45. final SharedStringsTable sharedStringsTable = mock(SharedStringsTable.class);
  46. final XSSFReader xssfReader = mock(XSSFReader.class);
  47. when(xssfReader.getSheetsData()).thenReturn(sheetIterator);
  48. when(xssfReader.getSharedStringsTable()).thenReturn(sharedStringsTable);
  49. final XSSFReaderFactory xssfReaderFactory = mock(XSSFReaderFactory.class);
  50. when(xssfReaderFactory.create(inputStream)).thenReturn(xssfReader);
  51. return xssfReaderFactory;
  52. }
  53. private XSSFWorksheetParserFactory xssfWorksheetParserFactory(final String location, final InputStream inputStream, final RowProcessor rowProcessor)
  54. {
  55. fileUtils(location, inputStream);
  56. final XSSFWorksheetParserFactory xssfWorksheetParserFactory = mock(XSSFWorksheetParserFactory.class);
  57. final WorksheetParser worksheetParser = mock(WorksheetParser.class);
  58. when(xssfWorksheetParserFactory.createParserFor(any(SharedStringsTableLookup.class), eq(rowProcessor))).thenReturn(worksheetParser);
  59. return xssfWorksheetParserFactory;
  60. }
  61. private FileUtils fileUtils(final String location, final InputStream inputStream)
  62. {
  63. final FileUtils fileUtils = mock(FileUtils.class);
  64. when(fileUtils.inputStreamFrom(location)).thenReturn(inputStream);
  65. return fileUtils;
  66. }
  67. }