1 package storagetesting
;
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 storagetesting
.TestHelper
;
17 import storagetesting
.StorageTest
;
19 public class Test04
implements StorageTest
{
21 XMultiServiceFactory m_xMSF
;
22 XSingleServiceFactory m_xStorageFactory
;
23 TestHelper m_aTestHelper
;
25 public Test04( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
28 m_xStorageFactory
= xStorageFactory
;
29 m_aTestHelper
= new TestHelper( "Test04: " );
36 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
37 if ( sTempFileURL
== null || sTempFileURL
== "" )
39 m_aTestHelper
.Error( "No valid temporary file was created!" );
43 // create temporary storage based on arbitrary medium
44 // after such a storage is closed it is lost
45 Object oTempStorage
= m_xStorageFactory
.createInstance();
46 XStorage xTempStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
47 if ( xTempStorage
== null )
49 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
53 // open substorages and create streams there
55 // first substorage of the root storage
56 XStorage xTempSubStorage1
= m_aTestHelper
.openSubStorage( xTempStorage
,
58 ElementModes
.ELEMENT_WRITE
);
59 if ( xTempSubStorage1
== null )
61 m_aTestHelper
.Error( "Can't create substorage!" );
65 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
67 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
68 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage1
, "SubStream1", "MediaType1", true, pBytes1
) )
71 // second substorage of the root storage
72 XStorage xTempSubStorage2
= m_aTestHelper
.openSubStorage( xTempStorage
,
74 ElementModes
.ELEMENT_WRITE
);
75 if ( xTempSubStorage2
== null )
77 m_aTestHelper
.Error( "Can't create substorage!" );
81 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
83 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
84 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage2
, "SubStream2", "MediaType2", false, pBytes2
) )
87 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
88 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
91 ElementModes
.ELEMENT_READWRITE
) )
94 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
95 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage1
,
98 ElementModes
.ELEMENT_WRITE
) )
101 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
102 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage2
,
105 ElementModes
.ELEMENT_WRITE
) )
108 // create temporary storage based on a previously created temporary file
109 Object pArgs
[] = new Object
[2];
110 pArgs
[0] = (Object
) sTempFileURL
;
111 pArgs
[1] = new Integer( ElementModes
.ELEMENT_WRITE
);
113 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
114 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
115 if ( xTempFileStorage
== null )
117 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
121 if ( !m_aTestHelper
.copyElementTo( xTempStorage
, "SubStorage1", xTempFileStorage
) )
124 // if storage is not commited before disposing all the changes will be lost
125 if ( !m_aTestHelper
.commitStorage( xTempSubStorage2
) )
128 // a storage must be disposed before moving/removing otherwise the access will be denied
129 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage2
) )
132 if ( !m_aTestHelper
.moveElementTo( xTempStorage
, "SubStorage2", xTempFileStorage
) )
135 // SubStorage2 must be removed and disposed now
138 xTempSubStorage2
.isStreamElement( "SubStream2" );
139 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
142 catch( com
.sun
.star
.lang
.DisposedException de
)
147 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
151 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
154 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "SubStream1", "SubStream1_copy" ) )
157 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
160 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
163 // dispose used storages to free resources
164 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
167 // ================================================
168 // now check all the written and copied information
169 // ================================================
171 // the temporary file must not be locked any more after storage disposing
172 pArgs
[1] = new Integer( ElementModes
.ELEMENT_READWRITE
);
173 Object oResStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
174 XStorage xResStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResStorage
);
175 if ( xResStorage
== null )
177 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
181 // open and check SubStorage1
182 XStorage xResSubStorage1
= m_aTestHelper
.openSubStorage( xResStorage
,
184 ElementModes
.ELEMENT_READ
);
185 if ( xResSubStorage1
== null )
187 m_aTestHelper
.Error( "Can't open existing substorage!" );
191 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage1
, "MediaType4", false, ElementModes
.ELEMENT_READ
) )
195 // open and check SubStorage2
196 XStorage xResSubStorage2
= m_aTestHelper
.openSubStorage( xResStorage
,
198 ElementModes
.ELEMENT_READ
);
199 if ( xResSubStorage2
== null )
201 m_aTestHelper
.Error( "Can't open existing substorage!" );
205 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage2
, "MediaType5", false, ElementModes
.ELEMENT_READ
) )
209 // check all the result streams
211 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1", "MediaType1", pBytes1
) )
214 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1_copy", "MediaType1", pBytes1
) )
217 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "SubStream1", "MediaType1", pBytes1
) )
220 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "SubStream2", "MediaType2", pBytes2
) )
223 // the storage must be disposed before removing
224 if ( !m_aTestHelper
.disposeStorage( xResSubStorage2
) )
227 // remove element and check that it was removed completelly
228 if ( !m_aTestHelper
.removeElement( xResStorage
, "SubStorage2" ) )
233 XNameAccess xResAccess
= (XNameAccess
) UnoRuntime
.queryInterface( XNameAccess
.class, xResStorage
);
234 if ( xResAccess
.hasByName( "SubStorage2" ) )
235 m_aTestHelper
.Error( "SubStorage2 must be removed already!" );
239 m_aTestHelper
.Error( "Can't get access to root storage, exception: " + e
);
245 xResSubStorage2
.isStreamElement( "SubStream2" );
247 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
250 catch( com
.sun
.star
.lang
.DisposedException de
)
255 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
259 // dispose used storages to free resources
260 if ( !m_aTestHelper
.disposeStorage( xResStorage
) )
267 m_aTestHelper
.Error( "Exception: " + e
);