1 package complex.ofopxmlstorages;
2 
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
6 import com.sun.star.lang.DisposedException;
7 
8 import com.sun.star.bridge.XUnoUrlResolver;
9 import com.sun.star.uno.UnoRuntime;
10 import com.sun.star.uno.XInterface;
11 
12 import com.sun.star.container.XNameAccess;
13 
14 import com.sun.star.embed.*;
15 import com.sun.star.beans.StringPair;
16 
17 import share.LogWriter;
18 import complex.ofopxmlstorages.TestHelper;
19 import complex.ofopxmlstorages.StorageTest;
20 
21 public class Test04 implements StorageTest {
22 
23 	XMultiServiceFactory m_xMSF;
24 	XSingleServiceFactory m_xStorageFactory;
25 	TestHelper m_aTestHelper;
26 
27 	public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
28 	{
29 		m_xMSF = xMSF;
30 		m_xStorageFactory = xStorageFactory;
31 		m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
32 	}
33 
34     public boolean test()
35 	{
36 		StringPair[][] aRelations1 =
37 			{ { new StringPair( "Id", "Num1" ) },
38 			  { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
39 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
40 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
41 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
42 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
43 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
44 			};
45 
46 		StringPair[][] aRelations2 =
47 			{ { new StringPair( "Id", "Num1" ) },
48 			  { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
49 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
50 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
51 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
52 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
53 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
54 			};
55 
56 		try
57 		{
58 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
59 			if ( sTempFileURL == null || sTempFileURL == "" )
60 			{
61 				m_aTestHelper.Error( "No valid temporary file was created!" );
62 				return false;
63 			}
64 
65 			// create temporary storage based on arbitrary medium
66 			// after such a storage is closed it is lost
67 			XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
68 			if ( xTempStorage == null )
69 			{
70 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
71 				return false;
72 			}
73 
74 			// open substorages and create streams there
75 
76 			// first substorage of the root storage
77 			XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
78 																		"SubStorage1",
79 																		ElementModes.WRITE );
80 			if ( xTempSubStorage1 == null )
81 			{
82 				m_aTestHelper.Error( "Can't create substorage!" );
83 				return false;
84 			}
85 
86 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
87 
88 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
89 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1,
90 														"SubStream1",
91 														"MediaType1",
92 														true,
93 														pBytes1,
94 														aRelations1 ) )
95 				return false;
96 
97 			// second substorage of the root storage
98 			XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
99 																		"SubStorage2",
100 																		ElementModes.WRITE );
101 			if ( xTempSubStorage2 == null )
102 			{
103 				m_aTestHelper.Error( "Can't create substorage!" );
104 				return false;
105 			}
106 
107 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
108 
109 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
110 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2,
111 														"SubStream2",
112 														"MediaType2",
113 														false,
114 														pBytes2,
115 														aRelations2 ) )
116 				return false;
117 
118 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
119 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
120 															true,
121 															ElementModes.WRITE,
122 															aRelations2 ) )
123 				return false;
124 
125 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
126 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
127 															false,
128 															ElementModes.WRITE,
129 															aRelations2 ) )
130 				return false;
131 
132 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
133 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
134 															false,
135 															ElementModes.WRITE,
136 															aRelations2 ) )
137 				return false;
138 
139 			// create temporary storage based on a previously created temporary file
140 			XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
141 																			sTempFileURL,
142 																			ElementModes.WRITE );
143 			if ( xTempFileStorage == null )
144 			{
145 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
146 				return false;
147 			}
148 
149 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
150 				return false;
151 
152 			// if storage is not commited before disposing all the changes will be lost
153 			if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
154 				return false;
155 
156 			// a storage must be disposed before moving/removing otherwise the access will be denied
157 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
158 				return false;
159 
160 			if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
161 				return false;
162 
163 			// SubStorage2 must be removed and disposed now
164 			try
165 			{
166 				xTempSubStorage2.isStreamElement( "SubStream2" );
167 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
168 				return false;
169 			}
170 			catch( com.sun.star.lang.DisposedException de )
171 			{
172 			}
173 			catch( Exception e )
174 			{
175 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
176 				return false;
177 			}
178 
179 			if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
180 				return false;
181 
182 			if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
183 				return false;
184 
185 			if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
186 				return false;
187 
188 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
189 				return false;
190 
191 			// dispose used storages to free resources
192 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
193 				return false;
194 
195 			// ================================================
196 			// now check all the written and copied information
197 			// ================================================
198 
199 			// the temporary file must not be locked any more after storage disposing
200 			XStorage xResStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
201 																		sTempFileURL,
202 																		ElementModes.WRITE );
203 
204 			if ( xResStorage == null )
205 			{
206 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
207 				return false;
208 			}
209 
210 			// open and check SubStorage1
211 			XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
212 																		"SubStorage1",
213 																		ElementModes.READ );
214 			if ( xResSubStorage1 == null )
215 			{
216 				m_aTestHelper.Error( "Can't open existing substorage!" );
217 				return false;
218 			}
219 
220 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1,
221 														false,
222 														ElementModes.READ,
223 														aRelations2 ) )
224 				return false;
225 
226 
227 			// open and check SubStorage2
228 			XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
229 																		"SubStorage2",
230 																		ElementModes.READ );
231 			if ( xResSubStorage2 == null )
232 			{
233 				m_aTestHelper.Error( "Can't open existing substorage!" );
234 				return false;
235 			}
236 
237 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2,
238 														false,
239 														ElementModes.READ,
240 														aRelations2 ) )
241 				return false;
242 
243 
244 			// check all the result streams
245 
246 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1, aRelations1 ) )
247 				return false;
248 
249 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1, aRelations1 ) )
250 				return false;
251 
252 			if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1, aRelations1 ) )
253 				return false;
254 
255 			if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2, aRelations2 ) )
256 				return false;
257 
258 			// the storage must be disposed before removing
259 			if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
260 				return false;
261 
262 			// remove element and check that it was removed completelly
263 			if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
264 				return false;
265 
266 			try
267 			{
268 				XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
269 				if ( xResAccess.hasByName( "SubStorage2" ) )
270 					m_aTestHelper.Error( "SubStorage2 must be removed already!" );
271 			}
272 			catch( Exception e )
273 			{
274 				m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
275 				return false;
276 			}
277 
278 			try
279 			{
280 				xResSubStorage2.isStreamElement( "SubStream2" );
281 
282 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
283 				return false;
284 			}
285 			catch( com.sun.star.lang.DisposedException de )
286 			{
287 			}
288 			catch( Exception e )
289 			{
290 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
291 				return false;
292 			}
293 
294 			// dispose used storages to free resources
295 			if ( !m_aTestHelper.disposeStorage( xResStorage ) )
296 				return false;
297 
298 			return true;
299 		}
300 		catch( Exception e )
301 		{
302 			m_aTestHelper.Error( "Exception: " + e );
303 			return false;
304 		}
305     }
306 
307 }
308 
309