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