xref: /trunk/main/package/qa/storages/RegressionTest_i59886.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1*a740f2aaSAndrew Rist /**************************************************************
2*a740f2aaSAndrew Rist  *
3*a740f2aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a740f2aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a740f2aaSAndrew Rist  * distributed with this work for additional information
6*a740f2aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a740f2aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a740f2aaSAndrew Rist  * "License"); you may not use this file except in compliance
9*a740f2aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a740f2aaSAndrew Rist  *
11*a740f2aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a740f2aaSAndrew Rist  *
13*a740f2aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a740f2aaSAndrew Rist  * software distributed under the License is distributed on an
15*a740f2aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a740f2aaSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a740f2aaSAndrew Rist  * specific language governing permissions and limitations
18*a740f2aaSAndrew Rist  * under the License.
19*a740f2aaSAndrew Rist  *
20*a740f2aaSAndrew Rist  *************************************************************/
21*a740f2aaSAndrew Rist 
22cdf0e10cSrcweir package complex.storages;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.XInterface;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.uno.XInterface;
31cdf0e10cSrcweir import com.sun.star.io.XStream;
32cdf0e10cSrcweir import com.sun.star.io.XInputStream;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.embed.*;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import share.LogWriter;
37cdf0e10cSrcweir import complex.storages.TestHelper;
38cdf0e10cSrcweir import complex.storages.StorageTest;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir public class RegressionTest_i59886 implements StorageTest {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir     XMultiServiceFactory m_xMSF;
43cdf0e10cSrcweir     XSingleServiceFactory m_xStorageFactory;
44cdf0e10cSrcweir     TestHelper m_aTestHelper;
45cdf0e10cSrcweir 
RegressionTest_i59886( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46cdf0e10cSrcweir     public RegressionTest_i59886( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47cdf0e10cSrcweir     {
48cdf0e10cSrcweir         m_xMSF = xMSF;
49cdf0e10cSrcweir         m_xStorageFactory = xStorageFactory;
50cdf0e10cSrcweir         m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i59886: " );
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir 
test()53cdf0e10cSrcweir     public boolean test()
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         try
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir             XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
58cdf0e10cSrcweir             if ( xTempFileStream == null )
59cdf0e10cSrcweir                 return false;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir             // create storage based on the temporary stream
62cdf0e10cSrcweir             Object pArgs[] = new Object[2];
63cdf0e10cSrcweir             pArgs[0] = (Object) xTempFileStream;
64cdf0e10cSrcweir             pArgs[1] = new Integer( ElementModes.WRITE );
65cdf0e10cSrcweir 
66cdf0e10cSrcweir             Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67cdf0e10cSrcweir             XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
68cdf0e10cSrcweir             if ( xTempStorage == null )
69cdf0e10cSrcweir             {
70cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
71cdf0e10cSrcweir                 return false;
72cdf0e10cSrcweir             }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir             byte pBytes[] = new byte[36000];
75cdf0e10cSrcweir             for ( int nInd = 0; nInd < 36000; nInd++ )
76cdf0e10cSrcweir                 pBytes[nInd] = (byte)( nInd % 128 );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir             String sPass = "12345";
79cdf0e10cSrcweir 
80cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
81cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes, sPass ) )
82cdf0e10cSrcweir                 return false;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             // open a new substorage
85cdf0e10cSrcweir             XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
86cdf0e10cSrcweir                                                                         "SubStorage1",
87cdf0e10cSrcweir                                                                         ElementModes.WRITE );
88cdf0e10cSrcweir             if ( xTempSubStorage == null )
89cdf0e10cSrcweir             {
90cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create substorage!" );
91cdf0e10cSrcweir                 return false;
92cdf0e10cSrcweir             }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes, sPass ) )
96cdf0e10cSrcweir                 return false;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
99cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
100cdf0e10cSrcweir                                                             "MediaType3",
101cdf0e10cSrcweir                                                             true,
102cdf0e10cSrcweir                                                             ElementModes.WRITE ) )
103cdf0e10cSrcweir                 return false;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir             // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
106cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
107cdf0e10cSrcweir                                                             "MediaType4",
108cdf0e10cSrcweir                                                             false,
109cdf0e10cSrcweir                                                             ElementModes.WRITE ) )
110cdf0e10cSrcweir                 return false;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir             // commit substorage first
113cdf0e10cSrcweir             if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
114cdf0e10cSrcweir                 return false;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir             // commit the root storage so the contents must be stored now
117cdf0e10cSrcweir             if ( !m_aTestHelper.commitStorage( xTempStorage ) )
118cdf0e10cSrcweir                 return false;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             // dispose used storage to free resources
121cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
122cdf0e10cSrcweir                 return false;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             // ================================================
125cdf0e10cSrcweir             // now reopen the storage, set the common storage key
126cdf0e10cSrcweir             // and copy the storage
127cdf0e10cSrcweir             // ================================================
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             Object oStep2TempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
130cdf0e10cSrcweir             XStorage xStep2TempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStep2TempStorage );
131cdf0e10cSrcweir             if ( xStep2TempStorage == null )
132cdf0e10cSrcweir             {
133cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
134cdf0e10cSrcweir                 return false;
135cdf0e10cSrcweir             }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             XStorage xStep2TempSubStorage = m_aTestHelper.openSubStorage( xStep2TempStorage,
139cdf0e10cSrcweir                                                                         "SubStorage1",
140cdf0e10cSrcweir                                                                         ElementModes.WRITE );
141cdf0e10cSrcweir             if ( xStep2TempSubStorage == null )
142cdf0e10cSrcweir             {
143cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create substorage!" );
144cdf0e10cSrcweir                 return false;
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir             // set the common storage password
148cdf0e10cSrcweir             XEncryptionProtectedSource xEncr = (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xStep2TempStorage );
149cdf0e10cSrcweir             if ( xEncr == null )
150cdf0e10cSrcweir             {
151cdf0e10cSrcweir                 m_aTestHelper.Error( "The storage does not support encryption access!" );
152cdf0e10cSrcweir                 return false;
153cdf0e10cSrcweir             }
154cdf0e10cSrcweir             try
155cdf0e10cSrcweir             {
156cdf0e10cSrcweir                 xEncr.setEncryptionPassword( sPass );
157cdf0e10cSrcweir             }
158cdf0e10cSrcweir             catch( Exception e )
159cdf0e10cSrcweir             {
160cdf0e10cSrcweir                 m_aTestHelper.Error( "Can not set the common storage password!" );
161cdf0e10cSrcweir                 return false;
162cdf0e10cSrcweir             }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir             // open the stream for writing and read them so that the cache is created, but do not change
165cdf0e10cSrcweir             byte pDummyData[][] = new byte[1][3];
166cdf0e10cSrcweir             XStream xTempStream1 = m_aTestHelper.OpenStream( xStep2TempStorage, "SubStream1", ElementModes.WRITE );
167cdf0e10cSrcweir             XStream xTempStream2 = m_aTestHelper.OpenStream( xStep2TempSubStorage, "SubStream2", ElementModes.WRITE );
168cdf0e10cSrcweir             if ( xTempStream1 == null || xTempStream2 == null )
169cdf0e10cSrcweir                 return false;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             XInputStream xTempInStream1 = xTempStream1.getInputStream();
172cdf0e10cSrcweir             XInputStream xTempInStream2 = xTempStream2.getInputStream();
173cdf0e10cSrcweir             if ( xTempInStream1 == null || xTempInStream2 == null )
174cdf0e10cSrcweir             {
175cdf0e10cSrcweir                 m_aTestHelper.Error( "No input stream is available!" );
176cdf0e10cSrcweir                 return false;
177cdf0e10cSrcweir             }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             xTempInStream1.readBytes( pDummyData, 3 );
180cdf0e10cSrcweir             xTempInStream2.readBytes( pDummyData, 3 );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 
183cdf0e10cSrcweir             // create temporary storage, it will be checked later
184cdf0e10cSrcweir             Object oTargetStorage = m_xStorageFactory.createInstance();
185cdf0e10cSrcweir             XStorage xTargetStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTargetStorage );
186cdf0e10cSrcweir             if ( xTargetStorage == null )
187cdf0e10cSrcweir             {
188cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
189cdf0e10cSrcweir                 return false;
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir             // copy the current storage to the target
193cdf0e10cSrcweir             try
194cdf0e10cSrcweir             {
195cdf0e10cSrcweir                 xStep2TempStorage.copyToStorage( xTargetStorage );
196cdf0e10cSrcweir             }
197cdf0e10cSrcweir             catch( Exception e )
198cdf0e10cSrcweir             {
199cdf0e10cSrcweir                 m_aTestHelper.Error( "Can not copy the storage with common storage password!" );
200cdf0e10cSrcweir                 return false;
201cdf0e10cSrcweir             }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             // dispose used storage to free resources
204cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xStep2TempStorage ) )
205cdf0e10cSrcweir                 return false;
206cdf0e10cSrcweir 
207cdf0e10cSrcweir             // ================================================
208cdf0e10cSrcweir             // now check all the information in the copy
209cdf0e10cSrcweir             // ================================================
210cdf0e10cSrcweir 
211cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xTargetStorage, "MediaType3", true, ElementModes.WRITE ) )
212cdf0e10cSrcweir                 return false;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir             // open existing substorage
215cdf0e10cSrcweir             XStorage xTargetSubStorage = m_aTestHelper.openSubStorage( xTargetStorage,
216cdf0e10cSrcweir                                                                         "SubStorage1",
217cdf0e10cSrcweir                                                                         ElementModes.WRITE );
218cdf0e10cSrcweir             if ( xTargetSubStorage == null )
219cdf0e10cSrcweir             {
220cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't open existing substorage!" );
221cdf0e10cSrcweir                 return false;
222cdf0e10cSrcweir             }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xTargetSubStorage, "MediaType4", false, ElementModes.WRITE ) )
225cdf0e10cSrcweir                 return false;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir             // set the common storage password
228cdf0e10cSrcweir             XEncryptionProtectedSource xTargetEncr = (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTargetStorage );
229cdf0e10cSrcweir             if ( xTargetEncr == null )
230cdf0e10cSrcweir             {
231cdf0e10cSrcweir                 m_aTestHelper.Error( "The storage does not support encryption access!" );
232cdf0e10cSrcweir                 return false;
233cdf0e10cSrcweir             }
234cdf0e10cSrcweir             try
235cdf0e10cSrcweir             {
236cdf0e10cSrcweir                 xTargetEncr.setEncryptionPassword( sPass );
237cdf0e10cSrcweir             }
238cdf0e10cSrcweir             catch( Exception e )
239cdf0e10cSrcweir             {
240cdf0e10cSrcweir                 m_aTestHelper.Error( "Can not set the common storage password!" );
241cdf0e10cSrcweir                 return false;
242cdf0e10cSrcweir             }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir             // check the streams
245cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xTargetStorage, "SubStream1", "MediaType1", true, pBytes ) )
246cdf0e10cSrcweir                 return false;
247cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xTargetSubStorage, "SubStream2", "MediaType2", true, pBytes ) )
248cdf0e10cSrcweir                 return false;
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 
251cdf0e10cSrcweir             // dispose used storages to free resources
252cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xTargetStorage ) )
253cdf0e10cSrcweir                 return false;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir             return true;
256cdf0e10cSrcweir         }
257cdf0e10cSrcweir         catch( Exception e )
258cdf0e10cSrcweir         {
259cdf0e10cSrcweir             m_aTestHelper.Error( "Exception: " + e );
260cdf0e10cSrcweir             return false;
261cdf0e10cSrcweir         }
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir }
264