Update ooo320-m1
[ooovba.git] / odk / examples / java / Storage / Test04.java
blob943fcfff8a38250ed5978c124ca980d7d9b6f587
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 )
27 m_xMSF = xMSF;
28 m_xStorageFactory = xStorageFactory;
29 m_aTestHelper = new TestHelper( "Test04: " );
32 public boolean test()
34 try
36 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
37 if ( sTempFileURL == null || sTempFileURL == "" )
39 m_aTestHelper.Error( "No valid temporary file was created!" );
40 return false;
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!" );
50 return false;
53 // open substorages and create streams there
55 // first substorage of the root storage
56 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
57 "SubStorage1",
58 ElementModes.ELEMENT_WRITE );
59 if ( xTempSubStorage1 == null )
61 m_aTestHelper.Error( "Can't create substorage!" );
62 return false;
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 ) )
69 return false;
71 // second substorage of the root storage
72 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
73 "SubStorage2",
74 ElementModes.ELEMENT_WRITE );
75 if ( xTempSubStorage2 == null )
77 m_aTestHelper.Error( "Can't create substorage!" );
78 return false;
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 ) )
85 return false;
87 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
88 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
89 "MediaType3",
90 true,
91 ElementModes.ELEMENT_READWRITE ) )
92 return false;
94 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
95 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
96 "MediaType4",
97 false,
98 ElementModes.ELEMENT_WRITE ) )
99 return false;
101 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
102 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
103 "MediaType5",
104 false,
105 ElementModes.ELEMENT_WRITE ) )
106 return false;
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!" );
118 return false;
121 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
122 return false;
124 // if storage is not commited before disposing all the changes will be lost
125 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
126 return false;
128 // a storage must be disposed before moving/removing otherwise the access will be denied
129 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
130 return false;
132 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
133 return false;
135 // SubStorage2 must be removed and disposed now
138 xTempSubStorage2.isStreamElement( "SubStream2" );
139 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
140 return false;
142 catch( com.sun.star.lang.DisposedException de )
145 catch( Exception e )
147 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
148 return false;
151 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
152 return false;
154 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
155 return false;
157 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
158 return false;
160 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
161 return false;
163 // dispose used storages to free resources
164 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
165 return false;
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!" );
178 return false;
181 // open and check SubStorage1
182 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
183 "SubStorage1",
184 ElementModes.ELEMENT_READ );
185 if ( xResSubStorage1 == null )
187 m_aTestHelper.Error( "Can't open existing substorage!" );
188 return false;
191 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.ELEMENT_READ ) )
192 return false;
195 // open and check SubStorage2
196 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
197 "SubStorage2",
198 ElementModes.ELEMENT_READ );
199 if ( xResSubStorage2 == null )
201 m_aTestHelper.Error( "Can't open existing substorage!" );
202 return false;
205 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.ELEMENT_READ ) )
206 return false;
209 // check all the result streams
211 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1 ) )
212 return false;
214 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1 ) )
215 return false;
217 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1 ) )
218 return false;
220 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2 ) )
221 return false;
223 // the storage must be disposed before removing
224 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
225 return false;
227 // remove element and check that it was removed completelly
228 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
229 return false;
233 XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
234 if ( xResAccess.hasByName( "SubStorage2" ) )
235 m_aTestHelper.Error( "SubStorage2 must be removed already!" );
237 catch( Exception e )
239 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
240 return false;
245 xResSubStorage2.isStreamElement( "SubStream2" );
247 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
248 return false;
250 catch( com.sun.star.lang.DisposedException de )
253 catch( Exception e )
255 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
256 return false;
259 // dispose used storages to free resources
260 if ( !m_aTestHelper.disposeStorage( xResStorage ) )
261 return false;
263 return true;
265 catch( Exception e )
267 m_aTestHelper.Error( "Exception: " + e );
268 return false;