2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
18 package complex
.tempfile
;
22 import com
.sun
.star
.io
.*;
24 import com
.sun
.star
.uno
.AnyConverter
;
25 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
28 public class TestHelper
{
30 private String m_sTestPrefix
;
32 public TestHelper( String sTestPrefix
) {
34 m_sTestPrefix
= sTestPrefix
;
36 public void SetTempFileRemove( XTempFile xTempFile
, boolean b
) {
38 xTempFile
.setRemoveFile( b
);
39 } catch( Exception e
) {
40 Error( "Cannot set TempFileRemove. exception: " + e
);
44 public String
GetTempFileURL ( XTempFile xTempFile
) {
45 String sTempFileURL
= null;
47 sTempFileURL
= AnyConverter
.toString( xTempFile
.getUri() );
48 } catch (Exception e
) {
49 Error ( "Cannot get TempFileURL. exception: " + e
);
51 if ( sTempFileURL
== null || sTempFileURL
.equals("") ) {
52 Error ( "Temporary file not valid." );
57 public String
GetTempFileName( XTempFile xTempFile
) {
58 String sTempFileName
= null;
60 sTempFileName
= AnyConverter
.toString( xTempFile
.getResourceName() );
61 } catch ( Exception e
) {
62 Error( "Cannot get TempFileName. exception: " + e
);
64 if ( sTempFileName
== null || sTempFileName
.equals("") ) {
65 Error( "Temporary file not valid." );
70 public boolean CompareFileNameAndURL ( String sTempFileName
, String sTempFileURL
) {
73 bRet
= sTempFileURL
.endsWith( sTempFileName
.replaceAll( "\\\\" , "/" ) );
74 Message ( "Compare file name and URL: " +
75 ( bRet ?
"OK." : "ERROR: FILE NAME AND URL DO NOT MATCH." ) );
77 catch ( Exception e
) {
78 Error ( "exception: " + e
);
83 public void WriteBytesWithStream( byte [] pBytes
, XTempFile xTempFile
) {
85 XOutputStream xOutTemp
= xTempFile
.getOutputStream();
86 if ( xOutTemp
== null ) {
87 Error( "Cannot get output stream." );
89 xOutTemp
.writeBytes( pBytes
);
90 Message ( "Write to tempfile successfully." );
92 } catch ( Exception e
) {
93 Error( "Cannot write to stream. exception: " + e
);
97 public void ReadBytesWithStream( byte [][] pBytes
, int nBytes
, XTempFile xTempFile
) {
99 XInputStream xInTemp
= xTempFile
.getInputStream();
100 if ( xInTemp
== null ) {
101 Error( "Cannot get input stream from tempfile." );
103 xInTemp
.readBytes( pBytes
, nBytes
);
104 Message ( "Read from tempfile successfully." );
106 } catch ( Exception e
) {
107 Error( "Cannot read from stream. exception: " + e
);
110 public void ReadDirectlyFromTempFile( byte [][] pBytes
, int nBytes
, XSimpleFileAccess xSFA
, String sTempFileURL
)
114 if ( xSFA
!= null ) {
115 XInputStream xInTemp
= xSFA
.openFileRead( sTempFileURL
);
116 if ( xInTemp
!= null )
118 xInTemp
.readBytes( pBytes
, nBytes
);
119 xInTemp
.closeInput();
120 Message ( "Read directly from tempfile successfully." );
122 Error ( "Cannot create input stream from URL." );
128 Error( "Exception caught in TestHelper." +
129 "ReadDirectlyFromTempFile(). exception: " + e
);
133 public void CloseTempFile( XTempFile xTempFile
) {
134 XOutputStream xOutTemp
= null;
135 XInputStream xInTemp
= null;
137 xOutTemp
= xTempFile
.getOutputStream();
138 if ( xOutTemp
== null ) {
139 Error( "Cannot get output stream." );
141 } catch ( Exception e
) {
142 Error( "Cannot get output stream. exception:" + e
);
145 xOutTemp
.closeOutput();
146 } catch( Exception e
) {
147 Error( "Cannot close output stream. exception:" + e
);
150 xInTemp
= xTempFile
.getInputStream();
151 if ( xInTemp
== null ) {
152 Error( "Cannot get input stream." );
154 } catch ( Exception e
) {
155 Error( "Cannot get input stream. exception:" + e
);
158 xInTemp
.closeInput();
159 Message ( "Tempfile closed successfully." );
160 } catch( Exception e
) {
161 Error( "Cannot close input stream. exception:" + e
);
165 public void KillTempFile ( String sTempFileURL
, XSimpleFileAccess xSFA
) {
167 if ( sTempFileURL
!= null && xSFA
!= null ) {
168 xSFA
.kill( sTempFileURL
);
169 Message ( "Tempfile killed successfully." );
172 catch ( Exception e
) {
173 Error ( "Exception caught in TestHelper." +
174 "KillTempFile(): " + e
);
178 public boolean IfTempFileExists( XSimpleFileAccess xSFA
, String sTempFileURL
) {
179 boolean bRet
= false;
181 if ( sTempFileURL
!= null && xSFA
!= null ) {
182 bRet
= xSFA
.exists( sTempFileURL
);
183 Message ( "Tempfile " + ( bRet ?
"still " : "no longer " ) + "exists." );
186 catch( Exception e
) {
187 Error( "Exception caught in TestHelper." +
188 "IfTempFileExists(): " + e
);
193 public void Error( String sError
) {
194 System
.out
.println( m_sTestPrefix
+ "Error: " + sError
);
197 public void Message( String sMessage
) {
198 System
.out
.println( m_sTestPrefix
+ sMessage
);