merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / java / Storage / TestHelper.java
blob2868de481f81f494aea2c86b3beb43100f97024d
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 {
15 String m_sTestPrefix;
17 public TestHelper( String sTestPrefix )
19 m_sTestPrefix = sTestPrefix;
22 public boolean WriteBytesToStream( XStream xStream,
23 String sStreamName,
24 String sMediaType,
25 boolean bCompressed,
26 byte[] pBytes )
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 + "'!" );
33 return false;
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 + "'!" );
41 return false;
44 // write requested byte sequence
45 try
47 xTruncate.truncate();
48 xOutput.writeBytes( pBytes );
50 catch( Exception e )
52 Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
53 return false;
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 + "'!" );
61 return false;
64 // set properties to the stream
65 try
67 xPropSet.setPropertyValue( "MediaType", sMediaType );
68 xPropSet.setPropertyValue( "Compressed", new Boolean( bCompressed ) );
70 catch( Exception e )
72 Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
73 return false;
76 // check size property of the stream
77 try
79 int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
80 if ( nSize != pBytes.length )
82 Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
83 return false;
86 catch( Exception e )
88 Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
89 return false;
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 + "'!" );
97 return false;
99 xComponent.dispose();
101 return true;
105 public boolean WriteBytesToSubstream( XStorage xStorage,
106 String sStreamName,
107 String sMediaType,
108 boolean bCompressed,
109 byte[] pBytes )
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 + "'!" );
120 return false;
123 catch( Exception e )
125 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
126 return false;
129 return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
132 public boolean WriteBytesToEncrSubstream( XStorage xStorage,
133 String sStreamName,
134 String sMediaType,
135 boolean bCompressed,
136 byte[] pBytes,
137 byte[] pPass )
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 + "'!" );
148 return false;
151 catch( Exception e )
153 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
154 return false;
157 return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
160 public boolean WBToSubstrOfEncr( XStorage xStorage,
161 String sStreamName,
162 String sMediaType,
163 boolean bCompressed,
164 byte[] pBytes,
165 boolean bEncrypted )
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 + "'!" );
176 return false;
179 catch( Exception e )
181 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
182 return false;
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 + "'!" );
190 return false;
193 // set properties to the stream
196 xPropSet.setPropertyValue( "Encrypted", new Boolean( bEncrypted ) );
198 catch( Exception e )
200 Error( "Can't set 'Encrypted' property to substream '" + sStreamName + "', exception: " + e );
201 return false;
204 return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
207 public int ChangeStreamPass( XStorage xStorage,
208 String sStreamName,
209 byte[] pOldPass,
210 byte[] pNewPass )
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 + "'!" );
221 return 0;
224 catch( Exception e )
226 Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
227 return 0;
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!" );
238 return -1;
241 try {
242 xStreamEncryption.setEncryptionKey( pNewPass );
244 catch( Exception e )
246 Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
247 return 0;
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 + "'!" );
255 return 0;
257 xComponent.dispose();
259 return 1;
262 public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
264 boolean bOk = false;
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" ) );
279 bOk = true;
280 if ( bPropIsRoot != bIsRoot )
282 Error( "'IsRoot' property contains wrong value!" );
283 bOk = false;
286 if ( ( bIsRoot && ( nPropMode | ElementModes.ELEMENT_READ ) != ( nMode | ElementModes.ELEMENT_READ ) )
287 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
289 Error( "'OpenMode' property contains wrong value!" );
290 bOk = false;
293 catch( Exception e )
295 Error( "Can't control properties of substorage, exception: " + e );
298 else
300 Error( "Can't get XPropertySet implementation from storage!" );
303 return bOk;
306 public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
308 boolean bOk = false;
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" ) );
321 bOk = true;
322 if ( !sPropMediaType.equals( sMediaType ) )
324 Error( "'MediaType' property contains wrong value, expected '"
325 + sMediaType + "', set '" + sPropMediaType + "' !" );
326 bOk = false;
329 if ( bPropIsRoot != bIsRoot )
331 Error( "'IsRoot' property contains wrong value!" );
332 bOk = false;
335 if ( ( bIsRoot && ( nPropMode | ElementModes.ELEMENT_READ ) != ( nMode | ElementModes.ELEMENT_READ ) )
336 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
338 Error( "'OpenMode' property contains wrong value!" );
339 bOk = false;
342 catch( Exception e )
344 Error( "Can't get properties of substorage, exception: " + e );
347 else
349 Error( "Can't get XPropertySet implementation from storage!" );
352 return bOk;
355 public boolean InternalCheckStream( XStream xStream,
356 String sName,
357 String sMediaType,
358 byte[] pBytes )
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 + "'!" );
365 return false;
368 byte pContents[][] = new byte[1][]; // ???
370 // read contents
373 xInput.readBytes( pContents, pBytes.length + 1 );
375 catch( Exception e )
377 Error( "Can't read from stream '" + sName + "', exception: " + e );
378 return false;
381 // check size of stream data
382 if ( pContents.length == 0 )
384 Error( "SubStream '" + sName + "' reading produced disaster!" );
385 return false;
388 if ( pBytes.length != pContents[0].length )
390 Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
391 return false;
394 // check stream data
395 for ( int ind = 0; ind < pBytes.length; ind++ )
397 if ( pBytes[ind] != pContents[0][ind] )
399 Error( "SubStream '" + sName + "' contains wrong data!" );
400 return false;
405 // check properties
406 boolean bOk = false;
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" ) );
418 bOk = true;
419 if ( !sPropMediaType.equals( sMediaType ) )
421 Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
422 + sMediaType + "', set: '" + sPropMediaType + "'!" );
423 bOk = false;
426 if ( nPropSize != pBytes.length )
428 Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
429 bOk = false;
432 catch( Exception e )
434 Error( "Can't get properties of substream '" + sName + "', exception: " + e );
437 else
439 Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
442 return bOk;
445 public boolean checkStream( XStorage xParentStorage,
446 String sName,
447 String sMediaType,
448 byte[] pBytes )
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 + "'!" );
459 return false;
462 catch( Exception e )
464 Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
465 return false;
468 return InternalCheckStream( xSubStream, sName, sMediaType, pBytes );
471 public boolean checkEncrStream( XStorage xParentStorage,
472 String sName,
473 String sMediaType,
474 byte[] pBytes,
475 byte[] pPass )
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!" );
483 return false;
488 Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.ELEMENT_READ );
489 Error( "Encrypted stream '" + sName + "' was opened without password!" );
490 return false;
492 catch( WrongPasswordException wpe )
494 catch( Exception e )
496 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
497 return false;
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!" );
506 return false;
508 catch( WrongPasswordException wpe )
510 catch( Exception e )
512 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
513 return false;
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 + "'!" );
524 return false;
527 catch( Exception e )
529 Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
530 return false;
533 return InternalCheckStream( xSubStream, sName, sMediaType, pBytes );
536 public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
538 // copy xSourceStorage to xDestStorage
541 xSourceStorage.copyToStorage( xDestStorage );
543 catch( Exception e )
545 Error( "Storage copying failed, exception: " + e );
546 return false;
549 return true;
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!" );
559 return false;
564 xTransact.commit();
566 catch( Exception e )
568 Error( "Storage commit failed, exception:" + e );
569 return false;
572 return true;
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!" );
582 return false;
587 xComponent.dispose();
589 catch( Exception e )
591 Error( "Storage disposing failed!" );
592 return false;
595 return true;
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 );
612 return xInTemp;
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!" );
624 return false;
627 catch ( Exception e )
629 Error( "Can't get the output part of a stream, exception :" + e );
630 return false;
635 xOutTemp.closeOutput();
637 catch ( Exception e )
639 Error( "Can't close output part of a stream, exception :" + e );
640 return false;
643 return true;
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 );
653 return xSubStorage;
655 catch( Exception e )
657 Error( "Can't open substorage '" + sName + "', exception: " + e );
660 return null;
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 );
672 catch( Exception e )
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 );
692 catch( Exception e )
695 if ( xTempFileProps != null )
699 xTempFileProps.setPropertyValue( "RemoveFile", new Boolean( false ) );
700 sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
702 catch( Exception e )
704 Error( "Can't control TempFile properties, exception: " + e );
707 else
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();
719 if ( xOut != null )
720 xOut.closeOutput();
722 XInputStream xIn = xStream.getInputStream();
723 if ( xIn != null )
724 xIn.closeInput();
726 else
727 Error( "Can't close TempFile!" );
729 catch( Exception e )
731 Error( "Can't close TempFile, exception: " + e );
734 return sResult;
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 );
744 catch( Exception e )
746 Error( "Element copying failed, exception: " + e );
747 return false;
750 return true;
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 );
760 catch( Exception e )
762 Error( "Element moving failed, exception: " + e );
763 return false;
766 return true;
769 public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
771 // rename element with name sOldName to sNewName
774 xStorage.renameElement( sOldName, sNewName );
776 catch( Exception e )
778 Error( "Element renaming failed, exception: " + e );
779 return false;
782 return true;
785 public boolean removeElement( XStorage xStorage, String sName )
787 // remove element with name sName
790 xStorage.removeElement( sName );
792 catch( Exception e )
794 Error( "Element removing failed, exception: " + e );
795 return false;
798 return true;
801 public XStream OpenStream( XStorage xStorage,
802 String sStreamName,
803 int nMode )
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 + "'!" );
814 catch( Exception e )
816 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
819 return xSubStream;
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!" );
830 catch( Exception e )
832 return true;
835 return false;
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!" );
846 catch( Exception e )
848 return true;
851 return false;
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 );