1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.uno
.UnoRuntime
;
21 import com
.sun
.star
.uno
.AnyConverter
;
23 import com
.sun
.star
.lang
.*;
24 import com
.sun
.star
.embed
.*;
25 import com
.sun
.star
.packages
.*;
26 import com
.sun
.star
.io
.*;
27 import com
.sun
.star
.beans
.*;
29 public class TestHelper
{
33 public TestHelper( String sTestPrefix
)
35 m_sTestPrefix
= sTestPrefix
;
38 public boolean WriteBytesToStream( XStream xStream
,
44 // get output stream of substream
45 XOutputStream xOutput
= xStream
.getOutputStream();
46 if ( xOutput
== null )
48 Error( "Can't get XOutputStream implementation from substream '" + sStreamName
+ "'!" );
52 // get XTruncate implementation from output stream
53 XTruncate xTruncate
= UnoRuntime
.queryInterface( XTruncate
.class, xOutput
);
54 if ( xTruncate
== null )
56 Error( "Can't get XTruncate implementation from substream '" + sStreamName
+ "'!" );
60 // write requested byte sequence
64 xOutput
.writeBytes( pBytes
);
68 Error( "Can't write to stream '" + sStreamName
+ "', exception: " + e
);
72 // get access to the XPropertySet interface
73 XPropertySet xPropSet
= UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
74 if ( xPropSet
== null )
76 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
80 // set properties to the stream
83 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
84 xPropSet
.setPropertyValue( "Compressed", Boolean
.valueOf( bCompressed
) );
88 Error( "Can't set properties to substream '" + sStreamName
+ "', exception: " + e
);
92 // check size property of the stream
95 int nSize
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "Size" ) );
96 if ( nSize
!= pBytes
.length
)
98 Error( "The 'Size' property of substream '" + sStreamName
+ "' contains wrong value!" );
104 Error( "Can't get 'Size' property from substream '" + sStreamName
+ "', exception: " + e
);
108 // free the stream resources, garbage collector may remove the object too late
109 XComponent xComponent
= UnoRuntime
.queryInterface( XComponent
.class, xStream
);
110 if ( xComponent
== null )
112 Error( "Can't get XComponent implementation from substream '" + sStreamName
+ "'!" );
115 xComponent
.dispose();
121 public boolean WriteBytesToSubstream( XStorage xStorage
,
127 // open substream element
128 XStream xSubStream
= null;
131 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
132 xSubStream
= UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
133 if ( xSubStream
== null )
135 Error( "Can't create substream '" + sStreamName
+ "'!" );
141 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
145 return WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
);
148 public boolean WriteBytesToEncrSubstream( XStorage xStorage
,
155 // open substream element
156 XStream xSubStream
= null;
159 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.WRITE
, new String(pPass
) );
160 xSubStream
= UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
161 if ( xSubStream
== null )
163 Error( "Can't create substream '" + sStreamName
+ "'!" );
169 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
173 return WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
);
176 public boolean WBToSubstrOfEncr( XStorage xStorage
,
183 // open substream element
184 XStream xSubStream
= null;
187 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
188 xSubStream
= UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
189 if ( xSubStream
== null )
191 Error( "Can't create substream '" + sStreamName
+ "'!" );
197 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
201 // get access to the XPropertySet interface
202 XPropertySet xPropSet
= UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
203 if ( xPropSet
== null )
205 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
209 // set properties to the stream
212 xPropSet
.setPropertyValue( "Encrypted", Boolean
.valueOf( bEncrypted
) );
216 Error( "Can't set 'Encrypted' property to substream '" + sStreamName
+ "', exception: " + e
);
220 return WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
);
223 public int ChangeStreamPass( XStorage xStorage
,
228 // open substream element
229 XStream xSubStream
= null;
232 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.WRITE
, new String(pOldPass
) );
233 xSubStream
= UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
234 if ( xSubStream
== null )
236 Error( "Can't open substream '" + sStreamName
+ "'!" );
242 Error( "Can't open substream '" + sStreamName
+ "', exception : " + e
+ "!" );
247 // change the password for the stream
248 XEncryptionProtectedSource xStreamEncryption
=
249 UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xSubStream
);
251 if ( xStreamEncryption
== null )
253 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
258 xStreamEncryption
.setEncryptionPassword( new String(pNewPass
) );
262 Error( "Can't change encryption key of the substream '" + sStreamName
+ "', exception:" + e
);
266 // free the stream resources, garbage collector may remove the object too late
267 XComponent xComponent
= UnoRuntime
.queryInterface( XComponent
.class, xSubStream
);
268 if ( xComponent
== null )
270 Error( "Can't get XComponent implementation from substream '" + sStreamName
+ "'!" );
273 xComponent
.dispose();
278 public boolean setStorageTypeAndCheckProps( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
282 // get access to the XPropertySet interface
283 XPropertySet xPropSet
= UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
284 if ( xPropSet
!= null )
288 // set "MediaType" property to the stream
289 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
291 // get "IsRoot" and "OpenMode" properties and control there values
292 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
293 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
296 if ( bPropIsRoot
!= bIsRoot
)
298 Error( "'IsRoot' property contains wrong value!" );
302 if ( ( bIsRoot
&& ( nPropMode
| ElementModes
.READ
) != ( nMode
| ElementModes
.READ
) )
303 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
305 Error( "'OpenMode' property contains wrong value!" );
311 Error( "Can't control properties of substorage, exception: " + e
);
316 Error( "Can't get XPropertySet implementation from storage!" );
322 public boolean checkStorageProperties( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
326 // get access to the XPropertySet interface
327 XPropertySet xPropSet
= UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
328 if ( xPropSet
!= null )
332 // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
333 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
334 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
335 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
338 if ( !sPropMediaType
.equals( sMediaType
) )
340 Error( "'MediaType' property contains wrong value, expected '"
341 + sMediaType
+ "', set '" + sPropMediaType
+ "' !" );
345 if ( bPropIsRoot
!= bIsRoot
)
347 Error( "'IsRoot' property contains wrong value!" );
351 if ( ( bIsRoot
&& ( nPropMode
| ElementModes
.READ
) != ( nMode
| ElementModes
.READ
) )
352 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
354 Error( "'OpenMode' property contains wrong value!" );
360 Error( "Can't get properties of substorage, exception: " + e
);
365 Error( "Can't get XPropertySet implementation from storage!" );
371 public boolean InternalCheckStream( XStream xStream
,
376 // get input stream of substream
377 XInputStream xInput
= xStream
.getInputStream();
378 if ( xInput
== null )
380 Error( "Can't get XInputStream implementation from substream '" + sName
+ "'!" );
384 byte pContents
[][] = new byte[1][]; // ???
389 xInput
.readBytes( pContents
, pBytes
.length
+ 1 );
393 Error( "Can't read from stream '" + sName
+ "', exception: " + e
);
397 // check size of stream data
398 if ( pContents
.length
== 0 )
400 Error( "SubStream '" + sName
+ "' reading produced disaster!" );
404 if ( pBytes
.length
!= pContents
[0].length
)
406 Error( "SubStream '" + sName
+ "' contains wrong amount of data! (" + pContents
[0].length
+ "/" + pBytes
.length
+ ")" );
411 for ( int ind
= 0; ind
< pBytes
.length
; ind
++ )
413 if ( pBytes
[ind
] != pContents
[0][ind
] )
415 Error( "SubStream '" + sName
+ "' contains wrong data!" );
424 // get access to the XPropertySet interface
425 XPropertySet xPropSet
= UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
426 if ( xPropSet
!= null )
430 // get "MediaType" and "Size" properties and control there values
431 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
432 long nPropSize
= AnyConverter
.toLong( xPropSet
.getPropertyValue( "Size" ) );
435 if ( !sPropMediaType
.equals( sMediaType
) )
437 Error( "'MediaType' property contains wrong value for stream '" + sName
+ "',\nexpected: '"
438 + sMediaType
+ "', set: '" + sPropMediaType
+ "'!" );
442 if ( nPropSize
!= pBytes
.length
)
444 Error( "'Size' property contains wrong value for stream'" + sName
+ "'!" );
450 Error( "Can't get properties of substream '" + sName
+ "', exception: " + e
);
455 Error( "Can't get XPropertySet implementation from stream '" + sName
+ "'!" );
461 public boolean checkStream( XStorage xParentStorage
,
466 // open substream element first
467 XStream xSubStream
= null;
470 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.READ
);
471 xSubStream
= UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
472 if ( xSubStream
== null )
474 Error( "Can't open substream '" + sName
+ "'!" );
480 Error( "Can't open substream '" + sName
+ "', exception : " + e
+ "!" );
484 return InternalCheckStream( xSubStream
, sName
, sMediaType
, pBytes
);
487 public boolean checkEncrStream( XStorage xParentStorage
,
493 // Important: a common password for any of parent storage should not be set or
494 // should be different from pPass
496 if ( pPass
.length
== 0 )
498 Error( "Wrong password is used in the test!" );
504 xParentStorage
.openStreamElement( sName
, ElementModes
.READ
);
505 Error( "Encrypted stream '" + sName
+ "' was opened without password!" );
508 catch( WrongPasswordException wpe
)
512 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' without password: " + e
+ "!" );
516 byte pWrongPass
[] = { 1, 1 };
517 pWrongPass
[0] += pPass
[0];
520 xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.READ
, new String(pWrongPass
) );
521 Error( "Encrypted stream '" + sName
+ "' was opened with wrong password!" );
524 catch( WrongPasswordException wpe
)
528 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' with wrong password: " + e
+ "!" );
532 XStream xSubStream
= null;
535 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.READ
, new String(pPass
) );
536 xSubStream
= UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
537 if ( xSubStream
== null )
539 Error( "Can't open encrypted substream '" + sName
+ "'!" );
545 Error( "Can't open encrypted substream '" + sName
+ "', exception : " + e
+ "!" );
549 return InternalCheckStream( xSubStream
, sName
, sMediaType
, pBytes
);
552 public boolean copyStorage( XStorage xSourceStorage
, XStorage xDestStorage
)
554 // copy xSourceStorage to xDestStorage
557 xSourceStorage
.copyToStorage( xDestStorage
);
561 Error( "Storage copying failed, exception: " + e
);
568 public boolean commitStorage( XStorage xStorage
)
570 // XTransactedObject must be supported by storages
571 XTransactedObject xTransact
= UnoRuntime
.queryInterface( XTransactedObject
.class, xStorage
);
572 if ( xTransact
== null )
574 Error( "Storage doesn't implement transacted access!" );
584 Error( "Storage commit failed, exception:" + e
);
591 public boolean disposeStorage( XStorage xStorage
)
593 // dispose the storage
594 XComponent xComponent
= UnoRuntime
.queryInterface( XComponent
.class, xStorage
);
595 if ( xComponent
== null )
597 Error( "Can't retrieve XComponent implementation from storage!" );
603 xComponent
.dispose();
607 Error( "Storage disposing failed!" );
614 public XInputStream
getInputStream( XStream xStream
)
616 XInputStream xInTemp
= null;
619 xInTemp
= xStream
.getInputStream();
620 if ( xInTemp
== null )
621 Error( "Can't get the input part of a stream!" );
623 catch ( Exception e
)
625 Error( "Can't get the input part of a stream, exception :" + e
);
631 public boolean closeOutput( XStream xStream
)
633 XOutputStream xOutTemp
= null;
636 xOutTemp
= xStream
.getOutputStream();
637 if ( xOutTemp
== null )
639 Error( "Can't get the output part of a stream!" );
643 catch ( Exception e
)
645 Error( "Can't get the output part of a stream, exception :" + e
);
651 xOutTemp
.closeOutput();
653 catch ( Exception e
)
655 Error( "Can't close output part of a stream, exception :" + e
);
662 public XStorage
openSubStorage( XStorage xStorage
, String sName
, int nMode
)
664 // open existing substorage
667 Object oSubStorage
= xStorage
.openStorageElement( sName
, nMode
);
668 XStorage xSubStorage
= UnoRuntime
.queryInterface( XStorage
.class, oSubStorage
);
673 Error( "Can't open substorage '" + sName
+ "', exception: " + e
);
679 public XStream
CreateTempFileStream( XMultiServiceFactory xMSF
)
681 // try to get temporary file representation
682 XStream xTempFileStream
= null;
685 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
686 xTempFileStream
= UnoRuntime
.queryInterface( XStream
.class, oTempFile
);
691 if ( xTempFileStream
== null )
692 Error( "Can't create temporary file!" );
694 return xTempFileStream
;
697 public String
CreateTempFile( XMultiServiceFactory xMSF
)
699 String sResult
= null;
701 // try to get temporary file representation
702 XPropertySet xTempFileProps
= null;
705 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
706 xTempFileProps
= UnoRuntime
.queryInterface( XPropertySet
.class, oTempFile
);
711 if ( xTempFileProps
!= null )
715 xTempFileProps
.setPropertyValue( "RemoveFile", Boolean
.FALSE
);
716 sResult
= AnyConverter
.toString( xTempFileProps
.getPropertyValue( "Uri" ) );
720 Error( "Can't control TempFile properties, exception: " + e
);
725 Error( "Can't create temporary file representation!" );
728 // close temporary file explicitly
731 XStream xStream
= UnoRuntime
.queryInterface( XStream
.class, xTempFileProps
);
732 if ( xStream
!= null )
734 XOutputStream xOut
= xStream
.getOutputStream();
738 XInputStream xIn
= xStream
.getInputStream();
743 Error( "Can't close TempFile!" );
747 Error( "Can't close TempFile, exception: " + e
);
753 public boolean copyElementTo( XStorage xSource
, String sName
, XStorage xDest
)
755 // copy element with name sName from xSource to xDest
758 xSource
.copyElementTo( sName
, xDest
, sName
);
762 Error( "Element copying failed, exception: " + e
);
769 public boolean moveElementTo( XStorage xSource
, String sName
, XStorage xDest
)
771 // move element with name sName from xSource to xDest
774 xSource
.moveElementTo( sName
, xDest
, sName
);
778 Error( "Element moving failed, exception: " + e
);
785 public boolean renameElement( XStorage xStorage
, String sOldName
, String sNewName
)
787 // rename element with name sOldName to sNewName
790 xStorage
.renameElement( sOldName
, sNewName
);
794 Error( "Element renaming failed, exception: " + e
);
801 public boolean removeElement( XStorage xStorage
, String sName
)
803 // remove element with name sName
806 xStorage
.removeElement( sName
);
810 Error( "Element removing failed, exception: " + e
);
817 public XStream
OpenStream( XStorage xStorage
,
821 // open substream element
822 XStream xSubStream
= null;
825 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, nMode
);
826 xSubStream
= UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
827 if ( xSubStream
== null )
828 Error( "Can't create substream '" + sStreamName
+ "'!" );
832 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
838 public boolean cantOpenStorage( XStorage xStorage
, String sName
)
840 // try to open an opened substorage, open call must fail
843 Object oDummyStorage
= xStorage
.openStorageElement( sName
, ElementModes
.READ
);
844 Error( "The trying to reopen opened substorage '" + sName
+ "' must fail!" );
854 public boolean cantOpenStream( XStorage xStorage
, String sName
, int nMode
)
856 // try to open the substream with specified mode must fail
859 Object oDummyStream
= xStorage
.openStreamElement( sName
, nMode
);
860 Error( "The trying to open substream '" + sName
+ "' must fail!" );
870 public void Error( String sError
)
872 System
.out
.println( m_sTestPrefix
+ "Error: " + sError
);
875 public void Message( String sError
)
877 System
.out
.println( m_sTestPrefix
+ sError
);
881 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */