summaryrefslogtreecommitdiff
path: root/unotools/qa/complex/tempfile/TestHelper.java
blob: e0eaad111589d2e0423894e436c562e68727d8e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: TestHelper.java,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: rt $ $Date: 2008-03-20 15:27:27 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
package complex.tempfile;

import complexlib.ComplexTestCase;
import com.sun.star.lang.XMultiServiceFactory;

import com.sun.star.io.*;

import com.sun.star.uno.AnyConverter;
import com.sun.star.ucb.XSimpleFileAccess;
import com.sun.star.uno.UnoRuntime;
import share.LogWriter;

public class TestHelper {
    LogWriter m_aLogWriter;
    String m_sTestPrefix;

    public TestHelper( LogWriter aLogWriter, String sTestPrefix ) {
        m_aLogWriter = aLogWriter;
        m_sTestPrefix = sTestPrefix;
    }
    public void SetTempFileRemove( XTempFile xTempFile, boolean b ) {
        try {
            xTempFile.setRemoveFile( b );
        } catch( Exception e ) {
            Error( "Cannot set TempFileRemove. exception: " + e );
        }
    }

    public boolean GetTempFileRemove ( XTempFile xTempFile ) {
        boolean b = false;
        try {
            b = xTempFile.getRemoveFile();
        } catch( Exception e) {
            Error( "Cannot get TempFileRemove. exception: " + e );
        }
        return b;
    }

    public String GetTempFileURL ( XTempFile xTempFile ) {
        String sTempFileURL = null;
        try {
            sTempFileURL = AnyConverter.toString( xTempFile.getUri() );
        } catch (Exception e) {
            Error ( "Cannot get TempFileURL. exception: " + e );
        }
        if ( sTempFileURL == null || sTempFileURL == "" ) {
            Error ( "Temporary file not valid." );
        }
        return sTempFileURL;
    }

    public String GetTempFileName( XTempFile xTempFile ) {
        String sTempFileName = null;
        try {
            sTempFileName = AnyConverter.toString( xTempFile.getResourceName() );
        } catch ( Exception e ) {
            Error( "Cannot get TempFileName. exception: " + e );
        }
        if ( sTempFileName == null || sTempFileName == "") {
            Error( "Temporary file not valid." );
        }
        return sTempFileName;
    }

    public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) {
        boolean bRet = false;
        try {
            bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) );
            Message ( "Compare file name and URL: " +
                    ( bRet ? "OK." : "ERROR: FILE NAME AND URL DO NOT MATCH." ) );
        }
        catch ( Exception e ) {
            Error ( "exception: " + e);
        }
        return bRet;
    }

    public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) {
        try {
            XOutputStream xOutTemp = xTempFile.getOutputStream();
            if ( xOutTemp == null ) {
                Error( "Cannot get output stream." );
            } else {
                xOutTemp.writeBytes( pBytes );
                Message ( "Write to tempfile successfully." );
            }
        } catch ( Exception e ) {
            Error( "Cannot write to stream. exception: " + e );
        }
    }

    public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) {
        try {
            XInputStream xInTemp = xTempFile.getInputStream();
            if ( xInTemp == null ) {
                Error( "Cannot get input stream from tempfile." );
            } else {
                xInTemp.readBytes( pBytes, nBytes );
                Message ( "Read from tempfile successfully." );
            }
        } catch ( Exception e ) {
            Error( "Cannot read from stream. exception: " + e );
        }
    }
    public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes,  XSimpleFileAccess xSFA, String sTempFileURL )
    {
        try
        {
            if ( xSFA != null ) {
                XInputStream xInTemp = xSFA.openFileRead( sTempFileURL );
                if ( xInTemp != null )
                {
                    xInTemp.readBytes( pBytes, nBytes );
                    xInTemp.closeInput();
                    Message ( "Read directly from tempfile successfully." );
                } else {
                    Error ( "Cannot creat input stream from URL." );
                }
            }
        }
        catch ( Exception e)
        {
            Error( "Exception caught in TestHelper." +
                    "ReadDirectlyFromTempFile(). exception: " + e );
        }
    }

    public void CloseTempFile( XTempFile xTempFile ) {
        XOutputStream xOutTemp = null;
        XInputStream xInTemp = null;
        try {
            xOutTemp = xTempFile.getOutputStream();
            if ( xOutTemp == null ) {
                Error( "Cannot get output stream." );
            }
        } catch ( Exception e ) {
            Error( "Cannot get output stream. exception:" + e );
        }
        try {
            xOutTemp.closeOutput();
        } catch( Exception e ) {
            Error( "Cannot close output stream. exception:" + e );
        }
        try {
            xInTemp = xTempFile.getInputStream();
            if ( xInTemp == null ) {
                Error( "Cannot get input stream." );
            }
        } catch ( Exception e ) {
            Error( "Cannot get input stream. exception:" + e );
        }
        try {
            xInTemp.closeInput();
            Message ( "Tempfile closed successfully." );
        } catch( Exception e ) {
            Error( "Cannot close input stream. exception:" + e );
        }
    }

    public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) {
        try {
            if ( sTempFileURL != null ) {
                if ( xSFA != null ) {
                    xSFA.kill( sTempFileURL );
                    Message ( "Tempfile killed successfully." );
                }
            }
        }
        catch ( Exception e ) {
            Error ( "Exception caught in TestHelper." +
                    "KillTempFile(): " + e);
        }
    }

    public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL ) {
        boolean bRet = false;
        try {
            if ( sTempFileURL != null ) {
                if ( xSFA != null ) {
                    bRet = xSFA.exists( sTempFileURL );
                    Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." );
                }
            }
        }
        catch( Exception e ) {
            Error( "Exception caught in TestHelper." +
                    "IfTempFileExists(): " + e );
        }
        return bRet;
    }

    public void Error( String sError ) {
        m_aLogWriter.println( m_sTestPrefix + "Error: " + sError );
    }

    public void Message( String sMessage ) {
        m_aLogWriter.println( m_sTestPrefix + sMessage );
    }
}