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.ofopxmlstorages;
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 import com.sun.star.beans.StringPair;
37 
38 import share.LogWriter;
39 import complex.ofopxmlstorages.TestHelper;
40 import complex.ofopxmlstorages.StorageTest;
41 
42 public class Test04 implements StorageTest {
43 
44 	XMultiServiceFactory m_xMSF;
45 	XSingleServiceFactory m_xStorageFactory;
46 	TestHelper m_aTestHelper;
47 
Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )48 	public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
49 	{
50 		m_xMSF = xMSF;
51 		m_xStorageFactory = xStorageFactory;
52 		m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
53 	}
54 
test()55     public boolean test()
56 	{
57 		StringPair[][] aRelations1 =
58 			{ { new StringPair( "Id", "Num1" ) },
59 			  { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
60 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
61 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
62 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
63 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
64 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
65 			};
66 
67 		StringPair[][] aRelations2 =
68 			{ { new StringPair( "Id", "Num1" ) },
69 			  { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
70 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
71 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
72 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
73 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
74 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
75 			};
76 
77 		try
78 		{
79 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
80 			if ( sTempFileURL == null || sTempFileURL == "" )
81 			{
82 				m_aTestHelper.Error( "No valid temporary file was created!" );
83 				return false;
84 			}
85 
86 			// create temporary storage based on arbitrary medium
87 			// after such a storage is closed it is lost
88 			XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
89 			if ( xTempStorage == null )
90 			{
91 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
92 				return false;
93 			}
94 
95 			// open substorages and create streams there
96 
97 			// first substorage of the root storage
98 			XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
99 																		"SubStorage1",
100 																		ElementModes.WRITE );
101 			if ( xTempSubStorage1 == null )
102 			{
103 				m_aTestHelper.Error( "Can't create substorage!" );
104 				return false;
105 			}
106 
107 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
108 
109 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
110 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1,
111 														"SubStream1",
112 														"MediaType1",
113 														true,
114 														pBytes1,
115 														aRelations1 ) )
116 				return false;
117 
118 			// second substorage of the root storage
119 			XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
120 																		"SubStorage2",
121 																		ElementModes.WRITE );
122 			if ( xTempSubStorage2 == null )
123 			{
124 				m_aTestHelper.Error( "Can't create substorage!" );
125 				return false;
126 			}
127 
128 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
129 
130 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
131 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2,
132 														"SubStream2",
133 														"MediaType2",
134 														false,
135 														pBytes2,
136 														aRelations2 ) )
137 				return false;
138 
139 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
140 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
141 															true,
142 															ElementModes.WRITE,
143 															aRelations2 ) )
144 				return false;
145 
146 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
147 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
148 															false,
149 															ElementModes.WRITE,
150 															aRelations2 ) )
151 				return false;
152 
153 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
154 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
155 															false,
156 															ElementModes.WRITE,
157 															aRelations2 ) )
158 				return false;
159 
160 			// create temporary storage based on a previously created temporary file
161 			XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
162 																			sTempFileURL,
163 																			ElementModes.WRITE );
164 			if ( xTempFileStorage == null )
165 			{
166 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
167 				return false;
168 			}
169 
170 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
171 				return false;
172 
173 			// if storage is not committed before disposing all the changes will be lost
174 			if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
175 				return false;
176 
177 			// a storage must be disposed before moving/removing otherwise the access will be denied
178 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
179 				return false;
180 
181 			if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
182 				return false;
183 
184 			// SubStorage2 must be removed and disposed now
185 			try
186 			{
187 				xTempSubStorage2.isStreamElement( "SubStream2" );
188 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
189 				return false;
190 			}
191 			catch( com.sun.star.lang.DisposedException de )
192 			{
193 			}
194 			catch( Exception e )
195 			{
196 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
197 				return false;
198 			}
199 
200 			if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
201 				return false;
202 
203 			if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
204 				return false;
205 
206 			if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
207 				return false;
208 
209 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
210 				return false;
211 
212 			// dispose used storages to free resources
213 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
214 				return false;
215 
216 			// ================================================
217 			// now check all the written and copied information
218 			// ================================================
219 
220 			// the temporary file must not be locked any more after storage disposing
221 			XStorage xResStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
222 																		sTempFileURL,
223 																		ElementModes.WRITE );
224 
225 			if ( xResStorage == null )
226 			{
227 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
228 				return false;
229 			}
230 
231 			// open and check SubStorage1
232 			XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
233 																		"SubStorage1",
234 																		ElementModes.READ );
235 			if ( xResSubStorage1 == null )
236 			{
237 				m_aTestHelper.Error( "Can't open existing substorage!" );
238 				return false;
239 			}
240 
241 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1,
242 														false,
243 														ElementModes.READ,
244 														aRelations2 ) )
245 				return false;
246 
247 
248 			// open and check SubStorage2
249 			XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
250 																		"SubStorage2",
251 																		ElementModes.READ );
252 			if ( xResSubStorage2 == null )
253 			{
254 				m_aTestHelper.Error( "Can't open existing substorage!" );
255 				return false;
256 			}
257 
258 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2,
259 														false,
260 														ElementModes.READ,
261 														aRelations2 ) )
262 				return false;
263 
264 
265 			// check all the result streams
266 
267 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1, aRelations1 ) )
268 				return false;
269 
270 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1, aRelations1 ) )
271 				return false;
272 
273 			if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1, aRelations1 ) )
274 				return false;
275 
276 			if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2, aRelations2 ) )
277 				return false;
278 
279 			// the storage must be disposed before removing
280 			if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
281 				return false;
282 
283 			// remove element and check that it was removed completelly
284 			if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
285 				return false;
286 
287 			try
288 			{
289 				XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
290 				if ( xResAccess.hasByName( "SubStorage2" ) )
291 					m_aTestHelper.Error( "SubStorage2 must be removed already!" );
292 			}
293 			catch( Exception e )
294 			{
295 				m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
296 				return false;
297 			}
298 
299 			try
300 			{
301 				xResSubStorage2.isStreamElement( "SubStream2" );
302 
303 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
304 				return false;
305 			}
306 			catch( com.sun.star.lang.DisposedException de )
307 			{
308 			}
309 			catch( Exception e )
310 			{
311 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
312 				return false;
313 			}
314 
315 			// dispose used storages to free resources
316 			if ( !m_aTestHelper.disposeStorage( xResStorage ) )
317 				return false;
318 
319 			return true;
320 		}
321 		catch( Exception e )
322 		{
323 			m_aTestHelper.Error( "Exception: " + e );
324 			return false;
325 		}
326     }
327 
328 }
329 
330