fix toolbar import
[ooovba.git] / package / qa / storages / Test04.java
blob9435b73894c32ffa86bf92e4d4e6f07cbc55169b
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 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 ) )
70 return false;
72 // second substorage of the root storage
73 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
74 "SubStorage2",
75 ElementModes.WRITE );
76 if ( xTempSubStorage2 == null )
78 m_aTestHelper.Error( "Can't create substorage!" );
79 return false;
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 ) )
86 return false;
88 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
89 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
90 "MediaType3",
91 true,
92 ElementModes.WRITE ) )
93 return false;
95 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
96 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
97 "MediaType4",
98 false,
99 ElementModes.WRITE ) )
100 return false;
102 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
103 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
104 "MediaType5",
105 false,
106 ElementModes.WRITE ) )
107 return false;
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!" );
119 return false;
122 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
123 return false;
125 // if storage is not commited before disposing all the changes will be lost
126 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
127 return false;
129 // a storage must be disposed before moving/removing otherwise the access will be denied
130 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
131 return false;
133 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
134 return false;
136 // SubStorage2 must be removed and disposed now
139 xTempSubStorage2.isStreamElement( "SubStream2" );
140 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
141 return false;
143 catch( com.sun.star.lang.DisposedException de )
146 catch( Exception e )
148 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
149 return false;
152 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
153 return false;
155 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
156 return false;
158 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
159 return false;
161 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
162 return false;
164 // dispose used storages to free resources
165 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
166 return false;
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!" );
179 return false;
182 // open and check SubStorage1
183 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
184 "SubStorage1",
185 ElementModes.READ );
186 if ( xResSubStorage1 == null )
188 m_aTestHelper.Error( "Can't open existing substorage!" );
189 return false;
192 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
193 return false;
196 // open and check SubStorage2
197 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
198 "SubStorage2",
199 ElementModes.READ );
200 if ( xResSubStorage2 == null )
202 m_aTestHelper.Error( "Can't open existing substorage!" );
203 return false;
206 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
207 return false;
210 // check all the result streams
212 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
213 return false;
215 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", true, pBytes1 ) )
216 return false;
218 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
219 return false;
221 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
222 return false;
224 // the storage must be disposed before removing
225 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
226 return false;
228 // remove element and check that it was removed completelly
229 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
230 return false;
234 XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
235 if ( xResAccess.hasByName( "SubStorage2" ) )
236 m_aTestHelper.Error( "SubStorage2 must be removed already!" );
238 catch( Exception e )
240 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
241 return false;
246 xResSubStorage2.isStreamElement( "SubStream2" );
248 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
249 return false;
251 catch( com.sun.star.lang.DisposedException de )
254 catch( Exception e )
256 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
257 return false;
260 // dispose used storages to free resources
261 if ( !m_aTestHelper.disposeStorage( xResStorage ) )
262 return false;
264 return true;
266 catch( Exception e )
268 m_aTestHelper.Error( "Exception: " + e );
269 return false;