Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / qa / complex / tempfile / TestHelper.java
blob237c95b9ef11da0f6942810b465a9942c0568829
1 /*
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 ) {
37 try {
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;
46 try {
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." );
54 return sTempFileURL;
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 );
64 if ( sTempFileName == null || sTempFileName.equals("") ) {
65 Error( "Temporary file not valid." );
67 return sTempFileName;
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." ) );
77 catch ( Exception e ) {
78 Error ( "exception: " + e);
80 return bRet;
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." );
92 } catch ( Exception e ) {
93 Error( "Cannot write to stream. exception: " + e );
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." );
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." );
121 } else {
122 Error ( "Cannot create input stream from URL." );
126 catch ( Exception e)
128 Error( "Exception caught in TestHelper." +
129 "ReadDirectlyFromTempFile(). exception: " + e );
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." );
141 } catch ( Exception e ) {
142 Error( "Cannot get output stream. exception:" + e );
144 try {
145 xOutTemp.closeOutput();
146 } catch( Exception e ) {
147 Error( "Cannot close output stream. exception:" + e );
149 try {
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 );
157 try {
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 ) {
166 try {
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;
180 try {
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 );
190 return bRet;
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 );