bump product version to 5.0.4.1
[LibreOffice.git] / odk / examples / java / Storage / Test02.java
blob5750f253939c8f6b367301deefdc15f3f4a0bb0b
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 .
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.io.XStream;
25 import com.sun.star.io.XInputStream;
27 import com.sun.star.embed.*;
29 public class Test02 implements StorageTest {
31 XMultiServiceFactory m_xMSF;
32 XSingleServiceFactory m_xStorageFactory;
33 TestHelper m_aTestHelper;
35 public Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
37 m_xMSF = xMSF;
38 m_xStorageFactory = xStorageFactory;
39 m_aTestHelper = new TestHelper( "Test02: " );
42 public boolean test()
44 try
46 XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
47 if ( xTempFileStream == null )
48 return false;
50 // create storage based on the temporary stream
51 Object pArgs[] = new Object[2];
52 pArgs[0] = xTempFileStream;
53 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
55 Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
56 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
57 if ( xTempStorage == null )
59 m_aTestHelper.Error( "Can't create temporary storage representation!" );
60 return false;
63 // open a new substorage
64 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
65 "SubStorage1",
66 ElementModes.WRITE );
67 if ( xTempSubStorage == null )
69 m_aTestHelper.Error( "Can't create substorage!" );
70 return false;
73 byte pBytes1[] = { 1, 1, 1, 1, 1 };
75 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
76 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
77 return false;
79 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
80 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
81 "MediaType2",
82 true,
83 ElementModes.WRITE ) )
84 return false;
86 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
87 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
88 "MediaType3",
89 false,
90 ElementModes.WRITE ) )
91 return false;
93 // commit substorage first
94 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
95 return false;
97 // commit the root storage so the contents must be stored now
98 if ( !m_aTestHelper.commitStorage( xTempStorage ) )
99 return false;
101 // dispose used storage to free resources
102 // the substorage dispose will be triggered by this call
103 if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
104 return false;
108 // now check all the written information
111 // close the output part of the temporary stream
112 // the output part must present since we already wrote to the stream
113 if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
114 return false;
116 XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
117 if ( xTempInStream == null )
118 return false;
121 // open input stream
122 // since no mode is provided the result storage must be opened readonly
123 Object pOneArg[] = new Object[1];
124 pOneArg[0] = xTempInStream;
126 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
127 XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
128 if ( xResultStorage == null )
130 m_aTestHelper.Error( "Can't open storage based on input stream!" );
131 return false;
134 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.READ ) )
135 return false;
137 // open existing substorage
138 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
139 "SubStorage1",
140 ElementModes.READ );
141 if ( xResultSubStorage == null )
143 m_aTestHelper.Error( "Can't open existing substorage!" );
144 return false;
147 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
148 return false;
150 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
151 return false;
153 return true;
155 catch( Exception e )
157 m_aTestHelper.Error( "Exception: " + e );
158 return false;