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
.storages
;
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
.container
.XNameAccess
;
30 import com
.sun
.star
.io
.XStream
;
32 import com
.sun
.star
.embed
.*;
34 import share
.LogWriter
;
35 import complex
.storages
.TestHelper
;
36 import complex
.storages
.StorageTest
;
38 public class Test11
implements StorageTest
{
40 XMultiServiceFactory m_xMSF
;
41 XSingleServiceFactory m_xStorageFactory
;
42 TestHelper m_aTestHelper
;
44 public Test11( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
47 m_xStorageFactory
= xStorageFactory
;
48 m_aTestHelper
= new TestHelper( aLogWriter
, "Test11: " );
55 // create temporary storage based on arbitrary medium
56 // after such a storage is closed it is lost
57 Object oTempStorage
= m_xStorageFactory
.createInstance();
58 XStorage xTempStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
59 if ( xTempStorage
== null )
61 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
65 byte pBigBytes
[] = new byte[33000];
66 for ( int nInd
= 0; nInd
< 33000; nInd
++ )
67 pBigBytes
[nInd
] = (byte)( nInd
% 128 );
69 String sPass1
= "111111111";
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
.WriteBytesToEncrSubstream( xTempStorage
, "SubStream1", "MediaType1", true, pBytes1
, sPass1
) )
76 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
77 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
, sPass1
) )
80 // open a new substorage
81 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
84 if ( xTempSubStorage
== null )
86 m_aTestHelper
.Error( "Can't create substorage!" );
90 String sPass2
= "2222222222";
91 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
93 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
94 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempSubStorage
, "SubStream2", "MediaType2", true, pBytes2
, sPass2
) )
97 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempSubStorage
, "BigSubStream2", "MediaType2", true, pBigBytes
, sPass2
) )
101 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
102 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
105 ElementModes
.WRITE
) )
108 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
111 ElementModes
.WRITE
) )
115 // check cloning at current state
118 // the new storage still was not committed so the clone must be empty
119 XStorage xClonedSubStorage
= m_aTestHelper
.cloneSubStorage( m_xStorageFactory
, xTempStorage
, "SubStorage1" );
121 if ( xClonedSubStorage
== null )
123 m_aTestHelper
.Error( "The result of clone is empty!" );
127 XNameAccess xClonedNameAccess
= (XNameAccess
) UnoRuntime
.queryInterface( XNameAccess
.class, xClonedSubStorage
);
128 if ( xClonedNameAccess
== null )
130 m_aTestHelper
.Error( "XNameAccess is not implemented by the clone!" );
134 if ( !m_aTestHelper
.checkStorageProperties( xClonedSubStorage
, "", true, ElementModes
.WRITE
) )
137 if ( xClonedNameAccess
.hasElements() )
139 m_aTestHelper
.Error( "The new substorage still was not committed so it must be empty!" );
143 if ( !m_aTestHelper
.disposeStorage( xClonedSubStorage
) )
146 xClonedSubStorage
= null;
147 xClonedNameAccess
= null;
149 // the new stream was opened, written and closed, that means flashed
150 // so the clone must contain all the information
151 XStream xClonedSubStream
= m_aTestHelper
.cloneEncrSubStream( xTempStorage
, "SubStream1", sPass1
);
152 if ( !m_aTestHelper
.InternalCheckStream( xClonedSubStream
, "SubStream1", "MediaType1", true, pBytes1
, true ) )
155 XStream xClonedBigSubStream
= m_aTestHelper
.cloneEncrSubStream( xTempStorage
, "BigSubStream1", sPass1
);
156 if ( !m_aTestHelper
.InternalCheckStream( xClonedBigSubStream
, "BigSubStream1", "MediaType1", true, pBigBytes
, true ) )
159 if ( !m_aTestHelper
.disposeStream( xClonedSubStream
, "SubStream1" ) )
162 if ( !m_aTestHelper
.disposeStream( xClonedBigSubStream
, "BigSubStream1" ) )
166 // commit substorage and check cloning
169 if ( !m_aTestHelper
.commitStorage( xTempSubStorage
) )
172 xClonedSubStorage
= m_aTestHelper
.cloneSubStorage( m_xStorageFactory
, xTempStorage
, "SubStorage1" );
173 if ( xClonedSubStorage
== null )
175 m_aTestHelper
.Error( "The result of clone is empty!" );
179 if ( !m_aTestHelper
.checkStorageProperties( xClonedSubStorage
, "MediaType4", true, ElementModes
.WRITE
) )
182 if ( !m_aTestHelper
.checkEncrStream( xClonedSubStorage
, "SubStream2", "MediaType2", pBytes2
, sPass2
) )
185 if ( !m_aTestHelper
.checkEncrStream( xClonedSubStorage
, "BigSubStream2", "MediaType2", pBigBytes
, sPass2
) )
189 // commit the root storage and check cloning
192 if ( !m_aTestHelper
.commitStorage( xTempStorage
) )
195 XStorage xCloneOfRoot
= m_aTestHelper
.cloneStorage( m_xStorageFactory
, xTempStorage
);
196 if ( xCloneOfRoot
== null )
198 m_aTestHelper
.Error( "The result of root clone is empty!" );
202 if ( !m_aTestHelper
.checkStorageProperties( xCloneOfRoot
, "MediaType3", true, ElementModes
.WRITE
) )
205 if ( !m_aTestHelper
.checkEncrStream( xCloneOfRoot
, "SubStream1", "MediaType1", pBytes1
, sPass1
) )
208 if ( !m_aTestHelper
.checkEncrStream( xCloneOfRoot
, "BigSubStream1", "MediaType1", pBigBytes
, sPass1
) )
211 XStorage xSubStorageOfClone
= xCloneOfRoot
.openStorageElement( "SubStorage1", ElementModes
.READ
);
212 if ( xSubStorageOfClone
== null )
214 m_aTestHelper
.Error( "The result of root clone is wrong!" );
218 if ( !m_aTestHelper
.checkStorageProperties( xSubStorageOfClone
, "MediaType4", false, ElementModes
.READ
) )
221 if ( !m_aTestHelper
.checkEncrStream( xSubStorageOfClone
, "SubStream2", "MediaType2", pBytes2
, sPass2
) )
224 if ( !m_aTestHelper
.checkEncrStream( xSubStorageOfClone
, "BigSubStream2", "MediaType2", pBigBytes
, sPass2
) )
231 m_aTestHelper
.Error( "Exception: " + e
);