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