merge the formfield patch from ooo-build
[ooovba.git] / package / qa / ofopxmlstorages / Test04.java
blob5188b32b040f95d75283a8ad0c974bb44d37e802
1 package complex.ofopxmlstorages;
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.*;
15 import com.sun.star.beans.StringPair;
17 import share.LogWriter;
18 import complex.ofopxmlstorages.TestHelper;
19 import complex.ofopxmlstorages.StorageTest;
21 public class Test04 implements StorageTest {
23 XMultiServiceFactory m_xMSF;
24 XSingleServiceFactory m_xStorageFactory;
25 TestHelper m_aTestHelper;
27 public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
29 m_xMSF = xMSF;
30 m_xStorageFactory = xStorageFactory;
31 m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
34 public boolean test()
36 StringPair[][] aRelations1 =
37 { { new StringPair( "Id", "Num1" ) },
38 { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
39 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
40 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
41 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
42 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
43 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
46 StringPair[][] aRelations2 =
47 { { new StringPair( "Id", "Num1" ) },
48 { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
49 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
50 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
51 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
52 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
53 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
56 try
58 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
59 if ( sTempFileURL == null || sTempFileURL == "" )
61 m_aTestHelper.Error( "No valid temporary file was created!" );
62 return false;
65 // create temporary storage based on arbitrary medium
66 // after such a storage is closed it is lost
67 XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
68 if ( xTempStorage == null )
70 m_aTestHelper.Error( "Can't create temporary storage representation!" );
71 return false;
74 // open substorages and create streams there
76 // first substorage of the root storage
77 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
78 "SubStorage1",
79 ElementModes.WRITE );
80 if ( xTempSubStorage1 == null )
82 m_aTestHelper.Error( "Can't create substorage!" );
83 return false;
86 byte pBytes1[] = { 1, 1, 1, 1, 1 };
88 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
89 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1,
90 "SubStream1",
91 "MediaType1",
92 true,
93 pBytes1,
94 aRelations1 ) )
95 return false;
97 // second substorage of the root storage
98 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
99 "SubStorage2",
100 ElementModes.WRITE );
101 if ( xTempSubStorage2 == null )
103 m_aTestHelper.Error( "Can't create substorage!" );
104 return false;
107 byte pBytes2[] = { 2, 2, 2, 2, 2 };
109 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
110 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2,
111 "SubStream2",
112 "MediaType2",
113 false,
114 pBytes2,
115 aRelations2 ) )
116 return false;
118 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
119 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
120 true,
121 ElementModes.WRITE,
122 aRelations2 ) )
123 return false;
125 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
126 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
127 false,
128 ElementModes.WRITE,
129 aRelations2 ) )
130 return false;
132 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
133 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
134 false,
135 ElementModes.WRITE,
136 aRelations2 ) )
137 return false;
139 // create temporary storage based on a previously created temporary file
140 XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
141 sTempFileURL,
142 ElementModes.WRITE );
143 if ( xTempFileStorage == null )
145 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
146 return false;
149 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
150 return false;
152 // if storage is not commited before disposing all the changes will be lost
153 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
154 return false;
156 // a storage must be disposed before moving/removing otherwise the access will be denied
157 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
158 return false;
160 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
161 return false;
163 // SubStorage2 must be removed and disposed now
166 xTempSubStorage2.isStreamElement( "SubStream2" );
167 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
168 return false;
170 catch( com.sun.star.lang.DisposedException de )
173 catch( Exception e )
175 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
176 return false;
179 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
180 return false;
182 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
183 return false;
185 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
186 return false;
188 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
189 return false;
191 // dispose used storages to free resources
192 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
193 return false;
195 // ================================================
196 // now check all the written and copied information
197 // ================================================
199 // the temporary file must not be locked any more after storage disposing
200 XStorage xResStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
201 sTempFileURL,
202 ElementModes.WRITE );
204 if ( xResStorage == null )
206 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
207 return false;
210 // open and check SubStorage1
211 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
212 "SubStorage1",
213 ElementModes.READ );
214 if ( xResSubStorage1 == null )
216 m_aTestHelper.Error( "Can't open existing substorage!" );
217 return false;
220 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1,
221 false,
222 ElementModes.READ,
223 aRelations2 ) )
224 return false;
227 // open and check SubStorage2
228 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
229 "SubStorage2",
230 ElementModes.READ );
231 if ( xResSubStorage2 == null )
233 m_aTestHelper.Error( "Can't open existing substorage!" );
234 return false;
237 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2,
238 false,
239 ElementModes.READ,
240 aRelations2 ) )
241 return false;
244 // check all the result streams
246 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1, aRelations1 ) )
247 return false;
249 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1, aRelations1 ) )
250 return false;
252 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1, aRelations1 ) )
253 return false;
255 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2, aRelations2 ) )
256 return false;
258 // the storage must be disposed before removing
259 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
260 return false;
262 // remove element and check that it was removed completelly
263 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
264 return false;
268 XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
269 if ( xResAccess.hasByName( "SubStorage2" ) )
270 m_aTestHelper.Error( "SubStorage2 must be removed already!" );
272 catch( Exception e )
274 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
275 return false;
280 xResSubStorage2.isStreamElement( "SubStream2" );
282 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
283 return false;
285 catch( com.sun.star.lang.DisposedException de )
288 catch( Exception e )
290 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
291 return false;
294 // dispose used storages to free resources
295 if ( !m_aTestHelper.disposeStorage( xResStorage ) )
296 return false;
298 return true;
300 catch( Exception e )
302 m_aTestHelper.Error( "Exception: " + e );
303 return false;