1*bc3375a3SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*bc3375a3SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*bc3375a3SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*bc3375a3SAndrew Rist * distributed with this work for additional information 6*bc3375a3SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*bc3375a3SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*bc3375a3SAndrew Rist * "License"); you may not use this file except in compliance 9*bc3375a3SAndrew Rist * with the License. You may obtain a copy of the License at 10*bc3375a3SAndrew Rist * 11*bc3375a3SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*bc3375a3SAndrew Rist * 13*bc3375a3SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*bc3375a3SAndrew Rist * software distributed under the License is distributed on an 15*bc3375a3SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*bc3375a3SAndrew Rist * KIND, either express or implied. See the License for the 17*bc3375a3SAndrew Rist * specific language governing permissions and limitations 18*bc3375a3SAndrew Rist * under the License. 19*bc3375a3SAndrew Rist * 20*bc3375a3SAndrew Rist *************************************************************/ 21*bc3375a3SAndrew Rist 22*bc3375a3SAndrew Rist 23cdf0e10cSrcweir package complex.tempfile; 24cdf0e10cSrcweir 25cdf0e10cSrcweir // import complexlib.ComplexTestCase; 26cdf0e10cSrcweir 27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 28cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess; 29cdf0e10cSrcweir import com.sun.star.io.*; 30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 31cdf0e10cSrcweir import java.util.Random; 32cdf0e10cSrcweir 33cdf0e10cSrcweir import share.LogWriter; 34cdf0e10cSrcweir 35cdf0e10cSrcweir public class Test01 implements TempFileTest { 36cdf0e10cSrcweir LogWriter m_aLogWriter; 37cdf0e10cSrcweir XMultiServiceFactory m_xMSF = null; 38cdf0e10cSrcweir XSimpleFileAccess m_xSFA = null; 39cdf0e10cSrcweir TestHelper m_aTestHelper = null; 40cdf0e10cSrcweir Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA)41cdf0e10cSrcweir public Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) { 42cdf0e10cSrcweir m_xMSF = xMSF; 43cdf0e10cSrcweir m_xSFA = xSFA; 44cdf0e10cSrcweir m_aTestHelper = new TestHelper( "Test01: "); 45cdf0e10cSrcweir } 46cdf0e10cSrcweir test()47cdf0e10cSrcweir public boolean test() { 48cdf0e10cSrcweir XTempFile xTempFile = null; 49cdf0e10cSrcweir XTruncate xTruncate = null; 50cdf0e10cSrcweir String sFileURL = null; 51cdf0e10cSrcweir String sFileName = null; 52cdf0e10cSrcweir //create a temporary file. 53cdf0e10cSrcweir try { 54cdf0e10cSrcweir Object oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" ); 55cdf0e10cSrcweir xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile); 56cdf0e10cSrcweir m_aTestHelper.Message( "Tempfile created." ); 57cdf0e10cSrcweir xTruncate = UnoRuntime.queryInterface(XTruncate.class, oTempFile); 58cdf0e10cSrcweir } catch( Exception e ) { 59cdf0e10cSrcweir m_aTestHelper.Error( "Cannot create TempFile. exception: " + e ); 60cdf0e10cSrcweir return false; 61cdf0e10cSrcweir } 62cdf0e10cSrcweir 63cdf0e10cSrcweir //retrieve the tempfile URL 64cdf0e10cSrcweir if ( xTempFile == null ) { 65cdf0e10cSrcweir m_aTestHelper.Error( "Cannot get XTempFile interface." ); 66cdf0e10cSrcweir return false; 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir try { 70cdf0e10cSrcweir //compare the file name with the name in the URL. 71cdf0e10cSrcweir sFileURL = m_aTestHelper.GetTempFileURL( xTempFile ); 72cdf0e10cSrcweir sFileName = m_aTestHelper.GetTempFileName( xTempFile ); 73cdf0e10cSrcweir m_aTestHelper.CompareFileNameAndURL( sFileName, sFileURL ); 74cdf0e10cSrcweir 75cdf0e10cSrcweir //write to the stream using the service. 76cdf0e10cSrcweir byte pBytesIn[] = new byte[9]; 77cdf0e10cSrcweir byte pBytesOut1[][] = new byte [1][9]; 78cdf0e10cSrcweir byte pBytesOut2[][] = new byte [1][9]; 79cdf0e10cSrcweir Random oRandom = new Random(); 80cdf0e10cSrcweir oRandom.nextBytes( pBytesIn ); 81cdf0e10cSrcweir m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile ); 82cdf0e10cSrcweir 83cdf0e10cSrcweir //check the result by reading from the service. 84cdf0e10cSrcweir xTempFile.seek(0); 85cdf0e10cSrcweir m_aTestHelper.ReadBytesWithStream( pBytesOut1, pBytesIn.length + 1, xTempFile ); 86cdf0e10cSrcweir for ( int i = 0; i < pBytesIn.length ; i++ ) { 87cdf0e10cSrcweir if ( pBytesOut1[0][i] != pBytesIn[i] ) { 88cdf0e10cSrcweir m_aTestHelper.Error( "Tempfile outputs false data!" ); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir //check the result by reading from the file directly. 93cdf0e10cSrcweir m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut2, pBytesIn.length + 1, m_xSFA, sFileURL ); 94cdf0e10cSrcweir for ( int i = 0; i < pBytesIn.length; i++ ) { 95cdf0e10cSrcweir if ( pBytesOut2[0][i] != pBytesIn[i] ) { 96cdf0e10cSrcweir m_aTestHelper.Error( "Tempfile contains false data!" ); 97cdf0e10cSrcweir } 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir //close the object(by closing input and output), check that the file was removed. 101cdf0e10cSrcweir xTempFile.setRemoveFile( false ); 102cdf0e10cSrcweir m_aTestHelper.CloseTempFile( xTempFile ); 103cdf0e10cSrcweir if( !m_aTestHelper.IfTempFileExists( m_xSFA, sFileURL ) ) { 104cdf0e10cSrcweir m_aTestHelper.Error( "TempFile mistakenly removed. " ); 105cdf0e10cSrcweir } else { 106cdf0e10cSrcweir m_aTestHelper.KillTempFile( sFileURL, m_xSFA ); 107cdf0e10cSrcweir } 108cdf0e10cSrcweir } catch ( Exception e ) { 109cdf0e10cSrcweir m_aTestHelper.Error( "Exception: " + e ); 110cdf0e10cSrcweir return false; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir return true; 113cdf0e10cSrcweir } 114cdf0e10cSrcweir } 115