xref: /trunk/main/package/qa/storages/Test07.java (revision a893be29)
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.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.embed.*;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import share.LogWriter;
35cdf0e10cSrcweir import complex.storages.TestHelper;
36cdf0e10cSrcweir import complex.storages.StorageTest;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public class Test07 implements StorageTest {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
41cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
42cdf0e10cSrcweir 	TestHelper m_aTestHelper;
43cdf0e10cSrcweir 
Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44cdf0e10cSrcweir 	public Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45cdf0e10cSrcweir 	{
46cdf0e10cSrcweir 		m_xMSF = xMSF;
47cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
48cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test07: " );
49cdf0e10cSrcweir 	}
50cdf0e10cSrcweir 
test()51cdf0e10cSrcweir     public boolean test()
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 		try
54cdf0e10cSrcweir 		{
55cdf0e10cSrcweir 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
56cdf0e10cSrcweir 			if ( sTempFileURL == null || sTempFileURL == "" )
57cdf0e10cSrcweir 			{
58cdf0e10cSrcweir 				m_aTestHelper.Error( "No valid temporary file was created!" );
59cdf0e10cSrcweir 				return false;
60cdf0e10cSrcweir 			}
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
63cdf0e10cSrcweir 			// after such a storage is closed it is lost
64cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstance();
65cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
66cdf0e10cSrcweir 			if ( xTempStorage == null )
67cdf0e10cSrcweir 			{
68cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
69cdf0e10cSrcweir 				return false;
70cdf0e10cSrcweir 			}
71cdf0e10cSrcweir 
72cdf0e10cSrcweir             byte pBigBytes[] = new byte[33000];
73cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 33000; nInd++ )
74cdf0e10cSrcweir 				pBigBytes[nInd] = (byte)( nInd % 128 );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
77cdf0e10cSrcweir 			String sPass1 = "12345";
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 substream, set "MediaType" and "Compressed" properties to it and write some bytes
84cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1, sPass1 ) )
85cdf0e10cSrcweir 				return false;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
88cdf0e10cSrcweir 			String sPass2 = "54321";
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream2", "MediaType2", false, pBigBytes, sPass2 ) )
92cdf0e10cSrcweir 				return false;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream2", "MediaType2", false, pBytes2, sPass2 ) )
96cdf0e10cSrcweir 				return false;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 			// create temporary storage based on a previously created temporary file
99cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
100cdf0e10cSrcweir 			pArgs[0] = (Object) sTempFileURL;
101cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
104cdf0e10cSrcweir 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
105cdf0e10cSrcweir 			if ( xTempFileStorage == null )
106cdf0e10cSrcweir 			{
107cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
108cdf0e10cSrcweir 				return false;
109cdf0e10cSrcweir 			}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 			// copy xTempStorage to xTempFileStorage
112*a893be29SPedro Giffuni 			// xTempFileStorage will be automatically committed
113cdf0e10cSrcweir 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
114cdf0e10cSrcweir 				return false;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 			// dispose used storages to free resources
117cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
118cdf0e10cSrcweir 				return false;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 			// ================================================
121cdf0e10cSrcweir 			// now check all the written and copied information
122cdf0e10cSrcweir 			// ================================================
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 			// the temporary file must not be locked any more after storage disposing
125cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
126cdf0e10cSrcweir 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
127cdf0e10cSrcweir 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
128cdf0e10cSrcweir 			if ( xResultStorage == null )
129cdf0e10cSrcweir 			{
130cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
131cdf0e10cSrcweir 				return false;
132cdf0e10cSrcweir 			}
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 			Object o2CopyStorage = m_xStorageFactory.createInstance();
135cdf0e10cSrcweir 			XStorage x2CopyStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, o2CopyStorage );
136cdf0e10cSrcweir 			if ( x2CopyStorage == null )
137cdf0e10cSrcweir 			{
138cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
139cdf0e10cSrcweir 				return false;
140cdf0e10cSrcweir 			}
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 			if ( !m_aTestHelper.copyStorage( xResultStorage, x2CopyStorage ) )
143cdf0e10cSrcweir 				return false;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes1, sPass1 ) )
146cdf0e10cSrcweir 				return false;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) )
149cdf0e10cSrcweir 				return false;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream2", "MediaType2", pBytes2, sPass2 ) )
152cdf0e10cSrcweir 				return false;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream2", "MediaType2", pBigBytes, sPass2 ) )
155cdf0e10cSrcweir 				return false;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream1", "MediaType1", pBytes1, sPass1 ) )
158cdf0e10cSrcweir 				return false;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) )
161cdf0e10cSrcweir 				return false;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream2", "MediaType2", pBytes2, sPass2 ) )
164cdf0e10cSrcweir 				return false;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "BigSubStream2", "MediaType2", pBigBytes, sPass2 ) )
167cdf0e10cSrcweir 				return false;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 			// dispose used storages to free resources
170cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
171cdf0e10cSrcweir 				return false;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 			return true;
174cdf0e10cSrcweir 		}
175cdf0e10cSrcweir 		catch( Exception e )
176cdf0e10cSrcweir 		{
177cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
178cdf0e10cSrcweir 			return false;
179cdf0e10cSrcweir 		}
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184