RepositoryCleaner { public synchronized void gc() { Repository jcrRepository = PentahoSystem.get( Repository.class, "jcrRepository", null ); if ( jcrRepository == null ) { logger.error( "Cannot obtain JCR repository. Exiting" ); return; } if ( !( jcrRepository instanceof RepositoryImpl ) ) { logger.error( String.format( "Expected RepositoryImpl, but got: [%s]. Exiting", jcrRepository.getClass().getName() ) ); return; } final RepositoryImpl repository = (RepositoryImpl) jcrRepository; try { logger.debug( "Starting Orphaned Version Purge" ); Session systemSession = systemSessionFactory.create( repository ); Node node = systemSession.getNode( "/jcr:system/jcr:versionStorage" ); findVersionNodesAndPurge( node, systemSession ); systemSession.save(); logger.debug( "Finished Orphaned Version Purge" ); } catch ( RepositoryException e ) { logger.error( "Error running Orphaned Version purge", e ); } try { logger.info( "Creating garbage collector" ); DataStoreGarbageCollector gc = repository.createDataStoreGarbageCollector(); try { logger.debug( "Starting marking stage" ); gc.setPersistenceManagerScan( false ); gc.mark(); logger.debug( "Starting sweeping stage" ); int deleted = gc.sweep(); logger.info( String.format( "Garbage collecting completed. %d items were deleted", deleted ) ); } finally { gc.close(); } } catch ( RepositoryException e ) { logger.error( "Error during garbage collecting", e ); } }  void setSystemSessionFactory( IPentahoSystemSessionFactory systemSessionFactory ); synchronized void gc();  }
@Test public void gc() throws Exception { GarbageCollector collector = mock( GarbageCollector.class ); RepositoryImpl repository = mock( RepositoryImpl.class ); when( repository.createDataStoreGarbageCollector() ).thenReturn( collector ); MicroPlatform mp = new MicroPlatform( getSolutionPath() ); mp.defineInstance( Repository.class, repository ); mp.defineInstance( "jcrRepository", repository ); mp.start(); RepositoryCleaner cleaner = new RepositoryCleaner(); Session systemSession = mock( Session.class ); IPentahoSystemSessionFactory sessionFactory = mock( IPentahoSystemSessionFactory.class ); when( sessionFactory.create( repository ) ).thenReturn( systemSession ); cleaner.setSystemSessionFactory( sessionFactory ); try { cleaner.gc(); } finally { mp.stop(); } verify( collector, times( 1 ) ).mark(); verify( collector, times( 1 ) ).sweep(); verify( collector, times( 1 ) ).close(); }