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 import com.sun.star.io.XInputStream;
12 
13 import com.sun.star.embed.*;
14 import com.sun.star.beans.StringPair;
15 
16 import share.LogWriter;
17 import complex.ofopxmlstorages.TestHelper;
18 import complex.ofopxmlstorages.StorageTest;
19 
20 public class Test08 implements StorageTest {
21 
22 	XMultiServiceFactory m_xMSF;
23 	XSingleServiceFactory m_xStorageFactory;
24 	TestHelper m_aTestHelper;
25 
26 	public Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
27 	{
28 		m_xMSF = xMSF;
29 		m_xStorageFactory = xStorageFactory;
30 		m_aTestHelper = new TestHelper( aLogWriter, "Test08: " );
31 	}
32 
33     public boolean test()
34 	{
35 		StringPair[][] aRelations1 =
36 			{ { new StringPair( "Id", "Num1" ) },
37 			  { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
38 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
39 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
40 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
41 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
42 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
43 			};
44 
45 		try
46 		{
47 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
48 			if ( xTempFileStream == null )
49 				return false;
50 
51 			// create storage based on the temporary stream
52 			XStorage xTempStorage = m_aTestHelper.createStorageFromStream( m_xStorageFactory,
53 																			xTempFileStream,
54 																			ElementModes.WRITE );
55 			if ( xTempStorage == null )
56 			{
57 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
58 				return false;
59 			}
60 
61 			// open a new substorage
62 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
63 																	"SubStorage1",
64 																	ElementModes.WRITE );
65 			if ( xTempSubStorage == null )
66 			{
67 				m_aTestHelper.Error( "Can't create substorage!" );
68 				return false;
69 			}
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( xTempSubStorage,
75 														"SubStream1",
76 														"MediaType1",
77 														true,
78 														pBytes1,
79 														aRelations1 ) )
80 				return false;
81 
82 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
83 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
84 															true,
85 															ElementModes.WRITE,
86 															aRelations1 ) )
87 				return false;
88 
89 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
90 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
91 															false,
92 															ElementModes.WRITE,
93 															aRelations1 ) )
94 				return false;
95 
96 			// commit substorage first
97 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
98 				return false;
99 
100 			// commit the root storage so the contents must be stored now
101 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
102 				return false;
103 
104 			// dispose substorage
105 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
106 				return false;
107 
108 			// ================================================
109 			// check substorage
110 			// ================================================
111 
112 			if ( !checkSubStorages( xTempStorage, pBytes1, aRelations1 ) )
113 				return false;
114 
115 			// dispose used storage to free resources
116 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
117 				return false;
118 
119 			// ================================================
120 			// now check all the written information with readwrite access
121 			// ================================================
122 
123 			XStorage xResWriteStorage = m_aTestHelper.createStorageFromStream( m_xStorageFactory,
124 																			xTempFileStream,
125 																			ElementModes.WRITE );
126 			if ( xResWriteStorage == null )
127 			{
128 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
129 				return false;
130 			}
131 
132 			if ( !m_aTestHelper.checkStorageProperties( xResWriteStorage,
133 														true,
134 														ElementModes.WRITE,
135 														aRelations1 ) )
136 				return false;
137 
138 			if( !checkSubStorages( xResWriteStorage, pBytes1, aRelations1 ) )
139 				return false;
140 
141 			// try to open for writing after opening for reading
142 			XStorage xResWSubStorage = m_aTestHelper.openSubStorage( xResWriteStorage,
143 																	"SubStorage1",
144 																	ElementModes.WRITE );
145 			if ( xResWSubStorage == null )
146 			{
147 				m_aTestHelper.Error( "Can't open substorage for writing after it was opened for reading!" );
148 				return false;
149 			}
150 
151 			if ( !m_aTestHelper.checkStorageProperties( xResWSubStorage,
152 														false,
153 														ElementModes.WRITE,
154 														aRelations1 ) )
155 				return false;
156 
157 			if ( !m_aTestHelper.checkStream( xResWSubStorage,
158 											"SubStream1",
159 											"MediaType1",
160 											pBytes1,
161 											aRelations1 ) )
162 				return false;
163 
164 			// dispose used storage to free resources
165 			if ( !m_aTestHelper.disposeStorage( xResWriteStorage ) )
166 				return false;
167 
168 
169 			// ================================================
170 			// now check all the written information with readonly access
171 			// ================================================
172 
173 			// close the output part of the temporary stream
174 			// the output part must present since we already wrote to the stream
175 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
176 				return false;
177 
178 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
179 			if ( xTempInStream == null )
180 				return false;
181 
182 			// open input stream
183 			// since no mode is provided the result storage must be opened readonly
184 			XStorage xResultStorage = m_aTestHelper.createStorageFromInputStream( m_xStorageFactory,
185 																					xTempInStream );
186 			if ( xResultStorage == null )
187 			{
188 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
189 				return false;
190 			}
191 
192 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage,
193 														true,
194 														ElementModes.READ,
195 														aRelations1 ) )
196 				return false;
197 
198 			if( !checkSubStorages( xResultStorage, pBytes1, aRelations1 ) )
199 				return false;
200 
201 			return true;
202 		}
203 		catch( Exception e )
204 		{
205 			m_aTestHelper.Error( "Exception: " + e );
206 			return false;
207 		}
208     }
209 
210 	private boolean checkSubStorages( XStorage xStorage, byte[] pBytes1, StringPair[][] aRelations )
211 	{
212 		XStorage xReadSubStorage1 = m_aTestHelper.openSubStorage( xStorage,
213 																"SubStorage1",
214 																ElementModes.READ );
215 
216 		XStorage xReadSubStorage2 = m_aTestHelper.openSubStorage( xStorage,
217 																"SubStorage1",
218 																ElementModes.READ );
219 
220 		if ( xReadSubStorage1 == null || xReadSubStorage2 == null )
221 		{
222 			m_aTestHelper.Error( "Can't open substorage for reading!" );
223 			return false;
224 		}
225 
226 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage1,
227 													false,
228 													ElementModes.READ,
229 													aRelations ) )
230 			return false;
231 
232 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage2,
233 													false,
234 													ElementModes.READ,
235 													aRelations ) )
236 			return false;
237 
238 		if ( !m_aTestHelper.checkStream( xReadSubStorage1,
239 											"SubStream1",
240 											"MediaType1",
241 											pBytes1,
242 											aRelations ) )
243 			return false;
244 
245 		if ( !m_aTestHelper.checkStream( xReadSubStorage2,
246 										"SubStream1",
247 										"MediaType1",
248 										pBytes1,
249 										aRelations ) )
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