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