PageableHTMLOutput extends AbstractHtmlOutput { public int generate( final MasterReport report, final int acceptedPage, final OutputStream outputStream, final int yieldRate ) throws ReportProcessingException, IOException, ContentIOException { if ( proc == null ) { proc = createReportProcessor( report, yieldRate ); } final boolean singlePageRequested = acceptedPage >= 0; final IAsyncReportListener listener = ReportListenerThreadHolder.getListener(); final boolean asyncMode = listener != null; if ( asyncMode ) { proc.addReportProgressListener( listener ); } final PageableHtmlOutputProcessor outputProcessor = (PageableHtmlOutputProcessor) proc.getOutputProcessor(); if ( singlePageRequested && !asyncMode ) { outputProcessor.setFlowSelector( new SinglePageFlowSelector( acceptedPage ) ); } else { outputProcessor.setFlowSelector( new DisplayAllFlowSelector() ); } try { if ( !asyncMode || !singlePageRequested ) { proxyOutputStream.setParent( outputStream ); reinitOutputTarget(); proc.processReport(); if ( listener != null ) { listener.setIsQueryLimitReached( proc.isQueryLimitReached() ); } return proc.getLogicalPageCount(); } else { final Repository repository = reinitOutputTargetRepo(); proc.processReport(); final IReportContent completeReport = produceReportContent( proc, repository ); if ( completeReport == null ) { throw new ReportProcessingException( "Can't generate report" ); } if ( listener != null ) { listener.setIsQueryLimitReached( proc.isQueryLimitReached() ); } final boolean forceAllPages = isForceAllPages( report ); if ( forceAllPages || listener.isScheduled() ) { PaginationControlWrapper.write( outputStream, completeReport ); } else { final byte[] pageData = completeReport.getPageData( acceptedPage ); if ( pageData != null ) { outputStream.write( pageData ); } } return completeReport.getPageCount(); } } finally { if ( asyncMode ) { proc.removeReportProgressListener( listener ); } outputStream.flush(); printer.setContentWriter( null, null ); printer.setDataWriter( null, null ); } } PageableHTMLOutput(); Object getReportLock(); ProxyOutputStream getProxyOutputStream(); void setProxyOutputStream( final ProxyOutputStream proxyOutputStream ); HtmlPrinter getPrinter(); void setPrinter( final AllItemsHtmlPrinter printer ); PageableReportProcessor getReportProcessor(); void setReportProcessor( final PageableReportProcessor proc ); int paginate( final MasterReport report, final int yieldRate ); int generate( final MasterReport report, final int acceptedPage, final OutputStream outputStream,
                       final int yieldRate ); boolean supportsPagination(); void close();  }
