xref: /trunk/main/odk/examples/java/Storage/Test03.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1*ae15d43aSAndrew Rist /**************************************************************
2*ae15d43aSAndrew Rist  *
3*ae15d43aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ae15d43aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ae15d43aSAndrew Rist  * distributed with this work for additional information
6*ae15d43aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ae15d43aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ae15d43aSAndrew Rist  * "License"); you may not use this file except in compliance
9*ae15d43aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ae15d43aSAndrew Rist  *
11*ae15d43aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ae15d43aSAndrew Rist  *
13*ae15d43aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ae15d43aSAndrew Rist  * software distributed under the License is distributed on an
15*ae15d43aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ae15d43aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ae15d43aSAndrew Rist  * specific language governing permissions and limitations
18*ae15d43aSAndrew Rist  * under the License.
19*ae15d43aSAndrew Rist  *
20*ae15d43aSAndrew Rist  *************************************************************/
21*ae15d43aSAndrew Rist 
22cdf0e10cSrcweir package storagetesting;
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 
35cdf0e10cSrcweir import storagetesting.TestHelper;
36cdf0e10cSrcweir import storagetesting.StorageTest;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public class Test03 implements StorageTest {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     XMultiServiceFactory m_xMSF;
41cdf0e10cSrcweir     XSingleServiceFactory m_xStorageFactory;
42cdf0e10cSrcweir     TestHelper m_aTestHelper;
43cdf0e10cSrcweir 
Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )44cdf0e10cSrcweir     public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         m_xMSF = xMSF;
47cdf0e10cSrcweir         m_xStorageFactory = xStorageFactory;
48cdf0e10cSrcweir         m_aTestHelper = new TestHelper( "Test03: " );
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
test()51cdf0e10cSrcweir     public boolean test()
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         try
54cdf0e10cSrcweir         {
55cdf0e10cSrcweir             // create temporary storage based on arbitrary medium
56cdf0e10cSrcweir             // after such a storage is closed it is lost
57cdf0e10cSrcweir             Object oTempStorage = m_xStorageFactory.createInstance();
58cdf0e10cSrcweir             XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
59cdf0e10cSrcweir             if ( xTempStorage == null )
60cdf0e10cSrcweir             {
61cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
62cdf0e10cSrcweir                 return false;
63cdf0e10cSrcweir             }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir             // open a new substorage
66cdf0e10cSrcweir             XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
67cdf0e10cSrcweir                                                                         "SubStorage1",
68cdf0e10cSrcweir                                                                         ElementModes.ELEMENT_WRITE );
69cdf0e10cSrcweir             if ( xTempSubStorage == null )
70cdf0e10cSrcweir             {
71cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create substorage!" );
72cdf0e10cSrcweir                 return false;
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir             byte pBytes1[] = { 1, 1, 1, 1, 1 };
76cdf0e10cSrcweir 
77cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
78cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
79cdf0e10cSrcweir                 return false;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir             byte pBytes2[] = { 2, 2, 2, 2, 2 };
82cdf0e10cSrcweir 
83cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
84cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
85cdf0e10cSrcweir                 return false;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
88cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
89cdf0e10cSrcweir                                                             "MediaType3",
90cdf0e10cSrcweir                                                             false,
91cdf0e10cSrcweir                                                             ElementModes.ELEMENT_WRITE ) )
92cdf0e10cSrcweir                 return false;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
95cdf0e10cSrcweir                 return false;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
98cdf0e10cSrcweir                 return false;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             // ================================================
101cdf0e10cSrcweir             // check storage hyerarchy tree
102cdf0e10cSrcweir             // ================================================
103cdf0e10cSrcweir 
104cdf0e10cSrcweir             // check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
105cdf0e10cSrcweir             try {
106cdf0e10cSrcweir                 xTempStorage.isStorageElement( "does not exist" );
107cdf0e10cSrcweir                 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
108cdf0e10cSrcweir                 return false;
109cdf0e10cSrcweir             }
110cdf0e10cSrcweir             catch( com.sun.star.container.NoSuchElementException ne )
111cdf0e10cSrcweir             {
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir             catch( Exception e )
114cdf0e10cSrcweir             {
115cdf0e10cSrcweir                 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
116cdf0e10cSrcweir                 return false;
117cdf0e10cSrcweir             }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             try {
120cdf0e10cSrcweir                 xTempStorage.isStreamElement( "does not exist" );
121cdf0e10cSrcweir                 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
122cdf0e10cSrcweir                 return false;
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir             catch( com.sun.star.container.NoSuchElementException ne )
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir             }
127cdf0e10cSrcweir             catch( Exception e )
128cdf0e10cSrcweir             {
129cdf0e10cSrcweir                 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
130cdf0e10cSrcweir                 return false;
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
134cdf0e10cSrcweir             if ( xRootNameAccess == null )
135cdf0e10cSrcweir             {
136cdf0e10cSrcweir                 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
137cdf0e10cSrcweir                 return false;
138cdf0e10cSrcweir             }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             try {
141cdf0e10cSrcweir                 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
142cdf0e10cSrcweir                 {
143cdf0e10cSrcweir                     m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
144cdf0e10cSrcweir                     return false;
145cdf0e10cSrcweir                 }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir                 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
148cdf0e10cSrcweir                 {
149cdf0e10cSrcweir                     m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
150cdf0e10cSrcweir                     return false;
151cdf0e10cSrcweir                 }
152cdf0e10cSrcweir             }
153cdf0e10cSrcweir             catch( Exception e )
154cdf0e10cSrcweir             {
155cdf0e10cSrcweir                 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
156cdf0e10cSrcweir                 return false;
157cdf0e10cSrcweir             }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             // check that root storage contents are represented correctly
161cdf0e10cSrcweir             String sRootCont[] = xRootNameAccess.getElementNames();
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             if ( sRootCont.length != 2 )
164cdf0e10cSrcweir             {
165cdf0e10cSrcweir                 m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
166cdf0e10cSrcweir                 return false;
167cdf0e10cSrcweir             }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir             if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
170cdf0e10cSrcweir                  || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
171cdf0e10cSrcweir               || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
172cdf0e10cSrcweir             {
173cdf0e10cSrcweir                 m_aTestHelper.Error( "Root storage contains wrong list of children!" );
174cdf0e10cSrcweir                 return false;
175cdf0e10cSrcweir             }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             // get storage through XNameAccess
178cdf0e10cSrcweir             XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
179cdf0e10cSrcweir             if ( xResultSubStorage == null )
180cdf0e10cSrcweir                 return false;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.ELEMENT_READ ) )
183cdf0e10cSrcweir                 return false;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir             XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
186cdf0e10cSrcweir             if ( xChildAccess == null )
187cdf0e10cSrcweir             {
188cdf0e10cSrcweir                 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
189cdf0e10cSrcweir                 return false;
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir             if ( !xChildAccess.hasByName( "SubStream2" )
193cdf0e10cSrcweir               || !xResultSubStorage.isStreamElement( "SubStream2" )
194cdf0e10cSrcweir               || xResultSubStorage.isStorageElement( "SubStream2" ) )
195cdf0e10cSrcweir             {
196cdf0e10cSrcweir                 m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
197cdf0e10cSrcweir                 return false;
198cdf0e10cSrcweir             }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir             return true;
201cdf0e10cSrcweir         }
202cdf0e10cSrcweir         catch( Exception e )
203cdf0e10cSrcweir         {
204cdf0e10cSrcweir             m_aTestHelper.Error( "Exception: " + e );
205cdf0e10cSrcweir             return false;
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir 
getStorageFromNameAccess( XNameAccess xAccess, String sName )209cdf0e10cSrcweir     public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
210cdf0e10cSrcweir     {
211cdf0e10cSrcweir         try
212cdf0e10cSrcweir         {
213cdf0e10cSrcweir             Object oStorage = xAccess.getByName( sName );
214cdf0e10cSrcweir             XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
215cdf0e10cSrcweir 
216cdf0e10cSrcweir             if ( xResult != null )
217cdf0e10cSrcweir                 return xResult;
218cdf0e10cSrcweir             else
219cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
220cdf0e10cSrcweir         }
221cdf0e10cSrcweir         catch( Exception e )
222cdf0e10cSrcweir         {
223cdf0e10cSrcweir             m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir         return null;
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir }
230