1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package complex.tempfile; 28 29 30 import com.sun.star.lang.XMultiServiceFactory; 31 import com.sun.star.ucb.XSimpleFileAccess; 32 import com.sun.star.io.*; 33 34 import com.sun.star.uno.UnoRuntime; 35 import java.util.Random; 36 37 public class Test02 implements TempFileTest { 38 39 XMultiServiceFactory m_xMSF; 40 XSimpleFileAccess m_xSFA; 41 TestHelper m_aTestHelper; 42 43 public Test02(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) { 44 m_xMSF = xMSF; 45 m_xSFA = xSFA; 46 m_aTestHelper = new TestHelper( "Test02: "); 47 } 48 49 public boolean test() { 50 Object oTempFile = null; 51 XTempFile xTempFile = null; 52 XTruncate xTruncate = null; 53 String sFileURL = null; 54 String sFileName = null; 55 //create a temporary file. 56 try { 57 oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" ); 58 xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile); 59 m_aTestHelper.Message( "Tempfile created." ); 60 xTruncate = UnoRuntime.queryInterface(XTruncate.class, oTempFile); 61 } catch(Exception e) { 62 m_aTestHelper.Error( "Cannot create TempFile. exception: " + e ); 63 return false; 64 } 65 try { 66 //write something. 67 byte pBytesIn[] = new byte[9]; 68 byte pBytesOut[][] = new byte[1][9]; 69 Random oRandom = new Random(); 70 oRandom.nextBytes( pBytesIn ); 71 m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile ); 72 73 //get the URL. 74 sFileURL = m_aTestHelper.GetTempFileURL( xTempFile ); 75 76 //let the service not to remove the URL. 77 m_aTestHelper.SetTempFileRemove( xTempFile, false ); 78 79 //close the tempfile by closing input and output. 80 m_aTestHelper.CloseTempFile( xTempFile ); 81 82 //check that the file is still available. 83 //xTempFile.seek(0); 84 m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut, pBytesIn.length + 1, m_xSFA, sFileURL ); 85 for ( int i = 0; i < pBytesIn.length; i++ ) { 86 if ( pBytesOut[0][i] != pBytesIn[i] ) { 87 m_aTestHelper.Error( "Tempfile contains false data!" ); 88 } 89 } 90 } catch ( Exception e) { 91 m_aTestHelper.Error("Exception: " + e); 92 return false; 93 } 94 return true; 95 } 96 } 97