Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / package / qa / ofopxmlstorages / Test01.java
blobdd8665bc54729e3fe1f02b41ef0462ef5d9742b8
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 package complex.ofopxmlstorages;
21 import com.sun.star.uno.XInterface;
22 import com.sun.star.lang.XMultiServiceFactory;
23 import com.sun.star.lang.XSingleServiceFactory;
25 import com.sun.star.bridge.XUnoUrlResolver;
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.uno.XInterface;
29 import com.sun.star.embed.*;
30 import com.sun.star.beans.StringPair;
32 import share.LogWriter;
33 import complex.ofopxmlstorages.TestHelper;
34 import complex.ofopxmlstorages.StorageTest;
36 public class Test01 implements StorageTest {
38 XMultiServiceFactory m_xMSF;
39 XSingleServiceFactory m_xStorageFactory;
40 TestHelper m_aTestHelper;
42 public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
44 m_xMSF = xMSF;
45 m_xStorageFactory = xStorageFactory;
46 m_aTestHelper = new TestHelper( aLogWriter, "Test01: " );
49 public boolean test()
51 StringPair[][] aRelations1 =
52 { { new StringPair( "Id", "Num1" ) },
53 { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
54 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
55 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
56 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
57 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
58 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
61 StringPair[][] aRelations2 =
62 { { new StringPair( "Id", "Num1" ) },
63 { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
64 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
65 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
66 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
67 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
68 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
71 try
73 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
74 if ( sTempFileURL == null || sTempFileURL == "" )
76 m_aTestHelper.Error( "No valid temporary file was created!" );
77 return false;
80 // create temporary storage based on arbitrary medium
81 // after such a storage is closed it is lost
82 XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
83 if ( xTempStorage == null )
85 m_aTestHelper.Error( "Can't create temporary storage representation!" );
86 return false;
89 // open a new substorage
90 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
91 "SubStorage1",
92 ElementModes.WRITE );
93 if ( xTempSubStorage == null )
95 m_aTestHelper.Error( "Can't create substorage!" );
96 return false;
99 byte pBytes1[] = { 1, 1, 1, 1, 1 };
101 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
102 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
103 "SubStream1",
104 "MediaType1",
105 true,
106 pBytes1,
107 aRelations1 ) )
108 return false;
110 byte pBytes2[] = { 2, 2, 2, 2, 2 };
112 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
113 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
114 "SubStream2",
115 "MediaType2",
116 false,
117 pBytes2,
118 aRelations2 ) )
119 return false;
121 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
122 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
123 true,
124 ElementModes.WRITE,
125 aRelations1 ) )
126 return false;
128 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
129 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
130 false,
131 ElementModes.WRITE,
132 aRelations1 ) )
133 return false;
135 // create temporary storage based on a previously created temporary file
136 XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
137 sTempFileURL,
138 ElementModes.WRITE );
139 if ( xTempFileStorage == null )
141 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
142 return false;
145 // copy xTempStorage to xTempFileStorage
146 // xTempFileStorage will be automatically committed
147 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
148 return false;
150 // dispose used storages to free resources
151 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
152 return false;
155 // now check all the written and copied information
158 // the temporary file must not be locked any more after storage disposing
159 XStorage xResultStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
160 sTempFileURL,
161 ElementModes.WRITE );
162 if ( xResultStorage == null )
164 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
165 return false;
168 if ( !m_aTestHelper.checkStorageProperties( xResultStorage,
169 true,
170 ElementModes.WRITE,
171 aRelations1 ) )
172 return false;
174 // open existing substorage
175 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
176 "SubStorage1",
177 ElementModes.READ );
178 if ( xResultSubStorage == null )
180 m_aTestHelper.Error( "Can't open existing substorage!" );
181 return false;
184 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage,
185 false,
186 ElementModes.READ,
187 aRelations1 ) )
188 return false;
190 if ( !m_aTestHelper.checkStream( xResultSubStorage,
191 "SubStream1",
192 "MediaType1",
193 pBytes1,
194 aRelations1 ) )
195 return false;
197 if ( !m_aTestHelper.checkStream( xResultSubStorage,
198 "SubStream2",
199 "MediaType2",
200 pBytes2,
201 aRelations2 ) )
202 return false;
204 // dispose used storages to free resources
205 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
206 return false;
208 return true;
210 catch( Exception e )
212 m_aTestHelper.Error( "Exception: " + e );
213 return false;