xref: /aoo42x/main/package/qa/storages/Test13.java (revision a893be29)
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 
32 import com.sun.star.embed.*;
33 
34 import share.LogWriter;
35 import complex.storages.TestHelper;
36 import complex.storages.StorageTest;
37 
38 public class Test13 implements StorageTest {
39 
40 	XMultiServiceFactory m_xMSF;
41 	XSingleServiceFactory m_xStorageFactory;
42 	TestHelper m_aTestHelper;
43 
Test13( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44 	public Test13( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 	{
46 		m_xMSF = xMSF;
47 		m_xStorageFactory = xStorageFactory;
48 		m_aTestHelper = new TestHelper( aLogWriter, "Test13: " );
49 	}
50 
test()51     public boolean test()
52 	{
53 		String aStreamPrefix = "";
54 		for ( int nInd = 0; nInd < 4; ++nInd, aStreamPrefix += "SubStorage" + nInd )
55 			if ( !testForPath( aStreamPrefix ) )
56 				return false;
57 
58 		return true;
59 	}
60 
testForPath( String aStreamPrefix )61 	public boolean testForPath( String aStreamPrefix )
62 	{
63 		try
64 		{
65 			String aSubStream1Path = aStreamPrefix + "SubStream1";
66 			String aSubStream2Path = aStreamPrefix + "SubStream2";
67 			String aSubStream3Path = aStreamPrefix + "SubStream3";
68 			String aBigSubStream1Path = aStreamPrefix + "BigSubStream1";
69 			String aBigSubStream2Path = aStreamPrefix + "BigSubStream2";
70 			String aBigSubStream3Path = aStreamPrefix + "BigSubStream3";
71 
72 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
73 			if ( sTempFileURL == null || sTempFileURL == "" )
74 			{
75 				m_aTestHelper.Error( "No valid temporary file was created!" );
76 				return false;
77 			}
78 
79 			// create temporary storage based on a previously created temporary file
80 			Object pArgs[] = new Object[2];
81 			pArgs[0] = (Object) sTempFileURL;
82 			pArgs[1] = new Integer( ElementModes.WRITE );
83 
84 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
85 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
86 			if ( xTempFileStorage == null )
87 			{
88 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
89 				return false;
90 			}
91 
92             byte pBigBytes[] = new byte[33000];
93 			for ( int nInd = 0; nInd < 33000; nInd++ )
94 				pBigBytes[nInd] = (byte)( nInd % 128 );
95 
96 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
97 
98 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
99 			// and commit
100 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, true ) )
101 				return false;
102 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream1Path, "MediaType1", true, pBigBytes, true ) )
103 				return false;
104 
105 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
106 
107 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
108 			// and commit
109 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, true ) )
110 				return false;
111             if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream2Path, "MediaType2", false, pBigBytes, true ) )
112 				return false;
113 
114 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
115 			// and don't commit
116 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream3Path, "MediaType2", false, pBytes2, false ) )
117 				return false;
118 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream3Path, "MediaType2", false, pBigBytes, false ) )
119 				return false;
120 
121 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
122 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
123 															"MediaType3",
124 															true,
125 															ElementModes.WRITE ) )
126 				return false;
127 
128 			// commit the root storage so the contents must be stored now
129 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
130 				return false;
131 
132 			// dispose used storages to free resources
133 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
134 				return false;
135 
136 			// ================================================
137 			// now reopen the storage,
138 			// check all the written and copied information
139 			// and change it
140 			// ================================================
141 
142 			// the temporary file must not be locked any more after storage disposing
143 			oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
144 			xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
145 			if ( xTempFileStorage == null )
146 			{
147 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
148 				return false;
149 			}
150 
151 			if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) )
152 				return false;
153 
154 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1 ) )
155 				return false;
156 
157 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aBigSubStream1Path, "MediaType1", true, pBigBytes ) )
158 				return false;
159 
160 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2 ) )
161 				return false;
162 
163 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aBigSubStream2Path, "MediaType2", false, pBigBytes ) )
164 				return false;
165 
166 			if ( !m_aTestHelper.cantOpenStreamH( xTempFileStorage, aSubStream3Path, ElementModes.READ ) )
167 				return false;
168 
169 			if ( !m_aTestHelper.cantOpenStreamH( xTempFileStorage, aBigSubStream3Path, ElementModes.READ ) )
170 				return false;
171 
172 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
173 			// and commit
174 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream1Path, "MediaType3", true, pBytes2, true ) )
175 				return false;
176 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream1Path, "MediaType3", true, pBigBytes, true ) )
177 				return false;
178 
179 
180 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
181 			// and don't commit
182 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream2Path, "MediaType3", true, pBytes1, false ) )
183 				return false;
184 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream2Path, "MediaType3", true, pBigBytes, false ) )
185 				return false;
186 
187 			// commit the root storage so the contents must be stored now
188 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
189 				return false;
190 
191 			// dispose used storages to free resources
192 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
193 				return false;
194 
195 			// ================================================
196 			// now reopen the storage,
197 			// check all the written information
198 			// ================================================
199 
200 			// the temporary file must not be locked any more after storage disposing
201 			pArgs[1] = new Integer( ElementModes.READ );
202 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
203 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
204 			if ( xResultStorage == null )
205 			{
206 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
207 				return false;
208 			}
209 
210 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
211 				return false;
212 
213 			if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream1Path, "MediaType3", true, pBytes2 ) )
214 				return false;
215 			if ( !m_aTestHelper.checkStreamH( xResultStorage, aBigSubStream1Path, "MediaType3", true, pBigBytes ) )
216 				return false;
217 
218 			// the following stream was not committed last time, so the last change must be lost
219 			if ( !m_aTestHelper.checkStreamH( xResultStorage, aBigSubStream2Path, "MediaType2", false, pBigBytes ) )
220 				return false;
221 
222 			// dispose used storages to free resources
223 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
224 				return false;
225 
226 			return true;
227 		}
228 		catch( Exception e )
229 		{
230 			m_aTestHelper.Error( "Exception: " + e );
231 			return false;
232 		}
233     }
234 
235 }
236 
237