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