xref: /aoo41x/main/package/qa/storages/Test12.java (revision a740f2aa)
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 com.sun.star.uno.XInterface;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XSingleServiceFactory;
27 
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 import com.sun.star.io.XStream;
32 import com.sun.star.io.XInputStream;
33 
34 import com.sun.star.embed.*;
35 
36 import share.LogWriter;
37 import complex.storages.TestHelper;
38 import complex.storages.StorageTest;
39 
40 public class Test12 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
Test12( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public Test12( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "Test12: " );
51 	}
52 
test()53     public boolean test()
54 	{
55 		try
56 		{
57 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
58 			if ( xTempFileStream == null )
59 				return false;
60 
61 			// create storage based on the temporary stream
62 			Object pArgs[] = new Object[2];
63 			pArgs[0] = (Object) xTempFileStream;
64 			pArgs[1] = new Integer( ElementModes.WRITE );
65 
66 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
68 			if ( xTempStorage == null )
69 			{
70 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
71 				return false;
72 			}
73 
74 			// open a new substorage
75 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
76 																	"SubStorage1",
77 																	ElementModes.WRITE );
78 			if ( xTempSubStorage == null )
79 			{
80 				m_aTestHelper.Error( "Can't create substorage!" );
81 				return false;
82 			}
83 
84             byte pBigBytes[] = new byte[33000];
85 			for ( int nInd = 0; nInd < 33000; nInd++ )
86 				pBigBytes[nInd] = (byte)( nInd % 128 );
87 
88 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
89 
90 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
92 				return false;
93 
94 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
96 				return false;
97 
98 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
99 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
100 															"MediaType2",
101 															true,
102 															ElementModes.WRITE ) )
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( xTempSubStorage,
107 															"MediaType3",
108 															false,
109 															ElementModes.WRITE ) )
110 				return false;
111 
112 			// commit substorage first
113 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
114 				return false;
115 
116 			// commit the root storage so the contents must be stored now
117 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
118 				return false;
119 
120 			// dispose substorage
121 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
122 				return false;
123 
124 			// ================================================
125 			// check substorage
126 			// ================================================
127 
128 			if ( !checkSubStorages( xTempStorage, pBytes1, pBigBytes ) )
129 				return false;
130 
131 			// dispose used storage to free resources
132 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
133 				return false;
134 
135 			// ================================================
136 			// now check all the written information with readwrite access
137 			// ================================================
138 
139 			Object oResWriteStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
140 			XStorage xResWriteStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResWriteStorage );
141 			if ( xResWriteStorage == null )
142 			{
143 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
144 				return false;
145 			}
146 
147 			if ( !m_aTestHelper.checkStorageProperties( xResWriteStorage, "MediaType2", true, ElementModes.WRITE ) )
148 				return false;
149 
150 			if( !checkSubStorages( xResWriteStorage, pBytes1, pBigBytes ) )
151 				return false;
152 
153 			// try to open for writing after opening for reading
154 			XStorage xResWSubStorage = m_aTestHelper.openSubStorage( xResWriteStorage,
155 																	"SubStorage1",
156 																	ElementModes.WRITE );
157 			if ( xResWSubStorage == null )
158 			{
159 				m_aTestHelper.Error( "Can't open substorage for writing after it was opened for reading!" );
160 				return false;
161 			}
162 
163 			if ( !m_aTestHelper.checkStorageProperties( xResWSubStorage, "MediaType3", false, ElementModes.WRITE ) )
164 				return false;
165 
166 			if ( !m_aTestHelper.checkStream( xResWSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
167 				return false;
168 
169 			if ( !m_aTestHelper.checkStream( xResWSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
170 				return false;
171 
172 			// dispose used storage to free resources
173 			if ( !m_aTestHelper.disposeStorage( xResWriteStorage ) )
174 				return false;
175 
176 
177 			// ================================================
178 			// now check all the written information with readonly access
179 			// ================================================
180 
181 			// close the output part of the temporary stream
182 			// the output part must present since we already wrote to the stream
183 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
184 				return false;
185 
186 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
187 			if ( xTempInStream == null )
188 				return false;
189 
190 			// open input stream
191 			// since no mode is provided the result storage must be opened readonly
192 			Object pOneArg[] = new Object[1];
193 			pOneArg[0] = (Object) xTempInStream;
194 
195 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
196 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
197 			if ( xResultStorage == null )
198 			{
199 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
200 				return false;
201 			}
202 
203 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.READ ) )
204 				return false;
205 
206 			if( !checkSubStorages( xResultStorage, pBytes1, pBigBytes ) )
207 				return false;
208 
209 			return true;
210 		}
211 		catch( Exception e )
212 		{
213 			m_aTestHelper.Error( "Exception: " + e );
214 			return false;
215 		}
216     }
217 
checkSubStorages( XStorage xStorage, byte[] pBytes1, byte[] pBigBytes )218 	private boolean checkSubStorages( XStorage xStorage, byte[] pBytes1, byte[] pBigBytes )
219 	{
220 		XStorage xReadSubStorage1 = m_aTestHelper.openSubStorage( xStorage,
221 																"SubStorage1",
222 																ElementModes.READ );
223 
224 		XStorage xReadSubStorage2 = m_aTestHelper.openSubStorage( xStorage,
225 																"SubStorage1",
226 																ElementModes.READ );
227 
228 		if ( xReadSubStorage1 == null || xReadSubStorage2 == null )
229 		{
230 			m_aTestHelper.Error( "Can't open substorage for reading!" );
231 			return false;
232 		}
233 
234 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage1, "MediaType3", false, ElementModes.READ ) )
235 			return false;
236 
237 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage2, "MediaType3", false, ElementModes.READ ) )
238 			return false;
239 
240 		if ( !m_aTestHelper.checkStream( xReadSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
241 			return false;
242 
243 		if ( !m_aTestHelper.checkStream( xReadSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
244 			return false;
245 
246 		if ( !m_aTestHelper.checkStream( xReadSubStorage2, "SubStream1", "MediaType1", true, pBytes1 ) )
247 			return false;
248 
249 		if ( !m_aTestHelper.checkStream( xReadSubStorage2, "BigSubStream1", "MediaType1", true, pBigBytes ) )
250 			return false;
251 
252 		if ( !m_aTestHelper.disposeStorage( xReadSubStorage1 ) )
253 			return false;
254 
255 		if ( !m_aTestHelper.disposeStorage( xReadSubStorage2 ) )
256 			return false;
257 
258 		return true;
259 	}
260 }
261 
262