ProductLoader { public Product[] loadProducts() { final ArrayList<Product> products = new ArrayList<Product>(); for (String sourceProductPath : sourceProductPaths) { final File[] files; try { files = WildcardMatcher.glob(sourceProductPath); } catch (IOException e) { logger.severe("'" + sourceProductPath + "' is not a valid products wildcard path."); logger.log(Level.SEVERE, e.getMessage(), e); continue; } for (File productFile : files) { final Product product; try { logger.info("Trying to open product file '" + productFile.getAbsolutePath() + "'."); product = loadProduct(productFile); } catch (IOException e) { logger.severe("Unable to read product '" + productFile.getAbsolutePath() + "'."); logger.log(Level.SEVERE, e.getMessage(), e); continue; } if (productValidator.isValid(product)) { products.add(product); } else { if (product != null) { product.dispose(); } } } } return products.toArray(new Product[products.size()]); } ProductLoader(String[] sourceProductPaths, ProductValidator productValidator, Logger logger); Product[] loadProducts();  }
@Test public void testAllWorksFine() { Product[] products = productLoader.loadProducts(); assertNotNull(products); assertEquals(2, products.length); assertSame(product1, products[0]); assertSame(product2, products[1]); verifyNoMoreInteractions(product1); verifyNoMoreInteractions(product2); verify(M_logger).info("Trying to open product file '" + FILE_1.getAbsolutePath() + "'."); verify(M_logger).info("Trying to open product file '" + FILE_2.getAbsolutePath() + "'."); verifyNoMoreInteractions(M_logger); }
@Test public void testAllProductsAreInvalid() { when(M_productValidator.isValid(product1)).thenReturn(false); when(M_productValidator.isValid(product2)).thenReturn(false); Product[] products = productLoader.loadProducts(); assertNotNull(products); assertEquals(0, products.length); verify(product1, times(1)).dispose(); verify(product2, times(1)).dispose(); verifyNoMoreInteractions(product1); verifyNoMoreInteractions(product2); verify(M_logger).info("Trying to open product file '" + FILE_1.getAbsolutePath() + "'."); verify(M_logger).info("Trying to open product file '" + FILE_2.getAbsolutePath() + "'."); verifyNoMoreInteractions(M_logger); }
@Test public void testProduct1isInvalid() { when(M_productValidator.isValid(product1)).thenReturn(false); Product[] products = productLoader.loadProducts(); assertNotNull(products); assertEquals(1, products.length); assertSame(product2, products[0]); verify(product1, times(1)).dispose(); verifyNoMoreInteractions(product1); verifyNoMoreInteractions(product2); verify(M_logger).info("Trying to open product file '" + FILE_1.getAbsolutePath() + "'."); verify(M_logger).info("Trying to open product file '" + FILE_2.getAbsolutePath() + "'."); verifyNoMoreInteractions(M_logger); }
@Test public void testInvalidProductWildcard() throws IOException { IOException exception = new IOException("message"); PowerMockito.when(WildcardMatcher.glob(PATH_1)).thenThrow(exception); Product[] products = productLoader.loadProducts(); assertNotNull(products); assertEquals(1, products.length); assertSame(product2, products[0]); verifyNoMoreInteractions(product1); verifyNoMoreInteractions(product2); verify(M_logger).severe("'" + PATH_1 + "' is not a valid products wildcard path."); verify(M_logger).info("Trying to open product file '" + FILE_2.getAbsolutePath() + "'."); verify(M_logger, times(1)).log(Level.SEVERE, "message", exception); verifyNoMoreInteractions(M_logger); }
@Test public void testProductIOException() throws IOException { IOException exception = new IOException("message"); PowerMockito.when(ProductIO.readProduct(FILE_1)).thenThrow(exception); Product[] products = productLoader.loadProducts(); assertNotNull(products); assertEquals(1, products.length); assertSame(product2, products[0]); verifyNoMoreInteractions(product1); verifyNoMoreInteractions(product2); verify(M_logger).info("Trying to open product file '" + FILE_1.getAbsolutePath() + "'."); verify(M_logger).severe("Unable to read product '" + FILE_1.getAbsolutePath() + "'."); verify(M_logger, times(1)).log(Level.SEVERE, "message", exception); verify(M_logger).info("Trying to open product file '" + FILE_2.getAbsolutePath() + "'."); verifyNoMoreInteractions(M_logger); }