1*a740f2aaSAndrew Rist /**************************************************************
2*a740f2aaSAndrew Rist  *
3*a740f2aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a740f2aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a740f2aaSAndrew Rist  * distributed with this work for additional information
6*a740f2aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a740f2aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a740f2aaSAndrew Rist  * "License"); you may not use this file except in compliance
9*a740f2aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a740f2aaSAndrew Rist  *
11*a740f2aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a740f2aaSAndrew Rist  *
13*a740f2aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a740f2aaSAndrew Rist  * software distributed under the License is distributed on an
15*a740f2aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a740f2aaSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a740f2aaSAndrew Rist  * specific language governing permissions and limitations
18*a740f2aaSAndrew Rist  * under the License.
19*a740f2aaSAndrew Rist  *
20*a740f2aaSAndrew Rist  *************************************************************/
21*a740f2aaSAndrew Rist 
22cdf0e10cSrcweir package complex.ofopxmlstorages;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.XInterface;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.uno.XInterface;
31cdf0e10cSrcweir import com.sun.star.io.XStream;
32cdf0e10cSrcweir import com.sun.star.io.XInputStream;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.embed.*;
35cdf0e10cSrcweir import com.sun.star.beans.StringPair;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import share.LogWriter;
38cdf0e10cSrcweir import complex.ofopxmlstorages.TestHelper;
39cdf0e10cSrcweir import complex.ofopxmlstorages.StorageTest;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir public class Test08 implements StorageTest {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
44cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
45cdf0e10cSrcweir 	TestHelper m_aTestHelper;
46cdf0e10cSrcweir 
Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )47cdf0e10cSrcweir 	public Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
48cdf0e10cSrcweir 	{
49cdf0e10cSrcweir 		m_xMSF = xMSF;
50cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
51cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test08: " );
52cdf0e10cSrcweir 	}
53cdf0e10cSrcweir 
test()54cdf0e10cSrcweir     public boolean test()
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		StringPair[][] aRelations1 =
57cdf0e10cSrcweir 			{ { new StringPair( "Id", "Num1" ) },
58cdf0e10cSrcweir 			  { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
59cdf0e10cSrcweir 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
60cdf0e10cSrcweir 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
61cdf0e10cSrcweir 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
62cdf0e10cSrcweir 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
63cdf0e10cSrcweir 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
64cdf0e10cSrcweir 			};
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 		try
67cdf0e10cSrcweir 		{
68cdf0e10cSrcweir 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
69cdf0e10cSrcweir 			if ( xTempFileStream == null )
70cdf0e10cSrcweir 				return false;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 			// create storage based on the temporary stream
73cdf0e10cSrcweir 			XStorage xTempStorage = m_aTestHelper.createStorageFromStream( m_xStorageFactory,
74cdf0e10cSrcweir 																			xTempFileStream,
75cdf0e10cSrcweir 																			ElementModes.WRITE );
76cdf0e10cSrcweir 			if ( xTempStorage == null )
77cdf0e10cSrcweir 			{
78cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
79cdf0e10cSrcweir 				return false;
80cdf0e10cSrcweir 			}
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 			// open a new substorage
83cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
84cdf0e10cSrcweir 																	"SubStorage1",
85cdf0e10cSrcweir 																	ElementModes.WRITE );
86cdf0e10cSrcweir 			if ( xTempSubStorage == null )
87cdf0e10cSrcweir 			{
88cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
89cdf0e10cSrcweir 				return false;
90cdf0e10cSrcweir 			}
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
96cdf0e10cSrcweir 														"SubStream1",
97cdf0e10cSrcweir 														"MediaType1",
98cdf0e10cSrcweir 														true,
99cdf0e10cSrcweir 														pBytes1,
100cdf0e10cSrcweir 														aRelations1 ) )
101cdf0e10cSrcweir 				return false;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
104cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
105cdf0e10cSrcweir 															true,
106cdf0e10cSrcweir 															ElementModes.WRITE,
107cdf0e10cSrcweir 															aRelations1 ) )
108cdf0e10cSrcweir 				return false;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
111cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
112cdf0e10cSrcweir 															false,
113cdf0e10cSrcweir 															ElementModes.WRITE,
114cdf0e10cSrcweir 															aRelations1 ) )
115cdf0e10cSrcweir 				return false;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 			// commit substorage first
118cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
119cdf0e10cSrcweir 				return false;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
122cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
123cdf0e10cSrcweir 				return false;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 			// dispose substorage
126cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
127cdf0e10cSrcweir 				return false;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 			// ================================================
130cdf0e10cSrcweir 			// check substorage
131cdf0e10cSrcweir 			// ================================================
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 			if ( !checkSubStorages( xTempStorage, pBytes1, aRelations1 ) )
134cdf0e10cSrcweir 				return false;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 			// dispose used storage to free resources
137cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
138cdf0e10cSrcweir 				return false;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 			// ================================================
141cdf0e10cSrcweir 			// now check all the written information with readwrite access
142cdf0e10cSrcweir 			// ================================================
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 			XStorage xResWriteStorage = m_aTestHelper.createStorageFromStream( m_xStorageFactory,
145cdf0e10cSrcweir 																			xTempFileStream,
146cdf0e10cSrcweir 																			ElementModes.WRITE );
147cdf0e10cSrcweir 			if ( xResWriteStorage == null )
148cdf0e10cSrcweir 			{
149cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
150cdf0e10cSrcweir 				return false;
151cdf0e10cSrcweir 			}
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResWriteStorage,
154cdf0e10cSrcweir 														true,
155cdf0e10cSrcweir 														ElementModes.WRITE,
156cdf0e10cSrcweir 														aRelations1 ) )
157cdf0e10cSrcweir 				return false;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 			if( !checkSubStorages( xResWriteStorage, pBytes1, aRelations1 ) )
160cdf0e10cSrcweir 				return false;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 			// try to open for writing after opening for reading
163cdf0e10cSrcweir 			XStorage xResWSubStorage = m_aTestHelper.openSubStorage( xResWriteStorage,
164cdf0e10cSrcweir 																	"SubStorage1",
165cdf0e10cSrcweir 																	ElementModes.WRITE );
166cdf0e10cSrcweir 			if ( xResWSubStorage == null )
167cdf0e10cSrcweir 			{
168cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open substorage for writing after it was opened for reading!" );
169cdf0e10cSrcweir 				return false;
170cdf0e10cSrcweir 			}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResWSubStorage,
173cdf0e10cSrcweir 														false,
174cdf0e10cSrcweir 														ElementModes.WRITE,
175cdf0e10cSrcweir 														aRelations1 ) )
176cdf0e10cSrcweir 				return false;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResWSubStorage,
179cdf0e10cSrcweir 											"SubStream1",
180cdf0e10cSrcweir 											"MediaType1",
181cdf0e10cSrcweir 											pBytes1,
182cdf0e10cSrcweir 											aRelations1 ) )
183cdf0e10cSrcweir 				return false;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 			// dispose used storage to free resources
186cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResWriteStorage ) )
187cdf0e10cSrcweir 				return false;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 			// ================================================
191cdf0e10cSrcweir 			// now check all the written information with readonly access
192cdf0e10cSrcweir 			// ================================================
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 			// close the output part of the temporary stream
195cdf0e10cSrcweir 			// the output part must present since we already wrote to the stream
196cdf0e10cSrcweir 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
197cdf0e10cSrcweir 				return false;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
200cdf0e10cSrcweir 			if ( xTempInStream == null )
201cdf0e10cSrcweir 				return false;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 			// open input stream
204cdf0e10cSrcweir 			// since no mode is provided the result storage must be opened readonly
205cdf0e10cSrcweir 			XStorage xResultStorage = m_aTestHelper.createStorageFromInputStream( m_xStorageFactory,
206cdf0e10cSrcweir 																					xTempInStream );
207cdf0e10cSrcweir 			if ( xResultStorage == null )
208cdf0e10cSrcweir 			{
209cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
210cdf0e10cSrcweir 				return false;
211cdf0e10cSrcweir 			}
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage,
214cdf0e10cSrcweir 														true,
215cdf0e10cSrcweir 														ElementModes.READ,
216cdf0e10cSrcweir 														aRelations1 ) )
217cdf0e10cSrcweir 				return false;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 			if( !checkSubStorages( xResultStorage, pBytes1, aRelations1 ) )
220cdf0e10cSrcweir 				return false;
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 			return true;
223cdf0e10cSrcweir 		}
224cdf0e10cSrcweir 		catch( Exception e )
225cdf0e10cSrcweir 		{
226cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
227cdf0e10cSrcweir 			return false;
228cdf0e10cSrcweir 		}
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir 
checkSubStorages( XStorage xStorage, byte[] pBytes1, StringPair[][] aRelations )231cdf0e10cSrcweir 	private boolean checkSubStorages( XStorage xStorage, byte[] pBytes1, StringPair[][] aRelations )
232cdf0e10cSrcweir 	{
233cdf0e10cSrcweir 		XStorage xReadSubStorage1 = m_aTestHelper.openSubStorage( xStorage,
234cdf0e10cSrcweir 																"SubStorage1",
235cdf0e10cSrcweir 																ElementModes.READ );
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 		XStorage xReadSubStorage2 = m_aTestHelper.openSubStorage( xStorage,
238cdf0e10cSrcweir 																"SubStorage1",
239cdf0e10cSrcweir 																ElementModes.READ );
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 		if ( xReadSubStorage1 == null || xReadSubStorage2 == null )
242cdf0e10cSrcweir 		{
243cdf0e10cSrcweir 			m_aTestHelper.Error( "Can't open substorage for reading!" );
244cdf0e10cSrcweir 			return false;
245cdf0e10cSrcweir 		}
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage1,
248cdf0e10cSrcweir 													false,
249cdf0e10cSrcweir 													ElementModes.READ,
250cdf0e10cSrcweir 													aRelations ) )
251cdf0e10cSrcweir 			return false;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage2,
254cdf0e10cSrcweir 													false,
255cdf0e10cSrcweir 													ElementModes.READ,
256cdf0e10cSrcweir 													aRelations ) )
257cdf0e10cSrcweir 			return false;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 		if ( !m_aTestHelper.checkStream( xReadSubStorage1,
260cdf0e10cSrcweir 											"SubStream1",
261cdf0e10cSrcweir 											"MediaType1",
262cdf0e10cSrcweir 											pBytes1,
263cdf0e10cSrcweir 											aRelations ) )
264cdf0e10cSrcweir 			return false;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 		if ( !m_aTestHelper.checkStream( xReadSubStorage2,
267cdf0e10cSrcweir 										"SubStream1",
268cdf0e10cSrcweir 										"MediaType1",
269cdf0e10cSrcweir 										pBytes1,
270cdf0e10cSrcweir 										aRelations ) )
271cdf0e10cSrcweir 			return false;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 		if ( !m_aTestHelper.disposeStorage( xReadSubStorage1 ) )
274cdf0e10cSrcweir 			return false;
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 		if ( !m_aTestHelper.disposeStorage( xReadSubStorage2 ) )
277cdf0e10cSrcweir 			return false;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 		return true;
280cdf0e10cSrcweir 	}
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283