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