FileDocCategorySizeDatePackage
UploadDownloadMgrTest.javaAPI DocGlassfish v2 API10209Fri May 04 22:23:54 BST 2007com.sun.enterprise.management.base

UploadDownloadMgrTest

public final class UploadDownloadMgrTest extends com.sun.enterprise.management.AMXTestBase
Tests {@link UploadDownloadMgr}.

NOTE: multiple uploads and downloads are designed to test the thread-safeness of the MBean.

Fields Summary
private final int
K
private final int
MEGABYTE
Constructors Summary
public UploadDownloadMgrTest()

	
Methods Summary
private java.io.FilecreateTempFile(long totalSize)

		final long	start	= now();
		
		final File	temp	= File.createTempFile( "UploadDownloadMgrTest", "junk" );
		
		temp.deleteOnExit();
		
		final FileOutputStream	os	= new FileOutputStream( temp );
		
		try
		{
			long	remaining	= totalSize;
			
			final byte[]	junk	= new byte[ 1024 * 1024 ];
			
			while ( remaining != 0 )
			{
				final long	actual	= remaining < junk.length ? remaining : junk.length;
				
				os.write( junk, 0, (int)actual );
				remaining	-= actual;
			}
			os.close();
		}
		catch( IOException e )
		{
			os.close();
			temp.delete();
			throw e;
		}
		
		assert( temp.length() == totalSize );
		
		printElapsed( "UploadDownloadMgr.createTempFile: " +
		    totalSize + " bytes", start );
		return( temp );
	
public static com.sun.enterprise.management.CapabilitiesgetCapabilities()

	    return getOfflineCapableCapabilities( true );
	
public com.sun.appserv.management.base.UploadDownloadMgrgetUploadDownloadMgr()

		return( getDomainRoot().getUploadDownloadMgr() );
	
public voidtestDownloadBigFile()

		final long	start	= now();
		Integer def = new Integer(PropertyKeys.DEFAULT_UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB);
		final int	kb	=
			getEnvInteger( PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB, def).intValue();
		assert( kb >= 1 ) :
			"Test size must be positive, value for " +
			PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB +
			": " + kb;
			
		testDownloadFile( kb * K, MEGABYTE );
		
		printElapsed( "UploadDownloadMgrTest.testDownloadBigFile: " + kb + "kb", start);
	
public java.io.FiletestDownloadFile(int testSize, int chunkSize)

		final UploadDownloadMgr	mgr	=
				getDomainRoot().getUploadDownloadMgr();
		
		final File	testFile	= createTempFile( testSize );
		
		final long	start	= now();
		final Object	id	= mgr.initiateDownload( testFile, true );
		
		//trace( "initated download for: " + id + " file = " + testFile.toString() );
		final int	maxChunkSize	= mgr.getMaxDownloadChunkSize();
		final int	actualChunkSize	= chunkSize < maxChunkSize ?
										chunkSize : maxChunkSize;
			
		final long	length	= mgr.getDownloadLength( id );
		long	doneSoFar	= 0;
		while ( doneSoFar < length  )
		{
			final byte[]	bytes	= mgr.downloadBytes( id, actualChunkSize );
			doneSoFar	+= bytes.length;
		}
		
		printElapsed( "UploadDownloadMgr.testDownloadFile: " +
		    testSize + " bytes" + " chunksize = " + actualChunkSize, start );
		return( testFile );
	
public voidtestDownloadFileBufferLargerThanDownload()

	    final int   size    = 256 * K;
		testDownloadFile( size, size + 1 );
	
public voidtestDownloadFileBufferSameSizeAsDownload()

	    final int   size    = 256 * K;
		testDownloadFile( size, size );
	
public voidtestDownloadFileBufferSameSizeSmallerThanDownload()

	
		 
	
		 
	
	    final int   size    = 256 * K;
	    
		testDownloadFile( size, size -1 );
	
public voidtestDownloadSmallFile()

	    final int   size    = 50 * K;
		testDownloadFile( size, size + 1 );
		testDownloadFile( size, size );
	
public voidtestDownloadTinyFile()

	    final int   size    = 1;
		testDownloadFile( size, size + 1 );
		testDownloadFile( size, size );
	
public voidtestHeavilyThreaded()
This test is an attempt to find any synchronization bugs.

		Integer def = new Integer(PropertyKeys.DEFAULT_UPLOAD_DOWNLOAD_MGR_TEST_THREADS);
		
		int	numThreads	= getEnvInteger( PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_THREADS, def).intValue();
		if ( numThreads <= 0 )
		{
			numThreads	= 1;
		}
		
		printVerbose( "UploadDownloadMgrTest.testHeavilyThreaded: using " + numThreads + " threads." );
			
		final UploadDownloadTestThread[]	threads	=
			new UploadDownloadTestThread[ numThreads ];
		
		// create and start all the threads
		for( int i = 0; i < numThreads; ++i )
		{
			threads[ i ]	= new UploadDownloadTestThread( i * K + 1 );
			threads[ i ].start();
		}
		
		// wait till done
		boolean	done	= false;
		while ( true )
		{
			int numDone	= 0;
			for( int i = 0; i < numThreads; ++i )
			{
				if ( threads[ i ].done() )
				{
					++numDone;
				}
			}
			
			if ( numDone == numThreads )
				break;
				
			printVerbose( "UploadDownloadMgrTest.testHeavilyThreaded: waiting for " +
				(numThreads - numDone) + " of " + numThreads + " threads ");
			mySleep( 1000 );
		}
		
		// verify success
		for( int i = 0; i < numThreads; ++i )
		{
			assert( threads[ i ].done() );
			assert( threads[ i ].getThrowable() == null ) :
				ExceptionUtil.getStackTrace( threads[ i ].getThrowable() );
		}

	
public voidtestUploadFile1()

		final Object	id	= upload( "./deploy.temp1." + now(), 1024 * K );
	
public voidtestUploadFile2()

		final Object	id	= upload( "./deploy.temp2." + now(), 1 + 100 * K );
	
public voidtestUploadFile3()

		final Object	id	= upload( "./deploy.temp3." + now(), 1 );
	
public voidtestUploadFile4()

		final Object	id	= upload( "./deploy.temp4." + now(), K + 1 );
	
public voidtestUploadFile5()

		final Object	id	= upload( null, 1 + 2048 * K );
	
public java.lang.Objectupload(java.lang.String name, int totalSize)

		final UploadDownloadMgr	mgr	= getUploadDownloadMgr();
		//mgr.setTrace( true );
		
		final int	chunkSize	= 32 * 1024;
		
		final long	start	= now();
		
		final Object	uploadID	= mgr.initiateUpload( name, totalSize );
		int remaining	= totalSize;
		boolean	done	= false;
		while ( remaining != 0 )
		{
			final int	actual	= remaining < chunkSize ? remaining : chunkSize;
			
			final byte[]	bytes	= new byte[ actual ];
			done	= mgr.uploadBytes( uploadID, bytes );
			remaining	-= actual;
			//trace( "uploaded: " + (totalSize - remaining) );
		}
		assert( done );
		
		printElapsed( "UploadDownloadMgr.upload: " + totalSize + " bytes", start );
		return( uploadID );