
package org.eclipse.birt.report.engine.api;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.HashMap;

import org.eclipse.birt.report.engine.EngineCase;
import org.eclipse.birt.report.engine.api.script.IReportContext;

/**
 * unit test used to test if the cached image handle is called.
 */
public class CachedImageHandlerTest extends EngineCase
{

	static final String DESIGN_RESOURCE = "org/eclipse/birt/report/engine/api/cached-image-handler.rptdesign";
	static final String REPORT_DESIGN = "./utest/test.rptdesign";
	static final String REPORT_DOCUMENT = "./utest/test.rptdocument";
	static final String TEST_FOLDER = "./utest/";

	public void setUp( )
	{
		removeFile( TEST_FOLDER );
		copyResource( DESIGN_RESOURCE, REPORT_DESIGN );
	}

	public void tearDown( )
	{
		removeFile( TEST_FOLDER );
	}

	public void testRender( ) throws Exception
	{
		CachedImageHandler imageHandler = new CachedImageHandler( );

		EngineConfig config = new EngineConfig( );
		HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
		emitterConfig.setImageHandler( imageHandler );
		config.setEmitterConfiguration( "html", emitterConfig );
		IReportEngine engine = new ReportEngine( config );

		// first we need create the report document
		// open the report runnable to execute.
		IReportRunnable report = engine.openReportDesign( REPORT_DESIGN );
		IRunTask task = engine.createRunTask( report );
		task.run( REPORT_DOCUMENT );
		task.close( );

		IReportDocument document = engine.openReportDocument( REPORT_DOCUMENT );

		// then we need render the report, this time the image is cached.
		IRenderTask render = engine.createRenderTask( document );
		HTMLRenderOption options = new HTMLRenderOption( );
		options.setOutputFormat( "html" );
		options.setOutputStream( new ByteArrayOutputStream( ) );
		render.setRenderOption( options );
		render.render( );
		render.close( );

		assertEquals( 1, imageHandler.customImageCount );
		// render the report again, the cached image should be return.
		render = engine.createRenderTask( document );
		render.setRenderOption( options );
		render.render( );
		render.close( );

		assertEquals( 1, imageHandler.cachedImageCount );
		assertEquals( 1, imageHandler.customImageCount );
		assertEquals( 1, imageHandler.fileImageCount );
		document.close( );

		engine.shutdown( );
	}

	class CachedImageHandler extends HTMLImageHandler
	{

		int cachedImageCount = 0;
		int customImageCount = 0;
		int fileImageCount = 0;

		HashMap map = new HashMap( );

		public CachedImage getCachedImage( String id, int sourceType,
				IReportContext context )
		{
			String url = (String) map.get( id );
			if ( url != null )
			{
				cachedImageCount++;
				return new CachedImage( id, "CACHED_IMAGE:" + url );
			}
			return null;

		}

		public String onCustomImage( IImage image, IReportContext context )
		{
			customImageCount++;
			String date = new Date( ).toString( );
			map.put( image.getID( ), date );
			return "CACHED_IMAGE:" + date;
		}

		public String onFileImage( IImage image, IReportContext context )
		{
			fileImageCount++;
			return image.getID( );
		}
	}

}

@@ -81,31 +81,8 @@
 am;%0A
-import java.util.Date;%0A
 impo
@@ -967,83 +967,59 @@
 ;%0A%09%09
-HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );%0A%09%09emitterConfig
+RenderOption option = new RenderOption( );%0A%09%09option
 .set
@@ -1056,16 +1056,21 @@
 %09%09config
+%0A%09%09%09%09
 .setEmit
@@ -1091,29 +1091,53 @@
 on( 
-%22html%22, emitterConfig
+RenderOption.OUTPUT_FORMAT_HTML,%0A%09%09%09%09%09%09option
  );%0A
@@ -1902,37 +1902,139 @@
 , imageHandler.c
-ustom
+achedImageCount );%0A%09%09assertEquals( 0, imageHandler.customImageCount );%0A%09%09assertEquals( 1, imageHandler.file
 ImageCount );%0A%09%09
@@ -2281,33 +2281,33 @@
 %09%09assertEquals( 
-1
+0
 , imageHandler.c
@@ -2333,33 +2333,33 @@
 %09%09assertEquals( 
-1
+2
 , imageHandler.f
@@ -2410,16 +2410,15 @@
 ine.
-shutdown
+destroy
 ( );
@@ -2761,32 +2761,8 @@
 %09%09%7B%0A
-%09%09%09%09cachedImageCount++;%0A
 %09%09%09%09
@@ -2788,34 +2788,16 @@
 age( id,
- %22CACHED_IMAGE:%22 +
  url );%0A
@@ -2817,21 +2817,22 @@
 n null;%0A
-%0A
 %09%09%7D%0A
+%09%09
 %0A%09%09publi
@@ -2837,29 +2837,61 @@
 lic 
-String onCustomImage(
+CachedImage addCachedImage(String id, int sourceType,
  IIm
@@ -2915,33 +2915,32 @@
 tContext context
- 
 )%0A%09%09%7B%0A%09%09%09customI
@@ -2933,21 +2933,21 @@
 %09%09%7B%0A%09%09%09c
-ustom
+ached
 ImageCou
@@ -2966,74 +2966,241 @@
 ing 
-date = new Date( ).toString( );%0A%09%09%09map.put( image.getID( ), date )
+url = %22CACHED_IMAGE:%22 + cachedImageCount;%0A%09%09%09map.put( id, url );%0A%09%09%09CachedImage cache = new CachedImage( id, url );%0A%09%09%09return cache;%0A%09%09%7D%0A%0A%09%09public String onCustomImage( IImage image, IReportContext context )%0A%09%09%7B%0A%09%09%09customImageCount++
 ;%0A%09%09
@@ -3209,21 +3209,21 @@
 eturn %22C
-ACHED
+USTOM
 _IMAGE:%22
@@ -3229,12 +3229,24 @@
 %22 + 
-date
+customImageCount
 ;%0A%09%09

fix unit test