xref: /trunk/main/package/qa/ofopxmlstorages/Test07.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
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.lang.XMultiServiceFactory;
25cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir import com.sun.star.uno.XInterface;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
32cdf0e10cSrcweir import com.sun.star.io.XStream;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.embed.*;
35cdf0e10cSrcweir import com.sun.star.beans.StringPair;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import share.LogWriter;
38cdf0e10cSrcweir import complex.ofopxmlstorages.TestHelper;
39cdf0e10cSrcweir import complex.ofopxmlstorages.StorageTest;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir public class Test07 implements StorageTest {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     XMultiServiceFactory m_xMSF;
44cdf0e10cSrcweir     XSingleServiceFactory m_xStorageFactory;
45cdf0e10cSrcweir     TestHelper m_aTestHelper;
46cdf0e10cSrcweir 
Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )47cdf0e10cSrcweir     public Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         m_xMSF = xMSF;
50cdf0e10cSrcweir         m_xStorageFactory = xStorageFactory;
51cdf0e10cSrcweir         m_aTestHelper = new TestHelper( aLogWriter, "Test07: " );
52cdf0e10cSrcweir     }
53cdf0e10cSrcweir 
test()54cdf0e10cSrcweir     public boolean test()
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         StringPair[][] aRelations1 =
57cdf0e10cSrcweir             { { new StringPair( "Id", "Num1" ) },
58cdf0e10cSrcweir               { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
59cdf0e10cSrcweir               { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
60cdf0e10cSrcweir               { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
61cdf0e10cSrcweir               { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
62cdf0e10cSrcweir               { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
63cdf0e10cSrcweir               { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
64cdf0e10cSrcweir             };
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         StringPair[][] aRelations2 =
67cdf0e10cSrcweir             { { new StringPair( "Id", "Num1" ) },
68cdf0e10cSrcweir               { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
69cdf0e10cSrcweir               { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
70cdf0e10cSrcweir               { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
71cdf0e10cSrcweir               { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
72cdf0e10cSrcweir               { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
73cdf0e10cSrcweir               { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
74cdf0e10cSrcweir             };
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         try
77cdf0e10cSrcweir         {
78cdf0e10cSrcweir             // create temporary storage based on arbitrary medium
79cdf0e10cSrcweir             // after such a storage is closed it is lost
80cdf0e10cSrcweir             XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
81cdf0e10cSrcweir             if ( xTempStorage == null )
82cdf0e10cSrcweir             {
83cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
84cdf0e10cSrcweir                 return false;
85cdf0e10cSrcweir             }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             byte pBytes1[] = { 1, 1, 1, 1, 1 };
88cdf0e10cSrcweir 
89cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
90cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage,
91cdf0e10cSrcweir                                                         "SubStream1",
92cdf0e10cSrcweir                                                         "MediaType1",
93cdf0e10cSrcweir                                                         true,
94cdf0e10cSrcweir                                                         pBytes1,
95cdf0e10cSrcweir                                                         aRelations1 ) )
96cdf0e10cSrcweir                 return false;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 
99cdf0e10cSrcweir             // open a new substorage
100cdf0e10cSrcweir             XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
101cdf0e10cSrcweir                                                                         "SubStorage1",
102cdf0e10cSrcweir                                                                         ElementModes.WRITE );
103cdf0e10cSrcweir             if ( xTempSubStorage == null )
104cdf0e10cSrcweir             {
105cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create substorage!" );
106cdf0e10cSrcweir                 return false;
107cdf0e10cSrcweir             }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir             byte pBytes2[] = { 2, 2, 2, 2, 2 };
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
112cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
113cdf0e10cSrcweir                                                         "SubStream2",
114cdf0e10cSrcweir                                                         "MediaType2",
115cdf0e10cSrcweir                                                         true,
116cdf0e10cSrcweir                                                         pBytes2,
117cdf0e10cSrcweir                                                         aRelations2 ) )
118cdf0e10cSrcweir                 return false;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
121cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
122cdf0e10cSrcweir                                                             true,
123cdf0e10cSrcweir                                                             ElementModes.WRITE,
124cdf0e10cSrcweir                                                             aRelations2 ) )
125cdf0e10cSrcweir                 return false;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
128cdf0e10cSrcweir                                                             false,
129cdf0e10cSrcweir                                                             ElementModes.WRITE,
130cdf0e10cSrcweir                                                             aRelations2 ) )
131cdf0e10cSrcweir                 return false;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             // ==============================
134cdf0e10cSrcweir             // check cloning at current state
135cdf0e10cSrcweir             // ==============================
136cdf0e10cSrcweir 
137*a893be29SPedro Giffuni             // the new storage still was not committed so the clone must be empty
138cdf0e10cSrcweir             XStorage xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xMSF, m_xStorageFactory, xTempStorage, "SubStorage1" );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             if ( xClonedSubStorage == null )
141cdf0e10cSrcweir             {
142cdf0e10cSrcweir                 m_aTestHelper.Error( "The result of clone is empty!" );
143cdf0e10cSrcweir                 return false;
144cdf0e10cSrcweir             }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir             XNameAccess xClonedNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xClonedSubStorage );
147cdf0e10cSrcweir             if ( xClonedNameAccess == null )
148cdf0e10cSrcweir             {
149cdf0e10cSrcweir                 m_aTestHelper.Error( "XNameAccess is not implemented by the clone!" );
150cdf0e10cSrcweir                 return false;
151cdf0e10cSrcweir             }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage,
154cdf0e10cSrcweir                                                         true,
155cdf0e10cSrcweir                                                         ElementModes.WRITE,
156cdf0e10cSrcweir                                                         new StringPair[0][0] ) )
157cdf0e10cSrcweir                 return false;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir             if ( xClonedNameAccess.hasElements() )
160cdf0e10cSrcweir             {
161*a893be29SPedro Giffuni                 m_aTestHelper.Error( "The new substorage still was not committed so it must be empty!" );
162cdf0e10cSrcweir                 return false;
163cdf0e10cSrcweir             }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xClonedSubStorage ) )
166cdf0e10cSrcweir                 return false;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir             xClonedSubStorage = null;
169cdf0e10cSrcweir             xClonedNameAccess = null;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             // the new stream was opened, written and closed, that means flashed
172cdf0e10cSrcweir             // so the clone must contain all the information
173cdf0e10cSrcweir             XStream xClonedSubStream = m_aTestHelper.cloneSubStream( xTempStorage, "SubStream1" );
174cdf0e10cSrcweir             if ( !m_aTestHelper.InternalCheckStream( xClonedSubStream,
175cdf0e10cSrcweir                                                     "SubStream1",
176cdf0e10cSrcweir                                                     "MediaType1",
177cdf0e10cSrcweir                                                     pBytes1,
178cdf0e10cSrcweir                                                     aRelations1 ) )
179cdf0e10cSrcweir                 return false;
180cdf0e10cSrcweir 
181cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStream( xClonedSubStream, "SubStream1" ) )
182cdf0e10cSrcweir                 return false;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             // ==============================
185cdf0e10cSrcweir             // commit substorage and check cloning
186cdf0e10cSrcweir             // ==============================
187cdf0e10cSrcweir 
188cdf0e10cSrcweir             if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
189cdf0e10cSrcweir                 return false;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir             xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xMSF, m_xStorageFactory, xTempStorage, "SubStorage1" );
192cdf0e10cSrcweir             if ( xClonedSubStorage == null )
193cdf0e10cSrcweir             {
194cdf0e10cSrcweir                 m_aTestHelper.Error( "The result of clone is empty!" );
195cdf0e10cSrcweir                 return false;
196cdf0e10cSrcweir             }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage,
199cdf0e10cSrcweir                                                         true,
200cdf0e10cSrcweir                                                         ElementModes.WRITE,
201cdf0e10cSrcweir                                                         aRelations2 ) )
202cdf0e10cSrcweir                 return false;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xClonedSubStorage,
205cdf0e10cSrcweir                                             "SubStream2",
206cdf0e10cSrcweir                                             "MediaType2",
207cdf0e10cSrcweir                                             pBytes2,
208cdf0e10cSrcweir                                             aRelations2 ) )
209cdf0e10cSrcweir                 return false;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir             XStorage xCloneOfRoot = m_aTestHelper.cloneStorage( m_xMSF, m_xStorageFactory, xTempStorage );
212cdf0e10cSrcweir             if ( xCloneOfRoot == null )
213cdf0e10cSrcweir             {
214cdf0e10cSrcweir                 m_aTestHelper.Error( "The result of root clone is empty!" );
215cdf0e10cSrcweir                 return false;
216cdf0e10cSrcweir             }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir             XNameAccess xCloneOfRootNA = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xCloneOfRoot );
219cdf0e10cSrcweir             if ( xCloneOfRootNA == null )
220cdf0e10cSrcweir             {
221cdf0e10cSrcweir                 m_aTestHelper.Error( "XNameAccess is not implemented by the root clone!" );
222cdf0e10cSrcweir                 return false;
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir             if ( xCloneOfRootNA.hasElements() )
226cdf0e10cSrcweir             {
227*a893be29SPedro Giffuni                 m_aTestHelper.Error( "The root storage still was not committed so it's clone must be empty!" );
228cdf0e10cSrcweir                 return false;
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xCloneOfRoot ) )
232cdf0e10cSrcweir                 return false;
233cdf0e10cSrcweir 
234cdf0e10cSrcweir             xCloneOfRoot = null;
235cdf0e10cSrcweir 
236cdf0e10cSrcweir             // ==============================
237cdf0e10cSrcweir             // commit root storage and check cloning
238cdf0e10cSrcweir             // ==============================
239cdf0e10cSrcweir 
240cdf0e10cSrcweir             if ( !m_aTestHelper.commitStorage( xTempStorage ) )
241cdf0e10cSrcweir                 return false;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir             xCloneOfRoot = m_aTestHelper.cloneStorage( m_xMSF, m_xStorageFactory, xTempStorage );
244cdf0e10cSrcweir             if ( xCloneOfRoot == null )
245cdf0e10cSrcweir             {
246cdf0e10cSrcweir                 m_aTestHelper.Error( "The result of root clone is empty!" );
247cdf0e10cSrcweir                 return false;
248cdf0e10cSrcweir             }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir             XStorage xSubStorageOfClone = xCloneOfRoot.openStorageElement( "SubStorage1", ElementModes.READ );
251cdf0e10cSrcweir             if ( xSubStorageOfClone == null )
252cdf0e10cSrcweir             {
253cdf0e10cSrcweir                 m_aTestHelper.Error( "The result of root clone is wrong!" );
254cdf0e10cSrcweir                 return false;
255cdf0e10cSrcweir             }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xSubStorageOfClone,
258cdf0e10cSrcweir                                                         false,
259cdf0e10cSrcweir                                                         ElementModes.READ,
260cdf0e10cSrcweir                                                         aRelations2 ) )
261cdf0e10cSrcweir                 return false;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xSubStorageOfClone,
264cdf0e10cSrcweir                                             "SubStream2",
265cdf0e10cSrcweir                                             "MediaType2",
266cdf0e10cSrcweir                                             pBytes2,
267cdf0e10cSrcweir                                             aRelations2 ) )
268cdf0e10cSrcweir                 return false;
269cdf0e10cSrcweir 
270cdf0e10cSrcweir             return true;
271cdf0e10cSrcweir         }
272cdf0e10cSrcweir         catch( Exception e )
273cdf0e10cSrcweir         {
274cdf0e10cSrcweir             m_aTestHelper.Error( "Exception: " + e );
275cdf0e10cSrcweir             return false;
276cdf0e10cSrcweir         }
277cdf0e10cSrcweir     }
278cdf0e10cSrcweir }
279