1 /* 2 * This file is part of the LibreOffice project. 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 10 * Licensed to the Apache Software Foundation (ASF) under one or more 11 * contributor license agreements. See the NOTICE file distributed 12 * with this work for additional information regarding copyright 13 * ownership. The ASF licenses this file to you under the Apache 14 * License, Version 2.0 (the "License"); you may not use this file 15 * except in compliance with the License. You may obtain a copy of 16 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 17 */ 18 package complex.tempfile; 19 20 21 22 import com.sun.star.io.*; 23 24 import com.sun.star.uno.AnyConverter; 25 import com.sun.star.ucb.XSimpleFileAccess; 26 27 28 public class TestHelper { 29 30 private String m_sTestPrefix; 31 TestHelper( String sTestPrefix )32 public TestHelper( String sTestPrefix ) { 33 34 m_sTestPrefix = sTestPrefix; 35 } SetTempFileRemove( XTempFile xTempFile, boolean b )36 public void SetTempFileRemove( XTempFile xTempFile, boolean b ) { 37 try { 38 xTempFile.setRemoveFile( b ); 39 } catch( Exception e ) { 40 Error( "Cannot set TempFileRemove. exception: " + e ); 41 } 42 } 43 GetTempFileURL( XTempFile xTempFile )44 public String GetTempFileURL ( XTempFile xTempFile ) { 45 String sTempFileURL = null; 46 try { 47 sTempFileURL = AnyConverter.toString( xTempFile.getUri() ); 48 } catch (Exception e) { 49 Error ( "Cannot get TempFileURL. exception: " + e ); 50 } 51 if ( sTempFileURL == null || sTempFileURL.equals("") ) { 52 Error ( "Temporary file not valid." ); 53 } 54 return sTempFileURL; 55 } 56 GetTempFileName( XTempFile xTempFile )57 public String GetTempFileName( XTempFile xTempFile ) { 58 String sTempFileName = null; 59 try { 60 sTempFileName = AnyConverter.toString( xTempFile.getResourceName() ); 61 } catch ( Exception e ) { 62 Error( "Cannot get TempFileName. exception: " + e ); 63 } 64 if ( sTempFileName == null || sTempFileName.equals("") ) { 65 Error( "Temporary file not valid." ); 66 } 67 return sTempFileName; 68 } 69 CompareFileNameAndURL( String sTempFileName, String sTempFileURL )70 public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) { 71 boolean bRet = false; 72 try { 73 bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) ); 74 Message ( "Compare file name and URL: " + 75 ( bRet ? "OK." : "ERROR: FILE NAME AND URL DO NOT MATCH." ) ); 76 } 77 catch ( Exception e ) { 78 Error ( "exception: " + e); 79 } 80 return bRet; 81 } 82 WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile )83 public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) { 84 try { 85 XOutputStream xOutTemp = xTempFile.getOutputStream(); 86 if ( xOutTemp == null ) { 87 Error( "Cannot get output stream." ); 88 } else { 89 xOutTemp.writeBytes( pBytes ); 90 Message ( "Write to tempfile successfully." ); 91 } 92 } catch ( Exception e ) { 93 Error( "Cannot write to stream. exception: " + e ); 94 } 95 } 96 ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile )97 public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) { 98 try { 99 XInputStream xInTemp = xTempFile.getInputStream(); 100 if ( xInTemp == null ) { 101 Error( "Cannot get input stream from tempfile." ); 102 } else { 103 xInTemp.readBytes( pBytes, nBytes ); 104 Message ( "Read from tempfile successfully." ); 105 } 106 } catch ( Exception e ) { 107 Error( "Cannot read from stream. exception: " + e ); 108 } 109 } ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL )110 public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL ) 111 { 112 try 113 { 114 if ( xSFA != null ) { 115 XInputStream xInTemp = xSFA.openFileRead( sTempFileURL ); 116 if ( xInTemp != null ) 117 { 118 xInTemp.readBytes( pBytes, nBytes ); 119 xInTemp.closeInput(); 120 Message ( "Read directly from tempfile successfully." ); 121 } else { 122 Error ( "Cannot create input stream from URL." ); 123 } 124 } 125 } 126 catch ( Exception e) 127 { 128 Error( "Exception caught in TestHelper." + 129 "ReadDirectlyFromTempFile(). exception: " + e ); 130 } 131 } 132 CloseTempFile( XTempFile xTempFile )133 public void CloseTempFile( XTempFile xTempFile ) { 134 XOutputStream xOutTemp = null; 135 XInputStream xInTemp = null; 136 try { 137 xOutTemp = xTempFile.getOutputStream(); 138 if ( xOutTemp == null ) { 139 Error( "Cannot get output stream." ); 140 } 141 } catch ( Exception e ) { 142 Error( "Cannot get output stream. exception:" + e ); 143 } 144 try { 145 xOutTemp.closeOutput(); 146 } catch( Exception e ) { 147 Error( "Cannot close output stream. exception:" + e ); 148 } 149 try { 150 xInTemp = xTempFile.getInputStream(); 151 if ( xInTemp == null ) { 152 Error( "Cannot get input stream." ); 153 } 154 } catch ( Exception e ) { 155 Error( "Cannot get input stream. exception:" + e ); 156 } 157 try { 158 xInTemp.closeInput(); 159 Message ( "Tempfile closed successfully." ); 160 } catch( Exception e ) { 161 Error( "Cannot close input stream. exception:" + e ); 162 } 163 } 164 KillTempFile( String sTempFileURL, XSimpleFileAccess xSFA )165 public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) { 166 try { 167 if ( sTempFileURL != null && xSFA != null ) { 168 xSFA.kill( sTempFileURL ); 169 Message ( "Tempfile killed successfully." ); 170 } 171 } 172 catch ( Exception e ) { 173 Error ( "Exception caught in TestHelper." + 174 "KillTempFile(): " + e); 175 } 176 } 177 IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL )178 public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL ) { 179 boolean bRet = false; 180 try { 181 if ( sTempFileURL != null && xSFA != null ) { 182 bRet = xSFA.exists( sTempFileURL ); 183 Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." ); 184 } 185 } 186 catch( Exception e ) { 187 Error( "Exception caught in TestHelper." + 188 "IfTempFileExists(): " + e ); 189 } 190 return bRet; 191 } 192 Error( String sError )193 public void Error( String sError ) { 194 System.out.println( m_sTestPrefix + "Error: " + sError ); 195 } 196 Message( String sMessage )197 public void Message( String sMessage ) { 198 System.out.println( m_sTestPrefix + sMessage ); 199 } 200 } 201