1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.tempfile;
28 
29 
30 
31 import com.sun.star.io.*;
32 
33 import com.sun.star.uno.AnyConverter;
34 import com.sun.star.ucb.XSimpleFileAccess;
35 
36 
37 public class TestHelper {
38 
39     String m_sTestPrefix;
40 
41     public TestHelper( String sTestPrefix ) {
42 
43         m_sTestPrefix = sTestPrefix;
44     }
45     public void SetTempFileRemove( XTempFile xTempFile, boolean b ) {
46         try {
47             xTempFile.setRemoveFile( b );
48         } catch( Exception e ) {
49             Error( "Cannot set TempFileRemove. exception: " + e );
50         }
51     }
52 
53     public boolean GetTempFileRemove ( XTempFile xTempFile ) {
54         boolean b = false;
55         try {
56             b = xTempFile.getRemoveFile();
57         } catch( Exception e) {
58             Error( "Cannot get TempFileRemove. exception: " + e );
59         }
60         return b;
61     }
62 
63     public String GetTempFileURL ( XTempFile xTempFile ) {
64         String sTempFileURL = null;
65         try {
66             sTempFileURL = AnyConverter.toString( xTempFile.getUri() );
67         } catch (Exception e) {
68             Error ( "Cannot get TempFileURL. exception: " + e );
69         }
70         if ( sTempFileURL == null || sTempFileURL.equals("") ) {
71             Error ( "Temporary file not valid." );
72         }
73         return sTempFileURL;
74     }
75 
76     public String GetTempFileName( XTempFile xTempFile ) {
77         String sTempFileName = null;
78         try {
79             sTempFileName = AnyConverter.toString( xTempFile.getResourceName() );
80         } catch ( Exception e ) {
81             Error( "Cannot get TempFileName. exception: " + e );
82         }
83         if ( sTempFileName == null || sTempFileName.equals("") ) {
84             Error( "Temporary file not valid." );
85         }
86         return sTempFileName;
87     }
88 
89     public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) {
90         boolean bRet = false;
91         try {
92             bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) );
93             Message ( "Compare file name and URL: " +
94                     ( bRet ? "OK." : "ERROR: FILE NAME AND URL DO NOT MATCH." ) );
95         }
96         catch ( Exception e ) {
97             Error ( "exception: " + e);
98         }
99         return bRet;
100     }
101 
102     public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) {
103         try {
104             XOutputStream xOutTemp = xTempFile.getOutputStream();
105             if ( xOutTemp == null ) {
106                 Error( "Cannot get output stream." );
107             } else {
108                 xOutTemp.writeBytes( pBytes );
109                 Message ( "Write to tempfile successfully." );
110             }
111         } catch ( Exception e ) {
112             Error( "Cannot write to stream. exception: " + e );
113         }
114     }
115 
116     public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) {
117         try {
118             XInputStream xInTemp = xTempFile.getInputStream();
119             if ( xInTemp == null ) {
120                 Error( "Cannot get input stream from tempfile." );
121             } else {
122                 xInTemp.readBytes( pBytes, nBytes );
123                 Message ( "Read from tempfile successfully." );
124             }
125         } catch ( Exception e ) {
126             Error( "Cannot read from stream. exception: " + e );
127         }
128     }
129     public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes,  XSimpleFileAccess xSFA, String sTempFileURL )
130     {
131         try
132         {
133             if ( xSFA != null ) {
134                 XInputStream xInTemp = xSFA.openFileRead( sTempFileURL );
135                 if ( xInTemp != null )
136                 {
137                     xInTemp.readBytes( pBytes, nBytes );
138                     xInTemp.closeInput();
139                     Message ( "Read directly from tempfile successfully." );
140                 } else {
141                     Error ( "Cannot creat input stream from URL." );
142                 }
143             }
144         }
145         catch ( Exception e)
146         {
147             Error( "Exception caught in TestHelper." +
148                     "ReadDirectlyFromTempFile(). exception: " + e );
149         }
150     }
151 
152     public void CloseTempFile( XTempFile xTempFile ) {
153         XOutputStream xOutTemp = null;
154         XInputStream xInTemp = null;
155         try {
156             xOutTemp = xTempFile.getOutputStream();
157             if ( xOutTemp == null ) {
158                 Error( "Cannot get output stream." );
159             }
160         } catch ( Exception e ) {
161             Error( "Cannot get output stream. exception:" + e );
162         }
163         try {
164             xOutTemp.closeOutput();
165         } catch( Exception e ) {
166             Error( "Cannot close output stream. exception:" + e );
167         }
168         try {
169             xInTemp = xTempFile.getInputStream();
170             if ( xInTemp == null ) {
171                 Error( "Cannot get input stream." );
172             }
173         } catch ( Exception e ) {
174             Error( "Cannot get input stream. exception:" + e );
175         }
176         try {
177             xInTemp.closeInput();
178             Message ( "Tempfile closed successfully." );
179         } catch( Exception e ) {
180             Error( "Cannot close input stream. exception:" + e );
181         }
182     }
183 
184     public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) {
185         try {
186             if ( sTempFileURL != null ) {
187                 if ( xSFA != null ) {
188                     xSFA.kill( sTempFileURL );
189                     Message ( "Tempfile killed successfully." );
190                 }
191             }
192         }
193         catch ( Exception e ) {
194             Error ( "Exception caught in TestHelper." +
195                     "KillTempFile(): " + e);
196         }
197     }
198 
199     public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL ) {
200         boolean bRet = false;
201         try {
202             if ( sTempFileURL != null ) {
203                 if ( xSFA != null ) {
204                     bRet = xSFA.exists( sTempFileURL );
205                     Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." );
206                 }
207             }
208         }
209         catch( Exception e ) {
210             Error( "Exception caught in TestHelper." +
211                     "IfTempFileExists(): " + e );
212         }
213         return bRet;
214     }
215 
216     public void Error( String sError ) {
217         System.out.println( m_sTestPrefix + "Error: " + sError );
218     }
219 
220     public void Message( String sMessage ) {
221         System.out.println( m_sTestPrefix + sMessage );
222     }
223 }
224