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_i49755 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
RegressionTest_i49755( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public RegressionTest_i49755( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i49755: " );
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 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
75 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
76 															"MediaType1",
77 															true,
78 															ElementModes.WRITE ) )
79 				return false;
80 
81 
82 			byte pBytes[] = new byte[36000];
83 			for ( int nInd = 0; nInd < 36000; nInd++ )
84 				pBytes[nInd] = (byte)( nInd % 128 );
85 
86 			// open a new substorage
87 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
88 																		"SubStorage1",
89 																		ElementModes.WRITE );
90 			if ( xTempSubStorage == null )
91 			{
92 				m_aTestHelper.Error( "Can't create substorage!" );
93 				return false;
94 			}
95 
96 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
97 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
98 															"MediaType2",
99 															false,
100 															ElementModes.WRITE ) )
101 				return false;
102 
103 			// open a new substorage
104 			XStorage xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
105 																		"SubStorage2",
106 																		ElementModes.WRITE );
107 			if ( xTempSubStorage == null )
108 			{
109 				m_aTestHelper.Error( "Can't create substorage!" );
110 				return false;
111 			}
112 
113 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
114 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubSubStorage,
115 															"MediaType3",
116 															false,
117 															ElementModes.WRITE ) )
118 				return false;
119 
120 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
121 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubSubStorage, "SubStream1", "MediaType4", true, pBytes ) )
122 				return false;
123 
124 			// open a new substorage
125 			XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
126 																		"SubStorage3",
127 																		ElementModes.WRITE );
128 			if ( xTempSubStorage1 == null )
129 			{
130 				m_aTestHelper.Error( "Can't create substorage!" );
131 				return false;
132 			}
133 
134 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
135 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
136 															"MediaType5",
137 															false,
138 															ElementModes.WRITE ) )
139 				return false;
140 
141 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
142 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream2", "MediaType4", false, pBytes ) )
143 				return false;
144 
145 
146 			// commit substorages first
147 			if ( !m_aTestHelper.commitStorage( xTempSubSubStorage ) )
148 				return false;
149 
150 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
151 				return false;
152 
153 			if ( !m_aTestHelper.commitStorage( xTempSubStorage1 ) )
154 				return false;
155 
156 			// commit the root storage so the contents must be stored now
157 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
158 				return false;
159 
160 			// dispose used storage to free resources
161 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
162 				return false;
163 
164 			// ================================================
165 			// now change the contents of the second substorage
166 			// without changing of the contents of the first substorage
167 			// ================================================
168 
169 			Object oStep2TempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
170 			XStorage xStep2TempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStep2TempStorage );
171 			if ( xStep2TempStorage == null )
172 			{
173 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
174 				return false;
175 			}
176 
177 			XStorage xStep2TempSubStorage1 = m_aTestHelper.openSubStorage( xStep2TempStorage,
178 																			"SubStorage3",
179 																			ElementModes.WRITE );
180 			if ( xStep2TempSubStorage1 == null )
181 			{
182 				m_aTestHelper.Error( "Can't create substorage!" );
183 				return false;
184 			}
185 
186 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
187 			if ( !m_aTestHelper.WriteBytesToSubstream( xStep2TempSubStorage1, "SubStream2", "MediaType4", false, pBytes ) )
188 				return false;
189 
190 			if ( !m_aTestHelper.commitStorage( xStep2TempSubStorage1 ) )
191 				return false;
192 
193 			// commit the root storage so the contents must be stored now
194 			if ( !m_aTestHelper.commitStorage( xStep2TempStorage ) )
195 				return false;
196 
197 			// dispose used storage to free resources
198 			if ( !m_aTestHelper.disposeStorage( xStep2TempStorage ) )
199 				return false;
200 
201 
202 			// ================================================
203 			// now check all the written information
204 			// and the raw stream contents
205 			// ================================================
206 
207 			// close the output part of the temporary stream
208 			// the output part must present since we already wrote to the stream
209 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
210 				return false;
211 
212 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
213 			if ( xTempInStream == null )
214 				return false;
215 
216 			// open input stream
217 			// since no mode is provided the result storage must be opened readonly
218 			Object pOneArg[] = new Object[1];
219 			pOneArg[0] = (Object) xTempInStream;
220 
221 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
222 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
223 			if ( xResultStorage == null )
224 			{
225 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
226 				return false;
227 			}
228 
229 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType1", true, ElementModes.READ ) )
230 				return false;
231 
232 			// open existing substorage
233 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
234 																		"SubStorage1",
235 																		ElementModes.READ );
236 			if ( xResultSubStorage == null )
237 			{
238 				m_aTestHelper.Error( "Can't open existing substorage!" );
239 				return false;
240 			}
241 
242 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType2", false, ElementModes.READ ) )
243 				return false;
244 
245 			// open existing substorage
246 			XStorage xResultSubSubStorage = m_aTestHelper.openSubStorage( xResultSubStorage,
247 																			"SubStorage2",
248 																			ElementModes.READ );
249 			if ( xResultSubSubStorage == null )
250 			{
251 				m_aTestHelper.Error( "Can't open existing substorage!" );
252 				return false;
253 			}
254 
255 			if ( !m_aTestHelper.checkStorageProperties( xResultSubSubStorage, "MediaType3", false, ElementModes.READ ) )
256 				return false;
257 
258 			if ( !m_aTestHelper.checkStream( xResultSubSubStorage, "SubStream1", "MediaType4", true, pBytes ) )
259 				return false;
260 
261 
262 
263 			XStorage xResultSubStorage1 = m_aTestHelper.openSubStorage( xResultStorage,
264 																		"SubStorage3",
265 																		ElementModes.READ );
266 			if ( xResultSubStorage1 == null )
267 			{
268 				m_aTestHelper.Error( "Can't open existing substorage!" );
269 				return false;
270 			}
271 
272 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage1, "MediaType5", false, ElementModes.READ ) )
273 				return false;
274 
275 			if ( !m_aTestHelper.checkStream( xResultSubStorage1, "SubStream2", "MediaType4", false, pBytes ) )
276 				return false;
277 
278 
279 			// dispose used storages to free resources
280 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
281 				return false;
282 
283 			return true;
284 		}
285 		catch( Exception e )
286 		{
287 			m_aTestHelper.Error( "Exception: " + e );
288 			return false;
289 		}
290     }
291 
292 }
293 
294