xref: /AOO41X/main/odk/examples/java/Storage/TestHelper.java (revision ae15d43ae9bc0d57b88b38bfa728519a0f7db121)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package storagetesting;
23 
24 import com.sun.star.uno.UnoRuntime;
25 import com.sun.star.uno.XInterface;
26 import com.sun.star.uno.AnyConverter;
27 
28 import com.sun.star.lang.*;
29 import com.sun.star.embed.*;
30 import com.sun.star.packages.*;
31 import com.sun.star.io.*;
32 import com.sun.star.beans.*;
33 
34 public class TestHelper  {
35 
36     String m_sTestPrefix;
37 
TestHelper( String sTestPrefix )38     public TestHelper( String sTestPrefix )
39     {
40         m_sTestPrefix = sTestPrefix;
41     }
42 
WriteBytesToStream( XStream xStream, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes )43     public boolean WriteBytesToStream( XStream xStream,
44                                         String sStreamName,
45                                         String sMediaType,
46                                         boolean bCompressed,
47                                         byte[] pBytes )
48     {
49         // get output stream of substream
50         XOutputStream xOutput = xStream.getOutputStream();
51         if ( xOutput == null )
52         {
53             Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
54             return false;
55         }
56 
57         // get XTrucate implementation from output stream
58         XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
59         if ( xTruncate == null )
60         {
61             Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
62             return false;
63         }
64 
65         // write requested byte sequence
66         try
67         {
68             xTruncate.truncate();
69             xOutput.writeBytes( pBytes );
70         }
71         catch( Exception e )
72         {
73             Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
74             return false;
75         }
76 
77         // get access to the XPropertySet interface
78         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
79         if ( xPropSet == null )
80         {
81             Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
82             return false;
83         }
84 
85         // set properties to the stream
86         try
87         {
88             xPropSet.setPropertyValue( "MediaType", sMediaType );
89             xPropSet.setPropertyValue( "Compressed", new Boolean( bCompressed ) );
90         }
91         catch( Exception e )
92         {
93             Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
94             return false;
95         }
96 
97         // check size property of the stream
98         try
99         {
100             int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
101             if ( nSize != pBytes.length )
102             {
103                 Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
104                 return false;
105             }
106         }
107         catch( Exception e )
108         {
109             Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
110             return false;
111         }
112 
113         // free the stream resources, garbage collector may remove the object too late
114         XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStream );
115         if ( xComponent == null )
116         {
117             Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
118             return false;
119         }
120         xComponent.dispose();
121 
122         return true;
123     }
124 
125 
WriteBytesToSubstream( XStorage xStorage, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes )126     public boolean WriteBytesToSubstream( XStorage xStorage,
127                                           String sStreamName,
128                                           String sMediaType,
129                                           boolean bCompressed,
130                                           byte[] pBytes )
131     {
132         // open substream element
133         XStream xSubStream = null;
134         try
135         {
136             Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.ELEMENT_WRITE );
137             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
138             if ( xSubStream == null )
139             {
140                 Error( "Can't create substream '" + sStreamName + "'!" );
141                 return false;
142             }
143         }
144         catch( Exception e )
145         {
146             Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
147             return false;
148         }
149 
150         return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
151     }
152 
WriteBytesToEncrSubstream( XStorage xStorage, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes, byte[] pPass )153     public boolean WriteBytesToEncrSubstream( XStorage xStorage,
154                                               String sStreamName,
155                                               String sMediaType,
156                                               boolean bCompressed,
157                                               byte[] pBytes,
158                                               byte[] pPass )
159     {
160         // open substream element
161         XStream xSubStream = null;
162         try
163         {
164             Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.ELEMENT_WRITE, pPass );
165             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
166             if ( xSubStream == null )
167             {
168                 Error( "Can't create substream '" + sStreamName + "'!" );
169                 return false;
170             }
171         }
172         catch( Exception e )
173         {
174             Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
175             return false;
176         }
177 
178         return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
179     }
180 
WBToSubstrOfEncr( XStorage xStorage, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes, boolean bEncrypted )181     public boolean WBToSubstrOfEncr( XStorage xStorage,
182                                               String sStreamName,
183                                               String sMediaType,
184                                               boolean bCompressed,
185                                               byte[] pBytes,
186                                               boolean bEncrypted )
187     {
188         // open substream element
189         XStream xSubStream = null;
190         try
191         {
192             Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.ELEMENT_WRITE );
193             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
194             if ( xSubStream == null )
195             {
196                 Error( "Can't create substream '" + sStreamName + "'!" );
197                 return false;
198             }
199         }
200         catch( Exception e )
201         {
202             Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
203             return false;
204         }
205 
206         // get access to the XPropertySet interface
207         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
208         if ( xPropSet == null )
209         {
210             Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
211             return false;
212         }
213 
214         // set properties to the stream
215         try
216         {
217             xPropSet.setPropertyValue( "Encrypted", new Boolean( bEncrypted ) );
218         }
219         catch( Exception e )
220         {
221             Error( "Can't set 'Encrypted' property to substream '" + sStreamName + "', exception: " + e );
222             return false;
223         }
224 
225         return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
226     }
227 
ChangeStreamPass( XStorage xStorage, String sStreamName, byte[] pOldPass, byte[] pNewPass )228     public int ChangeStreamPass( XStorage xStorage,
229                                  String sStreamName,
230                                  byte[] pOldPass,
231                                  byte[] pNewPass )
232     {
233         // open substream element
234         XStream xSubStream = null;
235         try
236         {
237             Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.ELEMENT_WRITE, pOldPass );
238             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
239             if ( xSubStream == null )
240             {
241                 Error( "Can't open substream '" + sStreamName + "'!" );
242                 return 0;
243             }
244         }
245         catch( Exception e )
246         {
247             Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
248             return 0;
249         }
250 
251 
252         // change the password for the stream
253         XEncryptionProtectedSource xStreamEncryption =
254                 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
255 
256         if ( xStreamEncryption == null )
257         {
258             Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
259             return -1;
260         }
261 
262         try {
263             xStreamEncryption.setEncryptionKey( pNewPass );
264         }
265         catch( Exception e )
266         {
267             Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
268             return 0;
269         }
270 
271         // free the stream resources, garbage collector may remove the object too late
272         XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xSubStream );
273         if ( xComponent == null )
274         {
275             Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
276             return 0;
277         }
278         xComponent.dispose();
279 
280         return 1;
281     }
282 
setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )283     public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
284     {
285         boolean bOk = false;
286 
287         // get access to the XPropertySet interface
288         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
289         if ( xPropSet != null )
290         {
291             try
292             {
293                 // set "MediaType" property to the stream
294                 xPropSet.setPropertyValue( "MediaType", sMediaType );
295 
296                 // get "IsRoot" and "OpenMode" properties and control there values
297                 boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
298                 int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
299 
300                 bOk = true;
301                 if ( bPropIsRoot != bIsRoot )
302                 {
303                     Error( "'IsRoot' property contains wrong value!" );
304                     bOk = false;
305                 }
306 
307                 if ( ( bIsRoot && ( nPropMode | ElementModes.ELEMENT_READ ) != ( nMode | ElementModes.ELEMENT_READ ) )
308                   || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
309                 {
310                     Error( "'OpenMode' property contains wrong value!" );
311                     bOk = false;
312                 }
313             }
314             catch( Exception e )
315             {
316                 Error( "Can't control properties of substorage, exception: " + e );
317             }
318         }
319         else
320         {
321             Error( "Can't get XPropertySet implementation from storage!" );
322         }
323 
324         return bOk;
325     }
326 
checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )327     public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
328     {
329         boolean bOk = false;
330 
331         // get access to the XPropertySet interface
332         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
333         if ( xPropSet != null )
334         {
335             try
336             {
337                 // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
338                 String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
339                 boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
340                 int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
341 
342                 bOk = true;
343                 if ( !sPropMediaType.equals( sMediaType ) )
344                 {
345                     Error( "'MediaType' property contains wrong value, expected '"
346                             + sMediaType + "', set '" + sPropMediaType + "' !" );
347                     bOk = false;
348                 }
349 
350                 if ( bPropIsRoot != bIsRoot )
351                 {
352                     Error( "'IsRoot' property contains wrong value!" );
353                     bOk = false;
354                 }
355 
356                 if ( ( bIsRoot && ( nPropMode | ElementModes.ELEMENT_READ ) != ( nMode | ElementModes.ELEMENT_READ ) )
357                   || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
358                 {
359                     Error( "'OpenMode' property contains wrong value!" );
360                     bOk = false;
361                 }
362             }
363             catch( Exception e )
364             {
365                 Error( "Can't get properties of substorage, exception: " + e );
366             }
367         }
368         else
369         {
370             Error( "Can't get XPropertySet implementation from storage!" );
371         }
372 
373         return bOk;
374     }
375 
InternalCheckStream( XStream xStream, String sName, String sMediaType, byte[] pBytes )376     public boolean InternalCheckStream( XStream xStream,
377                                         String sName,
378                                         String sMediaType,
379                                         byte[] pBytes )
380     {
381         // get input stream of substream
382         XInputStream xInput = xStream.getInputStream();
383         if ( xInput == null )
384         {
385             Error( "Can't get XInputStream implementation from substream '" + sName + "'!" );
386             return false;
387         }
388 
389         byte pContents[][] = new byte[1][]; // ???
390 
391         // read contents
392         try
393         {
394             xInput.readBytes( pContents, pBytes.length + 1 );
395         }
396         catch( Exception e )
397         {
398             Error( "Can't read from stream '" + sName + "', exception: " + e );
399             return false;
400         }
401 
402         // check size of stream data
403         if ( pContents.length == 0 )
404         {
405             Error( "SubStream '" + sName + "' reading produced disaster!"  );
406             return false;
407         }
408 
409         if ( pBytes.length != pContents[0].length )
410         {
411             Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
412             return false;
413         }
414 
415         // check stream data
416         for ( int ind = 0; ind < pBytes.length; ind++ )
417         {
418             if ( pBytes[ind] != pContents[0][ind] )
419             {
420                 Error( "SubStream '" + sName + "' contains wrong data!" );
421                 return false;
422             }
423         }
424 
425 
426         // check properties
427         boolean bOk = false;
428 
429         // get access to the XPropertySet interface
430         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
431         if ( xPropSet != null )
432         {
433             try
434             {
435                 // get "MediaType" and "Size" properties and control there values
436                 String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
437                 int nPropSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
438 
439                 bOk = true;
440                 if ( !sPropMediaType.equals( sMediaType ) )
441                 {
442                     Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
443                             + sMediaType + "', set: '" + sPropMediaType + "'!" );
444                     bOk = false;
445                 }
446 
447                 if ( nPropSize != pBytes.length )
448                 {
449                     Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
450                     bOk = false;
451                 }
452             }
453             catch( Exception e )
454             {
455                 Error( "Can't get properties of substream '" + sName + "', exception: " + e );
456             }
457         }
458         else
459         {
460             Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
461         }
462 
463         return bOk;
464     }
465 
checkStream( XStorage xParentStorage, String sName, String sMediaType, byte[] pBytes )466     public boolean checkStream( XStorage xParentStorage,
467                                 String sName,
468                                 String sMediaType,
469                                 byte[] pBytes )
470     {
471         // open substream element first
472         XStream xSubStream = null;
473         try
474         {
475             Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.ELEMENT_READ );
476             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
477             if ( xSubStream == null )
478             {
479                 Error( "Can't open substream '" + sName + "'!" );
480                 return false;
481             }
482         }
483         catch( Exception e )
484         {
485             Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
486             return false;
487         }
488 
489         return InternalCheckStream( xSubStream, sName, sMediaType, pBytes );
490     }
491 
checkEncrStream( XStorage xParentStorage, String sName, String sMediaType, byte[] pBytes, byte[] pPass )492     public boolean checkEncrStream( XStorage xParentStorage,
493                                     String sName,
494                                     String sMediaType,
495                                     byte[] pBytes,
496                                     byte[] pPass )
497     {
498         // Important: a common password for any of parent storage should not be set or
499         //            should be different from pPass
500 
501         if ( pPass.length == 0 )
502         {
503             Error( "Wrong password is used in the test!" );
504             return false;
505         }
506 
507         try
508         {
509             Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.ELEMENT_READ );
510             Error( "Encrypted stream '" + sName + "' was opened without password!" );
511             return false;
512         }
513         catch( WrongPasswordException wpe )
514         {}
515         catch( Exception e )
516         {
517             Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
518             return false;
519         }
520 
521         byte pWrongPass[] = { 1, 1 };
522         pWrongPass[0] += pPass[0];
523         try
524         {
525             Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.ELEMENT_READ, pWrongPass );
526             Error( "Encrypted stream '" + sName + "' was opened with wrong password!" );
527             return false;
528         }
529         catch( WrongPasswordException wpe )
530         {}
531         catch( Exception e )
532         {
533             Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
534             return false;
535         }
536 
537         XStream xSubStream = null;
538         try
539         {
540             Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.ELEMENT_READ, pPass );
541             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
542             if ( xSubStream == null )
543             {
544                 Error( "Can't open encrypted substream '" + sName + "'!" );
545                 return false;
546             }
547         }
548         catch( Exception e )
549         {
550             Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
551             return false;
552         }
553 
554         return InternalCheckStream( xSubStream, sName, sMediaType, pBytes );
555     }
556 
copyStorage( XStorage xSourceStorage, XStorage xDestStorage )557     public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
558     {
559         // copy xSourceStorage to xDestStorage
560         try
561         {
562             xSourceStorage.copyToStorage( xDestStorage );
563         }
564         catch( Exception e )
565         {
566             Error( "Storage copying failed, exception: " + e );
567             return false;
568         }
569 
570         return true;
571     }
572 
commitStorage( XStorage xStorage )573     public boolean commitStorage( XStorage xStorage )
574     {
575         // XTransactedObject must be supported by storages
576         XTransactedObject xTransact = (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xStorage );
577         if ( xTransact == null )
578         {
579             Error( "Storage doesn't implement transacted access!" );
580             return false;
581         }
582 
583         try
584         {
585             xTransact.commit();
586         }
587         catch( Exception e )
588         {
589             Error( "Storage commit failed, exception:" + e );
590             return false;
591         }
592 
593         return true;
594     }
595 
disposeStorage( XStorage xStorage )596     public boolean disposeStorage( XStorage xStorage )
597     {
598         // dispose the storage
599         XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStorage );
600         if ( xComponent == null )
601         {
602             Error( "Can't retrieve XComponent implementation from storage!" );
603             return false;
604         }
605 
606         try
607         {
608             xComponent.dispose();
609         }
610         catch( Exception e )
611         {
612             Error( "Storage disposing failed!" );
613             return false;
614         }
615 
616         return true;
617     }
618 
getInputStream( XStream xStream )619     public XInputStream getInputStream( XStream xStream )
620     {
621         XInputStream xInTemp = null;
622         try
623         {
624             xInTemp = xStream.getInputStream();
625             if ( xInTemp == null )
626                 Error( "Can't get the input part of a stream!" );
627         }
628         catch ( Exception e )
629         {
630             Error( "Can't get the input part of a stream, exception :" + e );
631         }
632 
633         return xInTemp;
634     }
635 
closeOutput( XStream xStream )636     public boolean closeOutput( XStream xStream )
637     {
638         XOutputStream xOutTemp = null;
639         try
640         {
641             xOutTemp = xStream.getOutputStream();
642             if ( xOutTemp == null )
643             {
644                 Error( "Can't get the output part of a stream!" );
645                 return false;
646             }
647         }
648         catch ( Exception e )
649         {
650             Error( "Can't get the output part of a stream, exception :" + e );
651             return false;
652         }
653 
654         try
655         {
656             xOutTemp.closeOutput();
657         }
658         catch ( Exception e )
659         {
660             Error( "Can't close output part of a stream, exception :" + e );
661             return false;
662         }
663 
664         return true;
665     }
666 
openSubStorage( XStorage xStorage, String sName, int nMode )667     public XStorage openSubStorage( XStorage xStorage, String sName, int nMode )
668     {
669         // open existing substorage
670         try
671         {
672             Object oSubStorage = xStorage.openStorageElement( sName, nMode );
673             XStorage xSubStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oSubStorage );
674             return xSubStorage;
675         }
676         catch( Exception e )
677         {
678             Error( "Can't open substorage '" + sName + "', exception: " + e );
679         }
680 
681         return null;
682     }
683 
CreateTempFileStream( XMultiServiceFactory xMSF )684     public XStream CreateTempFileStream( XMultiServiceFactory xMSF )
685     {
686         // try to get temporary file representation
687         XStream xTempFileStream = null;
688         try
689         {
690             Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
691             xTempFileStream = (XStream)UnoRuntime.queryInterface( XStream.class, oTempFile );
692         }
693         catch( Exception e )
694         {}
695 
696         if ( xTempFileStream == null )
697             Error( "Can't create temporary file!" );
698 
699         return xTempFileStream;
700     }
701 
CreateTempFile( XMultiServiceFactory xMSF )702     public String CreateTempFile( XMultiServiceFactory xMSF )
703     {
704         String sResult = null;
705 
706         // try to get temporary file representation
707         XPropertySet xTempFileProps = null;
708         try
709         {
710             Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
711             xTempFileProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oTempFile );
712         }
713         catch( Exception e )
714         {}
715 
716         if ( xTempFileProps != null )
717         {
718             try
719             {
720                 xTempFileProps.setPropertyValue( "RemoveFile", new Boolean( false ) );
721                 sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
722             }
723             catch( Exception e )
724             {
725                 Error( "Can't control TempFile properties, exception: " + e );
726             }
727         }
728         else
729         {
730             Error( "Can't create temporary file representation!" );
731         }
732 
733         // close temporary file explicitly
734         try
735         {
736             XStream xStream = (XStream)UnoRuntime.queryInterface( XStream.class, xTempFileProps );
737             if ( xStream != null )
738             {
739                 XOutputStream xOut = xStream.getOutputStream();
740                 if ( xOut != null )
741                     xOut.closeOutput();
742 
743                 XInputStream xIn = xStream.getInputStream();
744                 if ( xIn != null )
745                     xIn.closeInput();
746             }
747             else
748                 Error( "Can't close TempFile!" );
749         }
750         catch( Exception e )
751         {
752             Error( "Can't close TempFile, exception: " + e );
753         }
754 
755         return sResult;
756     }
757 
copyElementTo( XStorage xSource, String sName, XStorage xDest )758     public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest )
759     {
760         // copy element with name sName from xSource to xDest
761         try
762         {
763             xSource.copyElementTo( sName, xDest, sName );
764         }
765         catch( Exception e )
766         {
767             Error( "Element copying failed, exception: " + e );
768             return false;
769         }
770 
771         return true;
772     }
773 
moveElementTo( XStorage xSource, String sName, XStorage xDest )774     public boolean moveElementTo( XStorage xSource, String sName, XStorage xDest )
775     {
776         // move element with name sName from xSource to xDest
777         try
778         {
779             xSource.moveElementTo( sName, xDest, sName );
780         }
781         catch( Exception e )
782         {
783             Error( "Element moving failed, exception: " + e );
784             return false;
785         }
786 
787         return true;
788     }
789 
renameElement( XStorage xStorage, String sOldName, String sNewName )790     public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
791     {
792         // rename element with name sOldName to sNewName
793         try
794         {
795             xStorage.renameElement( sOldName, sNewName );
796         }
797         catch( Exception e )
798         {
799             Error( "Element renaming failed, exception: " + e );
800             return false;
801         }
802 
803         return true;
804     }
805 
removeElement( XStorage xStorage, String sName )806     public boolean removeElement( XStorage xStorage, String sName )
807     {
808         // remove element with name sName
809         try
810         {
811             xStorage.removeElement( sName );
812         }
813         catch( Exception e )
814         {
815             Error( "Element removing failed, exception: " + e );
816             return false;
817         }
818 
819         return true;
820     }
821 
OpenStream( XStorage xStorage, String sStreamName, int nMode )822     public XStream OpenStream( XStorage xStorage,
823                                 String sStreamName,
824                                 int nMode )
825     {
826         // open substream element
827         XStream xSubStream = null;
828         try
829         {
830             Object oSubStream = xStorage.openStreamElement( sStreamName, nMode );
831             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
832             if ( xSubStream == null )
833                 Error( "Can't create substream '" + sStreamName + "'!" );
834         }
835         catch( Exception e )
836         {
837             Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
838         }
839 
840         return xSubStream;
841     }
842 
cantOpenStorage( XStorage xStorage, String sName )843     public boolean cantOpenStorage( XStorage xStorage, String sName )
844     {
845         // try to open an opened substorage, open call must fail
846         try
847         {
848             Object oDummyStorage = xStorage.openStorageElement( sName, ElementModes.ELEMENT_READ );
849             Error( "The trying to reopen opened substorage '" + sName + "' must fail!" );
850         }
851         catch( Exception e )
852         {
853             return true;
854         }
855 
856         return false;
857     }
858 
cantOpenStream( XStorage xStorage, String sName, int nMode )859     public boolean cantOpenStream( XStorage xStorage, String sName, int nMode )
860     {
861         // try to open the substream with specified mode must fail
862         try
863         {
864             Object oDummyStream = xStorage.openStreamElement( sName, nMode );
865             Error( "The trying to open substoream '" + sName + "' must fail!" );
866         }
867         catch( Exception e )
868         {
869             return true;
870         }
871 
872         return false;
873     }
874 
Error( String sError )875     public void Error( String sError )
876     {
877         System.out.println( m_sTestPrefix + "Error: " + sError );
878     }
879 
Message( String sError )880     public void Message( String sError )
881     {
882         System.out.println( m_sTestPrefix + sError );
883     }
884 }
885 
886