xref: /trunk/main/package/qa/ofopxmlstorages/Test04.java (revision 152e651ef53ab2fae14bf93407606b2c7fcbd35b)
1a740f2aaSAndrew Rist /**************************************************************
2a740f2aaSAndrew Rist  *
3a740f2aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a740f2aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a740f2aaSAndrew Rist  * distributed with this work for additional information
6a740f2aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a740f2aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a740f2aaSAndrew Rist  * "License"); you may not use this file except in compliance
9a740f2aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10a740f2aaSAndrew Rist  *
11a740f2aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a740f2aaSAndrew Rist  *
13a740f2aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a740f2aaSAndrew Rist  * software distributed under the License is distributed on an
15a740f2aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a740f2aaSAndrew Rist  * KIND, either express or implied.  See the License for the
17a740f2aaSAndrew Rist  * specific language governing permissions and limitations
18a740f2aaSAndrew Rist  * under the License.
19a740f2aaSAndrew Rist  *
20a740f2aaSAndrew Rist  *************************************************************/
21a740f2aaSAndrew Rist 
22cdf0e10cSrcweir package complex.ofopxmlstorages;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.XInterface;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
27cdf0e10cSrcweir import com.sun.star.lang.DisposedException;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
31cdf0e10cSrcweir import com.sun.star.uno.XInterface;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.embed.*;
36cdf0e10cSrcweir import com.sun.star.beans.StringPair;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import share.LogWriter;
39cdf0e10cSrcweir import complex.ofopxmlstorages.TestHelper;
40cdf0e10cSrcweir import complex.ofopxmlstorages.StorageTest;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir public class Test04 implements StorageTest {
43cdf0e10cSrcweir 
44cdf0e10cSrcweir     XMultiServiceFactory m_xMSF;
45cdf0e10cSrcweir     XSingleServiceFactory m_xStorageFactory;
46cdf0e10cSrcweir     TestHelper m_aTestHelper;
47cdf0e10cSrcweir 
Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )48cdf0e10cSrcweir     public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         m_xMSF = xMSF;
51cdf0e10cSrcweir         m_xStorageFactory = xStorageFactory;
52cdf0e10cSrcweir         m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir 
test()55cdf0e10cSrcweir     public boolean test()
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir         StringPair[][] aRelations1 =
58cdf0e10cSrcweir             { { new StringPair( "Id", "Num1" ) },
59cdf0e10cSrcweir               { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
60cdf0e10cSrcweir               { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
61cdf0e10cSrcweir               { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
62cdf0e10cSrcweir               { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
63cdf0e10cSrcweir               { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
64cdf0e10cSrcweir               { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
65cdf0e10cSrcweir             };
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         StringPair[][] aRelations2 =
68cdf0e10cSrcweir             { { new StringPair( "Id", "Num1" ) },
69cdf0e10cSrcweir               { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
70cdf0e10cSrcweir               { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
71cdf0e10cSrcweir               { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
72cdf0e10cSrcweir               { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
73cdf0e10cSrcweir               { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
74cdf0e10cSrcweir               { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
75cdf0e10cSrcweir             };
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         try
78cdf0e10cSrcweir         {
79cdf0e10cSrcweir             String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
80cdf0e10cSrcweir             if ( sTempFileURL == null || sTempFileURL == "" )
81cdf0e10cSrcweir             {
82cdf0e10cSrcweir                 m_aTestHelper.Error( "No valid temporary file was created!" );
83cdf0e10cSrcweir                 return false;
84cdf0e10cSrcweir             }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir             // create temporary storage based on arbitrary medium
87cdf0e10cSrcweir             // after such a storage is closed it is lost
88cdf0e10cSrcweir             XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
89cdf0e10cSrcweir             if ( xTempStorage == null )
90cdf0e10cSrcweir             {
91cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
92cdf0e10cSrcweir                 return false;
93cdf0e10cSrcweir             }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir             // open substorages and create streams there
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             // first substorage of the root storage
98cdf0e10cSrcweir             XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
99cdf0e10cSrcweir                                                                         "SubStorage1",
100cdf0e10cSrcweir                                                                         ElementModes.WRITE );
101cdf0e10cSrcweir             if ( xTempSubStorage1 == null )
102cdf0e10cSrcweir             {
103cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create substorage!" );
104cdf0e10cSrcweir                 return false;
105cdf0e10cSrcweir             }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir             byte pBytes1[] = { 1, 1, 1, 1, 1 };
108cdf0e10cSrcweir 
109cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
110cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1,
111cdf0e10cSrcweir                                                         "SubStream1",
112cdf0e10cSrcweir                                                         "MediaType1",
113cdf0e10cSrcweir                                                         true,
114cdf0e10cSrcweir                                                         pBytes1,
115cdf0e10cSrcweir                                                         aRelations1 ) )
116cdf0e10cSrcweir                 return false;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             // second substorage of the root storage
119cdf0e10cSrcweir             XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
120cdf0e10cSrcweir                                                                         "SubStorage2",
121cdf0e10cSrcweir                                                                         ElementModes.WRITE );
122cdf0e10cSrcweir             if ( xTempSubStorage2 == null )
123cdf0e10cSrcweir             {
124cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create substorage!" );
125cdf0e10cSrcweir                 return false;
126cdf0e10cSrcweir             }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir             byte pBytes2[] = { 2, 2, 2, 2, 2 };
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
131cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2,
132cdf0e10cSrcweir                                                         "SubStream2",
133cdf0e10cSrcweir                                                         "MediaType2",
134cdf0e10cSrcweir                                                         false,
135cdf0e10cSrcweir                                                         pBytes2,
136cdf0e10cSrcweir                                                         aRelations2 ) )
137cdf0e10cSrcweir                 return false;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir             // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
140cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
141cdf0e10cSrcweir                                                             true,
142cdf0e10cSrcweir                                                             ElementModes.WRITE,
143cdf0e10cSrcweir                                                             aRelations2 ) )
144cdf0e10cSrcweir                 return false;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir             // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
147cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
148cdf0e10cSrcweir                                                             false,
149cdf0e10cSrcweir                                                             ElementModes.WRITE,
150cdf0e10cSrcweir                                                             aRelations2 ) )
151cdf0e10cSrcweir                 return false;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
154cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
155cdf0e10cSrcweir                                                             false,
156cdf0e10cSrcweir                                                             ElementModes.WRITE,
157cdf0e10cSrcweir                                                             aRelations2 ) )
158cdf0e10cSrcweir                 return false;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             // create temporary storage based on a previously created temporary file
161cdf0e10cSrcweir             XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
162cdf0e10cSrcweir                                                                             sTempFileURL,
163cdf0e10cSrcweir                                                                             ElementModes.WRITE );
164cdf0e10cSrcweir             if ( xTempFileStorage == null )
165cdf0e10cSrcweir             {
166cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
167cdf0e10cSrcweir                 return false;
168cdf0e10cSrcweir             }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir             if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
171cdf0e10cSrcweir                 return false;
172cdf0e10cSrcweir 
173a893be29SPedro Giffuni             // if storage is not committed before disposing all the changes will be lost
174cdf0e10cSrcweir             if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
175cdf0e10cSrcweir                 return false;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             // a storage must be disposed before moving/removing otherwise the access will be denied
178cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
179cdf0e10cSrcweir                 return false;
180cdf0e10cSrcweir 
181cdf0e10cSrcweir             if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
182cdf0e10cSrcweir                 return false;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             // SubStorage2 must be removed and disposed now
185cdf0e10cSrcweir             try
186cdf0e10cSrcweir             {
187cdf0e10cSrcweir                 xTempSubStorage2.isStreamElement( "SubStream2" );
188cdf0e10cSrcweir                 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
189cdf0e10cSrcweir                 return false;
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir             catch( com.sun.star.lang.DisposedException de )
192cdf0e10cSrcweir             {
193cdf0e10cSrcweir             }
194cdf0e10cSrcweir             catch( Exception e )
195cdf0e10cSrcweir             {
196cdf0e10cSrcweir                 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
197cdf0e10cSrcweir                 return false;
198cdf0e10cSrcweir             }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir             if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
201cdf0e10cSrcweir                 return false;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
204cdf0e10cSrcweir                 return false;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir             if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
207cdf0e10cSrcweir                 return false;
208cdf0e10cSrcweir 
209cdf0e10cSrcweir             if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
210cdf0e10cSrcweir                 return false;
211cdf0e10cSrcweir 
212cdf0e10cSrcweir             // dispose used storages to free resources
213cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
214cdf0e10cSrcweir                 return false;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir             // ================================================
217cdf0e10cSrcweir             // now check all the written and copied information
218cdf0e10cSrcweir             // ================================================
219cdf0e10cSrcweir 
220cdf0e10cSrcweir             // the temporary file must not be locked any more after storage disposing
221cdf0e10cSrcweir             XStorage xResStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
222cdf0e10cSrcweir                                                                         sTempFileURL,
223cdf0e10cSrcweir                                                                         ElementModes.WRITE );
224cdf0e10cSrcweir 
225cdf0e10cSrcweir             if ( xResStorage == null )
226cdf0e10cSrcweir             {
227cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
228cdf0e10cSrcweir                 return false;
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir             // open and check SubStorage1
232cdf0e10cSrcweir             XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
233cdf0e10cSrcweir                                                                         "SubStorage1",
234cdf0e10cSrcweir                                                                         ElementModes.READ );
235cdf0e10cSrcweir             if ( xResSubStorage1 == null )
236cdf0e10cSrcweir             {
237cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't open existing substorage!" );
238cdf0e10cSrcweir                 return false;
239cdf0e10cSrcweir             }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1,
242cdf0e10cSrcweir                                                         false,
243cdf0e10cSrcweir                                                         ElementModes.READ,
244cdf0e10cSrcweir                                                         aRelations2 ) )
245cdf0e10cSrcweir                 return false;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 
248cdf0e10cSrcweir             // open and check SubStorage2
249cdf0e10cSrcweir             XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
250cdf0e10cSrcweir                                                                         "SubStorage2",
251cdf0e10cSrcweir                                                                         ElementModes.READ );
252cdf0e10cSrcweir             if ( xResSubStorage2 == null )
253cdf0e10cSrcweir             {
254cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't open existing substorage!" );
255cdf0e10cSrcweir                 return false;
256cdf0e10cSrcweir             }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2,
259cdf0e10cSrcweir                                                         false,
260cdf0e10cSrcweir                                                         ElementModes.READ,
261cdf0e10cSrcweir                                                         aRelations2 ) )
262cdf0e10cSrcweir                 return false;
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 
265cdf0e10cSrcweir             // check all the result streams
266cdf0e10cSrcweir 
267cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1, aRelations1 ) )
268cdf0e10cSrcweir                 return false;
269cdf0e10cSrcweir 
270cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1, aRelations1 ) )
271cdf0e10cSrcweir                 return false;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1, aRelations1 ) )
274cdf0e10cSrcweir                 return false;
275cdf0e10cSrcweir 
276cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2, aRelations2 ) )
277cdf0e10cSrcweir                 return false;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir             // the storage must be disposed before removing
280cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
281cdf0e10cSrcweir                 return false;
282cdf0e10cSrcweir 
283*152e651eSJohn Bampton             // remove element and check that it was removed completely
284cdf0e10cSrcweir             if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
285cdf0e10cSrcweir                 return false;
286cdf0e10cSrcweir 
287cdf0e10cSrcweir             try
288cdf0e10cSrcweir             {
289cdf0e10cSrcweir                 XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
290cdf0e10cSrcweir                 if ( xResAccess.hasByName( "SubStorage2" ) )
291cdf0e10cSrcweir                     m_aTestHelper.Error( "SubStorage2 must be removed already!" );
292cdf0e10cSrcweir             }
293cdf0e10cSrcweir             catch( Exception e )
294cdf0e10cSrcweir             {
295cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
296cdf0e10cSrcweir                 return false;
297cdf0e10cSrcweir             }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir             try
300cdf0e10cSrcweir             {
301cdf0e10cSrcweir                 xResSubStorage2.isStreamElement( "SubStream2" );
302cdf0e10cSrcweir 
303cdf0e10cSrcweir                 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
304cdf0e10cSrcweir                 return false;
305cdf0e10cSrcweir             }
306cdf0e10cSrcweir             catch( com.sun.star.lang.DisposedException de )
307cdf0e10cSrcweir             {
308cdf0e10cSrcweir             }
309cdf0e10cSrcweir             catch( Exception e )
310cdf0e10cSrcweir             {
311cdf0e10cSrcweir                 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
312cdf0e10cSrcweir                 return false;
313cdf0e10cSrcweir             }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir             // dispose used storages to free resources
316cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xResStorage ) )
317cdf0e10cSrcweir                 return false;
318cdf0e10cSrcweir 
319cdf0e10cSrcweir             return true;
320cdf0e10cSrcweir         }
321cdf0e10cSrcweir         catch( Exception e )
322cdf0e10cSrcweir         {
323cdf0e10cSrcweir             m_aTestHelper.Error( "Exception: " + e );
324cdf0e10cSrcweir             return false;
325cdf0e10cSrcweir         }
326cdf0e10cSrcweir     }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir }
329