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 pBytes1
[] = { 1, 1, 1, 1, 1 };
68 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
69 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage1
, "SubStream1", "MediaType1", true, pBytes1
) )
72 // second substorage of the root storage
73 XStorage xTempSubStorage2
= m_aTestHelper
.openSubStorage( xTempStorage
,
76 if ( xTempSubStorage2
== null )
78 m_aTestHelper
.Error( "Can't create substorage!" );
82 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
84 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
85 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage2
, "SubStream2", "MediaType2", false, pBytes2
) )
88 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
89 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
92 ElementModes
.WRITE
) )
95 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
96 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage1
,
99 ElementModes
.WRITE
) )
102 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
103 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage2
,
106 ElementModes
.WRITE
) )
109 // create temporary storage based on a previously created temporary file
110 Object pArgs
[] = new Object
[2];
111 pArgs
[0] = (Object
) sTempFileURL
;
112 pArgs
[1] = new Integer( ElementModes
.WRITE
);
114 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
115 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
116 if ( xTempFileStorage
== null )
118 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
122 if ( !m_aTestHelper
.copyElementTo( xTempStorage
, "SubStorage1", xTempFileStorage
) )
125 // if storage is not commited before disposing all the changes will be lost
126 if ( !m_aTestHelper
.commitStorage( xTempSubStorage2
) )
129 // a storage must be disposed before moving/removing otherwise the access will be denied
130 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage2
) )
133 if ( !m_aTestHelper
.moveElementTo( xTempStorage
, "SubStorage2", xTempFileStorage
) )
136 // SubStorage2 must be removed and disposed now
139 xTempSubStorage2
.isStreamElement( "SubStream2" );
140 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
143 catch( com
.sun
.star
.lang
.DisposedException de
)
148 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
152 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
155 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "SubStream1", "SubStream1_copy" ) )
158 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
161 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
164 // dispose used storages to free resources
165 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
168 // ================================================
169 // now check all the written and copied information
170 // ================================================
172 // the temporary file must not be locked any more after storage disposing
173 pArgs
[1] = new Integer( ElementModes
.WRITE
);
174 Object oResStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
175 XStorage xResStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResStorage
);
176 if ( xResStorage
== null )
178 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
182 // open and check SubStorage1
183 XStorage xResSubStorage1
= m_aTestHelper
.openSubStorage( xResStorage
,
186 if ( xResSubStorage1
== null )
188 m_aTestHelper
.Error( "Can't open existing substorage!" );
192 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage1
, "MediaType4", false, ElementModes
.READ
) )
196 // open and check SubStorage2
197 XStorage xResSubStorage2
= m_aTestHelper
.openSubStorage( xResStorage
,
200 if ( xResSubStorage2
== null )
202 m_aTestHelper
.Error( "Can't open existing substorage!" );
206 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage2
, "MediaType5", false, ElementModes
.READ
) )
210 // check all the result streams
212 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
215 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1_copy", "MediaType1", true, pBytes1
) )
218 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "SubStream1", "MediaType1", true, pBytes1
) )
221 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "SubStream2", "MediaType2", false, pBytes2
) )
224 // the storage must be disposed before removing
225 if ( !m_aTestHelper
.disposeStorage( xResSubStorage2
) )
228 // remove element and check that it was removed completelly
229 if ( !m_aTestHelper
.removeElement( xResStorage
, "SubStorage2" ) )
234 XNameAccess xResAccess
= (XNameAccess
) UnoRuntime
.queryInterface( XNameAccess
.class, xResStorage
);
235 if ( xResAccess
.hasByName( "SubStorage2" ) )
236 m_aTestHelper
.Error( "SubStorage2 must be removed already!" );
240 m_aTestHelper
.Error( "Can't get access to root storage, exception: " + e
);
246 xResSubStorage2
.isStreamElement( "SubStream2" );
248 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
251 catch( com
.sun
.star
.lang
.DisposedException de
)
256 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
260 // dispose used storages to free resources
261 if ( !m_aTestHelper
.disposeStorage( xResStorage
) )
268 m_aTestHelper
.Error( "Exception: " + e
);