Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / package / qa / ofopxmlstorages / Test05.java
blob1ed0ba40298c71aa02d50f41fae8d2d330a967c9
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex.ofopxmlstorages;
21 import com.sun.star.uno.XInterface;
22 import com.sun.star.lang.XMultiServiceFactory;
23 import com.sun.star.lang.XSingleServiceFactory;
25 import com.sun.star.bridge.XUnoUrlResolver;
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.uno.XInterface;
28 import com.sun.star.io.XStream;
30 import com.sun.star.embed.*;
31 import com.sun.star.beans.StringPair;
33 import share.LogWriter;
34 import complex.ofopxmlstorages.TestHelper;
35 import complex.ofopxmlstorages.StorageTest;
37 public class Test05 implements StorageTest {
39 XMultiServiceFactory m_xMSF;
40 XSingleServiceFactory m_xStorageFactory;
41 TestHelper m_aTestHelper;
43 public Test05( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 m_xMSF = xMSF;
46 m_xStorageFactory = xStorageFactory;
47 m_aTestHelper = new TestHelper( aLogWriter, "Test05: " );
50 public boolean test()
52 StringPair[][] aRelations1 =
53 { { new StringPair( "Id", "Num1" ) },
54 { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
55 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
56 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
57 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
58 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
59 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
62 StringPair[][] aRelations2 =
63 { { new StringPair( "Id", "Num1" ) },
64 { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
65 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
66 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
67 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
68 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
69 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
72 try
74 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
75 if ( sTempFileURL == null || sTempFileURL == "" )
77 m_aTestHelper.Error( "No valid temporary file was created!" );
78 return false;
81 // create temporary storage based on a previously created temporary file
82 XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
83 sTempFileURL,
84 ElementModes.WRITE );
85 if ( xTempFileStorage == null )
87 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
88 return false;
91 // open a new substorage
92 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage,
93 "SubStorage1",
94 ElementModes.WRITE );
95 if ( xTempSubStorage == null )
97 m_aTestHelper.Error( "Can't create substorage!" );
98 return false;
101 // open a new substorage
102 XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
103 "SubSubStorage1",
104 ElementModes.WRITE );
105 if ( xSubSubStorage == null )
107 m_aTestHelper.Error( "Can't create substorage!" );
108 return false;
112 byte pBytes1[] = { 1, 1, 1, 1, 1 };
114 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
115 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage,
116 "SubStream1",
117 "MediaType1",
118 true,
119 pBytes1,
120 aRelations1 ) )
121 return false;
123 byte pBytes2[] = { 2, 2, 2, 2, 2 };
125 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
126 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage,
127 "SubStream2",
128 "MediaType2",
129 false,
130 pBytes2,
131 aRelations2 ) )
132 return false;
134 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
135 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
136 true,
137 ElementModes.WRITE,
138 aRelations2 ) )
139 return false;
141 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
142 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
143 false,
144 ElementModes.WRITE,
145 aRelations2 ) )
146 return false;
148 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
149 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage,
150 false,
151 ElementModes.WRITE,
152 aRelations2 ) )
153 return false;
156 // commit all the storages
157 if ( !m_aTestHelper.commitStorage( xSubSubStorage ) )
158 return false;
160 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
161 return false;
163 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
164 return false;
166 // try to open an opened substorage, open call must fail
167 if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) )
168 return false;
171 // reopen created streams
172 XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
173 "SubStream1",
174 ElementModes.WRITE | ElementModes.NOCREATE );
175 XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
176 "SubStream2",
177 ElementModes.READ | ElementModes.NOCREATE );
178 if ( xSubStream1 == null || xSubStream2 == null )
179 return false;
181 // it should be possible to have more than one copy of stream for reading
182 XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
183 "SubStream2",
184 ElementModes.READ | ElementModes.NOCREATE );
185 if ( xSubStream2 == null )
186 return false;
189 // so now the first stream can not be open neither for reading nor for writing
190 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.WRITE )
191 || !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.READ ) )
192 return false;
194 // the second stream can not be open for writing
195 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.WRITE ) )
196 return false;
199 // dispose xTestSubStorage, all the subtree must be disposed
200 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
201 return false;
203 // check that subtree was disposed correctly
206 xSubSubStorage.isStreamElement( "SubStream1" );
207 m_aTestHelper.Error( "Substorage was not disposed!" );
208 return false;
210 catch ( com.sun.star.lang.DisposedException de )
212 catch ( Exception e )
214 m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e );
215 return false;
220 xSubStream1.getInputStream();
221 m_aTestHelper.Error( "Writeable substream was not disposed!" );
222 return false;
224 catch ( com.sun.star.lang.DisposedException de )
226 catch ( Exception e )
228 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
229 return false;
234 xSubStream2.getInputStream();
235 m_aTestHelper.Error( "Readonly substream was not disposed!" );
236 return false;
238 catch ( com.sun.star.lang.DisposedException de )
240 catch ( Exception e )
242 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
243 return false;
247 // dispose root storage
248 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
249 return false;
253 // now check all the written and copied information
256 XStorage xResultStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
257 sTempFileURL,
258 ElementModes.READ );
259 if ( xResultStorage == null )
261 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
262 return false;
265 if ( !m_aTestHelper.checkStorageProperties( xResultStorage,
266 true,
267 ElementModes.READ,
268 aRelations2 ) )
269 return false;
271 // open existing substorage
272 XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
273 "SubStorage1",
274 ElementModes.READ );
275 if ( xResSubStorage == null )
277 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
278 return false;
281 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage,
282 false,
283 ElementModes.READ,
284 aRelations2 ) )
285 return false;
287 // open existing substorage
288 XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage,
289 "SubSubStorage1",
290 ElementModes.READ );
291 if ( xResSubSubStorage == null )
293 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
294 return false;
297 if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage,
298 false,
299 ElementModes.READ,
300 aRelations2 ) )
301 return false;
303 // check substreams
304 if ( !m_aTestHelper.checkStream( xResSubSubStorage,
305 "SubStream1",
306 "MediaType1",
307 pBytes1,
308 aRelations1 ) )
309 return false;
311 if ( !m_aTestHelper.checkStream( xResSubSubStorage,
312 "SubStream2",
313 "MediaType2",
314 pBytes2,
315 aRelations2 ) )
316 return false;
318 // dispose used storages to free resources
319 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
320 return false;
322 return true;
324 catch( Exception e )
326 m_aTestHelper.Error( "Exception: " + e );
327 return false;