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 
33cdf0e10cSrcweir import com.sun.star.embed.*;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import share.LogWriter;
36cdf0e10cSrcweir import complex.storages.TestHelper;
37cdf0e10cSrcweir import complex.storages.StorageTest;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir public class RegressionTest_i55821 implements StorageTest {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
42cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
43cdf0e10cSrcweir 	TestHelper m_aTestHelper;
44cdf0e10cSrcweir 
RegressionTest_i55821( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )45cdf0e10cSrcweir 	public RegressionTest_i55821( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
46cdf0e10cSrcweir 	{
47cdf0e10cSrcweir 		m_xMSF = xMSF;
48cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
49cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i55821: " );
50cdf0e10cSrcweir 	}
51cdf0e10cSrcweir 
test()52cdf0e10cSrcweir     public boolean test()
53cdf0e10cSrcweir 	{
54cdf0e10cSrcweir 		try
55cdf0e10cSrcweir 		{
56cdf0e10cSrcweir 			// ================================================
57cdf0e10cSrcweir 			// create a temporary stream and a storage based on it
58cdf0e10cSrcweir 			// fill the storage with the data that will be used for testing
59cdf0e10cSrcweir 			// ================================================
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
62cdf0e10cSrcweir 			if ( xTempFileStream == null )
63cdf0e10cSrcweir 				return false;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 			// create storage based on the temporary stream
66cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
67cdf0e10cSrcweir 			pArgs[0] = (Object) xTempFileStream;
68cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
71cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
72cdf0e10cSrcweir 			if ( xTempStorage == null )
73cdf0e10cSrcweir 			{
74cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
75cdf0e10cSrcweir 				return false;
76cdf0e10cSrcweir 			}
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 			String sPass = "12345";
79cdf0e10cSrcweir 			byte pBytes[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
82cdf0e10cSrcweir 			// the stream will not be encrypted
83cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, sPass ) )
84cdf0e10cSrcweir 				return false;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
87cdf0e10cSrcweir 				return false;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream2", "MediaType2", false, pBytes, sPass ) )
90cdf0e10cSrcweir 				return false;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
93cdf0e10cSrcweir 				return false;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 			// dispose used storages to free resources
96cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
97cdf0e10cSrcweir 				return false;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 			// ================================================
100cdf0e10cSrcweir 			// reopen the target storage readonly, and check contents
101cdf0e10cSrcweir 			// ================================================
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 			// the temporary file must not be locked any more after storage disposing
104cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.READ );
105cdf0e10cSrcweir 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
106cdf0e10cSrcweir 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
107cdf0e10cSrcweir 			if ( xResultStorage == null )
108cdf0e10cSrcweir 			{
109cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
110cdf0e10cSrcweir 				return false;
111cdf0e10cSrcweir 			}
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, sPass ) )
114cdf0e10cSrcweir 				return false;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream2", "MediaType2", pBytes, sPass ) )
117cdf0e10cSrcweir 				return false;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 			// dispose used storages to free resources
120cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
121cdf0e10cSrcweir 				return false;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 			return true;
124cdf0e10cSrcweir 		}
125cdf0e10cSrcweir 		catch( Exception e )
126cdf0e10cSrcweir 		{
127cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
128cdf0e10cSrcweir 			return false;
129cdf0e10cSrcweir 		}
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133