ProductLoop { public void loop(Product[] alreadyLoadedProducts, File[] productFilesToLoad) { if (alreadyLoadedProducts != null) { for (Product product : alreadyLoadedProducts) { compute(product); } } for (File productFile : productFilesToLoad) { if (productFile == null) { continue; } if (isProductAlreadyOpened(alreadyLoadedProducts, productFile)) { continue; } loadProductAndCompute(productFile); } if (productNames.size() == 0) { throw new OperatorException("No input products found."); } } ProductLoop(ProductLoader loader, ProductValidator productValidator, StatisticComputer statisticComputer, Logger logger); void loop(Product[] alreadyLoadedProducts, File[] productFilesToLoad); String[] getProductNames(); ProductData.UTC getNewestDate(); ProductData.UTC getOldestDate();  }
@Test public void testThatAlreadyLoadedProductsShouldNotBeDisposedAfterStatisticComputation() { final Product[] loadedProducts = {_validProductMock1, _validProductMock2}; _productLoop.loop(loadedProducts, new File[0]); final InOrder inOrder = inOrder(_statisticComputerMock); inOrder.verify(_statisticComputerMock).computeStatistic(same(_validProductMock1)); inOrder.verify(_statisticComputerMock).computeStatistic(same(_validProductMock2)); verify(_validProductMock1, never()).dispose(); verify(_validProductMock2, never()).dispose(); }
@Test public void testThatLoadedProductsShouldBeDisposedAfterStatisticComputation() throws IOException { final File file1 = new File("1"); final File file2 = new File("2"); when(_productLoaderMock.loadProduct(file1)).thenReturn(_validProductMock1); when(_productLoaderMock.loadProduct(file2)).thenReturn(_validProductMock2); final File[] productFilesToLoad = {file1, file2}; _productLoop.loop(new Product[0], productFilesToLoad); final InOrder inOrder = inOrder(_statisticComputerMock); inOrder.verify(_statisticComputerMock).computeStatistic(same(_validProductMock1)); inOrder.verify(_statisticComputerMock).computeStatistic(same(_validProductMock2)); verify(_validProductMock1, times(1)).dispose(); verify(_validProductMock2, times(1)).dispose(); }
@Test public void testThatFilesWhichPointToAnAlreadyLoadedProductShouldNotBeOpenedTwice() throws IOException { final File file1 = _validProductMock1.getFileLocation(); final File file2 = _validProductMock2.getFileLocation(); when(_productLoaderMock.loadProduct(file1)).thenReturn(_validProductMock1); final Product[] alreadyLoadedProducts = {_validProductMock2}; final File[] productFilesToLoad = {file1, file2}; _productLoop.loop(alreadyLoadedProducts, productFilesToLoad); final InOrder inOrder = inOrder(_statisticComputerMock); inOrder.verify(_statisticComputerMock).computeStatistic(same(_validProductMock2)); inOrder.verify(_statisticComputerMock).computeStatistic(same(_validProductMock1)); verify(_productLoaderMock).loadProduct(eq(file1)); verify(_validProductMock1, times(1)).dispose(); verify(_validProductMock2, never()).dispose(); verify(_validProductMock2, atLeastOnce()).getFileLocation(); }
@Test public void testThatOperatorExceptionOccursIfNoProductsAreComputed() { try { _productLoop.loop(new Product[0], new File[0]); fail("OperatorExceptionExpected"); } catch (OperatorException expected) { assertEquals("No input products found.", expected.getMessage()); } }
@Test public void testThatFilesWhichCanNotBeOpenedAreLogged() throws IOException { final File file1 = _validProductMock1.getFileLocation(); final File file2 = new File("No reader available"); when(_productLoaderMock.loadProduct(file1)).thenReturn(_validProductMock1); when(_productLoaderMock.loadProduct(file2)).thenReturn(null); final Product[] alreadyLoadedProducts = {}; final File[] productFilesToLoad = {file1, file2}; _productLoop.loop(alreadyLoadedProducts, productFilesToLoad); verify(_loggerMock).severe("Failed to read from 'No reader available' (not a data product or reader missing)"); verify(_statisticComputerMock, times(1)).computeStatistic(_validProductMock1); verify(_productLoaderMock, times(2)).loadProduct(any(File.class)); verifyNoMoreInteractions(_statisticComputerMock, _productLoaderMock); }
@Test public void testThatIOExceptionsAreLogged() throws IOException { final File file1 = _validProductMock1.getFileLocation(); final File file2 = new File("Causes IO Exception"); when(_productLoaderMock.loadProduct(file1)).thenReturn(_validProductMock1); when(_productLoaderMock.loadProduct(file2)).thenThrow(new IOException()); final Product[] alreadyLoadedProducts = {}; final File[] productFilesToLoad = {file1, file2}; _productLoop.loop(alreadyLoadedProducts, productFilesToLoad); verify(_loggerMock).severe("Failed to read from 'Causes IO Exception' (not a data product or reader missing)"); verify(_statisticComputerMock, times(1)).computeStatistic(_validProductMock1); verify(_productLoaderMock, times(2)).loadProduct(any(File.class)); verifyNoMoreInteractions(_statisticComputerMock, _productLoaderMock); }
@Test public void testThatAlreadyLoadedProductsAreSkippedIfOutOfDateRange() { final Product productMockBefore = createProductMock(4, 2, true, false); final Product productMockAfter = createProductMock(4, 2, false, true); final Product[] alreadyLoadedProducts = {productMockBefore, productMockAfter, _validProductMock1}; when(_validatorMock.isValid(productMockBefore)).thenReturn(false); when(_validatorMock.isValid(productMockAfter)).thenReturn(false); _productLoop.loop(alreadyLoadedProducts, new File[0]); verify(_statisticComputerMock).computeStatistic(_validProductMock1); verifyNoMoreInteractions(_statisticComputerMock); verify(productMockBefore, never()).dispose(); verify(productMockAfter, never()).dispose(); verify(_validProductMock1, never()).dispose(); }
@Test public void testThatProductsToBeLoadedAreSkippedIfOutOfDateRange() throws IOException { final File file1 = new File("before"); final File file2 = new File("after"); final File file3 = _validProductMock1.getFileLocation(); final Product productMockBefore = createProductMock(4, 2, true, false); final Product productMockAfter = createProductMock(4, 2, false, true); when(_productLoaderMock.loadProduct(file1)).thenReturn(productMockBefore); when(_productLoaderMock.loadProduct(file2)).thenReturn(productMockAfter); when(_productLoaderMock.loadProduct(file3)).thenReturn(_validProductMock1); final Product[] alreadyLoadedProducts = {}; final File[] productFilesToLoad = new File[]{file1, file2, file3}; when(_validatorMock.isValid(productMockBefore)).thenReturn(false); when(_validatorMock.isValid(productMockAfter)).thenReturn(false); _productLoop.loop(alreadyLoadedProducts, productFilesToLoad); verify(_statisticComputerMock).computeStatistic(_validProductMock1); verifyNoMoreInteractions(_statisticComputerMock); verify(productMockBefore, times(1)).dispose(); verify(productMockAfter, times(1)).dispose(); verify(_validProductMock1, times(1)).dispose(); }
@Test public void testThatLoopWorksIfAlreadyLoadedProductsIsNull() throws IOException { File file = _validProductMock1.getFileLocation(); when(_productLoaderMock.loadProduct(file)).thenReturn(_validProductMock1); _productLoop.loop(null, new File[]{file}); verify(_statisticComputerMock).computeStatistic(_validProductMock1); verify(_validProductMock1, times(1)).dispose(); }