1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package complex.storages;
20 
21 import com.sun.star.uno.XInterface;
22 import com.sun.star.lang.XMultiServiceFactory;
23 import com.sun.star.lang.XSingleServiceFactory;
24 
25 import com.sun.star.bridge.XUnoUrlResolver;
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.uno.XInterface;
28 
29 import com.sun.star.embed.*;
30 import com.sun.star.container.XNameAccess;
31 
32 import share.LogWriter;
33 import complex.storages.TestHelper;
34 import complex.storages.StorageTest;
35 
36 public class Test03 implements StorageTest {
37 
38     XMultiServiceFactory m_xMSF;
39     XSingleServiceFactory m_xStorageFactory;
40     TestHelper m_aTestHelper;
41 
Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )42     public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
43     {
44         m_xMSF = xMSF;
45         m_xStorageFactory = xStorageFactory;
46         m_aTestHelper = new TestHelper( aLogWriter, "Test03: " );
47     }
48 
test()49     public boolean test()
50     {
51         try
52         {
53             // create temporary storage based on arbitrary medium
54             // after such a storage is closed it is lost
55             Object oTempStorage = m_xStorageFactory.createInstance();
56             XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
57             if ( xTempStorage == null )
58             {
59                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
60                 return false;
61             }
62 
63             // open a new substorage
64             XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
65                                                                         "SubStorage1",
66                                                                         ElementModes.WRITE );
67             if ( xTempSubStorage == null )
68             {
69                 m_aTestHelper.Error( "Can't create substorage!" );
70                 return false;
71             }
72 
73             byte pBigBytes[] = new byte[33000];
74             for ( int nInd = 0; nInd < 33000; nInd++ )
75                 pBigBytes[nInd] = (byte)( nInd % 128 );
76 
77             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
78             if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
79                 return false;
80 
81             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
82             if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
83                 return false;
84 
85             byte pBytes1[] = { 1, 1, 1, 1, 1 };
86 
87             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
88             if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
89                 return false;
90 
91             byte pBytes2[] = { 2, 2, 2, 2, 2 };
92 
93             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
94             if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
95                 return false;
96 
97             // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
98             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
99                                                             "MediaType3",
100                                                             false,
101                                                             ElementModes.WRITE ) )
102                 return false;
103 
104             if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
105                 return false;
106 
107             if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
108                 return false;
109 
110 
111             // check storage hierarchy tree
112 
113 
114             // check that isStorageElement() and isStreamElement reacts to nonexistent object correctly
115             try {
116                 xTempStorage.isStorageElement( "does not exist" );
117                 m_aTestHelper.Error( "Nonexistent element doesn't detected by isStorageElement() call!" );
118                 return false;
119             }
120             catch( com.sun.star.container.NoSuchElementException ne )
121             {
122             }
123             catch( Exception e )
124             {
125                 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
126                 return false;
127             }
128 
129             try {
130                 xTempStorage.isStreamElement( "does not exist" );
131                 m_aTestHelper.Error( "Nonexistent element doesn't detected by isStreamElement() call!" );
132                 return false;
133             }
134             catch( com.sun.star.container.NoSuchElementException ne )
135             {
136             }
137             catch( Exception e )
138             {
139                 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
140                 return false;
141             }
142 
143             XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
144             if ( xRootNameAccess == null )
145             {
146                 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
147                 return false;
148             }
149 
150             try {
151                 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
152                 {
153                     m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
154                     return false;
155                 }
156 
157                 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
158                 {
159                     m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
160                     return false;
161                 }
162             }
163             catch( Exception e )
164             {
165                 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
166                 return false;
167             }
168 
169 
170             // check that root storage contents are represented correctly
171             String sRootCont[] = xRootNameAccess.getElementNames();
172 
173             if ( sRootCont.length != 3 )
174             {
175                 m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
176                 return false;
177             }
178 
179             int nFlag = 0;
180             for ( int nInd = 0; nInd < sRootCont.length; nInd++ )
181             {
182                 if ( sRootCont[nInd].equals( "SubStorage1" ) )
183                     nFlag |= 1;
184                 else if ( sRootCont[nInd].equals( "SubStream1" ) )
185                     nFlag |= 2;
186                 else if ( sRootCont[nInd].equals( "BigSubStream1" ) )
187                     nFlag |= 4;
188             }
189 
190             if ( nFlag != 7 || !( xRootNameAccess.hasByName( "BigSubStream1" ) && xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
191             {
192                 m_aTestHelper.Error( "Root storage contains wrong list of children!" );
193                 return false;
194             }
195 
196             // get storage through XNameAccess
197             XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
198             if ( xResultSubStorage == null )
199                 return false;
200 
201             if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
202                 return false;
203 
204             XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
205             if ( xChildAccess == null )
206             {
207                 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
208                 return false;
209             }
210 
211             if ( !( xChildAccess.hasByName( "SubStream2" ) && xChildAccess.hasByName( "BigSubStream2" ) )
212               || !xResultSubStorage.isStreamElement( "SubStream2" )
213               || !xResultSubStorage.isStreamElement( "BigSubStream2" ) )
214             {
215                 m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
216                 return false;
217             }
218 
219             return true;
220         }
221         catch( Exception e )
222         {
223             m_aTestHelper.Error( "Exception: " + e );
224             return false;
225         }
226     }
227 
getStorageFromNameAccess( XNameAccess xAccess, String sName )228     public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
229     {
230         try
231         {
232             Object oStorage = xAccess.getByName( sName );
233             XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
234 
235             if ( xResult != null )
236                 return xResult;
237             else
238                 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
239         }
240         catch( Exception e )
241         {
242             m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
243         }
244 
245         return null;
246     }
247 
248 }
249 
250