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 26cdf0e10cSrcweir 27cdf0e10cSrcweir import com.sun.star.io.*; 28cdf0e10cSrcweir 29cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 30cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess; 31cdf0e10cSrcweir 32cdf0e10cSrcweir 33cdf0e10cSrcweir public class TestHelper { 34cdf0e10cSrcweir 35cdf0e10cSrcweir String m_sTestPrefix; 36cdf0e10cSrcweir TestHelper( String sTestPrefix )37cdf0e10cSrcweir public TestHelper( String sTestPrefix ) { 38cdf0e10cSrcweir 39cdf0e10cSrcweir m_sTestPrefix = sTestPrefix; 40cdf0e10cSrcweir } SetTempFileRemove( XTempFile xTempFile, boolean b )41cdf0e10cSrcweir public void SetTempFileRemove( XTempFile xTempFile, boolean b ) { 42cdf0e10cSrcweir try { 43cdf0e10cSrcweir xTempFile.setRemoveFile( b ); 44cdf0e10cSrcweir } catch( Exception e ) { 45cdf0e10cSrcweir Error( "Cannot set TempFileRemove. exception: " + e ); 46cdf0e10cSrcweir } 47cdf0e10cSrcweir } 48cdf0e10cSrcweir GetTempFileRemove( XTempFile xTempFile )49cdf0e10cSrcweir public boolean GetTempFileRemove ( XTempFile xTempFile ) { 50cdf0e10cSrcweir boolean b = false; 51cdf0e10cSrcweir try { 52cdf0e10cSrcweir b = xTempFile.getRemoveFile(); 53cdf0e10cSrcweir } catch( Exception e) { 54cdf0e10cSrcweir Error( "Cannot get TempFileRemove. exception: " + e ); 55cdf0e10cSrcweir } 56cdf0e10cSrcweir return b; 57cdf0e10cSrcweir } 58cdf0e10cSrcweir GetTempFileURL( XTempFile xTempFile )59cdf0e10cSrcweir public String GetTempFileURL ( XTempFile xTempFile ) { 60cdf0e10cSrcweir String sTempFileURL = null; 61cdf0e10cSrcweir try { 62cdf0e10cSrcweir sTempFileURL = AnyConverter.toString( xTempFile.getUri() ); 63cdf0e10cSrcweir } catch (Exception e) { 64cdf0e10cSrcweir Error ( "Cannot get TempFileURL. exception: " + e ); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir if ( sTempFileURL == null || sTempFileURL.equals("") ) { 67cdf0e10cSrcweir Error ( "Temporary file not valid." ); 68cdf0e10cSrcweir } 69cdf0e10cSrcweir return sTempFileURL; 70cdf0e10cSrcweir } 71cdf0e10cSrcweir GetTempFileName( XTempFile xTempFile )72cdf0e10cSrcweir public String GetTempFileName( XTempFile xTempFile ) { 73cdf0e10cSrcweir String sTempFileName = null; 74cdf0e10cSrcweir try { 75cdf0e10cSrcweir sTempFileName = AnyConverter.toString( xTempFile.getResourceName() ); 76cdf0e10cSrcweir } catch ( Exception e ) { 77cdf0e10cSrcweir Error( "Cannot get TempFileName. exception: " + e ); 78cdf0e10cSrcweir } 79cdf0e10cSrcweir if ( sTempFileName == null || sTempFileName.equals("") ) { 80cdf0e10cSrcweir Error( "Temporary file not valid." ); 81cdf0e10cSrcweir } 82cdf0e10cSrcweir return sTempFileName; 83cdf0e10cSrcweir } 84cdf0e10cSrcweir CompareFileNameAndURL( String sTempFileName, String sTempFileURL )85cdf0e10cSrcweir public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) { 86cdf0e10cSrcweir boolean bRet = false; 87cdf0e10cSrcweir try { 88cdf0e10cSrcweir bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) ); 89cdf0e10cSrcweir Message ( "Compare file name and URL: " + 90cdf0e10cSrcweir ( bRet ? "OK." : "ERROR: FILE NAME AND URL DO NOT MATCH." ) ); 91cdf0e10cSrcweir } 92cdf0e10cSrcweir catch ( Exception e ) { 93cdf0e10cSrcweir Error ( "exception: " + e); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir return bRet; 96cdf0e10cSrcweir } 97cdf0e10cSrcweir WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile )98cdf0e10cSrcweir public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) { 99cdf0e10cSrcweir try { 100cdf0e10cSrcweir XOutputStream xOutTemp = xTempFile.getOutputStream(); 101cdf0e10cSrcweir if ( xOutTemp == null ) { 102cdf0e10cSrcweir Error( "Cannot get output stream." ); 103cdf0e10cSrcweir } else { 104cdf0e10cSrcweir xOutTemp.writeBytes( pBytes ); 105cdf0e10cSrcweir Message ( "Write to tempfile successfully." ); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir } catch ( Exception e ) { 108cdf0e10cSrcweir Error( "Cannot write to stream. exception: " + e ); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir } 111cdf0e10cSrcweir ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile )112cdf0e10cSrcweir public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) { 113cdf0e10cSrcweir try { 114cdf0e10cSrcweir XInputStream xInTemp = xTempFile.getInputStream(); 115cdf0e10cSrcweir if ( xInTemp == null ) { 116cdf0e10cSrcweir Error( "Cannot get input stream from tempfile." ); 117cdf0e10cSrcweir } else { 118cdf0e10cSrcweir xInTemp.readBytes( pBytes, nBytes ); 119cdf0e10cSrcweir Message ( "Read from tempfile successfully." ); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir } catch ( Exception e ) { 122cdf0e10cSrcweir Error( "Cannot read from stream. exception: " + e ); 123cdf0e10cSrcweir } 124cdf0e10cSrcweir } ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL )125cdf0e10cSrcweir public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL ) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir try 128cdf0e10cSrcweir { 129cdf0e10cSrcweir if ( xSFA != null ) { 130cdf0e10cSrcweir XInputStream xInTemp = xSFA.openFileRead( sTempFileURL ); 131cdf0e10cSrcweir if ( xInTemp != null ) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir xInTemp.readBytes( pBytes, nBytes ); 134cdf0e10cSrcweir xInTemp.closeInput(); 135cdf0e10cSrcweir Message ( "Read directly from tempfile successfully." ); 136cdf0e10cSrcweir } else { 137cdf0e10cSrcweir Error ( "Cannot creat input stream from URL." ); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir } 140cdf0e10cSrcweir } 141cdf0e10cSrcweir catch ( Exception e) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir Error( "Exception caught in TestHelper." + 144cdf0e10cSrcweir "ReadDirectlyFromTempFile(). exception: " + e ); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir } 147cdf0e10cSrcweir CloseTempFile( XTempFile xTempFile )148cdf0e10cSrcweir public void CloseTempFile( XTempFile xTempFile ) { 149cdf0e10cSrcweir XOutputStream xOutTemp = null; 150cdf0e10cSrcweir XInputStream xInTemp = null; 151cdf0e10cSrcweir try { 152cdf0e10cSrcweir xOutTemp = xTempFile.getOutputStream(); 153cdf0e10cSrcweir if ( xOutTemp == null ) { 154cdf0e10cSrcweir Error( "Cannot get output stream." ); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir } catch ( Exception e ) { 157cdf0e10cSrcweir Error( "Cannot get output stream. exception:" + e ); 158cdf0e10cSrcweir } 159cdf0e10cSrcweir try { 160cdf0e10cSrcweir xOutTemp.closeOutput(); 161cdf0e10cSrcweir } catch( Exception e ) { 162cdf0e10cSrcweir Error( "Cannot close output stream. exception:" + e ); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir try { 165cdf0e10cSrcweir xInTemp = xTempFile.getInputStream(); 166cdf0e10cSrcweir if ( xInTemp == null ) { 167cdf0e10cSrcweir Error( "Cannot get input stream." ); 168cdf0e10cSrcweir } 169cdf0e10cSrcweir } catch ( Exception e ) { 170cdf0e10cSrcweir Error( "Cannot get input stream. exception:" + e ); 171cdf0e10cSrcweir } 172cdf0e10cSrcweir try { 173cdf0e10cSrcweir xInTemp.closeInput(); 174cdf0e10cSrcweir Message ( "Tempfile closed successfully." ); 175cdf0e10cSrcweir } catch( Exception e ) { 176cdf0e10cSrcweir Error( "Cannot close input stream. exception:" + e ); 177cdf0e10cSrcweir } 178cdf0e10cSrcweir } 179cdf0e10cSrcweir KillTempFile( String sTempFileURL, XSimpleFileAccess xSFA )180cdf0e10cSrcweir public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) { 181cdf0e10cSrcweir try { 182cdf0e10cSrcweir if ( sTempFileURL != null ) { 183cdf0e10cSrcweir if ( xSFA != null ) { 184cdf0e10cSrcweir xSFA.kill( sTempFileURL ); 185cdf0e10cSrcweir Message ( "Tempfile killed successfully." ); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir } 188cdf0e10cSrcweir } 189cdf0e10cSrcweir catch ( Exception e ) { 190cdf0e10cSrcweir Error ( "Exception caught in TestHelper." + 191cdf0e10cSrcweir "KillTempFile(): " + e); 192cdf0e10cSrcweir } 193cdf0e10cSrcweir } 194cdf0e10cSrcweir IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL )195cdf0e10cSrcweir public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL ) { 196cdf0e10cSrcweir boolean bRet = false; 197cdf0e10cSrcweir try { 198cdf0e10cSrcweir if ( sTempFileURL != null ) { 199cdf0e10cSrcweir if ( xSFA != null ) { 200cdf0e10cSrcweir bRet = xSFA.exists( sTempFileURL ); 201cdf0e10cSrcweir Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." ); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir } 204cdf0e10cSrcweir } 205cdf0e10cSrcweir catch( Exception e ) { 206cdf0e10cSrcweir Error( "Exception caught in TestHelper." + 207cdf0e10cSrcweir "IfTempFileExists(): " + e ); 208cdf0e10cSrcweir } 209cdf0e10cSrcweir return bRet; 210cdf0e10cSrcweir } 211cdf0e10cSrcweir Error( String sError )212cdf0e10cSrcweir public void Error( String sError ) { 213cdf0e10cSrcweir System.out.println( m_sTestPrefix + "Error: " + sError ); 214cdf0e10cSrcweir } 215cdf0e10cSrcweir Message( String sMessage )216cdf0e10cSrcweir public void Message( String sMessage ) { 217cdf0e10cSrcweir System.out.println( m_sTestPrefix + sMessage ); 218cdf0e10cSrcweir } 219cdf0e10cSrcweir } 220