Update git submodules
[LibreOffice.git] / odk / examples / java / Storage / Test01.java
blob649f284401ce53acb5207c4acf47c9658968279b
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.*;
26 public class Test01 implements StorageTest {
28 XMultiServiceFactory m_xMSF;
29 XSingleServiceFactory m_xStorageFactory;
30 TestHelper m_aTestHelper;
32 public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
34 m_xMSF = xMSF;
35 m_xStorageFactory = xStorageFactory;
36 m_aTestHelper = new TestHelper( "Test01: " );
39 public boolean test()
41 try
43 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
44 if ( sTempFileURL == null || sTempFileURL.equals("") )
46 m_aTestHelper.Error( "No valid temporary file was created!" );
47 return false;
50 // create temporary storage based on arbitrary medium
51 // after such a storage is closed it is lost
52 Object oTempStorage = m_xStorageFactory.createInstance();
53 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
54 if ( xTempStorage == null )
56 m_aTestHelper.Error( "Can't create temporary storage representation!" );
57 return false;
60 // open a new substorage
61 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
62 "SubStorage1",
63 ElementModes.WRITE );
64 if ( xTempSubStorage == null )
66 m_aTestHelper.Error( "Can't create substorage!" );
67 return false;
70 byte pBytes1[] = { 1, 1, 1, 1, 1 };
72 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
73 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
74 return false;
76 byte pBytes2[] = { 2, 2, 2, 2, 2 };
78 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
79 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
80 return false;
82 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
83 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
84 "MediaType3",
85 true,
86 ElementModes.READWRITE ) )
87 return false;
89 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
90 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
91 "MediaType4",
92 false,
93 ElementModes.WRITE ) )
94 return false;
96 // create temporary storage based on a previously created temporary file
97 Object pArgs[] = new Object[2];
98 pArgs[0] = sTempFileURL;
99 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
101 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
102 XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
103 if ( xTempFileStorage == null )
105 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
106 return false;
109 // copy xTempStorage to xTempFileStorage
110 // xTempFileStorage will be automatically committed
111 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
112 return false;
114 // dispose used storages to free resources
115 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
116 return false;
119 // now check all the written and copied information
122 // the temporary file must not be locked any more after storage disposing
123 pArgs[1] = Integer.valueOf( ElementModes.READWRITE );
124 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
125 XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
126 if ( xResultStorage == null )
128 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
129 return false;
132 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READWRITE ) )
133 return false;
135 // open existing substorage
136 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
137 "SubStorage1",
138 ElementModes.READ );
139 if ( xResultSubStorage == null )
141 m_aTestHelper.Error( "Can't open existing substorage!" );
142 return false;
145 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
146 return false;
148 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
149 return false;
151 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
152 return false;
154 // dispose used storages to free resources
155 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
156 return false;
158 return true;
160 catch( Exception e )
162 m_aTestHelper.Error( "Exception: " + e );
163 return false;
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */