1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir package complex.tempfile; 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir import com.sun.star.io.*; 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 34*cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess; 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir public class TestHelper { 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir String m_sTestPrefix; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir public TestHelper( String sTestPrefix ) { 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir m_sTestPrefix = sTestPrefix; 44*cdf0e10cSrcweir } 45*cdf0e10cSrcweir public void SetTempFileRemove( XTempFile xTempFile, boolean b ) { 46*cdf0e10cSrcweir try { 47*cdf0e10cSrcweir xTempFile.setRemoveFile( b ); 48*cdf0e10cSrcweir } catch( Exception e ) { 49*cdf0e10cSrcweir Error( "Cannot set TempFileRemove. exception: " + e ); 50*cdf0e10cSrcweir } 51*cdf0e10cSrcweir } 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir public boolean GetTempFileRemove ( XTempFile xTempFile ) { 54*cdf0e10cSrcweir boolean b = false; 55*cdf0e10cSrcweir try { 56*cdf0e10cSrcweir b = xTempFile.getRemoveFile(); 57*cdf0e10cSrcweir } catch( Exception e) { 58*cdf0e10cSrcweir Error( "Cannot get TempFileRemove. exception: " + e ); 59*cdf0e10cSrcweir } 60*cdf0e10cSrcweir return b; 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir public String GetTempFileURL ( XTempFile xTempFile ) { 64*cdf0e10cSrcweir String sTempFileURL = null; 65*cdf0e10cSrcweir try { 66*cdf0e10cSrcweir sTempFileURL = AnyConverter.toString( xTempFile.getUri() ); 67*cdf0e10cSrcweir } catch (Exception e) { 68*cdf0e10cSrcweir Error ( "Cannot get TempFileURL. exception: " + e ); 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir if ( sTempFileURL == null || sTempFileURL.equals("") ) { 71*cdf0e10cSrcweir Error ( "Temporary file not valid." ); 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir return sTempFileURL; 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir public String GetTempFileName( XTempFile xTempFile ) { 77*cdf0e10cSrcweir String sTempFileName = null; 78*cdf0e10cSrcweir try { 79*cdf0e10cSrcweir sTempFileName = AnyConverter.toString( xTempFile.getResourceName() ); 80*cdf0e10cSrcweir } catch ( Exception e ) { 81*cdf0e10cSrcweir Error( "Cannot get TempFileName. exception: " + e ); 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir if ( sTempFileName == null || sTempFileName.equals("") ) { 84*cdf0e10cSrcweir Error( "Temporary file not valid." ); 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir return sTempFileName; 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) { 90*cdf0e10cSrcweir boolean bRet = false; 91*cdf0e10cSrcweir try { 92*cdf0e10cSrcweir bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) ); 93*cdf0e10cSrcweir Message ( "Compare file name and URL: " + 94*cdf0e10cSrcweir ( bRet ? "OK." : "ERROR: FILE NAME AND URL DO NOT MATCH." ) ); 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir catch ( Exception e ) { 97*cdf0e10cSrcweir Error ( "exception: " + e); 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir return bRet; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) { 103*cdf0e10cSrcweir try { 104*cdf0e10cSrcweir XOutputStream xOutTemp = xTempFile.getOutputStream(); 105*cdf0e10cSrcweir if ( xOutTemp == null ) { 106*cdf0e10cSrcweir Error( "Cannot get output stream." ); 107*cdf0e10cSrcweir } else { 108*cdf0e10cSrcweir xOutTemp.writeBytes( pBytes ); 109*cdf0e10cSrcweir Message ( "Write to tempfile successfully." ); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir } catch ( Exception e ) { 112*cdf0e10cSrcweir Error( "Cannot write to stream. exception: " + e ); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) { 117*cdf0e10cSrcweir try { 118*cdf0e10cSrcweir XInputStream xInTemp = xTempFile.getInputStream(); 119*cdf0e10cSrcweir if ( xInTemp == null ) { 120*cdf0e10cSrcweir Error( "Cannot get input stream from tempfile." ); 121*cdf0e10cSrcweir } else { 122*cdf0e10cSrcweir xInTemp.readBytes( pBytes, nBytes ); 123*cdf0e10cSrcweir Message ( "Read from tempfile successfully." ); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir } catch ( Exception e ) { 126*cdf0e10cSrcweir Error( "Cannot read from stream. exception: " + e ); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir try 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir if ( xSFA != null ) { 134*cdf0e10cSrcweir XInputStream xInTemp = xSFA.openFileRead( sTempFileURL ); 135*cdf0e10cSrcweir if ( xInTemp != null ) 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir xInTemp.readBytes( pBytes, nBytes ); 138*cdf0e10cSrcweir xInTemp.closeInput(); 139*cdf0e10cSrcweir Message ( "Read directly from tempfile successfully." ); 140*cdf0e10cSrcweir } else { 141*cdf0e10cSrcweir Error ( "Cannot creat input stream from URL." ); 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir catch ( Exception e) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir Error( "Exception caught in TestHelper." + 148*cdf0e10cSrcweir "ReadDirectlyFromTempFile(). exception: " + e ); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir public void CloseTempFile( XTempFile xTempFile ) { 153*cdf0e10cSrcweir XOutputStream xOutTemp = null; 154*cdf0e10cSrcweir XInputStream xInTemp = null; 155*cdf0e10cSrcweir try { 156*cdf0e10cSrcweir xOutTemp = xTempFile.getOutputStream(); 157*cdf0e10cSrcweir if ( xOutTemp == null ) { 158*cdf0e10cSrcweir Error( "Cannot get output stream." ); 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir } catch ( Exception e ) { 161*cdf0e10cSrcweir Error( "Cannot get output stream. exception:" + e ); 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir try { 164*cdf0e10cSrcweir xOutTemp.closeOutput(); 165*cdf0e10cSrcweir } catch( Exception e ) { 166*cdf0e10cSrcweir Error( "Cannot close output stream. exception:" + e ); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir try { 169*cdf0e10cSrcweir xInTemp = xTempFile.getInputStream(); 170*cdf0e10cSrcweir if ( xInTemp == null ) { 171*cdf0e10cSrcweir Error( "Cannot get input stream." ); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir } catch ( Exception e ) { 174*cdf0e10cSrcweir Error( "Cannot get input stream. exception:" + e ); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir try { 177*cdf0e10cSrcweir xInTemp.closeInput(); 178*cdf0e10cSrcweir Message ( "Tempfile closed successfully." ); 179*cdf0e10cSrcweir } catch( Exception e ) { 180*cdf0e10cSrcweir Error( "Cannot close input stream. exception:" + e ); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) { 185*cdf0e10cSrcweir try { 186*cdf0e10cSrcweir if ( sTempFileURL != null ) { 187*cdf0e10cSrcweir if ( xSFA != null ) { 188*cdf0e10cSrcweir xSFA.kill( sTempFileURL ); 189*cdf0e10cSrcweir Message ( "Tempfile killed successfully." ); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir catch ( Exception e ) { 194*cdf0e10cSrcweir Error ( "Exception caught in TestHelper." + 195*cdf0e10cSrcweir "KillTempFile(): " + e); 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL ) { 200*cdf0e10cSrcweir boolean bRet = false; 201*cdf0e10cSrcweir try { 202*cdf0e10cSrcweir if ( sTempFileURL != null ) { 203*cdf0e10cSrcweir if ( xSFA != null ) { 204*cdf0e10cSrcweir bRet = xSFA.exists( sTempFileURL ); 205*cdf0e10cSrcweir Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." ); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir catch( Exception e ) { 210*cdf0e10cSrcweir Error( "Exception caught in TestHelper." + 211*cdf0e10cSrcweir "IfTempFileExists(): " + e ); 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir return bRet; 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir public void Error( String sError ) { 217*cdf0e10cSrcweir System.out.println( m_sTestPrefix + "Error: " + sError ); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir public void Message( String sMessage ) { 221*cdf0e10cSrcweir System.out.println( m_sTestPrefix + sMessage ); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir } 224