1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package complex.storages;
23 
24 import java.net.URI;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.util.zip.ZipInputStream;
28 import java.util.zip.ZipEntry;
29 
30 import com.sun.star.uno.XInterface;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.lang.XSingleServiceFactory;
33 
34 import com.sun.star.bridge.XUnoUrlResolver;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 import com.sun.star.io.XStream;
38 import com.sun.star.io.XInputStream;
39 
40 import com.sun.star.embed.*;
41 
42 import share.LogWriter;
43 import complex.storages.TestHelper;
44 import complex.storages.StorageTest;
45 
46 public class RegressionTest_i61909 implements StorageTest {
47 
48 	XMultiServiceFactory m_xMSF;
49 	XSingleServiceFactory m_xStorageFactory;
50 	TestHelper m_aTestHelper;
51 
RegressionTest_i61909( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )52 	public RegressionTest_i61909( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
53 	{
54 		m_xMSF = xMSF;
55 		m_xStorageFactory = xStorageFactory;
56 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i61909: " );
57 	}
58 
test()59     public boolean test()
60 	{
61 		try
62 		{
63 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
64 			if ( sTempFileURL == null || sTempFileURL == "" )
65 			{
66 				m_aTestHelper.Error( "No valid temporary file was created!" );
67 				return false;
68 			}
69 
70 			// create storage based on the temporary stream
71 			Object pArgs[] = new Object[2];
72 			pArgs[0] = (Object) sTempFileURL;
73 			pArgs[1] = new Integer( ElementModes.WRITE );
74 
75 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
76 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
77 			if ( xTempStorage == null )
78 			{
79 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
80 				return false;
81 			}
82 
83 			byte pBytes[] = new byte[36000];
84 			for ( int nInd = 0; nInd < 36000; nInd++ )
85 				pBytes[nInd] = (byte)( nInd % 128 );
86 
87 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
88 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes ) )
89 				return false;
90 
91 			// open a new substorage
92 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
93 																		"SubStorage1",
94 																		ElementModes.WRITE );
95 			if ( xTempSubStorage == null )
96 			{
97 				m_aTestHelper.Error( "Can't create substorage!" );
98 				return false;
99 			}
100 
101 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
102 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", true, pBytes ) )
103 				return false;
104 
105 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
106 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
107 															"MediaType3",
108 															true,
109 															ElementModes.WRITE ) )
110 				return false;
111 
112 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
113 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
114 															"MediaType4",
115 															false,
116 															ElementModes.WRITE ) )
117 				return false;
118 
119 			// commit substorage first
120 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
121 				return false;
122 
123 			// commit the root storage so the contents must be stored now
124 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
125 				return false;
126 
127 			// dispose used storage to free resources
128 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
129 				return false;
130 
131 			// ================================================
132 			// now reopen the storage, and insert a new stream
133 			// ================================================
134 
135 			Object oStep2TempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
136 			XStorage xStep2TempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStep2TempStorage );
137 			if ( xStep2TempStorage == null )
138 			{
139 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
140 				return false;
141 			}
142 
143 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
144 			if ( !m_aTestHelper.WriteBytesToSubstream( xStep2TempStorage, "SubStream3", "MediaType5", true, pBytes ) )
145 				return false;
146 
147 			// commit the root storage so the contents must be stored now
148 			if ( !m_aTestHelper.commitStorage( xStep2TempStorage ) )
149 				return false;
150 
151 			// dispose used storage to free resources
152 			if ( !m_aTestHelper.disposeStorage( xStep2TempStorage ) )
153 				return false;
154 
155 			// ================================================
156 			// now access the stream using ZipInputStream
157 			// ================================================
158 
159 			URI aUri = new URI( sTempFileURL );
160 			File aFile = new File( aUri );
161 			FileInputStream aFileStream = new FileInputStream( aFile );
162 			ZipInputStream aZipStream = new ZipInputStream( aFileStream );
163 
164         	ZipEntry aEntry;
165 			int nNumber = 0;
166 			m_aTestHelper.Message( "Available entries:" );
167 			while ( ( aEntry = aZipStream.getNextEntry() ) != null )
168 			{
169 				nNumber++;
170 				m_aTestHelper.Message( aEntry.getName() );
171 			}
172 
173 			if ( nNumber != 6 )
174 			{
175 				m_aTestHelper.Error( "Wrong number of entries: " + nNumber + ", Expected: 6" );
176 				return false;
177 			}
178 
179 			return true;
180 		}
181 		catch( Exception e )
182 		{
183 			m_aTestHelper.Error( "Exception: " + e );
184 			return false;
185 		}
186     }
187 }
188 
189