1 package storagetesting
;
3 import com
.sun
.star
.uno
.UnoRuntime
;
4 import com
.sun
.star
.uno
.XInterface
;
5 import com
.sun
.star
.uno
.AnyConverter
;
7 import com
.sun
.star
.lang
.*;
8 import com
.sun
.star
.embed
.*;
9 import com
.sun
.star
.packages
.*;
10 import com
.sun
.star
.io
.*;
11 import com
.sun
.star
.beans
.*;
13 public class TestHelper
{
17 public TestHelper( String sTestPrefix
)
19 m_sTestPrefix
= sTestPrefix
;
22 public boolean WriteBytesToStream( XStream xStream
,
28 // get output stream of substream
29 XOutputStream xOutput
= xStream
.getOutputStream();
30 if ( xOutput
== null )
32 Error( "Can't get XOutputStream implementation from substream '" + sStreamName
+ "'!" );
36 // get XTrucate implementation from output stream
37 XTruncate xTruncate
= (XTruncate
) UnoRuntime
.queryInterface( XTruncate
.class, xOutput
);
38 if ( xTruncate
== null )
40 Error( "Can't get XTruncate implementation from substream '" + sStreamName
+ "'!" );
44 // write requested byte sequence
48 xOutput
.writeBytes( pBytes
);
52 Error( "Can't write to stream '" + sStreamName
+ "', exception: " + e
);
56 // get access to the XPropertySet interface
57 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
58 if ( xPropSet
== null )
60 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
64 // set properties to the stream
67 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
68 xPropSet
.setPropertyValue( "Compressed", new Boolean( bCompressed
) );
72 Error( "Can't set properties to substream '" + sStreamName
+ "', exception: " + e
);
76 // check size property of the stream
79 int nSize
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "Size" ) );
80 if ( nSize
!= pBytes
.length
)
82 Error( "The 'Size' property of substream '" + sStreamName
+ "' contains wrong value!" );
88 Error( "Can't get 'Size' property from substream '" + sStreamName
+ "', exception: " + e
);
92 // free the stream resources, garbage collector may remove the object too late
93 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xStream
);
94 if ( xComponent
== null )
96 Error( "Can't get XComponent implementation from substream '" + sStreamName
+ "'!" );
105 public boolean WriteBytesToSubstream( XStorage xStorage
,
111 // open substream element
112 XStream xSubStream
= null;
115 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.ELEMENT_WRITE
);
116 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
117 if ( xSubStream
== null )
119 Error( "Can't create substream '" + sStreamName
+ "'!" );
125 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
129 return WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
);
132 public boolean WriteBytesToEncrSubstream( XStorage xStorage
,
139 // open substream element
140 XStream xSubStream
= null;
143 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.ELEMENT_WRITE
, pPass
);
144 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
145 if ( xSubStream
== null )
147 Error( "Can't create substream '" + sStreamName
+ "'!" );
153 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
157 return WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
);
160 public boolean WBToSubstrOfEncr( XStorage xStorage
,
167 // open substream element
168 XStream xSubStream
= null;
171 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.ELEMENT_WRITE
);
172 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
173 if ( xSubStream
== null )
175 Error( "Can't create substream '" + sStreamName
+ "'!" );
181 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
185 // get access to the XPropertySet interface
186 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
187 if ( xPropSet
== null )
189 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
193 // set properties to the stream
196 xPropSet
.setPropertyValue( "Encrypted", new Boolean( bEncrypted
) );
200 Error( "Can't set 'Encrypted' property to substream '" + sStreamName
+ "', exception: " + e
);
204 return WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
);
207 public int ChangeStreamPass( XStorage xStorage
,
212 // open substream element
213 XStream xSubStream
= null;
216 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.ELEMENT_WRITE
, pOldPass
);
217 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
218 if ( xSubStream
== null )
220 Error( "Can't open substream '" + sStreamName
+ "'!" );
226 Error( "Can't open substream '" + sStreamName
+ "', exception : " + e
+ "!" );
231 // change the password for the stream
232 XEncryptionProtectedSource xStreamEncryption
=
233 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xSubStream
);
235 if ( xStreamEncryption
== null )
237 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
242 xStreamEncryption
.setEncryptionKey( pNewPass
);
246 Error( "Can't change encryption key of the substream '" + sStreamName
+ "', exception:" + e
);
250 // free the stream resources, garbage collector may remove the object too late
251 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xSubStream
);
252 if ( xComponent
== null )
254 Error( "Can't get XComponent implementation from substream '" + sStreamName
+ "'!" );
257 xComponent
.dispose();
262 public boolean setStorageTypeAndCheckProps( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
266 // get access to the XPropertySet interface
267 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
268 if ( xPropSet
!= null )
272 // set "MediaType" property to the stream
273 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
275 // get "IsRoot" and "OpenMode" properties and control there values
276 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
277 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
280 if ( bPropIsRoot
!= bIsRoot
)
282 Error( "'IsRoot' property contains wrong value!" );
286 if ( ( bIsRoot
&& ( nPropMode
| ElementModes
.ELEMENT_READ
) != ( nMode
| ElementModes
.ELEMENT_READ
) )
287 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
289 Error( "'OpenMode' property contains wrong value!" );
295 Error( "Can't control properties of substorage, exception: " + e
);
300 Error( "Can't get XPropertySet implementation from storage!" );
306 public boolean checkStorageProperties( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
310 // get access to the XPropertySet interface
311 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
312 if ( xPropSet
!= null )
316 // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
317 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
318 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
319 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
322 if ( !sPropMediaType
.equals( sMediaType
) )
324 Error( "'MediaType' property contains wrong value, expected '"
325 + sMediaType
+ "', set '" + sPropMediaType
+ "' !" );
329 if ( bPropIsRoot
!= bIsRoot
)
331 Error( "'IsRoot' property contains wrong value!" );
335 if ( ( bIsRoot
&& ( nPropMode
| ElementModes
.ELEMENT_READ
) != ( nMode
| ElementModes
.ELEMENT_READ
) )
336 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
338 Error( "'OpenMode' property contains wrong value!" );
344 Error( "Can't get properties of substorage, exception: " + e
);
349 Error( "Can't get XPropertySet implementation from storage!" );
355 public boolean InternalCheckStream( XStream xStream
,
360 // get input stream of substream
361 XInputStream xInput
= xStream
.getInputStream();
362 if ( xInput
== null )
364 Error( "Can't get XInputStream implementation from substream '" + sName
+ "'!" );
368 byte pContents
[][] = new byte[1][]; // ???
373 xInput
.readBytes( pContents
, pBytes
.length
+ 1 );
377 Error( "Can't read from stream '" + sName
+ "', exception: " + e
);
381 // check size of stream data
382 if ( pContents
.length
== 0 )
384 Error( "SubStream '" + sName
+ "' reading produced disaster!" );
388 if ( pBytes
.length
!= pContents
[0].length
)
390 Error( "SubStream '" + sName
+ "' contains wrong amount of data! (" + pContents
[0].length
+ "/" + pBytes
.length
+ ")" );
395 for ( int ind
= 0; ind
< pBytes
.length
; ind
++ )
397 if ( pBytes
[ind
] != pContents
[0][ind
] )
399 Error( "SubStream '" + sName
+ "' contains wrong data!" );
408 // get access to the XPropertySet interface
409 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
410 if ( xPropSet
!= null )
414 // get "MediaType" and "Size" properties and control there values
415 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
416 int nPropSize
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "Size" ) );
419 if ( !sPropMediaType
.equals( sMediaType
) )
421 Error( "'MediaType' property contains wrong value for stream '" + sName
+ "',\nexpected: '"
422 + sMediaType
+ "', set: '" + sPropMediaType
+ "'!" );
426 if ( nPropSize
!= pBytes
.length
)
428 Error( "'Size' property contains wrong value for stream'" + sName
+ "'!" );
434 Error( "Can't get properties of substream '" + sName
+ "', exception: " + e
);
439 Error( "Can't get XPropertySet implementation from stream '" + sName
+ "'!" );
445 public boolean checkStream( XStorage xParentStorage
,
450 // open substream element first
451 XStream xSubStream
= null;
454 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.ELEMENT_READ
);
455 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
456 if ( xSubStream
== null )
458 Error( "Can't open substream '" + sName
+ "'!" );
464 Error( "Can't open substream '" + sName
+ "', exception : " + e
+ "!" );
468 return InternalCheckStream( xSubStream
, sName
, sMediaType
, pBytes
);
471 public boolean checkEncrStream( XStorage xParentStorage
,
477 // Important: a common password for any of parent storage should not be set or
478 // should be different from pPass
480 if ( pPass
.length
== 0 )
482 Error( "Wrong password is used in the test!" );
488 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.ELEMENT_READ
);
489 Error( "Encrypted stream '" + sName
+ "' was opened without password!" );
492 catch( WrongPasswordException wpe
)
496 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' without password: " + e
+ "!" );
500 byte pWrongPass
[] = { 1, 1 };
501 pWrongPass
[0] += pPass
[0];
504 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.ELEMENT_READ
, pWrongPass
);
505 Error( "Encrypted stream '" + sName
+ "' was opened with wrong password!" );
508 catch( WrongPasswordException wpe
)
512 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' with wrong password: " + e
+ "!" );
516 XStream xSubStream
= null;
519 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.ELEMENT_READ
, pPass
);
520 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
521 if ( xSubStream
== null )
523 Error( "Can't open encrypted substream '" + sName
+ "'!" );
529 Error( "Can't open encrypted substream '" + sName
+ "', exception : " + e
+ "!" );
533 return InternalCheckStream( xSubStream
, sName
, sMediaType
, pBytes
);
536 public boolean copyStorage( XStorage xSourceStorage
, XStorage xDestStorage
)
538 // copy xSourceStorage to xDestStorage
541 xSourceStorage
.copyToStorage( xDestStorage
);
545 Error( "Storage copying failed, exception: " + e
);
552 public boolean commitStorage( XStorage xStorage
)
554 // XTransactedObject must be supported by storages
555 XTransactedObject xTransact
= (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xStorage
);
556 if ( xTransact
== null )
558 Error( "Storage doesn't implement transacted access!" );
568 Error( "Storage commit failed, exception:" + e
);
575 public boolean disposeStorage( XStorage xStorage
)
577 // dispose the storage
578 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xStorage
);
579 if ( xComponent
== null )
581 Error( "Can't retrieve XComponent implementation from storage!" );
587 xComponent
.dispose();
591 Error( "Storage disposing failed!" );
598 public XInputStream
getInputStream( XStream xStream
)
600 XInputStream xInTemp
= null;
603 xInTemp
= xStream
.getInputStream();
604 if ( xInTemp
== null )
605 Error( "Can't get the input part of a stream!" );
607 catch ( Exception e
)
609 Error( "Can't get the input part of a stream, exception :" + e
);
615 public boolean closeOutput( XStream xStream
)
617 XOutputStream xOutTemp
= null;
620 xOutTemp
= xStream
.getOutputStream();
621 if ( xOutTemp
== null )
623 Error( "Can't get the output part of a stream!" );
627 catch ( Exception e
)
629 Error( "Can't get the output part of a stream, exception :" + e
);
635 xOutTemp
.closeOutput();
637 catch ( Exception e
)
639 Error( "Can't close output part of a stream, exception :" + e
);
646 public XStorage
openSubStorage( XStorage xStorage
, String sName
, int nMode
)
648 // open existing substorage
651 Object oSubStorage
= xStorage
.openStorageElement( sName
, nMode
);
652 XStorage xSubStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oSubStorage
);
657 Error( "Can't open substorage '" + sName
+ "', exception: " + e
);
663 public XStream
CreateTempFileStream( XMultiServiceFactory xMSF
)
665 // try to get temporary file representation
666 XStream xTempFileStream
= null;
669 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
670 xTempFileStream
= (XStream
)UnoRuntime
.queryInterface( XStream
.class, oTempFile
);
675 if ( xTempFileStream
== null )
676 Error( "Can't create temporary file!" );
678 return xTempFileStream
;
681 public String
CreateTempFile( XMultiServiceFactory xMSF
)
683 String sResult
= null;
685 // try to get temporary file representation
686 XPropertySet xTempFileProps
= null;
689 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
690 xTempFileProps
= (XPropertySet
)UnoRuntime
.queryInterface( XPropertySet
.class, oTempFile
);
695 if ( xTempFileProps
!= null )
699 xTempFileProps
.setPropertyValue( "RemoveFile", new Boolean( false ) );
700 sResult
= AnyConverter
.toString( xTempFileProps
.getPropertyValue( "Uri" ) );
704 Error( "Can't control TempFile properties, exception: " + e
);
709 Error( "Can't create temporary file representation!" );
712 // close temporary file explicitly
715 XStream xStream
= (XStream
)UnoRuntime
.queryInterface( XStream
.class, xTempFileProps
);
716 if ( xStream
!= null )
718 XOutputStream xOut
= xStream
.getOutputStream();
722 XInputStream xIn
= xStream
.getInputStream();
727 Error( "Can't close TempFile!" );
731 Error( "Can't close TempFile, exception: " + e
);
737 public boolean copyElementTo( XStorage xSource
, String sName
, XStorage xDest
)
739 // copy element with name sName from xSource to xDest
742 xSource
.copyElementTo( sName
, xDest
, sName
);
746 Error( "Element copying failed, exception: " + e
);
753 public boolean moveElementTo( XStorage xSource
, String sName
, XStorage xDest
)
755 // move element with name sName from xSource to xDest
758 xSource
.moveElementTo( sName
, xDest
, sName
);
762 Error( "Element moving failed, exception: " + e
);
769 public boolean renameElement( XStorage xStorage
, String sOldName
, String sNewName
)
771 // rename element with name sOldName to sNewName
774 xStorage
.renameElement( sOldName
, sNewName
);
778 Error( "Element renaming failed, exception: " + e
);
785 public boolean removeElement( XStorage xStorage
, String sName
)
787 // remove element with name sName
790 xStorage
.removeElement( sName
);
794 Error( "Element removing failed, exception: " + e
);
801 public XStream
OpenStream( XStorage xStorage
,
805 // open substream element
806 XStream xSubStream
= null;
809 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, nMode
);
810 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
811 if ( xSubStream
== null )
812 Error( "Can't create substream '" + sStreamName
+ "'!" );
816 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
822 public boolean cantOpenStorage( XStorage xStorage
, String sName
)
824 // try to open an opened substorage, open call must fail
827 Object oDummyStorage
= xStorage
.openStorageElement( sName
, ElementModes
.ELEMENT_READ
);
828 Error( "The trying to reopen opened substorage '" + sName
+ "' must fail!" );
838 public boolean cantOpenStream( XStorage xStorage
, String sName
, int nMode
)
840 // try to open the substream with specified mode must fail
843 Object oDummyStream
= xStorage
.openStreamElement( sName
, nMode
);
844 Error( "The trying to open substoream '" + sName
+ "' must fail!" );
854 public void Error( String sError
)
856 System
.out
.println( m_sTestPrefix
+ "Error: " + sError
);
859 public void Message( String sError
)
861 System
.out
.println( m_sTestPrefix
+ sError
);