ArchiveLoader { public void loadAll( File directory, FilenameFilter filenameFilter ) { File[] files = directory.listFiles( filenameFilter ); if ( files != null ) { for ( File file : files ) { try { Logger.debug( this.getClass().getName(), this.getClass().getName() + ": importing " + file.getName() ); importer.importFile( createBundle( file ) ); } catch ( Exception e ) { Logger.error( this.getClass().getName(), e.getMessage(), e ); } finally { ImportSession.clearSession(); file.renameTo( new File( file.getPath() + DATE_FORMAT.format( loadStamp ) ) ); } } } } ArchiveLoader( IPlatformImporter importer );   ArchiveLoader( IPlatformImporter importer, Date loadStamp ); void loadAll( File directory, FilenameFilter filenameFilter ); static final FilenameFilter ZIPS_FILTER; }
@Test public void testWillImportAllZipsInADirectory() throws Exception { final IPlatformImporter importer = mock( IPlatformImporter.class ); final FileInputStream inputStream = mock( FileInputStream.class ); final ArchiveLoader loader = createArchiveLoader( importer, inputStream ); final File directory = mock( File.class ); final File jobs = mock( File.class ); String jobsName = "jobs.zip"; when( jobs.getName() ).thenReturn( jobsName ); when( jobs.getPath() ).thenReturn( "/root/path/" + jobsName ); final File reports = mock( File.class ); String reportsName = "reports.zip"; when( reports.getName() ).thenReturn( reportsName ); when( reports.getPath() ).thenReturn( "/root/path/" + reportsName ); when( directory.listFiles( ZIPS_FILTER ) ).thenReturn( new File[] { jobs, reports } ); IRepositoryImportLogger logger = mock( IRepositoryImportLogger.class ); when( importer.getRepositoryImportLogger() ).thenReturn( logger ); loader.loadAll( directory, ZIPS_FILTER ); verify( importer ).importFile( argThat( bundleMatcher( jobsName, inputStream ) ) ); verify( jobs ).renameTo( argThat( fileMatcher( jobs ) ) ); verify( importer ).importFile( argThat( bundleMatcher( reportsName, inputStream ) ) ); verify( reports ).renameTo( argThat( fileMatcher( reports ) ) ); }
@Test public void testWillContinueToLoadOnException() throws Exception { final IPlatformImporter importer = mock( IPlatformImporter.class ); final FileInputStream inputStream = mock( FileInputStream.class ); final ArchiveLoader loader = createArchiveLoader( importer, inputStream ); final File directory = mock( File.class ); final File jobs = mock( File.class ); String jobsName = "jobs.zip"; when( jobs.getName() ).thenReturn( jobsName ); final File reports = mock( File.class ); when( jobs.getPath() ).thenReturn( "/root/path/" + jobsName ); String reportsName = "reports.zip"; when( reports.getName() ).thenReturn( reportsName ); when( reports.getPath() ).thenReturn( "/root/path/" + reportsName ); when( directory.listFiles( ZIPS_FILTER ) ).thenReturn( new File[] { jobs, reports } ); Exception exception = new RuntimeException( "exception thrown on purpose from testWillContinueToLoadOnException" ); doThrow( exception ).when( importer ).importFile( argThat( bundleMatcher( jobsName, inputStream ) ) ); IRepositoryImportLogger logger = mock( IRepositoryImportLogger.class ); when( importer.getRepositoryImportLogger() ).thenReturn( logger ); loader.loadAll( directory, ZIPS_FILTER ); verify( importer ).importFile( argThat( bundleMatcher( jobsName, inputStream ) ) ); verify( importer ).importFile( argThat( bundleMatcher( reportsName, inputStream ) ) ); verify( jobs ).renameTo( argThat( fileMatcher( jobs ) ) ); verify( reports ).renameTo( argThat( fileMatcher( reports ) ) ); }
@Test public void testDoesNotBombWhenDirectoryDoesNotExist() throws Exception { IPlatformImporter importer = mock( IPlatformImporter.class ); ArchiveLoader loader = new ArchiveLoader( importer ); try { loader.loadAll( new File( "/fake/path/that/does/not/exist" ), ArchiveLoader.ZIPS_FILTER ); } catch ( Exception e ) { Assert.fail( "Expected no exception but got " + e.getMessage() ); } }
@Test public void testDoesNotBombWhenZeroFilesFound() throws Exception { final IPlatformImporter importer = mock( IPlatformImporter.class ); final ArchiveLoader loader = new ArchiveLoader( importer ); final File directory = mock( File.class ); when( directory.listFiles( ZIPS_FILTER ) ).thenReturn( new File[] {} ); loader.loadAll( directory, ZIPS_FILTER ); }
@Test public void testLoadAllClearSession() throws Exception { PowerMockito.mockStatic( ImportSession.class ); ImportSession importSession = mock( ImportSession.class ); when( ImportSession.getSession() ).thenReturn( importSession ); IPlatformImporter importer = mock( IPlatformImporter.class ); ArchiveLoader loader = new ArchiveLoader( importer ); File directoryMock = mock( File.class ); File file1Mock = mock( File.class ); File file2Mock = mock( File.class ); when( directoryMock.listFiles( ZIPS_FILTER ) ).thenReturn( new File[] { file1Mock, file2Mock } ); loader.loadAll( directoryMock, ArchiveLoader.ZIPS_FILTER ); PowerMockito.verifyStatic( VerificationModeFactory.times( 2 ) ); ImportSession.clearSession(); }