@Test public void testGenerateListener() throws Exception { ClassicEngineBoot.getInstance().start(); pageableHTMLOutput.generate( new MasterReport(), 1, new ByteArrayOutputStream(), 1 ); verify( listener, times( 1 ) ).reportProcessingStarted( any( ReportProgressEvent.class ) ); verify( listener, times( 1 ) ).reportProcessingFinished( any( ReportProgressEvent.class ) ); verify( listener, atLeastOnce() ).reportProcessingUpdate( any( ReportProgressEvent.class ) ); }
@Test public void testGenerate() throws Exception { ClassicEngineBoot.getInstance().start(); ReportListenerThreadHolder.clear(); pageableHTMLOutput.generate( new MasterReport(), 1, new ByteArrayOutputStream(), 1 ); verify( listener, times( 0 ) ).reportProcessingStarted( any( ReportProgressEvent.class ) ); verify( listener, times( 0 ) ).reportProcessingFinished( any( ReportProgressEvent.class ) ); verify( listener, times( 0 ) ).reportProcessingUpdate( any( ReportProgressEvent.class ) ); }
@Test public void testGenerateListenerFlow() throws Exception { ClassicEngineBoot.getInstance().start(); pageableHTMLOutput.generate( new MasterReport(), -1, new ByteArrayOutputStream(), 1 ); verify( listener, times( 1 ) ).reportProcessingStarted( any( ReportProgressEvent.class ) ); verify( listener, times( 1 ) ).reportProcessingFinished( any( ReportProgressEvent.class ) ); verify( listener, atLeastOnce() ).reportProcessingUpdate( any( ReportProgressEvent.class ) ); }
@Test public void testGenerateFlow() throws Exception { ClassicEngineBoot.getInstance().start(); ReportListenerThreadHolder.clear(); pageableHTMLOutput.generate( new MasterReport(), -1, new ByteArrayOutputStream(), 1 ); verify( listener, times( 0 ) ).reportProcessingStarted( any( ReportProgressEvent.class ) ); verify( listener, times( 0 ) ).reportProcessingFinished( any( ReportProgressEvent.class ) ); verify( listener, times( 0 ) ).reportProcessingUpdate( any( ReportProgressEvent.class ) ); }
@Ignore @Test public void testPageNotInCacheSchedule() throws PlatformInitializationException, ContentIOException, ReportProcessingException, IOException, ResourceException { ClassicEngineBoot.getInstance().start(); MicroPlatform microPlatform = MicroPlatformFactory.create(); try { microPlatform.define( ReportOutputHandlerFactory.class, FastExportReportOutputHandlerFactory.class ); final IReportContentCache mockCache = mock( IReportContentCache.class ); final IReportContent iReportContent = mock( IReportContent.class ); when( iReportContent.getPageData( 3 ) ).thenReturn( null ); final String key = "test"; when( mockCache.get( key ) ).thenReturn( iReportContent ); final IPluginCacheManager iPluginCacheManager = new PluginCacheManagerImpl( mockCache ); microPlatform.define( "IPluginCacheManager", iPluginCacheManager ); microPlatform.start(); final IPentahoSession session = new StandaloneSession(); PentahoSessionHolder.setSession( session ); final CachingPageableHTMLOutput cachingPageableHTMLOutput = spy( new CachingPageableHTMLOutput() ); final ResourceManager mgr = new ResourceManager(); final File src = new File( "target/test/resource/solution/test/reporting/report1.prpt" ); final MasterReport r = (MasterReport) mgr.createDirectly( src, MasterReport.class ).getResource(); r.setContentCacheKey( key ); final TestListener listener = new TestListener( "1", UUID.randomUUID(), "" ); listener.setStatus( AsyncExecutionStatus.SCHEDULED ); ReportListenerThreadHolder.setListener( listener ); try ( final java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream() ) { cachingPageableHTMLOutput.generate( r, 3, outputStream, 1 ); final String content = new String( outputStream.toByteArray(), "UTF-8" ); assertTrue( content.contains( "Scheduled paginated HTML report" ) ); } verify( cachingPageableHTMLOutput, times( 1 ) ).regenerateCache( r, 1, key, 3 ); } finally { ReportListenerThreadHolder.clear(); microPlatform.stop(); microPlatform = null; } }
@Ignore @Test public void testPageNotInCache() throws PlatformInitializationException, ContentIOException, ReportProcessingException, IOException, ResourceException { ClassicEngineBoot.getInstance().start(); MicroPlatform microPlatform = MicroPlatformFactory.create(); try { microPlatform.define( ReportOutputHandlerFactory.class, FastExportReportOutputHandlerFactory.class ); final IReportContentCache mockCache = mock( IReportContentCache.class ); final IReportContent iReportContent = mock( IReportContent.class ); when( iReportContent.getPageData( 3 ) ).thenReturn( null ); final String key = "test"; when( mockCache.get( key ) ).thenReturn( iReportContent ); final IPluginCacheManager iPluginCacheManager = new PluginCacheManagerImpl( mockCache ); microPlatform.define( "IPluginCacheManager", iPluginCacheManager ); microPlatform.start(); final IPentahoSession session = new StandaloneSession(); PentahoSessionHolder.setSession( session ); final CachingPageableHTMLOutput cachingPageableHTMLOutput = spy( new CachingPageableHTMLOutput() ); final ResourceManager mgr = new ResourceManager(); final File src = new File( "target/test/resource/solution/test/reporting/report1.prpt" ); final MasterReport r = (MasterReport) mgr.createDirectly( src, MasterReport.class ).getResource(); r.setContentCacheKey( key ); cachingPageableHTMLOutput.generate( r, 3, mock( OutputStream.class ), 1 ); verify( cachingPageableHTMLOutput, times( 1 ) ).regenerateCache( r, 1, key, 3 ); } finally { microPlatform.stop(); microPlatform = null; } }
@Test public void testNotCaching() throws ContentIOException, ReportProcessingException, IOException { final CachingPageableHTMLOutput mock = mock( CachingPageableHTMLOutput.class ); when( mock.generate( null, -1, null, 1 ) ).thenCallRealMethod(); mock.generate( null, -1, null, 1 ); verify( mock, times( 1 ) ).generateNonCaching( null, -1, null, 1 ); }
@Ignore @Test public void testIsQueryLimitReached() throws Exception { ClassicEngineBoot.getInstance().start(); MicroPlatform microPlatform = MicroPlatformFactory.create(); try { microPlatform.define( ReportOutputHandlerFactory.class, FastExportReportOutputHandlerFactory.class ); final IReportContentCache mockCache = mock( IReportContentCache.class ); final IReportContent iReportContent = mock( IReportContent.class ); final int value = new Random().nextInt(); when( iReportContent.getPageCount() ).thenReturn( value ); final String key = "test"; when( mockCache.get( key ) ).thenReturn( iReportContent ); final IPluginCacheManager iPluginCacheManager = new PluginCacheManagerImpl( mockCache ); microPlatform.define( "IPluginCacheManager", iPluginCacheManager ); microPlatform.start(); final IPentahoSession session = new StandaloneSession(); PentahoSessionHolder.setSession( session ); final TestListener listener = new TestListener( "1", UUID.randomUUID(), "" ); ReportListenerThreadHolder.setListener( listener ); final ResourceManager mgr = new ResourceManager(); final File src = new File( "target/test/resource/solution/test/reporting/report.prpt" ); final MasterReport masterReport = (MasterReport) mgr.createDirectly( src, MasterReport.class ).getResource(); masterReport.setQueryLimit( 50 ); pageableHTMLOutput.generate( masterReport, 1, new ByteArrayOutputStream(), 1 ); assertEquals( listener.isQueryLimitReached(), true ); } finally { microPlatform.stop(); microPlatform = null; } }
@Test public void testGenerateWErrorCache() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); final String key = "test"; MasterReport mockMasterReport = mock( MasterReport.class ); CachingPageableHTMLOutput mockCachingPageableHTMLOutput = mock( CachingPageableHTMLOutput.class ); ReportListenerThreadHolder.clear(); doReturn( key ).when( mockMasterReport ).getContentCacheKey(); doReturn( null ).when( mockCachingPageableHTMLOutput ).getCachedContent( key ); doReturn( null ).when( mockCachingPageableHTMLOutput ).regenerateCache( mockMasterReport, 1, key, 1 ); when( mockCachingPageableHTMLOutput.generate( mockMasterReport, 1, baos, 1 ) ).thenCallRealMethod(); int returnedValue = mockCachingPageableHTMLOutput.generate( mockMasterReport, 1, baos, 1 ); verify( mockCachingPageableHTMLOutput ).regenerateCache( mockMasterReport, 1, key, 1 ); assertTrue( returnedValue == -1 ); }