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 10cdf0e10cSrcweir * 11*bc3375a3SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 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. 19cdf0e10cSrcweir * 20*bc3375a3SAndrew Rist *************************************************************/ 21*bc3375a3SAndrew Rist 22*bc3375a3SAndrew Rist 23cdf0e10cSrcweir package complex.tempfile; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 26cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess; 27cdf0e10cSrcweir import com.sun.star.io.*; 28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 29cdf0e10cSrcweir import java.util.Random; 30cdf0e10cSrcweir 31cdf0e10cSrcweir public class Test01 implements TempFileTest { 32cdf0e10cSrcweir XMultiServiceFactory m_xMSF = null; 33cdf0e10cSrcweir XSimpleFileAccess m_xSFA = null; 34cdf0e10cSrcweir TestHelper m_aTestHelper = null; 35cdf0e10cSrcweir Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA)36cdf0e10cSrcweir public Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) { 37cdf0e10cSrcweir m_xMSF = xMSF; 38cdf0e10cSrcweir m_xSFA = xSFA; 39cdf0e10cSrcweir m_aTestHelper = new TestHelper( "Test01: "); 40cdf0e10cSrcweir } 41cdf0e10cSrcweir test()42cdf0e10cSrcweir public boolean test() { 43cdf0e10cSrcweir XTempFile xTempFile = null; 44cdf0e10cSrcweir XTruncate xTruncate = null; 45cdf0e10cSrcweir String sFileURL = null; 46cdf0e10cSrcweir String sFileName = null; 47cdf0e10cSrcweir //create a temporary file. 48cdf0e10cSrcweir try { 49cdf0e10cSrcweir Object oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" ); 50cdf0e10cSrcweir xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile); 51cdf0e10cSrcweir m_aTestHelper.Message( "Tempfile created." ); 52cdf0e10cSrcweir xTruncate = UnoRuntime.queryInterface(XTruncate.class, oTempFile); 53cdf0e10cSrcweir } catch( Exception e ) { 54cdf0e10cSrcweir m_aTestHelper.Error( "Cannot create TempFile. exception: " + e ); 55cdf0e10cSrcweir return false; 56cdf0e10cSrcweir } 57cdf0e10cSrcweir 58cdf0e10cSrcweir //retrieve the tempfile URL 59cdf0e10cSrcweir if ( xTempFile == null ) { 60cdf0e10cSrcweir m_aTestHelper.Error( "Cannot get XTempFile interface." ); 61cdf0e10cSrcweir return false; 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir try { 65cdf0e10cSrcweir //compare the file name with the name in the URL. 66cdf0e10cSrcweir sFileURL = m_aTestHelper.GetTempFileURL( xTempFile ); 67cdf0e10cSrcweir sFileName = m_aTestHelper.GetTempFileName( xTempFile ); 68cdf0e10cSrcweir m_aTestHelper.CompareFileNameAndURL( sFileName, sFileURL ); 69cdf0e10cSrcweir 70cdf0e10cSrcweir //write to the stream using the service. 71cdf0e10cSrcweir byte pBytesIn[] = new byte[9]; 72cdf0e10cSrcweir byte pBytesOut1[][] = new byte [1][9]; 73cdf0e10cSrcweir byte pBytesOut2[][] = new byte [1][9]; 74cdf0e10cSrcweir Random oRandom = new Random(); 75cdf0e10cSrcweir oRandom.nextBytes( pBytesIn ); 76cdf0e10cSrcweir m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile ); 77cdf0e10cSrcweir 78cdf0e10cSrcweir //check the result by reading from the service. 79cdf0e10cSrcweir xTempFile.seek(0); 80cdf0e10cSrcweir m_aTestHelper.ReadBytesWithStream( pBytesOut1, pBytesIn.length + 1, xTempFile ); 81cdf0e10cSrcweir for ( int i = 0; i < pBytesIn.length ; i++ ) { 82cdf0e10cSrcweir if ( pBytesOut1[0][i] != pBytesIn[i] ) { 83cdf0e10cSrcweir m_aTestHelper.Error( "Tempfile outputs false data!" ); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir //check the result by reading from the file directly. 88cdf0e10cSrcweir m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut2, pBytesIn.length + 1, m_xSFA, sFileURL ); 89cdf0e10cSrcweir for ( int i = 0; i < pBytesIn.length; i++ ) { 90cdf0e10cSrcweir if ( pBytesOut2[0][i] != pBytesIn[i] ) { 91cdf0e10cSrcweir m_aTestHelper.Error( "Tempfile contains false data!" ); 92cdf0e10cSrcweir } 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir //close the object(by closing input and output), check that the file was removed. 96cdf0e10cSrcweir xTempFile.setRemoveFile( false ); 97cdf0e10cSrcweir m_aTestHelper.CloseTempFile( xTempFile ); 98cdf0e10cSrcweir if( !m_aTestHelper.IfTempFileExists( m_xSFA, sFileURL ) ) { 99cdf0e10cSrcweir m_aTestHelper.Error( "TempFile mistakenly removed. " ); 100cdf0e10cSrcweir } else { 101cdf0e10cSrcweir m_aTestHelper.KillTempFile( sFileURL, m_xSFA ); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir } catch ( Exception e ) { 104cdf0e10cSrcweir m_aTestHelper.Error( "Exception: " + e ); 105cdf0e10cSrcweir return false; 106cdf0e10cSrcweir } 107cdf0e10cSrcweir return true; 108cdf0e10cSrcweir } 109cdf0e10cSrcweir } 110