1a740f2aaSAndrew Rist /************************************************************** 2a740f2aaSAndrew Rist * 3a740f2aaSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4a740f2aaSAndrew Rist * or more contributor license agreements. See the NOTICE file 5a740f2aaSAndrew Rist * distributed with this work for additional information 6a740f2aaSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7a740f2aaSAndrew Rist * to you under the Apache License, Version 2.0 (the 8a740f2aaSAndrew Rist * "License"); you may not use this file except in compliance 9a740f2aaSAndrew Rist * with the License. You may obtain a copy of the License at 10a740f2aaSAndrew Rist * 11a740f2aaSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12a740f2aaSAndrew Rist * 13a740f2aaSAndrew Rist * Unless required by applicable law or agreed to in writing, 14a740f2aaSAndrew Rist * software distributed under the License is distributed on an 15a740f2aaSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16a740f2aaSAndrew Rist * KIND, either express or implied. See the License for the 17a740f2aaSAndrew Rist * specific language governing permissions and limitations 18a740f2aaSAndrew Rist * under the License. 19a740f2aaSAndrew Rist * 20a740f2aaSAndrew Rist *************************************************************/ 21a740f2aaSAndrew Rist 22cdf0e10cSrcweir package complex.ofopxmlstorages; 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.uno.XInterface; 25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 30cdf0e10cSrcweir import com.sun.star.uno.XInterface; 31cdf0e10cSrcweir 32cdf0e10cSrcweir import com.sun.star.embed.*; 33cdf0e10cSrcweir import com.sun.star.beans.StringPair; 34cdf0e10cSrcweir 35cdf0e10cSrcweir import share.LogWriter; 36cdf0e10cSrcweir import complex.ofopxmlstorages.TestHelper; 37cdf0e10cSrcweir import complex.ofopxmlstorages.StorageTest; 38cdf0e10cSrcweir 39cdf0e10cSrcweir public class Test01 implements StorageTest { 40cdf0e10cSrcweir 41cdf0e10cSrcweir XMultiServiceFactory m_xMSF; 42cdf0e10cSrcweir XSingleServiceFactory m_xStorageFactory; 43cdf0e10cSrcweir TestHelper m_aTestHelper; 44cdf0e10cSrcweir Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )45cdf0e10cSrcweir public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 46cdf0e10cSrcweir { 47cdf0e10cSrcweir m_xMSF = xMSF; 48cdf0e10cSrcweir m_xStorageFactory = xStorageFactory; 49cdf0e10cSrcweir m_aTestHelper = new TestHelper( aLogWriter, "Test01: " ); 50cdf0e10cSrcweir } 51cdf0e10cSrcweir test()52cdf0e10cSrcweir public boolean test() 53cdf0e10cSrcweir { 54cdf0e10cSrcweir StringPair[][] aRelations1 = 55cdf0e10cSrcweir { { new StringPair( "Id", "Num1" ) }, 56cdf0e10cSrcweir { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) }, 57cdf0e10cSrcweir { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) }, 58cdf0e10cSrcweir { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 59cdf0e10cSrcweir { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 60cdf0e10cSrcweir { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 61cdf0e10cSrcweir { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) } 62cdf0e10cSrcweir }; 63cdf0e10cSrcweir 64cdf0e10cSrcweir StringPair[][] aRelations2 = 65cdf0e10cSrcweir { { new StringPair( "Id", "Num1" ) }, 66cdf0e10cSrcweir { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) }, 67cdf0e10cSrcweir { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) }, 68cdf0e10cSrcweir { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) }, 69cdf0e10cSrcweir { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) }, 70cdf0e10cSrcweir { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }, 71cdf0e10cSrcweir { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) } 72cdf0e10cSrcweir }; 73cdf0e10cSrcweir 74cdf0e10cSrcweir try 75cdf0e10cSrcweir { 76cdf0e10cSrcweir String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF ); 77cdf0e10cSrcweir if ( sTempFileURL == null || sTempFileURL == "" ) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir m_aTestHelper.Error( "No valid temporary file was created!" ); 80cdf0e10cSrcweir return false; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir 83cdf0e10cSrcweir // create temporary storage based on arbitrary medium 84cdf0e10cSrcweir // after such a storage is closed it is lost 85cdf0e10cSrcweir XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory ); 86cdf0e10cSrcweir if ( xTempStorage == null ) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir m_aTestHelper.Error( "Can't create temporary storage representation!" ); 89cdf0e10cSrcweir return false; 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir // open a new substorage 93cdf0e10cSrcweir XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage, 94cdf0e10cSrcweir "SubStorage1", 95cdf0e10cSrcweir ElementModes.WRITE ); 96cdf0e10cSrcweir if ( xTempSubStorage == null ) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir m_aTestHelper.Error( "Can't create substorage!" ); 99cdf0e10cSrcweir return false; 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir byte pBytes1[] = { 1, 1, 1, 1, 1 }; 103cdf0e10cSrcweir 104cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 105cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, 106cdf0e10cSrcweir "SubStream1", 107cdf0e10cSrcweir "MediaType1", 108cdf0e10cSrcweir true, 109cdf0e10cSrcweir pBytes1, 110cdf0e10cSrcweir aRelations1 ) ) 111cdf0e10cSrcweir return false; 112cdf0e10cSrcweir 113cdf0e10cSrcweir byte pBytes2[] = { 2, 2, 2, 2, 2 }; 114cdf0e10cSrcweir 115cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 116cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, 117cdf0e10cSrcweir "SubStream2", 118cdf0e10cSrcweir "MediaType2", 119cdf0e10cSrcweir false, 120cdf0e10cSrcweir pBytes2, 121cdf0e10cSrcweir aRelations2 ) ) 122cdf0e10cSrcweir return false; 123cdf0e10cSrcweir 124cdf0e10cSrcweir // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly 125cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage, 126cdf0e10cSrcweir true, 127cdf0e10cSrcweir ElementModes.WRITE, 128cdf0e10cSrcweir aRelations1 ) ) 129cdf0e10cSrcweir return false; 130cdf0e10cSrcweir 131cdf0e10cSrcweir // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly 132cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 133cdf0e10cSrcweir false, 134cdf0e10cSrcweir ElementModes.WRITE, 135cdf0e10cSrcweir aRelations1 ) ) 136cdf0e10cSrcweir return false; 137cdf0e10cSrcweir 138cdf0e10cSrcweir // create temporary storage based on a previously created temporary file 139cdf0e10cSrcweir XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory, 140cdf0e10cSrcweir sTempFileURL, 141cdf0e10cSrcweir ElementModes.WRITE ); 142cdf0e10cSrcweir if ( xTempFileStorage == null ) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 145cdf0e10cSrcweir return false; 146cdf0e10cSrcweir } 147cdf0e10cSrcweir 148cdf0e10cSrcweir // copy xTempStorage to xTempFileStorage 149*a893be29SPedro Giffuni // xTempFileStorage will be automatically committed 150cdf0e10cSrcweir if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) ) 151cdf0e10cSrcweir return false; 152cdf0e10cSrcweir 153cdf0e10cSrcweir // dispose used storages to free resources 154cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 155cdf0e10cSrcweir return false; 156cdf0e10cSrcweir 157cdf0e10cSrcweir // ================================================ 158cdf0e10cSrcweir // now check all the written and copied information 159cdf0e10cSrcweir // ================================================ 160cdf0e10cSrcweir 161cdf0e10cSrcweir // the temporary file must not be locked any more after storage disposing 162cdf0e10cSrcweir XStorage xResultStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory, 163cdf0e10cSrcweir sTempFileURL, 164cdf0e10cSrcweir ElementModes.WRITE ); 165cdf0e10cSrcweir if ( xResultStorage == null ) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 168cdf0e10cSrcweir return false; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultStorage, 172cdf0e10cSrcweir true, 173cdf0e10cSrcweir ElementModes.WRITE, 174cdf0e10cSrcweir aRelations1 ) ) 175cdf0e10cSrcweir return false; 176cdf0e10cSrcweir 177cdf0e10cSrcweir // open existing substorage 178cdf0e10cSrcweir XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage, 179cdf0e10cSrcweir "SubStorage1", 180cdf0e10cSrcweir ElementModes.READ ); 181cdf0e10cSrcweir if ( xResultSubStorage == null ) 182cdf0e10cSrcweir { 183cdf0e10cSrcweir m_aTestHelper.Error( "Can't open existing substorage!" ); 184cdf0e10cSrcweir return false; 185cdf0e10cSrcweir } 186cdf0e10cSrcweir 187cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, 188cdf0e10cSrcweir false, 189cdf0e10cSrcweir ElementModes.READ, 190cdf0e10cSrcweir aRelations1 ) ) 191cdf0e10cSrcweir return false; 192cdf0e10cSrcweir 193cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, 194cdf0e10cSrcweir "SubStream1", 195cdf0e10cSrcweir "MediaType1", 196cdf0e10cSrcweir pBytes1, 197cdf0e10cSrcweir aRelations1 ) ) 198cdf0e10cSrcweir return false; 199cdf0e10cSrcweir 200cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, 201cdf0e10cSrcweir "SubStream2", 202cdf0e10cSrcweir "MediaType2", 203cdf0e10cSrcweir pBytes2, 204cdf0e10cSrcweir aRelations2 ) ) 205cdf0e10cSrcweir return false; 206cdf0e10cSrcweir 207cdf0e10cSrcweir // dispose used storages to free resources 208cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xResultStorage ) ) 209cdf0e10cSrcweir return false; 210cdf0e10cSrcweir 211cdf0e10cSrcweir return true; 212cdf0e10cSrcweir } 213cdf0e10cSrcweir catch( Exception e ) 214cdf0e10cSrcweir { 215cdf0e10cSrcweir m_aTestHelper.Error( "Exception: " + e ); 216cdf0e10cSrcweir return false; 217cdf0e10cSrcweir } 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir } 221cdf0e10cSrcweir 222