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