Update ooo320-m1
[ooovba.git] / package / qa / storages / Test04.java
blob2f8ba736f1b335463892aa96b1b51f402f3faa2c
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 )
28 m_xMSF = xMSF;
29 m_xStorageFactory = xStorageFactory;
30 m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
33 public boolean test()
35 try
37 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
38 if ( sTempFileURL == null || sTempFileURL == "" )
40 m_aTestHelper.Error( "No valid temporary file was created!" );
41 return false;
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!" );
51 return false;
54 // open substorages and create streams there
56 // first substorage of the root storage
57 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
58 "SubStorage1",
59 ElementModes.WRITE );
60 if ( xTempSubStorage1 == null )
62 m_aTestHelper.Error( "Can't create substorage!" );
63 return false;
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 ) )
72 return false;
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 ) )
78 return false;
80 // second substorage of the root storage
81 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
82 "SubStorage2",
83 ElementModes.WRITE );
84 if ( xTempSubStorage2 == null )
86 m_aTestHelper.Error( "Can't create substorage!" );
87 return false;
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 ) )
92 return false;
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 ) )
98 return false;
100 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
101 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
102 "MediaType3",
103 true,
104 ElementModes.WRITE ) )
105 return false;
107 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
108 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
109 "MediaType4",
110 false,
111 ElementModes.WRITE ) )
112 return false;
114 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
115 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
116 "MediaType5",
117 false,
118 ElementModes.WRITE ) )
119 return false;
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!" );
131 return false;
134 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
135 return false;
137 // if storage is not commited before disposing all the changes will be lost
138 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
139 return false;
141 // a storage must be disposed before moving/removing otherwise the access will be denied
142 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
143 return false;
145 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
146 return false;
148 // SubStorage2 must be removed and disposed now
151 xTempSubStorage2.isStreamElement( "SubStream2" );
152 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
153 return false;
155 catch( com.sun.star.lang.DisposedException de )
158 catch( Exception e )
160 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
161 return false;
164 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
165 return false;
167 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
168 return false;
170 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
171 return false;
173 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
174 return false;
176 if ( !m_aTestHelper.renameElement( xTempFileStorage, "BigSubStream1", "BigSubStream1_copy" ) )
177 return false;
179 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
180 return false;
182 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
183 return false;
185 // dispose used storages to free resources
186 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
187 return false;
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!" );
200 return false;
203 // open and check SubStorage1
204 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
205 "SubStorage1",
206 ElementModes.READ );
207 if ( xResSubStorage1 == null )
209 m_aTestHelper.Error( "Can't open existing substorage!" );
210 return false;
213 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
214 return false;
217 // open and check SubStorage2
218 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
219 "SubStorage2",
220 ElementModes.READ );
221 if ( xResSubStorage2 == null )
223 m_aTestHelper.Error( "Can't open existing substorage!" );
224 return false;
227 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
228 return false;
231 // check all the result streams
233 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
234 return false;
236 if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
237 return false;
239 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", true, pBytes1 ) )
240 return false;
242 if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1_copy", "MediaType1", true, pBigBytes ) )
243 return false;
245 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
246 return false;
248 if ( !m_aTestHelper.checkStream( xResSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
249 return false;
251 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
252 return false;
254 if ( !m_aTestHelper.checkStream( xResSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
255 return false;
257 // the storage must be disposed before removing
258 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
259 return false;
261 // remove element and check that it was removed completelly
262 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
263 return false;
267 XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
268 if ( xResAccess.hasByName( "SubStorage2" ) )
269 m_aTestHelper.Error( "SubStorage2 must be removed already!" );
271 catch( Exception e )
273 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
274 return false;
279 xResSubStorage2.isStreamElement( "SubStream2" );
281 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
282 return false;
284 catch( com.sun.star.lang.DisposedException de )
287 catch( Exception e )
289 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
290 return false;
293 // dispose used storages to free resources
294 if ( !m_aTestHelper.disposeStorage( xResStorage ) )
295 return false;
297 return true;
299 catch( Exception e )
301 m_aTestHelper.Error( "Exception: " + e );
302 return false;