tdf#130857 qt weld: Introduce QtInstanceScrolledWindow
[LibreOffice.git] / odk / examples / java / Storage / Test02.java
blob92f5a8c81ce7635aa57133b6932237d851c7bc98
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 .
21 import com.sun.star.lang.XMultiServiceFactory;
22 import com.sun.star.lang.XSingleServiceFactory;
24 import com.sun.star.uno.UnoRuntime;
25 import com.sun.star.io.XStream;
26 import com.sun.star.io.XInputStream;
28 import com.sun.star.embed.*;
30 public class Test02 implements StorageTest {
32 XMultiServiceFactory m_xMSF;
33 XSingleServiceFactory m_xStorageFactory;
34 TestHelper m_aTestHelper;
36 public Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
38 m_xMSF = xMSF;
39 m_xStorageFactory = xStorageFactory;
40 m_aTestHelper = new TestHelper( "Test02: " );
43 public boolean test()
45 try
47 XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
48 if ( xTempFileStream == null )
49 return false;
51 // create storage based on the temporary stream
52 Object pArgs[] = new Object[2];
53 pArgs[0] = xTempFileStream;
54 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
56 Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
57 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
58 if ( xTempStorage == null )
60 m_aTestHelper.Error( "Can't create temporary storage representation!" );
61 return false;
64 // open a new substorage
65 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
66 "SubStorage1",
67 ElementModes.WRITE );
68 if ( xTempSubStorage == null )
70 m_aTestHelper.Error( "Can't create substorage!" );
71 return false;
74 byte pBytes1[] = { 1, 1, 1, 1, 1 };
76 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
77 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
78 return false;
80 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
81 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
82 "MediaType2",
83 true,
84 ElementModes.WRITE ) )
85 return false;
87 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
88 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
89 "MediaType3",
90 false,
91 ElementModes.WRITE ) )
92 return false;
94 // commit substorage first
95 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
96 return false;
98 // commit the root storage so the contents must be stored now
99 if ( !m_aTestHelper.commitStorage( xTempStorage ) )
100 return false;
102 // dispose used storage to free resources
103 // the substorage dispose will be triggered by this call
104 if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
105 return false;
109 // now check all the written information
112 // close the output part of the temporary stream
113 // the output part must present since we already wrote to the stream
114 if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
115 return false;
117 XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
118 if ( xTempInStream == null )
119 return false;
122 // open input stream
123 // since no mode is provided the result storage must be opened readonly
124 Object pOneArg[] = new Object[1];
125 pOneArg[0] = xTempInStream;
127 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
128 XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
129 if ( xResultStorage == null )
131 m_aTestHelper.Error( "Can't open storage based on input stream!" );
132 return false;
135 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.READ ) )
136 return false;
138 // open existing substorage
139 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
140 "SubStorage1",
141 ElementModes.READ );
142 if ( xResultSubStorage == null )
144 m_aTestHelper.Error( "Can't open existing substorage!" );
145 return false;
148 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
149 return false;
151 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
152 return false;
154 return true;
156 catch( Exception e )
158 m_aTestHelper.Error( "Exception: " + e );
159 return false;
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */