xref: /aoo41x/main/package/qa/storages/Test11.java (revision a740f2aa)
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 
32cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
33cdf0e10cSrcweir import com.sun.star.io.XStream;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.embed.*;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import share.LogWriter;
38cdf0e10cSrcweir import complex.storages.TestHelper;
39cdf0e10cSrcweir import complex.storages.StorageTest;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir public class Test11 implements StorageTest {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
44cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
45cdf0e10cSrcweir 	TestHelper m_aTestHelper;
46cdf0e10cSrcweir 
Test11( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )47cdf0e10cSrcweir 	public Test11( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
48cdf0e10cSrcweir 	{
49cdf0e10cSrcweir 		m_xMSF = xMSF;
50cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
51cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test11: " );
52cdf0e10cSrcweir 	}
53cdf0e10cSrcweir 
test()54cdf0e10cSrcweir     public boolean test()
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		try
57cdf0e10cSrcweir 		{
58cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
59cdf0e10cSrcweir 			// after such a storage is closed it is lost
60cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstance();
61cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
62cdf0e10cSrcweir 			if ( xTempStorage == null )
63cdf0e10cSrcweir 			{
64cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
65cdf0e10cSrcweir 				return false;
66cdf0e10cSrcweir 			}
67cdf0e10cSrcweir 
68cdf0e10cSrcweir             byte pBigBytes[] = new byte[33000];
69cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 33000; nInd++ )
70cdf0e10cSrcweir 				pBigBytes[nInd] = (byte)( nInd % 128 );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 			String sPass1 = "111111111";
73cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
76cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1, sPass1 ) )
77cdf0e10cSrcweir 				return false;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
80cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes, sPass1 ) )
81cdf0e10cSrcweir 				return false;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 			// open a new substorage
84cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
85cdf0e10cSrcweir 																		"SubStorage1",
86cdf0e10cSrcweir 																		ElementModes.WRITE );
87cdf0e10cSrcweir 			if ( xTempSubStorage == null )
88cdf0e10cSrcweir 			{
89cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
90cdf0e10cSrcweir 				return false;
91cdf0e10cSrcweir 			}
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 			String sPass2 = "2222222222";
94cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
97cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream2", "MediaType2", true, pBytes2, sPass2 ) )
98cdf0e10cSrcweir 				return false;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
101cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", true, pBigBytes, sPass2 ) )
102cdf0e10cSrcweir 				return false;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
105cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
106cdf0e10cSrcweir 															"MediaType3",
107cdf0e10cSrcweir 															true,
108cdf0e10cSrcweir 															ElementModes.WRITE ) )
109cdf0e10cSrcweir 				return false;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
112cdf0e10cSrcweir 															"MediaType4",
113cdf0e10cSrcweir 															false,
114cdf0e10cSrcweir 															ElementModes.WRITE ) )
115cdf0e10cSrcweir 				return false;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 			// ==============================
118cdf0e10cSrcweir 			// check cloning at current state
119cdf0e10cSrcweir 			// ==============================
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 			// the new storage still was not commited so the clone must be empty
122cdf0e10cSrcweir 			XStorage xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xStorageFactory, xTempStorage, "SubStorage1" );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 			if ( xClonedSubStorage == null )
125cdf0e10cSrcweir 			{
126cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of clone is empty!" );
127cdf0e10cSrcweir 				return false;
128cdf0e10cSrcweir 			}
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 			XNameAccess xClonedNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xClonedSubStorage );
131cdf0e10cSrcweir 			if ( xClonedNameAccess == null )
132cdf0e10cSrcweir 			{
133cdf0e10cSrcweir 				m_aTestHelper.Error( "XNameAccess is not implemented by the clone!" );
134cdf0e10cSrcweir 				return false;
135cdf0e10cSrcweir 			}
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage, "", true, ElementModes.WRITE ) )
138cdf0e10cSrcweir 				return false;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 			if ( xClonedNameAccess.hasElements() )
141cdf0e10cSrcweir 			{
142cdf0e10cSrcweir 				m_aTestHelper.Error( "The new substorage still was not commited so it must be empty!" );
143cdf0e10cSrcweir 				return false;
144cdf0e10cSrcweir 			}
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xClonedSubStorage ) )
147cdf0e10cSrcweir 				return false;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 			xClonedSubStorage = null;
150cdf0e10cSrcweir 			xClonedNameAccess = null;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 			// the new stream was opened, written and closed, that means flashed
153cdf0e10cSrcweir 			// so the clone must contain all the information
154cdf0e10cSrcweir 			XStream xClonedSubStream = m_aTestHelper.cloneEncrSubStream( xTempStorage, "SubStream1", sPass1 );
155cdf0e10cSrcweir 			if ( !m_aTestHelper.InternalCheckStream( xClonedSubStream, "SubStream1", "MediaType1", true, pBytes1, true ) )
156cdf0e10cSrcweir 				return false;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 			XStream xClonedBigSubStream = m_aTestHelper.cloneEncrSubStream( xTempStorage, "BigSubStream1", sPass1 );
159cdf0e10cSrcweir 			if ( !m_aTestHelper.InternalCheckStream( xClonedBigSubStream, "BigSubStream1", "MediaType1", true, pBigBytes, true ) )
160cdf0e10cSrcweir 				return false;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStream( xClonedSubStream, "SubStream1" ) )
163cdf0e10cSrcweir 				return false;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStream( xClonedBigSubStream, "BigSubStream1" ) )
166cdf0e10cSrcweir 				return false;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 			// ==============================
169cdf0e10cSrcweir 			// commit substorage and check cloning
170cdf0e10cSrcweir 			// ==============================
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
173cdf0e10cSrcweir 				return false;
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 			xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xStorageFactory, xTempStorage, "SubStorage1" );
176cdf0e10cSrcweir 			if ( xClonedSubStorage == null )
177cdf0e10cSrcweir 			{
178cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of clone is empty!" );
179cdf0e10cSrcweir 				return false;
180cdf0e10cSrcweir 			}
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage, "MediaType4", true, ElementModes.WRITE ) )
183cdf0e10cSrcweir 				return false;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xClonedSubStorage, "SubStream2", "MediaType2", pBytes2, sPass2 ) )
186cdf0e10cSrcweir 				return false;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xClonedSubStorage, "BigSubStream2", "MediaType2", pBigBytes, sPass2 ) )
189cdf0e10cSrcweir 				return false;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 			// ==============================
192cdf0e10cSrcweir 			// commit the root storage and check cloning
193cdf0e10cSrcweir 			// ==============================
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
196cdf0e10cSrcweir 				return false;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 			XStorage xCloneOfRoot = m_aTestHelper.cloneStorage( m_xStorageFactory, xTempStorage );
199cdf0e10cSrcweir 			if ( xCloneOfRoot == null )
200cdf0e10cSrcweir 			{
201cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of root clone is empty!" );
202cdf0e10cSrcweir 				return false;
203cdf0e10cSrcweir 			}
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xCloneOfRoot, "MediaType3", true, ElementModes.WRITE ) )
206cdf0e10cSrcweir 				return false;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xCloneOfRoot, "SubStream1", "MediaType1", pBytes1, sPass1 ) )
209cdf0e10cSrcweir 				return false;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xCloneOfRoot, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) )
212cdf0e10cSrcweir 				return false;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 			XStorage xSubStorageOfClone = xCloneOfRoot.openStorageElement( "SubStorage1", ElementModes.READ );
215cdf0e10cSrcweir 			if ( xSubStorageOfClone == null )
216cdf0e10cSrcweir 			{
217cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of root clone is wrong!" );
218cdf0e10cSrcweir 				return false;
219cdf0e10cSrcweir 			}
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xSubStorageOfClone, "MediaType4", false, ElementModes.READ ) )
222cdf0e10cSrcweir 				return false;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xSubStorageOfClone, "SubStream2", "MediaType2", pBytes2, sPass2 ) )
225cdf0e10cSrcweir 				return false;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xSubStorageOfClone, "BigSubStream2", "MediaType2", pBigBytes, sPass2 ) )
228cdf0e10cSrcweir 				return false;
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 			return true;
231cdf0e10cSrcweir 		}
232cdf0e10cSrcweir 		catch( Exception e )
233cdf0e10cSrcweir 		{
234cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
235cdf0e10cSrcweir 			return false;
236cdf0e10cSrcweir 		}
237cdf0e10cSrcweir     }
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
240