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 java.net.URI;
25cdf0e10cSrcweir import java.io.File;
26cdf0e10cSrcweir import java.io.FileInputStream;
27cdf0e10cSrcweir import java.util.zip.ZipInputStream;
28cdf0e10cSrcweir import java.util.zip.ZipEntry;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.uno.XInterface;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
32cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36cdf0e10cSrcweir import com.sun.star.uno.XInterface;
37cdf0e10cSrcweir import com.sun.star.io.XStream;
38cdf0e10cSrcweir import com.sun.star.io.XInputStream;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir import com.sun.star.embed.*;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir import share.LogWriter;
43cdf0e10cSrcweir import complex.storages.TestHelper;
44cdf0e10cSrcweir import complex.storages.StorageTest;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir public class RegressionTest_i61909 implements StorageTest {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
49cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
50cdf0e10cSrcweir 	TestHelper m_aTestHelper;
51cdf0e10cSrcweir 
RegressionTest_i61909( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )52cdf0e10cSrcweir 	public RegressionTest_i61909( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
53cdf0e10cSrcweir 	{
54cdf0e10cSrcweir 		m_xMSF = xMSF;
55cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
56cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i61909: " );
57cdf0e10cSrcweir 	}
58cdf0e10cSrcweir 
test()59cdf0e10cSrcweir     public boolean test()
60cdf0e10cSrcweir 	{
61cdf0e10cSrcweir 		try
62cdf0e10cSrcweir 		{
63cdf0e10cSrcweir 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
64cdf0e10cSrcweir 			if ( sTempFileURL == null || sTempFileURL == "" )
65cdf0e10cSrcweir 			{
66cdf0e10cSrcweir 				m_aTestHelper.Error( "No valid temporary file was created!" );
67cdf0e10cSrcweir 				return false;
68cdf0e10cSrcweir 			}
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 			// create storage based on the temporary stream
71cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
72cdf0e10cSrcweir 			pArgs[0] = (Object) sTempFileURL;
73cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
76cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
77cdf0e10cSrcweir 			if ( xTempStorage == null )
78cdf0e10cSrcweir 			{
79cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
80cdf0e10cSrcweir 				return false;
81cdf0e10cSrcweir 			}
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 			byte pBytes[] = new byte[36000];
84cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 36000; nInd++ )
85cdf0e10cSrcweir 				pBytes[nInd] = (byte)( nInd % 128 );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
88cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes ) )
89cdf0e10cSrcweir 				return false;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 			// open a new substorage
92cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
93cdf0e10cSrcweir 																		"SubStorage1",
94cdf0e10cSrcweir 																		ElementModes.WRITE );
95cdf0e10cSrcweir 			if ( xTempSubStorage == null )
96cdf0e10cSrcweir 			{
97cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
98cdf0e10cSrcweir 				return false;
99cdf0e10cSrcweir 			}
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
102cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", true, pBytes ) )
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( xTempStorage,
107cdf0e10cSrcweir 															"MediaType3",
108cdf0e10cSrcweir 															true,
109cdf0e10cSrcweir 															ElementModes.WRITE ) )
110cdf0e10cSrcweir 				return false;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
113cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
114cdf0e10cSrcweir 															"MediaType4",
115cdf0e10cSrcweir 															false,
116cdf0e10cSrcweir 															ElementModes.WRITE ) )
117cdf0e10cSrcweir 				return false;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 			// commit substorage first
120cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
121cdf0e10cSrcweir 				return false;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
124cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
125cdf0e10cSrcweir 				return false;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 			// dispose used storage to free resources
128cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
129cdf0e10cSrcweir 				return false;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 			// ================================================
132cdf0e10cSrcweir 			// now reopen the storage, and insert a new stream
133cdf0e10cSrcweir 			// ================================================
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 			Object oStep2TempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
136cdf0e10cSrcweir 			XStorage xStep2TempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStep2TempStorage );
137cdf0e10cSrcweir 			if ( xStep2TempStorage == null )
138cdf0e10cSrcweir 			{
139cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
140cdf0e10cSrcweir 				return false;
141cdf0e10cSrcweir 			}
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
144cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xStep2TempStorage, "SubStream3", "MediaType5", true, pBytes ) )
145cdf0e10cSrcweir 				return false;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
148cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xStep2TempStorage ) )
149cdf0e10cSrcweir 				return false;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 			// dispose used storage to free resources
152cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xStep2TempStorage ) )
153cdf0e10cSrcweir 				return false;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 			// ================================================
156cdf0e10cSrcweir 			// now access the stream using ZipInputStream
157cdf0e10cSrcweir 			// ================================================
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 			URI aUri = new URI( sTempFileURL );
160cdf0e10cSrcweir 			File aFile = new File( aUri );
161cdf0e10cSrcweir 			FileInputStream aFileStream = new FileInputStream( aFile );
162cdf0e10cSrcweir 			ZipInputStream aZipStream = new ZipInputStream( aFileStream );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         	ZipEntry aEntry;
165cdf0e10cSrcweir 			int nNumber = 0;
166cdf0e10cSrcweir 			m_aTestHelper.Message( "Available entries:" );
167cdf0e10cSrcweir 			while ( ( aEntry = aZipStream.getNextEntry() ) != null )
168cdf0e10cSrcweir 			{
169cdf0e10cSrcweir 				nNumber++;
170cdf0e10cSrcweir 				m_aTestHelper.Message( aEntry.getName() );
171cdf0e10cSrcweir 			}
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 			if ( nNumber != 6 )
174cdf0e10cSrcweir 			{
175cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong number of entries: " + nNumber + ", Expected: 6" );
176cdf0e10cSrcweir 				return false;
177cdf0e10cSrcweir 			}
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 			return true;
180cdf0e10cSrcweir 		}
181cdf0e10cSrcweir 		catch( Exception e )
182cdf0e10cSrcweir 		{
183cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
184cdf0e10cSrcweir 			return false;
185cdf0e10cSrcweir 		}
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189