PentahoJcrTemplate extends JcrTemplate { @Override public Object execute( JcrCallback action, boolean exposeNativeSession ) throws DataAccessException { Session session = null; try { session = getSession(); useSession( session ); Session sessionToExpose = ( exposeNativeSession ? session : createSessionProxy( session ) ); Object result = action.doInJcr( sessionToExpose ); return result; } catch ( RepositoryException ex ) { throw pentahoConvertJcrAccessException( ex ); } catch ( IOException ex ) { throw convertJcrAccessException( ex ); } catch ( RuntimeException ex ) { throw pentahoConvertJcrAccessException( ex ); } finally { releaseSession( session ); } } PentahoJcrTemplate(); @Override Object execute( JcrCallback action, boolean exposeNativeSession );  }
@Test public void executeUpdatesUsageCount() throws IOException, RepositoryException { jcrTemplate.setSessionFactory( sessionFactory ); jcrTemplate.execute( action, true ); verify( action ).doInJcr( session ); verify( session, times( 2 ) ).getAttribute( USAGE_COUNT ); assertThat( "first usage increments", usedUsageCount.get(), equalTo( 1 ) ); assertThat( "second usage decrements", releasedUsageCount.get(), equalTo( -1 ) ); }
@Test public void executeUpdatesUsageCountNestedExec() throws IOException, RepositoryException { jcrTemplate.setSessionFactory( sessionFactory ); when( session.getAttribute( USAGE_COUNT ) ) .thenReturn( usedUsageCount, usedUsageCount, releasedUsageCount, releasedUsageCount ); when( action.doInJcr( session ) ) .thenAnswer( s -> { jcrTemplate.execute( nestedAction, true ); return null; } ); jcrTemplate.execute( action, true ); verify( action ).doInJcr( session ); verify( nestedAction ).doInJcr( session ); verify( session, times( 4 ) ).getAttribute( USAGE_COUNT ); assertThat( "first usage increments", usedUsageCount.get(), equalTo( 2 ) ); assertThat( "second usage decrements", releasedUsageCount.get(), equalTo( -2 ) ); }