bump product version to 6.4.0.3
[LibreOffice.git] / package / qa / storages / Test05.java
blob9138b4f86bf15dee64bb9953877e0dc281d04133
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.storages;
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.*;
32 import share.LogWriter;
33 import complex.storages.TestHelper;
34 import complex.storages.StorageTest;
36 public class Test05 implements StorageTest {
38 XMultiServiceFactory m_xMSF;
39 XSingleServiceFactory m_xStorageFactory;
40 TestHelper m_aTestHelper;
42 public Test05( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
44 m_xMSF = xMSF;
45 m_xStorageFactory = xStorageFactory;
46 m_aTestHelper = new TestHelper( aLogWriter, "Test05: " );
49 public boolean test()
51 try
53 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
54 if ( sTempFileURL == null || sTempFileURL == "" )
56 m_aTestHelper.Error( "No valid temporary file was created!" );
57 return false;
60 // create temporary storage based on a previously created temporary file
61 Object pArgs[] = new Object[2];
62 pArgs[0] = (Object) sTempFileURL;
63 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
65 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
66 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
67 if ( xTempFileStorage == null )
69 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
70 return false;
73 // open a new substorage
74 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage,
75 "SubStorage1",
76 ElementModes.WRITE );
77 if ( xTempSubStorage == null )
79 m_aTestHelper.Error( "Can't create substorage!" );
80 return false;
83 // open a new substorage
84 XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
85 "SubSubStorage1",
86 ElementModes.WRITE );
87 if ( xSubSubStorage == null )
89 m_aTestHelper.Error( "Can't create substorage!" );
90 return false;
93 byte pBigBytes[] = new byte[33000];
94 for ( int nInd = 0; nInd < 33000; nInd++ )
95 pBigBytes[nInd] = (byte)( nInd % 128 );
97 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
99 return false;
101 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
102 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
103 return false;
105 byte pBytes1[] = { 1, 1, 1, 1, 1 };
107 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
108 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
109 return false;
111 byte pBytes2[] = { 2, 2, 2, 2, 2 };
113 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
114 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
115 return false;
117 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
118 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
119 "MediaType3",
120 true,
121 ElementModes.WRITE ) )
122 return false;
124 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
125 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
126 "MediaType4",
127 false,
128 ElementModes.WRITE ) )
129 return false;
131 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
132 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage,
133 "MediaType5",
134 false,
135 ElementModes.WRITE ) )
136 return false;
139 // commit all the storages
140 if ( !m_aTestHelper.commitStorage( xSubSubStorage ) )
141 return false;
143 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
144 return false;
146 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
147 return false;
149 // try to open an opened substorage, open call must fail
150 if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) )
151 return false;
154 // reopen created streams
155 XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
156 "SubStream1",
157 ElementModes.WRITE | ElementModes.NOCREATE );
158 XStream xBigSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
159 "BigSubStream1",
160 ElementModes.WRITE | ElementModes.NOCREATE );
161 XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
162 "SubStream2",
163 ElementModes.READ | ElementModes.NOCREATE );
164 XStream xBigSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
165 "BigSubStream2",
166 ElementModes.READ | ElementModes.NOCREATE );
168 if ( xSubStream1 == null || xBigSubStream1 == null || xSubStream2 == null || xBigSubStream2 == null )
169 return false;
171 // it should be possible to have more than one copy of stream for reading
172 XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
173 "SubStream2",
174 ElementModes.READ | ElementModes.NOCREATE );
175 XStream xBigSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
176 "BigSubStream2",
177 ElementModes.READ | ElementModes.NOCREATE );
178 if ( xSubStream2clone == null || xBigSubStream2clone == null )
179 return false;
182 // so now the first streams can not be open neither for reading nor for writing
183 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.WRITE )
184 || !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.READ )
185 || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream1", ElementModes.WRITE )
186 || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream1", ElementModes.READ ) )
187 return false;
189 // the second streams can not be open for writing
190 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.WRITE )
191 || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream2", ElementModes.WRITE ) )
192 return false;
195 // dispose xTestSubStorage, all the subtree must be disposed
196 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
197 return false;
199 // check that subtree was disposed correctly
202 xSubSubStorage.isStreamElement( "SubStream1" );
203 m_aTestHelper.Error( "Substorage was not disposed!" );
204 return false;
206 catch ( com.sun.star.lang.DisposedException de )
208 catch ( Exception e )
210 m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e );
211 return false;
216 xSubStream1.getInputStream();
217 m_aTestHelper.Error( "Writeable substream was not disposed!" );
218 return false;
220 catch ( com.sun.star.lang.DisposedException de )
222 catch ( Exception e )
224 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
225 return false;
230 xSubStream2.getInputStream();
231 m_aTestHelper.Error( "Readonly substream was not disposed!" );
232 return false;
234 catch ( com.sun.star.lang.DisposedException de )
236 catch ( Exception e )
238 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
239 return false;
243 // dispose root storage
244 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
245 return false;
249 // now check all the written and copied information
252 pArgs[1] = Integer.valueOf( ElementModes.READ );
253 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
254 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
255 if ( xResultStorage == null )
257 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
258 return false;
261 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
262 return false;
264 // open existing substorage
265 XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
266 "SubStorage1",
267 ElementModes.READ );
268 if ( xResSubStorage == null )
270 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
271 return false;
274 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage, "MediaType4", false, ElementModes.READ ) )
275 return false;
277 // open existing substorage
278 XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage,
279 "SubSubStorage1",
280 ElementModes.READ );
281 if ( xResSubSubStorage == null )
283 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
284 return false;
287 if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage, "MediaType5", false, ElementModes.READ ) )
288 return false;
290 // check substreams
291 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
292 return false;
294 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
295 return false;
297 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
298 return false;
300 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
301 return false;
303 // dispose used storages to free resources
304 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
305 return false;
307 return true;
309 catch( Exception e )
311 m_aTestHelper.Error( "Exception: " + e );
312 return false;