xref: /aoo4110/main/package/qa/storages/Test10.java (revision b1cdbd2c)
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.lang.XMultiServiceFactory;
25 import com.sun.star.lang.XSingleServiceFactory;
26 
27 import com.sun.star.bridge.XUnoUrlResolver;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.XInterface;
30 
31 import com.sun.star.container.XNameAccess;
32 import com.sun.star.io.XStream;
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 Test10 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
Test10( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public Test10( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "Test10: " );
51 	}
52 
test()53     public boolean test()
54 	{
55 		try
56 		{
57 			// create temporary storage based on arbitrary medium
58 			// after such a storage is closed it is lost
59 			Object oTempStorage = m_xStorageFactory.createInstance();
60 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
61 			if ( xTempStorage == null )
62 			{
63 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
64 				return false;
65 			}
66 
67             byte pBigBytes[] = new byte[33000];
68 			for ( int nInd = 0; nInd < 33000; nInd++ )
69 				pBigBytes[nInd] = (byte)( nInd % 128 );
70 
71 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
72 
73 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
74 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
75 				return false;
76 
77 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
78 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
79 				return false;
80 
81 
82 			// open a new substorage
83 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
84 																		"SubStorage1",
85 																		ElementModes.WRITE );
86 			if ( xTempSubStorage == null )
87 			{
88 				m_aTestHelper.Error( "Can't create substorage!" );
89 				return false;
90 			}
91 
92 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
93 
94 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", true, pBytes2 ) )
96 				return false;
97 
98 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
99 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", true, pBigBytes ) )
100 				return false;
101 
102 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
103 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
104 															"MediaType3",
105 															true,
106 															ElementModes.WRITE ) )
107 				return false;
108 
109 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
110 															"MediaType4",
111 															false,
112 															ElementModes.WRITE ) )
113 				return false;
114 
115 			// ==============================
116 			// check cloning at current state
117 			// ==============================
118 
119 			// the new storage still was not commited so the clone must be empty
120 			XStorage xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xStorageFactory, xTempStorage, "SubStorage1" );
121 
122 			if ( xClonedSubStorage == null )
123 			{
124 				m_aTestHelper.Error( "The result of clone is empty!" );
125 				return false;
126 			}
127 
128 			XNameAccess xClonedNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xClonedSubStorage );
129 			if ( xClonedNameAccess == null )
130 			{
131 				m_aTestHelper.Error( "XNameAccess is not implemented by the clone!" );
132 				return false;
133 			}
134 
135 			if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage, "", true, ElementModes.WRITE ) )
136 				return false;
137 
138 			if ( xClonedNameAccess.hasElements() )
139 			{
140 				m_aTestHelper.Error( "The new substorage still was not commited so it must be empty!" );
141 				return false;
142 			}
143 
144 			if ( !m_aTestHelper.disposeStorage( xClonedSubStorage ) )
145 				return false;
146 
147 			xClonedSubStorage = null;
148 			xClonedNameAccess = null;
149 
150 			// the new stream was opened, written and closed, that means flashed
151 			// so the clone must contain all the information
152 			XStream xClonedSubStream = m_aTestHelper.cloneSubStream( xTempStorage, "SubStream1" );
153 			if ( !m_aTestHelper.InternalCheckStream( xClonedSubStream, "SubStream1", "MediaType1", true, pBytes1, true ) )
154 				return false;
155 
156 			XStream xClonedBigSubStream = m_aTestHelper.cloneSubStream( xTempStorage, "BigSubStream1" );
157 			if ( !m_aTestHelper.InternalCheckStream( xClonedBigSubStream, "BigSubStream1", "MediaType1", true, pBigBytes, true ) )
158 				return false;
159 
160 			if ( !m_aTestHelper.disposeStream( xClonedSubStream, "SubStream1" ) )
161 				return false;
162 
163 			if ( !m_aTestHelper.disposeStream( xClonedBigSubStream, "BigSubStream1" ) )
164 				return false;
165 
166 			// ==============================
167 			// commit substorage and check cloning
168 			// ==============================
169 
170 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
171 				return false;
172 
173 			xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xStorageFactory, xTempStorage, "SubStorage1" );
174 			if ( xClonedSubStorage == null )
175 			{
176 				m_aTestHelper.Error( "The result of clone is empty!" );
177 				return false;
178 			}
179 
180 			if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage, "MediaType4", true, ElementModes.WRITE ) )
181 				return false;
182 
183 			if ( !m_aTestHelper.checkStream( xClonedSubStorage, "SubStream2", "MediaType2", true, pBytes2 ) )
184 				return false;
185 
186 			if ( !m_aTestHelper.checkStream( xClonedSubStorage, "BigSubStream2", "MediaType2", true, pBigBytes ) )
187 				return false;
188 
189 			XStorage xCloneOfRoot = m_aTestHelper.cloneStorage( m_xStorageFactory, xTempStorage );
190 			if ( xCloneOfRoot == null )
191 			{
192 				m_aTestHelper.Error( "The result of root clone is empty!" );
193 				return false;
194 			}
195 
196 			XNameAccess xCloneOfRootNA = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xCloneOfRoot );
197 			if ( xCloneOfRootNA == null )
198 			{
199 				m_aTestHelper.Error( "XNameAccess is not implemented by the root clone!" );
200 				return false;
201 			}
202 
203 			if ( xCloneOfRootNA.hasElements() )
204 			{
205 				m_aTestHelper.Error( "The root storage still was not commited so it's clone must be empty!" );
206 				return false;
207 			}
208 
209 			if ( !m_aTestHelper.disposeStorage( xCloneOfRoot ) )
210 				return false;
211 
212 			xCloneOfRoot = null;
213 
214 			// ==============================
215 			// commit root storage and check cloning
216 			// ==============================
217 
218 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
219 				return false;
220 
221 			xCloneOfRoot = m_aTestHelper.cloneStorage( m_xStorageFactory, xTempStorage );
222 			if ( xCloneOfRoot == null )
223 			{
224 				m_aTestHelper.Error( "The result of root clone is empty!" );
225 				return false;
226 			}
227 
228 			XStorage xSubStorageOfClone = xCloneOfRoot.openStorageElement( "SubStorage1", ElementModes.READ );
229 			if ( xSubStorageOfClone == null )
230 			{
231 				m_aTestHelper.Error( "The result of root clone is wrong!" );
232 				return false;
233 			}
234 
235 			if ( !m_aTestHelper.checkStorageProperties( xSubStorageOfClone, "MediaType4", false, ElementModes.READ ) )
236 				return false;
237 
238 			if ( !m_aTestHelper.checkStream( xSubStorageOfClone, "SubStream2", "MediaType2", true, pBytes2 ) )
239 				return false;
240 
241 			if ( !m_aTestHelper.checkStream( xSubStorageOfClone, "BigSubStream2", "MediaType2", true, pBigBytes ) )
242 				return false;
243 
244 			return true;
245 		}
246 		catch( Exception e )
247 		{
248 			m_aTestHelper.Error( "Exception: " + e );
249 			return false;
250 		}
251     }
252 }
253 
254