1 /*
2  * This file is part of the LibreOffice project.
3  *
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/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
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 .
17  */
18 package complex.tempfile;
19 
20 import com.sun.star.io.*;
21 import com.sun.star.uno.*;
22 import com.sun.star.uno.AnyConverter;
23 import com.sun.star.ucb.XSimpleFileAccess;
24 import java.io.*;
25 
26 public class TestHelper {
27 
28     private String m_sTestPrefix;
29 
TestHelper( String sTestPrefix )30     public TestHelper( String sTestPrefix ) {
31         m_sTestPrefix = sTestPrefix;
32     }
33 
SetTempFileRemove( XTempFile xTempFile, boolean b )34     public void SetTempFileRemove( XTempFile xTempFile, boolean b ) {
35         xTempFile.setRemoveFile( b );
36     }
37 
GetTempFileURL( XTempFile xTempFile )38     public String GetTempFileURL ( XTempFile xTempFile ) throws java.lang.Exception {
39         String sTempFileURL = AnyConverter.toString( xTempFile.getUri() );
40         if ( sTempFileURL == null || sTempFileURL.equals("") ) {
41             throw new java.lang.Exception( "Temporary file not valid." );
42         }
43         return sTempFileURL;
44     }
45 
GetTempFileName( XTempFile xTempFile )46     public String GetTempFileName( XTempFile xTempFile ) throws java.lang.Exception {
47         String sTempFileName = AnyConverter.toString( xTempFile.getResourceName() );
48         if ( sTempFileName == null || sTempFileName.equals("") ) {
49             throw new java.lang.Exception( "Temporary file not valid." );
50         }
51         return sTempFileName;
52     }
53 
CompareFileNameAndURL( String sTempFileName, String sTempFileURL )54     public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) throws java.lang.Exception {
55         boolean bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) );
56         if (!bRet)
57             throw new java.lang.Exception("FILE NAME AND URL DO NOT MATCH." );
58         return bRet;
59     }
60 
WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile )61     public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) throws java.lang.Exception {
62         XOutputStream xOutTemp = xTempFile.getOutputStream();
63         if ( xOutTemp == null )
64             throw new java.lang.Exception( "Cannot get output stream." );
65         xOutTemp.writeBytes( pBytes );
66         xOutTemp.flush();
67         Message ( "Write " + pBytes.length + " bytes to tempfile successfully." );
68     }
69 
ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile )70     public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) throws java.lang.Exception {
71         XInputStream xInTemp = xTempFile.getInputStream();
72         if ( xInTemp == null )
73             throw new java.lang.Exception( "Cannot get input stream from tempfile." );
74         int n = xInTemp.readBytes( pBytes, nBytes );
75         Message ( "Read " + n + " bytes from tempfile successfully." );
76     }
77 
ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL )78     public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes,  XSimpleFileAccess xSFA, String sTempFileURL )
79          throws java.lang.Exception
80     {
81         Message ( "Attempting to read directly from " + sTempFileURL );
82         XInputStream xInTemp = xSFA.openFileRead( sTempFileURL );
83         if ( xInTemp == null )
84             throw new java.lang.Exception("Cannot create input stream from URL.");
85         int n = xInTemp.readBytes( pBytes, nBytes );
86         xInTemp.closeInput();
87         Message ( "Read " + n + " bytes directly from tempfile successfully. " + sTempFileURL );
88     }
89 
CloseTempFile( XTempFile xTempFile )90     public void CloseTempFile( XTempFile xTempFile ) throws java.lang.Exception {
91         XOutputStream xOutTemp = null;
92         XInputStream xInTemp = null;
93         xOutTemp = xTempFile.getOutputStream();
94         if ( xOutTemp == null ) {
95             throw new java.lang.Exception( "Cannot get output stream." );
96         }
97         xOutTemp.closeOutput();
98         xInTemp = xTempFile.getInputStream();
99         if ( xInTemp == null ) {
100             throw new java.lang.Exception( "Cannot get input stream." );
101         }
102         xInTemp.closeInput();
103         Message ( "Tempfile closed successfully." );
104     }
105 
KillTempFile( String sTempFileURL, XSimpleFileAccess xSFA )106     public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) throws com.sun.star.uno.Exception {
107         xSFA.kill( sTempFileURL );
108         Message ( "Tempfile killed successfully." );
109     }
110 
IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL )111     public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL )
112         throws com.sun.star.uno.Exception
113     {
114         boolean bRet = false;
115         bRet = xSFA.exists( sTempFileURL );
116         Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." );
117         return bRet;
118     }
119 
Message( String sMessage )120     public void Message( String sMessage ) {
121         System.out.println( m_sTestPrefix + sMessage );
122     }
123 }
124