xref: /aoo41x/main/package/qa/storages/Test05.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 
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 import com.sun.star.io.XStream;
32 
33 import com.sun.star.embed.*;
34 
35 import share.LogWriter;
36 import complex.storages.TestHelper;
37 import complex.storages.StorageTest;
38 
39 public class Test05 implements StorageTest {
40 
41 	XMultiServiceFactory m_xMSF;
42 	XSingleServiceFactory m_xStorageFactory;
43 	TestHelper m_aTestHelper;
44 
Test05( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )45 	public Test05( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
46 	{
47 		m_xMSF = xMSF;
48 		m_xStorageFactory = xStorageFactory;
49 		m_aTestHelper = new TestHelper( aLogWriter, "Test05: " );
50 	}
51 
test()52     public boolean test()
53 	{
54 		try
55 		{
56 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
57 			if ( sTempFileURL == null || sTempFileURL == "" )
58 			{
59 				m_aTestHelper.Error( "No valid temporary file was created!" );
60 				return false;
61 			}
62 
63 			// create temporary storage based on a previously created temporary file
64 			Object pArgs[] = new Object[2];
65 			pArgs[0] = (Object) sTempFileURL;
66 			pArgs[1] = new Integer( ElementModes.WRITE );
67 
68 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
69 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
70 			if ( xTempFileStorage == null )
71 			{
72 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
73 				return false;
74 			}
75 
76 			// open a new substorage
77 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage,
78 																		"SubStorage1",
79 																		ElementModes.WRITE );
80 			if ( xTempSubStorage == null )
81 			{
82 				m_aTestHelper.Error( "Can't create substorage!" );
83 				return false;
84 			}
85 
86 			// open a new substorage
87 			XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
88 																		"SubSubStorage1",
89 																		ElementModes.WRITE );
90 			if ( xSubSubStorage == null )
91 			{
92 				m_aTestHelper.Error( "Can't create substorage!" );
93 				return false;
94 			}
95 
96             byte pBigBytes[] = new byte[33000];
97 			for ( int nInd = 0; nInd < 33000; nInd++ )
98 				pBigBytes[nInd] = (byte)( nInd % 128 );
99 
100 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
101 			if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
102 				return false;
103 
104 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
105 			if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
106 				return false;
107 
108 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
109 
110 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
111 			if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
112 				return false;
113 
114 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
115 
116 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
117 			if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
118 				return false;
119 
120 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
121 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
122 															"MediaType3",
123 															true,
124 															ElementModes.WRITE ) )
125 				return false;
126 
127 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
128 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
129 															"MediaType4",
130 															false,
131 															ElementModes.WRITE ) )
132 				return false;
133 
134 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
135 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage,
136 															"MediaType5",
137 															false,
138 															ElementModes.WRITE ) )
139 				return false;
140 
141 
142 			// commit all the storages
143 			if ( !m_aTestHelper.commitStorage( xSubSubStorage ) )
144 				return false;
145 
146 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
147 				return false;
148 
149 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
150 				return false;
151 
152 			// try to open an opened substorage, open call must fail
153 			if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) )
154 				return false;
155 
156 
157 			// reopen created streams
158 			XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
159 															"SubStream1",
160 															ElementModes.WRITE | ElementModes.NOCREATE );
161 			XStream xBigSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
162 															   "BigSubStream1",
163 															   ElementModes.WRITE | ElementModes.NOCREATE );
164 			XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
165 															"SubStream2",
166 															ElementModes.READ | ElementModes.NOCREATE );
167 			XStream xBigSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
168 															   "BigSubStream2",
169 															   ElementModes.READ | ElementModes.NOCREATE );
170 
171 			if ( xSubStream1 == null || xBigSubStream1 == null || xSubStream2 == null || xBigSubStream2 == null )
172 				return false;
173 
174 			// it should be possible to have more then one copy of stream for reading
175 			XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
176 																"SubStream2",
177 																ElementModes.READ | ElementModes.NOCREATE );
178 			XStream xBigSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
179 																    "BigSubStream2",
180 																    ElementModes.READ | ElementModes.NOCREATE );
181 			if ( xSubStream2clone == null || xBigSubStream2clone == null )
182 				return false;
183 
184 
185 			// so now the first streams can not be open neither for reading nor for writing
186 			if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.WRITE )
187 			  || !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.READ )
188 			  || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream1", ElementModes.WRITE )
189 			  || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream1", ElementModes.READ ) )
190 				return false;
191 
192 			// the second streams can not be open for writing
193 			if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.WRITE )
194 			  || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream2", ElementModes.WRITE ) )
195 				return false;
196 
197 
198 			// dispose xTestSubStorage, all the subtree must be disposed
199 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
200 				return false;
201 
202 			// check that subtree was disposed correctly
203 			try
204 			{
205 				xSubSubStorage.isStreamElement( "SubStream1" );
206 				m_aTestHelper.Error( "Substorage was not disposed!" );
207 				return false;
208 			}
209 			catch ( com.sun.star.lang.DisposedException de )
210 			{}
211 			catch ( Exception e )
212 			{
213 				m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e );
214 				return false;
215 			}
216 
217 			try
218 			{
219 				xSubStream1.getInputStream();
220 				m_aTestHelper.Error( "Writeable substream was not disposed!" );
221 				return false;
222 			}
223 			catch ( com.sun.star.lang.DisposedException de )
224 			{}
225 			catch ( Exception e )
226 			{
227 				m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
228 				return false;
229 			}
230 
231 			try
232 			{
233 				xSubStream2.getInputStream();
234 				m_aTestHelper.Error( "Readonly substream was not disposed!" );
235 				return false;
236 			}
237 			catch ( com.sun.star.lang.DisposedException de )
238 			{}
239 			catch ( Exception e )
240 			{
241 				m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
242 				return false;
243 			}
244 
245 
246 			// dispose root storage
247 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
248 				return false;
249 
250 
251 			// ================================================
252 			// now check all the written and copied information
253 			// ================================================
254 
255 			pArgs[1] = new Integer( ElementModes.READ );
256 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
257 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
258 			if ( xResultStorage == null )
259 			{
260 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
261 				return false;
262 			}
263 
264 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
265 				return false;
266 
267 			// open existing substorage
268 			XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
269 																	"SubStorage1",
270 																	ElementModes.READ );
271 			if ( xResSubStorage == null )
272 			{
273 				m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
274 				return false;
275 			}
276 
277 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage, "MediaType4", false, ElementModes.READ ) )
278 				return false;
279 
280 			// open existing substorage
281 			XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage,
282 																		"SubSubStorage1",
283 																		ElementModes.READ );
284 			if ( xResSubSubStorage == null )
285 			{
286 				m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
287 				return false;
288 			}
289 
290 			if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage, "MediaType5", false, ElementModes.READ ) )
291 				return false;
292 
293 			// check substreams
294 			if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
295 				return false;
296 
297 			if ( !m_aTestHelper.checkStream( xResSubSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
298 				return false;
299 
300 			if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
301 				return false;
302 
303 			if ( !m_aTestHelper.checkStream( xResSubSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
304 				return false;
305 
306 			// dispose used storages to free resources
307 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
308 				return false;
309 
310 			return true;
311 		}
312 		catch( Exception e )
313 		{
314 			m_aTestHelper.Error( "Exception: " + e );
315 			return false;
316 		}
317     }
318 
319 }
320 
321