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