KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > base > UploadDownloadMgrTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeanapi-impl/tests/com/sun/enterprise/management/base/UploadDownloadMgrTest.java,v 1.4 2006/03/09 20:30:52 llc Exp $
26  * $Revision: 1.4 $
27  * $Date: 2006/03/09 20:30:52 $
28  */

29 package com.sun.enterprise.management.base;
30
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Collections JavaDoc;
34
35 import java.io.IOException JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.AttributeList JavaDoc;
41 import javax.management.Notification JavaDoc;
42 import javax.management.MBeanServerConnection JavaDoc;
43 import javax.management.NotCompliantMBeanException JavaDoc;
44
45 import com.sun.appserv.management.base.UploadDownloadMgr;
46
47 import com.sun.enterprise.management.AMXTestBase;
48 import com.sun.enterprise.management.Capabilities;
49
50 import com.sun.appserv.management.util.misc.ExceptionUtil;
51
52 import com.sun.enterprise.management.PropertyKeys;
53
54
55 /**
56     Tests {@link UploadDownloadMgr}.
57     <p>
58     NOTE: multiple uploads and downloads are designed to test the thread-safeness
59     of the MBean.
60     <p>
61  */

62 public final class UploadDownloadMgrTest extends AMXTestBase
63 {
64         public
65     UploadDownloadMgrTest( )
66         throws IOException JavaDoc
67     {
68     }
69     
70         public static Capabilities
71     getCapabilities()
72     {
73         return getOfflineCapableCapabilities( true );
74     }
75     
76         public UploadDownloadMgr
77     getUploadDownloadMgr()
78     {
79         return( getDomainRoot().getUploadDownloadMgr() );
80     }
81     
82     
83         public Object JavaDoc
84     upload( final String JavaDoc name, final int totalSize )
85         throws IOException JavaDoc
86     {
87         final UploadDownloadMgr mgr = getUploadDownloadMgr();
88         //mgr.setTrace( true );
89

90         final int chunkSize = 32 * 1024;
91         
92         final long start = now();
93         
94         final Object JavaDoc uploadID = mgr.initiateUpload( name, totalSize );
95         int remaining = totalSize;
96         boolean done = false;
97         while ( remaining != 0 )
98         {
99             final int actual = remaining < chunkSize ? remaining : chunkSize;
100             
101             final byte[] bytes = new byte[ actual ];
102             done = mgr.uploadBytes( uploadID, bytes );
103             remaining -= actual;
104             //trace( "uploaded: " + (totalSize - remaining) );
105
}
106         assert( done );
107         
108         printElapsed( "UploadDownloadMgr.upload: " + totalSize + " bytes", start );
109         return( uploadID );
110     }
111     
112     
113         private File JavaDoc
114     createTempFile( final long totalSize )
115         throws IOException JavaDoc
116     {
117         final long start = now();
118         
119         final File JavaDoc temp = File.createTempFile( "UploadDownloadMgrTest", "junk" );
120         
121         temp.deleteOnExit();
122         
123         final FileOutputStream JavaDoc os = new FileOutputStream JavaDoc( temp );
124         
125         try
126         {
127             long remaining = totalSize;
128             
129             final byte[] junk = new byte[ 1024 * 1024 ];
130             
131             while ( remaining != 0 )
132             {
133                 final long actual = remaining < junk.length ? remaining : junk.length;
134                 
135                 os.write( junk, 0, (int)actual );
136                 remaining -= actual;
137             }
138             os.close();
139         }
140         catch( IOException JavaDoc e )
141         {
142             os.close();
143             temp.delete();
144             throw e;
145         }
146         
147         assert( temp.length() == totalSize );
148         
149         printElapsed( "UploadDownloadMgr.createTempFile: " +
150             totalSize + " bytes", start );
151         return( temp );
152     }
153         
154         public File JavaDoc
155     testDownloadFile(
156         final int testSize,
157         final int chunkSize )
158         throws IOException JavaDoc
159     {
160         final UploadDownloadMgr mgr =
161                 getDomainRoot().getUploadDownloadMgr();
162         
163         final File JavaDoc testFile = createTempFile( testSize );
164         
165         final long start = now();
166         final Object JavaDoc id = mgr.initiateDownload( testFile, true );
167         
168         //trace( "initated download for: " + id + " file = " + testFile.toString() );
169
final int maxChunkSize = mgr.getMaxDownloadChunkSize();
170         final int actualChunkSize = chunkSize < maxChunkSize ?
171                                         chunkSize : maxChunkSize;
172             
173         final long length = mgr.getDownloadLength( id );
174         long doneSoFar = 0;
175         while ( doneSoFar < length )
176         {
177             final byte[] bytes = mgr.downloadBytes( id, actualChunkSize );
178             doneSoFar += bytes.length;
179         }
180         
181         printElapsed( "UploadDownloadMgr.testDownloadFile: " +
182             testSize + " bytes" + " chunksize = " + actualChunkSize, start );
183         return( testFile );
184     }
185     
186     private final int K = 1024;
187     private final int MEGABYTE = K * K;
188     
189         public void
190     testDownloadFileBufferSameSizeSmallerThanDownload()
191         throws IOException JavaDoc
192     {
193         final int size = 256 * K;
194         
195         testDownloadFile( size, size -1 );
196     }
197     
198         public void
199     testDownloadFileBufferSameSizeAsDownload()
200         throws IOException JavaDoc
201     {
202         final int size = 256 * K;
203         testDownloadFile( size, size );
204     }
205     
206         public void
207     testDownloadFileBufferLargerThanDownload()
208         throws IOException JavaDoc
209     {
210         final int size = 256 * K;
211         testDownloadFile( size, size + 1 );
212     }
213     
214         public void
215     testDownloadSmallFile()
216         throws IOException JavaDoc
217     {
218         final int size = 50 * K;
219         testDownloadFile( size, size + 1 );
220         testDownloadFile( size, size );
221     }
222     
223     
224         public void
225     testDownloadTinyFile()
226         throws IOException JavaDoc
227     {
228         final int size = 1;
229         testDownloadFile( size, size + 1 );
230         testDownloadFile( size, size );
231     }
232     
233         public void
234     testDownloadBigFile()
235         throws IOException JavaDoc
236     {
237         final long start = now();
238         Integer JavaDoc def = new Integer JavaDoc(PropertyKeys.DEFAULT_UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB);
239         final int kb =
240             getEnvInteger( PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB, def).intValue();
241         assert( kb >= 1 ) :
242             "Test size must be positive, value for " +
243             PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB +
244             ": " + kb;
245             
246         testDownloadFile( kb * K, MEGABYTE );
247         
248         printElapsed( "UploadDownloadMgrTest.testDownloadBigFile: " + kb + "kb", start);
249     }
250     
251     
252     private final class UploadDownloadTestThread extends Thread JavaDoc
253     {
254         Throwable JavaDoc mThrowable;
255         boolean mDone;
256         final int mLength;
257         long mElapsed;
258         
259             public
260         UploadDownloadTestThread( final int length )
261         {
262             mThrowable = null;
263             mDone = false;
264             mLength = length;
265             mElapsed = 0;
266         }
267         
268             public void
269         run()
270         {
271             mDone = false;
272             try
273             {
274                 final long start = System.currentTimeMillis();
275                 
276                 final File JavaDoc f = testDownloadFile( mLength, 1 * K );
277                 upload( f.toString(), mLength );
278                 
279                 mElapsed = System.currentTimeMillis() - start;
280             }
281             catch( Throwable JavaDoc t )
282             {
283                 mThrowable = t;
284             }
285             mDone = true;
286         }
287         
288             long
289         getLength()
290         {
291             return mLength;
292         }
293         
294             long
295         getElapsed()
296         {
297             return mElapsed;
298         }
299         
300             Throwable JavaDoc
301         getThrowable()
302         {
303             return( mThrowable );
304         }
305         
306             public boolean
307         done()
308         {
309             return( mDone );
310         }
311     }
312     
313     /**
314         This test is an attempt to find any synchronization bugs.
315      */

316         public void
317     testHeavilyThreaded()
318         throws IOException JavaDoc
319     {
320         Integer JavaDoc def = new Integer JavaDoc(PropertyKeys.DEFAULT_UPLOAD_DOWNLOAD_MGR_TEST_THREADS);
321         
322         int numThreads = getEnvInteger( PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_THREADS, def).intValue();
323         if ( numThreads <= 0 )
324         {
325             numThreads = 1;
326         }
327         
328         printVerbose( "UploadDownloadMgrTest.testHeavilyThreaded: using " + numThreads + " threads." );
329             
330         final UploadDownloadTestThread[] threads =
331             new UploadDownloadTestThread[ numThreads ];
332         
333         // create and start all the threads
334
for( int i = 0; i < numThreads; ++i )
335         {
336             threads[ i ] = new UploadDownloadTestThread( i * K + 1 );
337             threads[ i ].start();
338         }
339         
340         // wait till done
341
boolean done = false;
342         while ( true )
343         {
344             int numDone = 0;
345             for( int i = 0; i < numThreads; ++i )
346             {
347                 if ( threads[ i ].done() )
348                 {
349                     ++numDone;
350                 }
351             }
352             
353             if ( numDone == numThreads )
354                 break;
355                 
356             printVerbose( "UploadDownloadMgrTest.testHeavilyThreaded: waiting for " +
357                 (numThreads - numDone) + " of " + numThreads + " threads ");
358             mySleep( 1000 );
359         }
360         
361         // verify success
362
for( int i = 0; i < numThreads; ++i )
363         {
364             assert( threads[ i ].done() );
365             assert( threads[ i ].getThrowable() == null ) :
366                 ExceptionUtil.getStackTrace( threads[ i ].getThrowable() );
367         }
368
369     }
370      
371         public void
372     testUploadFile1()
373         throws IOException JavaDoc
374     {
375         final Object JavaDoc id = upload( "./deploy.temp1." + now(), 1024 * K );
376     }
377     
378         public void
379     testUploadFile2()
380         throws IOException JavaDoc
381     {
382         final Object JavaDoc id = upload( "./deploy.temp2." + now(), 1 + 100 * K );
383     }
384     
385         public void
386     testUploadFile3()
387         throws IOException JavaDoc
388     {
389         final Object JavaDoc id = upload( "./deploy.temp3." + now(), 1 );
390     }
391     
392         public void
393     testUploadFile4()
394         throws IOException JavaDoc
395     {
396         final Object JavaDoc id = upload( "./deploy.temp4." + now(), K + 1 );
397     }
398     
399         public void
400     testUploadFile5()
401         throws IOException JavaDoc
402     {
403         final Object JavaDoc id = upload( null, 1 + 2048 * K );
404     }
405 }
406
407
408
Popular Tags