tdf#130857 qt weld: Introduce QtInstanceScrolledWindow
[LibreOffice.git] / odk / examples / java / Storage / Test03.java
blob500d46b923b7f8d78a85829baaa9aa31065bec23
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com.sun.star.lang.XMultiServiceFactory;
21 import com.sun.star.lang.XSingleServiceFactory;
23 import com.sun.star.uno.UnoRuntime;
24 import com.sun.star.embed.*;
25 import com.sun.star.container.XNameAccess;
27 public class Test03 implements StorageTest {
29 XMultiServiceFactory m_xMSF;
30 XSingleServiceFactory m_xStorageFactory;
31 TestHelper m_aTestHelper;
33 public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
35 m_xMSF = xMSF;
36 m_xStorageFactory = xStorageFactory;
37 m_aTestHelper = new TestHelper( "Test03: " );
40 public boolean test()
42 try
44 // create temporary storage based on arbitrary medium
45 // after such a storage is closed it is lost
46 Object oTempStorage = m_xStorageFactory.createInstance();
47 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
48 if ( xTempStorage == null )
50 m_aTestHelper.Error( "Can't create temporary storage representation!" );
51 return false;
54 // open a new substorage
55 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
56 "SubStorage1",
57 ElementModes.WRITE );
58 if ( xTempSubStorage == null )
60 m_aTestHelper.Error( "Can't create substorage!" );
61 return false;
64 byte pBytes1[] = { 1, 1, 1, 1, 1 };
66 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
67 if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
68 return false;
70 byte pBytes2[] = { 2, 2, 2, 2, 2 };
72 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
73 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
74 return false;
76 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
77 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
78 "MediaType3",
79 false,
80 ElementModes.WRITE ) )
81 return false;
83 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
84 return false;
86 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
87 return false;
90 // check storage hierarchy tree
93 // check that isStorageElement() and isStreamElement reacts to nonexistent object correctly
94 try {
95 xTempStorage.isStorageElement( "does not exist" );
96 m_aTestHelper.Error( "Nonexistent element doesn't detected by isStorageElement() call!" );
97 return false;
99 catch( com.sun.star.container.NoSuchElementException ne )
102 catch( Exception e )
104 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
105 return false;
108 try {
109 xTempStorage.isStreamElement( "does not exist" );
110 m_aTestHelper.Error( "Nonexistent element doesn't detected by isStreamElement() call!" );
111 return false;
113 catch( com.sun.star.container.NoSuchElementException ne )
116 catch( Exception e )
118 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
119 return false;
122 XNameAccess xRootNameAccess = UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
123 if ( xRootNameAccess == null )
125 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
126 return false;
129 try {
130 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
132 m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
133 return false;
136 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
138 m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
139 return false;
142 catch( Exception e )
144 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
145 return false;
149 // check that root storage contents are represented correctly
150 String sRootCont[] = xRootNameAccess.getElementNames();
152 if ( sRootCont.length != 2 )
154 m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
155 return false;
158 if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
159 || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
160 || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
162 m_aTestHelper.Error( "Root storage contains wrong list of children!" );
163 return false;
166 // get storage through XNameAccess
167 XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
168 if ( xResultSubStorage == null )
169 return false;
171 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
172 return false;
174 XNameAccess xChildAccess = UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
175 if ( xChildAccess == null )
177 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
178 return false;
181 if ( !xChildAccess.hasByName( "SubStream2" )
182 || !xResultSubStorage.isStreamElement( "SubStream2" )
183 || xResultSubStorage.isStorageElement( "SubStream2" ) )
185 m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
186 return false;
189 return true;
191 catch( Exception e )
193 m_aTestHelper.Error( "Exception: " + e );
194 return false;
198 public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
202 Object oStorage = xAccess.getByName( sName );
203 XStorage xResult = UnoRuntime.queryInterface( XStorage.class, oStorage );
205 if ( xResult != null )
206 return xResult;
207 else
208 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
210 catch( Exception e )
212 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
215 return null;
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */