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 import com.sun.star.io.XInputStream;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.uno.AnyConverter;
35 
36 import com.sun.star.embed.*;
37 
38 import share.LogWriter;
39 import complex.storages.TestHelper;
40 import complex.storages.StorageTest;
41 
42 ///////////////////////////////////
43 // Tests also fix for i51352
44 ///////////////////////////////////
45 
46 public class RegressionTest_i27773 implements StorageTest {
47 
48 	XMultiServiceFactory m_xMSF;
49 	XSingleServiceFactory m_xStorageFactory;
50 	TestHelper m_aTestHelper;
51 
RegressionTest_i27773( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )52 	public RegressionTest_i27773( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
53 	{
54 		m_xMSF = xMSF;
55 		m_xStorageFactory = xStorageFactory;
56 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i27773: " );
57 	}
58 
test()59     public boolean test()
60 	{
61 		try
62 		{
63 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
64 			if ( xTempFileStream == null )
65 				return false;
66 
67 			if ( true )
68 			{
69 				// for debugging proposes
70 
71 				XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xTempFileStream );
72 				if ( xPropSet != null )
73 				{
74 					try
75 					{
76 						String sTempURL = AnyConverter.toString( xPropSet.getPropertyValue( "Uri" ) );
77 						// m_aTestHelper.Message( "URL: " + sTempURL );
78 						xPropSet.setPropertyValue( "RemoveFile", new Boolean( false ) );
79 					}
80 					catch ( Exception e )
81 					{
82 					}
83 				}
84 			}
85 
86 			// create storage based on the temporary stream
87 			Object pArgs[] = new Object[2];
88 			pArgs[0] = (Object) xTempFileStream;
89 			pArgs[1] = new Integer( ElementModes.WRITE );
90 
91 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
92 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
93 			if ( xTempStorage == null )
94 			{
95 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
96 				return false;
97 			}
98 
99 			// open a new substorage
100 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
101 																	"SubStorage1",
102 																	ElementModes.WRITE );
103 			if ( xTempSubStorage == null )
104 			{
105 				m_aTestHelper.Error( "Can't create substorage!" );
106 				return false;
107 			}
108 
109 			// open an empty substorage
110 			XStorage xEmptySubStorage = m_aTestHelper.openSubStorage( xTempStorage,
111 																	"EmptySubStorage1",
112 																	ElementModes.WRITE );
113 			if ( xEmptySubStorage == null )
114 			{
115 				m_aTestHelper.Error( "Can't create substorage!" );
116 				return false;
117 			}
118 
119 			// open an empty substorage
120 			XStorage xEmptySubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
121 																		"EmptySubSubStorage1",
122 																		ElementModes.WRITE );
123 			if ( xEmptySubSubStorage == null )
124 			{
125 				m_aTestHelper.Error( "Can't create substorage!" );
126 				return false;
127 			}
128 
129 
130 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
131 
132 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
133 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
134 				return false;
135 
136 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
137 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
138 															"MediaType2",
139 															true,
140 															ElementModes.WRITE ) )
141 				return false;
142 
143 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
144 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xEmptySubStorage,
145 															"MediaType3",
146 															false,
147 															ElementModes.WRITE ) )
148 				return false;
149 
150 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
151 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
152 															"MediaType4",
153 															false,
154 															ElementModes.WRITE ) )
155 				return false;
156 
157 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
158 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xEmptySubSubStorage,
159 															"MediaType5",
160 															false,
161 															ElementModes.WRITE ) )
162 				return false;
163 
164 
165 			// make a copy of substorage
166 
167 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempStorage, "SubStorage1_copy" ) )
168 				return false;
169 
170 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "EmptySubStorage1", xTempStorage, "EmptySubStorage1_copy" ) )
171 				return false;
172 
173 			// ================================================
174 			// copy all the changed and noncommited substorages
175 			// and dispose them
176 			// ================================================
177 
178 			if ( !m_aTestHelper.commitStorage( xEmptySubSubStorage ) )
179 				return false;
180 
181 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
182 				return false;
183 
184 			if ( !m_aTestHelper.commitStorage( xEmptySubStorage ) )
185 				return false;
186 
187 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
188 				return false;
189 
190 			// dispose substorages
191 
192 			if ( !m_aTestHelper.disposeStorage( xEmptySubSubStorage ) )
193 				return false;
194 
195 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
196 				return false;
197 
198 			if ( !m_aTestHelper.disposeStorage( xEmptySubStorage ) )
199 				return false;
200 
201 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
202 				return false;
203 
204 			// ================================================
205 			// reopen the storage in readonly mode an check contents
206 			// ================================================
207 
208 			pArgs[1] = new Integer( ElementModes.READ );
209 
210 			oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
211 			xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
212 			if ( xTempStorage == null )
213 			{
214 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
215 				return false;
216 			}
217 
218 			// open original substorage
219 			xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
220 																	"SubStorage1",
221 																	ElementModes.READ );
222 			if ( xTempSubStorage == null )
223 			{
224 				m_aTestHelper.Error( "Can't create substorage!" );
225 				return false;
226 			}
227 
228 			// open copy of the original substorage
229 			XStorage xTempSubStorage_copy = m_aTestHelper.openSubStorage( xTempStorage,
230 																	"SubStorage1_copy",
231 																	ElementModes.READ );
232 			if ( xTempSubStorage_copy == null )
233 			{
234 				m_aTestHelper.Error( "Can't create substorage!" );
235 				return false;
236 			}
237 
238 			// open empty substorage
239 			xEmptySubStorage = m_aTestHelper.openSubStorage( xTempStorage,
240 															"EmptySubStorage1",
241 															ElementModes.READ );
242 			if ( xEmptySubStorage == null )
243 			{
244 				m_aTestHelper.Error( "Can't create substorage!" );
245 				return false;
246 			}
247 
248 			// open copy of empty substorage
249 			XStorage xEmptySubStorage_copy = m_aTestHelper.openSubStorage( xTempStorage,
250 																		"EmptySubStorage1_copy",
251 																		ElementModes.READ );
252 			if ( xEmptySubStorage_copy == null )
253 			{
254 				m_aTestHelper.Error( "Can't create substorage!" );
255 				return false;
256 			}
257 
258 			// open an empty substorage of the substorage
259 			xEmptySubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
260 																"EmptySubSubStorage1",
261 																ElementModes.READ );
262 			if ( xEmptySubSubStorage == null )
263 			{
264 				m_aTestHelper.Error( "Can't create substorage!" );
265 				return false;
266 			}
267 
268 			// open an empty substorage of the substorage copy
269 			XStorage xEmptySubSubStorage_inCopy = m_aTestHelper.openSubStorage( xTempSubStorage_copy,
270 																				"EmptySubSubStorage1",
271 																				ElementModes.READ );
272 			if ( xEmptySubSubStorage_inCopy == null )
273 			{
274 				m_aTestHelper.Error( "Can't create substorage!" );
275 				return false;
276 			}
277 
278 
279 			// check contents
280 
281 			if ( !m_aTestHelper.checkStorageProperties( xEmptySubSubStorage, "MediaType5", false, ElementModes.READ ) )
282 				return false;
283 
284 			if ( !m_aTestHelper.checkStorageProperties( xEmptySubSubStorage_inCopy, "MediaType5", false, ElementModes.READ ) )
285 				return false;
286 
287 			if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage, "MediaType4", false, ElementModes.READ ) )
288 				return false;
289 
290 			if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage_copy, "MediaType4", false, ElementModes.READ ) )
291 				return false;
292 
293 			if ( !m_aTestHelper.checkStorageProperties( xEmptySubStorage, "MediaType3", false, ElementModes.READ ) )
294 				return false;
295 
296 			if ( !m_aTestHelper.checkStorageProperties( xEmptySubStorage_copy, "MediaType3", false, ElementModes.READ ) )
297 				return false;
298 
299 			if ( !m_aTestHelper.checkStorageProperties( xTempStorage, "MediaType2", true, ElementModes.READ ) )
300 				return false;
301 
302 			if ( !m_aTestHelper.checkStream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
303 				return false;
304 
305 			if ( !m_aTestHelper.checkStream( xTempSubStorage_copy, "SubStream1", "MediaType1", true, pBytes1 ) )
306 				return false;
307 
308 			// the root storage is based on the temporary stream so it can be left undisposed, since it does not lock
309 			// any resource, later the garbage collector will release the object and it must die by refcount
310 
311 			return true;
312 		}
313 		catch( Exception e )
314 		{
315 			m_aTestHelper.Error( "Exception: " + e );
316 			return false;
317 		}
318     }
319 }
320 
321