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