tdf#130857 qt weld: Introduce QtInstanceScrolledWindow
[LibreOffice.git] / odk / examples / java / Storage / TestHelper.java
blob3fac93e96f84eb8a2f478aee9bdb088291409831
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 {
31 String m_sTestPrefix;
33 public TestHelper( String sTestPrefix )
35 m_sTestPrefix = sTestPrefix;
38 public boolean WriteBytesToStream( XStream xStream,
39 String sStreamName,
40 String sMediaType,
41 boolean bCompressed,
42 byte[] pBytes )
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 + "'!" );
49 return false;
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 + "'!" );
57 return false;
60 // write requested byte sequence
61 try
63 xTruncate.truncate();
64 xOutput.writeBytes( pBytes );
66 catch( Exception e )
68 Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
69 return false;
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 + "'!" );
77 return false;
80 // set properties to the stream
81 try
83 xPropSet.setPropertyValue( "MediaType", sMediaType );
84 xPropSet.setPropertyValue( "Compressed", Boolean.valueOf( bCompressed ) );
86 catch( Exception e )
88 Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
89 return false;
92 // check size property of the stream
93 try
95 int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
96 if ( nSize != pBytes.length )
98 Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
99 return false;
102 catch( Exception e )
104 Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
105 return false;
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 + "'!" );
113 return false;
115 xComponent.dispose();
117 return true;
121 public boolean WriteBytesToSubstream( XStorage xStorage,
122 String sStreamName,
123 String sMediaType,
124 boolean bCompressed,
125 byte[] pBytes )
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 + "'!" );
136 return false;
139 catch( Exception e )
141 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
142 return false;
145 return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
148 public boolean WriteBytesToEncrSubstream( XStorage xStorage,
149 String sStreamName,
150 String sMediaType,
151 boolean bCompressed,
152 byte[] pBytes,
153 byte[] pPass )
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 + "'!" );
164 return false;
167 catch( Exception e )
169 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
170 return false;
173 return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
176 public boolean WBToSubstrOfEncr( XStorage xStorage,
177 String sStreamName,
178 String sMediaType,
179 boolean bCompressed,
180 byte[] pBytes,
181 boolean bEncrypted )
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 + "'!" );
192 return false;
195 catch( Exception e )
197 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
198 return false;
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 + "'!" );
206 return false;
209 // set properties to the stream
212 xPropSet.setPropertyValue( "Encrypted", Boolean.valueOf( bEncrypted ) );
214 catch( Exception e )
216 Error( "Can't set 'Encrypted' property to substream '" + sStreamName + "', exception: " + e );
217 return false;
220 return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
223 public int ChangeStreamPass( XStorage xStorage,
224 String sStreamName,
225 byte[] pOldPass,
226 byte[] pNewPass )
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 + "'!" );
237 return 0;
240 catch( Exception e )
242 Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
243 return 0;
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!" );
254 return -1;
257 try {
258 xStreamEncryption.setEncryptionPassword( new String(pNewPass) );
260 catch( Exception e )
262 Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
263 return 0;
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 + "'!" );
271 return 0;
273 xComponent.dispose();
275 return 1;
278 public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
280 boolean bOk = false;
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" ) );
295 bOk = true;
296 if ( bPropIsRoot != bIsRoot )
298 Error( "'IsRoot' property contains wrong value!" );
299 bOk = false;
302 if ( ( bIsRoot && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
303 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
305 Error( "'OpenMode' property contains wrong value!" );
306 bOk = false;
309 catch( Exception e )
311 Error( "Can't control properties of substorage, exception: " + e );
314 else
316 Error( "Can't get XPropertySet implementation from storage!" );
319 return bOk;
322 public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
324 boolean bOk = false;
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" ) );
337 bOk = true;
338 if ( !sPropMediaType.equals( sMediaType ) )
340 Error( "'MediaType' property contains wrong value, expected '"
341 + sMediaType + "', set '" + sPropMediaType + "' !" );
342 bOk = false;
345 if ( bPropIsRoot != bIsRoot )
347 Error( "'IsRoot' property contains wrong value!" );
348 bOk = false;
351 if ( ( bIsRoot && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
352 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
354 Error( "'OpenMode' property contains wrong value!" );
355 bOk = false;
358 catch( Exception e )
360 Error( "Can't get properties of substorage, exception: " + e );
363 else
365 Error( "Can't get XPropertySet implementation from storage!" );
368 return bOk;
371 public boolean InternalCheckStream( XStream xStream,
372 String sName,
373 String sMediaType,
374 byte[] pBytes )
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 + "'!" );
381 return false;
384 byte pContents[][] = new byte[1][]; // ???
386 // read contents
389 xInput.readBytes( pContents, pBytes.length + 1 );
391 catch( Exception e )
393 Error( "Can't read from stream '" + sName + "', exception: " + e );
394 return false;
397 // check size of stream data
398 if ( pContents.length == 0 )
400 Error( "SubStream '" + sName + "' reading produced disaster!" );
401 return false;
404 if ( pBytes.length != pContents[0].length )
406 Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
407 return false;
410 // check stream data
411 for ( int ind = 0; ind < pBytes.length; ind++ )
413 if ( pBytes[ind] != pContents[0][ind] )
415 Error( "SubStream '" + sName + "' contains wrong data!" );
416 return false;
421 // check properties
422 boolean bOk = false;
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" ) );
434 bOk = true;
435 if ( !sPropMediaType.equals( sMediaType ) )
437 Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
438 + sMediaType + "', set: '" + sPropMediaType + "'!" );
439 bOk = false;
442 if ( nPropSize != pBytes.length )
444 Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
445 bOk = false;
448 catch( Exception e )
450 Error( "Can't get properties of substream '" + sName + "', exception: " + e );
453 else
455 Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
458 return bOk;
461 public boolean checkStream( XStorage xParentStorage,
462 String sName,
463 String sMediaType,
464 byte[] pBytes )
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 + "'!" );
475 return false;
478 catch( Exception e )
480 Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
481 return false;
484 return InternalCheckStream( xSubStream, sName, sMediaType, pBytes );
487 public boolean checkEncrStream( XStorage xParentStorage,
488 String sName,
489 String sMediaType,
490 byte[] pBytes,
491 byte[] pPass )
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!" );
499 return false;
504 xParentStorage.openStreamElement( sName, ElementModes.READ );
505 Error( "Encrypted stream '" + sName + "' was opened without password!" );
506 return false;
508 catch( WrongPasswordException wpe )
510 catch( Exception e )
512 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
513 return false;
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!" );
522 return false;
524 catch( WrongPasswordException wpe )
526 catch( Exception e )
528 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
529 return false;
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 + "'!" );
540 return false;
543 catch( Exception e )
545 Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
546 return false;
549 return InternalCheckStream( xSubStream, sName, sMediaType, pBytes );
552 public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
554 // copy xSourceStorage to xDestStorage
557 xSourceStorage.copyToStorage( xDestStorage );
559 catch( Exception e )
561 Error( "Storage copying failed, exception: " + e );
562 return false;
565 return true;
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!" );
575 return false;
580 xTransact.commit();
582 catch( Exception e )
584 Error( "Storage commit failed, exception:" + e );
585 return false;
588 return true;
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!" );
598 return false;
603 xComponent.dispose();
605 catch( Exception e )
607 Error( "Storage disposing failed!" );
608 return false;
611 return true;
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 );
628 return xInTemp;
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!" );
640 return false;
643 catch ( Exception e )
645 Error( "Can't get the output part of a stream, exception :" + e );
646 return false;
651 xOutTemp.closeOutput();
653 catch ( Exception e )
655 Error( "Can't close output part of a stream, exception :" + e );
656 return false;
659 return true;
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 );
669 return xSubStorage;
671 catch( Exception e )
673 Error( "Can't open substorage '" + sName + "', exception: " + e );
676 return null;
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 );
688 catch( Exception e )
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 );
708 catch( Exception e )
711 if ( xTempFileProps != null )
715 xTempFileProps.setPropertyValue( "RemoveFile", Boolean.FALSE );
716 sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
718 catch( Exception e )
720 Error( "Can't control TempFile properties, exception: " + e );
723 else
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();
735 if ( xOut != null )
736 xOut.closeOutput();
738 XInputStream xIn = xStream.getInputStream();
739 if ( xIn != null )
740 xIn.closeInput();
742 else
743 Error( "Can't close TempFile!" );
745 catch( Exception e )
747 Error( "Can't close TempFile, exception: " + e );
750 return sResult;
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 );
760 catch( Exception e )
762 Error( "Element copying failed, exception: " + e );
763 return false;
766 return true;
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 );
776 catch( Exception e )
778 Error( "Element moving failed, exception: " + e );
779 return false;
782 return true;
785 public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
787 // rename element with name sOldName to sNewName
790 xStorage.renameElement( sOldName, sNewName );
792 catch( Exception e )
794 Error( "Element renaming failed, exception: " + e );
795 return false;
798 return true;
801 public boolean removeElement( XStorage xStorage, String sName )
803 // remove element with name sName
806 xStorage.removeElement( sName );
808 catch( Exception e )
810 Error( "Element removing failed, exception: " + e );
811 return false;
814 return true;
817 public XStream OpenStream( XStorage xStorage,
818 String sStreamName,
819 int nMode )
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 + "'!" );
830 catch( Exception e )
832 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
835 return xSubStream;
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!" );
846 catch( Exception e )
848 return true;
851 return false;
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!" );
862 catch( Exception e )
864 return true;
867 return false;
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: */