bump product version to 5.0.4.1
[LibreOffice.git] / odk / examples / java / Storage / Test01.java
blobed2d1e9546aaa4b79e02b292029c5ade339f9664
1 /*
2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 import com.sun.star.lang.XMultiServiceFactory;
20 import com.sun.star.lang.XSingleServiceFactory;
22 import com.sun.star.uno.UnoRuntime;
23 import com.sun.star.embed.*;
25 public class Test01 implements StorageTest {
27 XMultiServiceFactory m_xMSF;
28 XSingleServiceFactory m_xStorageFactory;
29 TestHelper m_aTestHelper;
31 public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
33 m_xMSF = xMSF;
34 m_xStorageFactory = xStorageFactory;
35 m_aTestHelper = new TestHelper( "Test01: " );
38 public boolean test()
40 try
42 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
43 if ( sTempFileURL == null || sTempFileURL.equals("") )
45 m_aTestHelper.Error( "No valid temporary file was created!" );
46 return false;
49 // create temporary storage based on arbitrary medium
50 // after such a storage is closed it is lost
51 Object oTempStorage = m_xStorageFactory.createInstance();
52 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
53 if ( xTempStorage == null )
55 m_aTestHelper.Error( "Can't create temporary storage representation!" );
56 return false;
59 // open a new substorage
60 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
61 "SubStorage1",
62 ElementModes.WRITE );
63 if ( xTempSubStorage == null )
65 m_aTestHelper.Error( "Can't create substorage!" );
66 return false;
69 byte pBytes1[] = { 1, 1, 1, 1, 1 };
71 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
72 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
73 return false;
75 byte pBytes2[] = { 2, 2, 2, 2, 2 };
77 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
78 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
79 return false;
81 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
82 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
83 "MediaType3",
84 true,
85 ElementModes.READWRITE ) )
86 return false;
88 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
89 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
90 "MediaType4",
91 false,
92 ElementModes.WRITE ) )
93 return false;
95 // create temporary storage based on a previously created temporary file
96 Object pArgs[] = new Object[2];
97 pArgs[0] = sTempFileURL;
98 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
100 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
101 XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
102 if ( xTempFileStorage == null )
104 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
105 return false;
108 // copy xTempStorage to xTempFileStorage
109 // xTempFileStorage will be automatically committed
110 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
111 return false;
113 // dispose used storages to free resources
114 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
115 return false;
118 // now check all the written and copied information
121 // the temporary file must not be locked any more after storage disposing
122 pArgs[1] = Integer.valueOf( ElementModes.READWRITE );
123 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
124 XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
125 if ( xResultStorage == null )
127 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
128 return false;
131 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READWRITE ) )
132 return false;
134 // open existing substorage
135 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
136 "SubStorage1",
137 ElementModes.READ );
138 if ( xResultSubStorage == null )
140 m_aTestHelper.Error( "Can't open existing substorage!" );
141 return false;
144 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
145 return false;
147 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
148 return false;
150 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
151 return false;
153 // dispose used storages to free resources
154 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
155 return false;
157 return true;
159 catch( Exception e )
161 m_aTestHelper.Error( "Exception: " + e );
162 return false;