1 package complex
.storages
;
3 import com
.sun
.star
.uno
.XInterface
;
4 import com
.sun
.star
.lang
.XMultiServiceFactory
;
5 import com
.sun
.star
.lang
.XSingleServiceFactory
;
6 import com
.sun
.star
.lang
.DisposedException
;
8 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
9 import com
.sun
.star
.uno
.UnoRuntime
;
10 import com
.sun
.star
.uno
.XInterface
;
12 import com
.sun
.star
.container
.XNameAccess
;
14 import com
.sun
.star
.embed
.*;
16 import share
.LogWriter
;
17 import complex
.storages
.TestHelper
;
18 import complex
.storages
.StorageTest
;
20 public class Test04
implements StorageTest
{
22 XMultiServiceFactory m_xMSF
;
23 XSingleServiceFactory m_xStorageFactory
;
24 TestHelper m_aTestHelper
;
26 public Test04( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
29 m_xStorageFactory
= xStorageFactory
;
30 m_aTestHelper
= new TestHelper( aLogWriter
, "Test04: " );
37 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
38 if ( sTempFileURL
== null || sTempFileURL
== "" )
40 m_aTestHelper
.Error( "No valid temporary file was created!" );
44 // create temporary storage based on arbitrary medium
45 // after such a storage is closed it is lost
46 Object oTempStorage
= m_xStorageFactory
.createInstance();
47 XStorage xTempStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
48 if ( xTempStorage
== null )
50 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
54 // open substorages and create streams there
56 // first substorage of the root storage
57 XStorage xTempSubStorage1
= m_aTestHelper
.openSubStorage( xTempStorage
,
60 if ( xTempSubStorage1
== null )
62 m_aTestHelper
.Error( "Can't create substorage!" );
66 byte pBigBytes
[] = new byte[33000];
67 for ( int nInd
= 0; nInd
< 33000; nInd
++ )
68 pBigBytes
[nInd
] = (byte)( nInd
% 128 );
70 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
71 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage1
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
74 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
76 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
77 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage1
, "SubStream1", "MediaType1", true, pBytes1
) )
80 // second substorage of the root storage
81 XStorage xTempSubStorage2
= m_aTestHelper
.openSubStorage( xTempStorage
,
84 if ( xTempSubStorage2
== null )
86 m_aTestHelper
.Error( "Can't create substorage!" );
90 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage2
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
94 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
96 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
97 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage2
, "SubStream2", "MediaType2", false, pBytes2
) )
100 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
101 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
104 ElementModes
.WRITE
) )
107 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
108 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage1
,
111 ElementModes
.WRITE
) )
114 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
115 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage2
,
118 ElementModes
.WRITE
) )
121 // create temporary storage based on a previously created temporary file
122 Object pArgs
[] = new Object
[2];
123 pArgs
[0] = (Object
) sTempFileURL
;
124 pArgs
[1] = new Integer( ElementModes
.WRITE
);
126 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
127 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
128 if ( xTempFileStorage
== null )
130 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
134 if ( !m_aTestHelper
.copyElementTo( xTempStorage
, "SubStorage1", xTempFileStorage
) )
137 // if storage is not commited before disposing all the changes will be lost
138 if ( !m_aTestHelper
.commitStorage( xTempSubStorage2
) )
141 // a storage must be disposed before moving/removing otherwise the access will be denied
142 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage2
) )
145 if ( !m_aTestHelper
.moveElementTo( xTempStorage
, "SubStorage2", xTempFileStorage
) )
148 // SubStorage2 must be removed and disposed now
151 xTempSubStorage2
.isStreamElement( "SubStream2" );
152 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
155 catch( com
.sun
.star
.lang
.DisposedException de
)
160 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
164 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
167 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "SubStream1", "SubStream1_copy" ) )
170 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
173 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "BigSubStream1", xTempFileStorage
) )
176 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "BigSubStream1", "BigSubStream1_copy" ) )
179 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "BigSubStream1", xTempFileStorage
) )
182 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
185 // dispose used storages to free resources
186 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
189 // ================================================
190 // now check all the written and copied information
191 // ================================================
193 // the temporary file must not be locked any more after storage disposing
194 pArgs
[1] = new Integer( ElementModes
.WRITE
);
195 Object oResStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
196 XStorage xResStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResStorage
);
197 if ( xResStorage
== null )
199 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
203 // open and check SubStorage1
204 XStorage xResSubStorage1
= m_aTestHelper
.openSubStorage( xResStorage
,
207 if ( xResSubStorage1
== null )
209 m_aTestHelper
.Error( "Can't open existing substorage!" );
213 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage1
, "MediaType4", false, ElementModes
.READ
) )
217 // open and check SubStorage2
218 XStorage xResSubStorage2
= m_aTestHelper
.openSubStorage( xResStorage
,
221 if ( xResSubStorage2
== null )
223 m_aTestHelper
.Error( "Can't open existing substorage!" );
227 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage2
, "MediaType5", false, ElementModes
.READ
) )
231 // check all the result streams
233 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
236 if ( !m_aTestHelper
.checkStream( xResStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
239 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1_copy", "MediaType1", true, pBytes1
) )
242 if ( !m_aTestHelper
.checkStream( xResStorage
, "BigSubStream1_copy", "MediaType1", true, pBigBytes
) )
245 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "SubStream1", "MediaType1", true, pBytes1
) )
248 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
251 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "SubStream2", "MediaType2", false, pBytes2
) )
254 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
257 // the storage must be disposed before removing
258 if ( !m_aTestHelper
.disposeStorage( xResSubStorage2
) )
261 // remove element and check that it was removed completelly
262 if ( !m_aTestHelper
.removeElement( xResStorage
, "SubStorage2" ) )
267 XNameAccess xResAccess
= (XNameAccess
) UnoRuntime
.queryInterface( XNameAccess
.class, xResStorage
);
268 if ( xResAccess
.hasByName( "SubStorage2" ) )
269 m_aTestHelper
.Error( "SubStorage2 must be removed already!" );
273 m_aTestHelper
.Error( "Can't get access to root storage, exception: " + e
);
279 xResSubStorage2
.isStreamElement( "SubStream2" );
281 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
284 catch( com
.sun
.star
.lang
.DisposedException de
)
289 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
293 // dispose used storages to free resources
294 if ( !m_aTestHelper
.disposeStorage( xResStorage
) )
301 m_aTestHelper
.Error( "Exception: " + e
);