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 
32cdf0e10cSrcweir import com.sun.star.embed.*;
33cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
34cdf0e10cSrcweir import com.sun.star.beans.StringPair;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import share.LogWriter;
37cdf0e10cSrcweir import complex.ofopxmlstorages.TestHelper;
38cdf0e10cSrcweir import complex.ofopxmlstorages.StorageTest;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir public class Test03 implements StorageTest {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
43cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
44cdf0e10cSrcweir 	TestHelper m_aTestHelper;
45cdf0e10cSrcweir 
Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46cdf0e10cSrcweir 	public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47cdf0e10cSrcweir 	{
48cdf0e10cSrcweir 		m_xMSF = xMSF;
49cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
50cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test03: " );
51cdf0e10cSrcweir 	}
52cdf0e10cSrcweir 
test()53cdf0e10cSrcweir     public boolean test()
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		try
56cdf0e10cSrcweir 		{
57cdf0e10cSrcweir 			StringPair[][] aRelations =
58cdf0e10cSrcweir 				{ { new StringPair( "Id", "Num1" ) },
59cdf0e10cSrcweir 				  { new StringPair( "Target", "TargetURLValue" ), new StringPair( "Id", "Num6" ) },
60cdf0e10cSrcweir 				  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
61cdf0e10cSrcweir 				  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
62cdf0e10cSrcweir 				  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
63cdf0e10cSrcweir 				  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
64cdf0e10cSrcweir 				  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
65cdf0e10cSrcweir 				};
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
68cdf0e10cSrcweir 			// after such a storage is closed it is lost
69cdf0e10cSrcweir 			XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
70cdf0e10cSrcweir 			if ( xTempStorage == null )
71cdf0e10cSrcweir 			{
72cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
73cdf0e10cSrcweir 				return false;
74cdf0e10cSrcweir 			}
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 			// open a new substorage
77cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
78cdf0e10cSrcweir 																		"SubStorage1",
79cdf0e10cSrcweir 																		ElementModes.WRITE );
80cdf0e10cSrcweir 			if ( xTempSubStorage == null )
81cdf0e10cSrcweir 			{
82cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
83cdf0e10cSrcweir 				return false;
84cdf0e10cSrcweir 			}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
89cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage,
90cdf0e10cSrcweir 														"SubStream1",
91cdf0e10cSrcweir 														"MediaType1",
92cdf0e10cSrcweir 														true,
93cdf0e10cSrcweir 														pBytes1,
94cdf0e10cSrcweir 														aRelations ) )
95cdf0e10cSrcweir 				return false;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
100cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
101cdf0e10cSrcweir 														"SubStream2",
102cdf0e10cSrcweir 														"MediaType2",
103cdf0e10cSrcweir 														false,
104cdf0e10cSrcweir 														pBytes2,
105cdf0e10cSrcweir 														aRelations ) )
106cdf0e10cSrcweir 				return false;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
109cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
110cdf0e10cSrcweir 															false,
111cdf0e10cSrcweir 															ElementModes.WRITE,
112cdf0e10cSrcweir 															aRelations ) )
113cdf0e10cSrcweir 				return false;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
116cdf0e10cSrcweir 				return false;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
119cdf0e10cSrcweir 				return false;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 			// ================================================
122cdf0e10cSrcweir 			// check storage hyerarchy tree
123cdf0e10cSrcweir 			// ================================================
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 			// check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
126cdf0e10cSrcweir 			try {
127cdf0e10cSrcweir 				xTempStorage.isStorageElement( "does not exist" );
128cdf0e10cSrcweir 				m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
129cdf0e10cSrcweir 				return false;
130cdf0e10cSrcweir 			}
131cdf0e10cSrcweir 			catch( com.sun.star.container.NoSuchElementException ne )
132cdf0e10cSrcweir 			{
133cdf0e10cSrcweir 			}
134cdf0e10cSrcweir 			catch( Exception e )
135cdf0e10cSrcweir 			{
136cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
137cdf0e10cSrcweir 				return false;
138cdf0e10cSrcweir 			}
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 			try {
141cdf0e10cSrcweir 				xTempStorage.isStreamElement( "does not exist" );
142cdf0e10cSrcweir 				m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
143cdf0e10cSrcweir 				return false;
144cdf0e10cSrcweir 			}
145cdf0e10cSrcweir 			catch( com.sun.star.container.NoSuchElementException ne )
146cdf0e10cSrcweir 			{
147cdf0e10cSrcweir 			}
148cdf0e10cSrcweir 			catch( Exception e )
149cdf0e10cSrcweir 			{
150cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
151cdf0e10cSrcweir 				return false;
152cdf0e10cSrcweir 			}
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 			XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
155cdf0e10cSrcweir 			if ( xRootNameAccess == null )
156cdf0e10cSrcweir 			{
157cdf0e10cSrcweir 				m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
158cdf0e10cSrcweir 				return false;
159cdf0e10cSrcweir 			}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 			try {
162cdf0e10cSrcweir 				if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
163cdf0e10cSrcweir 				{
164cdf0e10cSrcweir 					m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
165cdf0e10cSrcweir 					return false;
166cdf0e10cSrcweir 				}
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 				if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
169cdf0e10cSrcweir 				{
170cdf0e10cSrcweir 					m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
171cdf0e10cSrcweir 					return false;
172cdf0e10cSrcweir 				}
173cdf0e10cSrcweir 			}
174cdf0e10cSrcweir 			catch( Exception e )
175cdf0e10cSrcweir 			{
176cdf0e10cSrcweir 				m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
177cdf0e10cSrcweir 				return false;
178cdf0e10cSrcweir 			}
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 			// check that root storage contents are represented correctly
182cdf0e10cSrcweir 			String sRootCont[] = xRootNameAccess.getElementNames();
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 			if ( sRootCont.length != 2 )
185cdf0e10cSrcweir 			{
186cdf0e10cSrcweir 				m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
187cdf0e10cSrcweir 				return false;
188cdf0e10cSrcweir 			}
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 			if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
191cdf0e10cSrcweir 			     || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
192cdf0e10cSrcweir 			  || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
193cdf0e10cSrcweir 			{
194cdf0e10cSrcweir 				m_aTestHelper.Error( "Root storage contains wrong list of children!" );
195cdf0e10cSrcweir 				return false;
196cdf0e10cSrcweir 			}
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 			// get storage through XNameAccess
199cdf0e10cSrcweir 			XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
200cdf0e10cSrcweir 			if ( xResultSubStorage == null )
201cdf0e10cSrcweir 				return false;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage,
204cdf0e10cSrcweir 														false,
205cdf0e10cSrcweir 														ElementModes.READ,
206cdf0e10cSrcweir 														aRelations ) )
207cdf0e10cSrcweir 				return false;
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 			XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
210cdf0e10cSrcweir 			if ( xChildAccess == null )
211cdf0e10cSrcweir 			{
212cdf0e10cSrcweir 				m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
213cdf0e10cSrcweir 				return false;
214cdf0e10cSrcweir 			}
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 			if ( !xChildAccess.hasByName( "SubStream2" )
217cdf0e10cSrcweir 			  || !xResultSubStorage.isStreamElement( "SubStream2" )
218cdf0e10cSrcweir 			  || xResultSubStorage.isStorageElement( "SubStream2" ) )
219cdf0e10cSrcweir 			{
220cdf0e10cSrcweir 				m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
221cdf0e10cSrcweir 				return false;
222cdf0e10cSrcweir 			}
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 			return true;
225cdf0e10cSrcweir 		}
226cdf0e10cSrcweir 		catch( Exception e )
227cdf0e10cSrcweir 		{
228cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
229cdf0e10cSrcweir 			return false;
230cdf0e10cSrcweir 		}
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir 
getStorageFromNameAccess( XNameAccess xAccess, String sName )233cdf0e10cSrcweir 	public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
234cdf0e10cSrcweir 	{
235cdf0e10cSrcweir 		try
236cdf0e10cSrcweir 		{
237cdf0e10cSrcweir 			Object oStorage = xAccess.getByName( sName );
238cdf0e10cSrcweir 			XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 			if ( xResult != null )
241cdf0e10cSrcweir 				return xResult;
242cdf0e10cSrcweir 			else
243cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
244cdf0e10cSrcweir 		}
245cdf0e10cSrcweir 		catch( Exception e )
246cdf0e10cSrcweir 		{
247cdf0e10cSrcweir 			m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
248cdf0e10cSrcweir 		}
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 		return null;
251cdf0e10cSrcweir 	}
252cdf0e10cSrcweir 
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
255