/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

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

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.birt.report.engine.api.HTMLCompleteImageHandler;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.RenderOptionBase;
import org.eclipse.birt.report.engine.api.impl.Image;
import org.eclipse.birt.report.tests.engine.EngineCase;

/**
 * <b>HTMLCompleteImageHandler test</b>
 * <p>
 * This case tests methods in HTMLCompleteImageHandler API.
 * 
 */

public class HTMLCompleteImageHandlerTest extends EngineCase
{
	final static String INPUT = "EmbedImage.txt";

	public void setUp( ) throws Exception
	{
		super.setUp( );
		removeResource( );
		copyResource_INPUT( INPUT, INPUT );
	}

	public void tearDown( )
	{
		removeResource( );
	}
	
	/**
	 * @param name
	 */

	/**
	 * Test OnDesignImage() method
	 * 
	 */
	public void testOnDesignImage( )
	{
		try
		{
			// Get embedded image byte array
			
			String input = this.genInputFile( INPUT );

			File imageFile = new File( input );
			long size = imageFile.length( );
			InputStream is = new BufferedInputStream( new FileInputStream(
					imageFile ) );
			byte[] imageBytes = new byte[(int) size];
			is.read( imageBytes );
			assertNotNull( imageBytes );

			// Test onDesignImage()

			HTMLRenderContext context = new HTMLRenderContext( );
			context.setImageDirectory( this.genOutputFile( "image" ) );
			HTMLCompleteImageHandler imageHandler = new HTMLCompleteImageHandler( );
			Image image = new Image( imageBytes, "image1" );
			RenderOptionBase option = new RenderOptionBase( );

			image.setRenderOption( option );
			File f = null;
			int count = 0;
			do
			{
				count++;
				String fp = this.genOutputFolder( ) + "/"
						+ "design" + String.valueOf( count );
				f = new File( fp ); //$NON-NLS-1$
				if ( f.exists( ) )
				{
					f.delete( );
					continue;
				}
				else
				{
					break;
				}
			} while ( true );

			String str = imageHandler.onDesignImage( image, context );
			str = str.substring( 6 );
			assertTrue( "Failed to get design image in " + str , new File( str ).exists( ) );
			removeFile( str );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
		}
	}

	/**
	 * Test onURLImage() method
	 * 
	 */
	public void testOnURLImage( )
	{
		String url = "http://image";
		Image image = new Image( url );
		RenderOptionBase option = new RenderOptionBase( );
		image.setRenderOption( option );
		HTMLRenderContext context = new HTMLRenderContext( );
		HTMLCompleteImageHandler handler = new HTMLCompleteImageHandler( );
		String urlGet = handler.onURLImage( image, context );
		assertEquals( "OnURLImage() fail", url, urlGet );
	}

	/**
	 * Test onCustomeImage() method
	 * 
	 */
	public void testOnCustomImage( )
	{
		try
		{
			// Get embedded image byte array

			String input = this.genInputFile( INPUT );

			File imageFile = new File( input );
			long size = imageFile.length( );
			InputStream is = new BufferedInputStream( new FileInputStream(
					imageFile ) );
			byte[] imageBytes = new byte[(int) size];
			is.read( imageBytes );

			// Test onDesignImage()

			HTMLRenderContext context = new HTMLRenderContext( );
			context.setImageDirectory( this.genOutputFile( "image" ) );
			HTMLCompleteImageHandler imageHandler = new HTMLCompleteImageHandler( );
			Image image = new Image( imageBytes, "image1" );
			RenderOptionBase option = new RenderOptionBase( );
			image.setRenderOption( option );
			File f = null;
			int count = 1;
			do
			{
				count++;
				String fp = this.genOutputFolder( ) + "/"
						+ "custom" + String.valueOf( count );
				f = new File( fp ); //$NON-NLS-1$
				if ( f.exists( ) )
				{
					f.delete( );
					continue;
				}
				else
				{
					break;
				}
			} while ( true );

			String str = imageHandler.onCustomImage( image, context );
			str = str.substring( 6 );
			assertTrue( "Failed to get custom image in " + str , new File( str ).exists( ) );
			removeFile( str );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
		}
	}

	/**
	 * Test onDocImage() method Not implemented at 1.0.1,so return null.
	 */
	public void testOnDocImage( )
	{
		String url = "http://image";
		Image image = new Image( url );
		RenderOptionBase option = new RenderOptionBase( );
		image.setRenderOption( option );
		HTMLRenderContext context = new HTMLRenderContext( );
		HTMLCompleteImageHandler handler = new HTMLCompleteImageHandler( );
		String urlGet = handler.onDocImage( image, context );
		assertNull( "OnDocImage() fail", urlGet );
	}
}

@@ -692,25 +692,24 @@
 Stream;%0A
-%0A
 import j
 unit.fra
@@ -704,61 +704,19 @@
 rt j
-unit.framework.Test;%0Aimport junit.framework.TestSuite
+ava.net.URL
 ;%0A%0Ai
@@ -2358,32 +2358,38 @@
 utFolder( ) + %22/
+image/
 %22%0A%09%09%09%09%09%09+ %22desig
@@ -2644,38 +2644,38 @@
 t );%0A%09%09%09
-str = str.substring( 6
+URL url = new URL( str
  );%0A%09%09%09a
@@ -2728,35 +2728,46 @@
 str , new File( 
-str
+url.getFile( )
  ).exists( ) );%0A
@@ -4204,16 +4204,22 @@
 ( ) + %22/
+image/
 %22%0A%09%09%09%09%09%09
@@ -4486,30 +4486,30 @@
 %0A%09%09%09
-str = str.substring( 6
+URL url = new URL( str
  );%0A
@@ -4574,19 +4574,30 @@
 w File( 
-str
+url.getFile( )
  ).exist

Modify the url path