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