xref: /aoo41x/main/package/qa/storages/Test04.java (revision a740f2aa)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package complex.storages;
23 
24 import com.sun.star.uno.XInterface;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XSingleServiceFactory;
27 import com.sun.star.lang.DisposedException;
28 
29 import com.sun.star.bridge.XUnoUrlResolver;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.uno.XInterface;
32 
33 import com.sun.star.container.XNameAccess;
34 
35 import com.sun.star.embed.*;
36 
37 import share.LogWriter;
38 import complex.storages.TestHelper;
39 import complex.storages.StorageTest;
40 
41 public class Test04 implements StorageTest {
42 
43 	XMultiServiceFactory m_xMSF;
44 	XSingleServiceFactory m_xStorageFactory;
45 	TestHelper m_aTestHelper;
46 
Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )47 	public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
48 	{
49 		m_xMSF = xMSF;
50 		m_xStorageFactory = xStorageFactory;
51 		m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
52 	}
53 
test()54     public boolean test()
55 	{
56 		try
57 		{
58 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
59 			if ( sTempFileURL == null || sTempFileURL == "" )
60 			{
61 				m_aTestHelper.Error( "No valid temporary file was created!" );
62 				return false;
63 			}
64 
65 			// create temporary storage based on arbitrary medium
66 			// after such a storage is closed it is lost
67 			Object oTempStorage = m_xStorageFactory.createInstance();
68 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
69 			if ( xTempStorage == null )
70 			{
71 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
72 				return false;
73 			}
74 
75 			// open substorages and create streams there
76 
77 			// first substorage of the root storage
78 			XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
79 																		"SubStorage1",
80 																		ElementModes.WRITE );
81 			if ( xTempSubStorage1 == null )
82 			{
83 				m_aTestHelper.Error( "Can't create substorage!" );
84 				return false;
85 			}
86 
87             byte pBigBytes[] = new byte[33000];
88 			for ( int nInd = 0; nInd < 33000; nInd++ )
89 				pBigBytes[nInd] = (byte)( nInd % 128 );
90 
91 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
92 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
93 				return false;
94 
95 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
96 
97 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
99 				return false;
100 
101 			// second substorage of the root storage
102 			XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
103 																		"SubStorage2",
104 																		ElementModes.WRITE );
105 			if ( xTempSubStorage2 == null )
106 			{
107 				m_aTestHelper.Error( "Can't create substorage!" );
108 				return false;
109 			}
110 
111 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
112 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
113 				return false;
114 
115 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
116 
117 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
118 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
119 				return false;
120 
121 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
122 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
123 															"MediaType3",
124 															true,
125 															ElementModes.WRITE ) )
126 				return false;
127 
128 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
129 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
130 															"MediaType4",
131 															false,
132 															ElementModes.WRITE ) )
133 				return false;
134 
135 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
136 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
137 															"MediaType5",
138 															false,
139 															ElementModes.WRITE ) )
140 				return false;
141 
142 			// create temporary storage based on a previously created temporary file
143 			Object pArgs[] = new Object[2];
144 			pArgs[0] = (Object) sTempFileURL;
145 			pArgs[1] = new Integer( ElementModes.WRITE );
146 
147 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
148 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
149 			if ( xTempFileStorage == null )
150 			{
151 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
152 				return false;
153 			}
154 
155 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
156 				return false;
157 
158 			// if storage is not commited before disposing all the changes will be lost
159 			if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
160 				return false;
161 
162 			// a storage must be disposed before moving/removing otherwise the access will be denied
163 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
164 				return false;
165 
166 			if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
167 				return false;
168 
169 			// SubStorage2 must be removed and disposed now
170 			try
171 			{
172 				xTempSubStorage2.isStreamElement( "SubStream2" );
173 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
174 				return false;
175 			}
176 			catch( com.sun.star.lang.DisposedException de )
177 			{
178 			}
179 			catch( Exception e )
180 			{
181 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
182 				return false;
183 			}
184 
185 			if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
186 				return false;
187 
188 			if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
189 				return false;
190 
191 			if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
192 				return false;
193 
194 			if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
195 				return false;
196 
197 			if ( !m_aTestHelper.renameElement( xTempFileStorage, "BigSubStream1", "BigSubStream1_copy" ) )
198 				return false;
199 
200 			if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
201 				return false;
202 
203 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
204 				return false;
205 
206 			// dispose used storages to free resources
207 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
208 				return false;
209 
210 			// ================================================
211 			// now check all the written and copied information
212 			// ================================================
213 
214 			// the temporary file must not be locked any more after storage disposing
215 			pArgs[1] = new Integer( ElementModes.WRITE );
216 			Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
217 			XStorage xResStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResStorage );
218 			if ( xResStorage == null )
219 			{
220 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
221 				return false;
222 			}
223 
224 			// open and check SubStorage1
225 			XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
226 																		"SubStorage1",
227 																		ElementModes.READ );
228 			if ( xResSubStorage1 == null )
229 			{
230 				m_aTestHelper.Error( "Can't open existing substorage!" );
231 				return false;
232 			}
233 
234 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
235 				return false;
236 
237 
238 			// open and check SubStorage2
239 			XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
240 																		"SubStorage2",
241 																		ElementModes.READ );
242 			if ( xResSubStorage2 == null )
243 			{
244 				m_aTestHelper.Error( "Can't open existing substorage!" );
245 				return false;
246 			}
247 
248 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
249 				return false;
250 
251 
252 			// check all the result streams
253 
254 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
255 				return false;
256 
257 			if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
258 				return false;
259 
260 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", true, pBytes1 ) )
261 				return false;
262 
263 			if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1_copy", "MediaType1", true, pBigBytes ) )
264 				return false;
265 
266 			if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
267 				return false;
268 
269 			if ( !m_aTestHelper.checkStream( xResSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
270 				return false;
271 
272 			if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
273 				return false;
274 
275 			if ( !m_aTestHelper.checkStream( xResSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
276 				return false;
277 
278 			// the storage must be disposed before removing
279 			if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
280 				return false;
281 
282 			// remove element and check that it was removed completelly
283 			if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
284 				return false;
285 
286 			try
287 			{
288 				XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
289 				if ( xResAccess.hasByName( "SubStorage2" ) )
290 					m_aTestHelper.Error( "SubStorage2 must be removed already!" );
291 			}
292 			catch( Exception e )
293 			{
294 				m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
295 				return false;
296 			}
297 
298 			try
299 			{
300 				xResSubStorage2.isStreamElement( "SubStream2" );
301 
302 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
303 				return false;
304 			}
305 			catch( com.sun.star.lang.DisposedException de )
306 			{
307 			}
308 			catch( Exception e )
309 			{
310 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
311 				return false;
312 			}
313 
314 			// dispose used storages to free resources
315 			if ( !m_aTestHelper.disposeStorage( xResStorage ) )
316 				return false;
317 
318 			return true;
319 		}
320 		catch( Exception e )
321 		{
322 			m_aTestHelper.Error( "Exception: " + e );
323 			return false;
324 		}
325     }
326 
327 }
328 
329