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 // import complexlib.ComplexTestCase;
26 
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.ucb.XSimpleFileAccess;
29 import com.sun.star.io.*;
30 import com.sun.star.uno.UnoRuntime;
31 import java.util.Random;
32 
33 import share.LogWriter;
34 
35 public class Test01 implements TempFileTest {
36     LogWriter m_aLogWriter;
37     XMultiServiceFactory m_xMSF = null;
38     XSimpleFileAccess m_xSFA = null;
39     TestHelper m_aTestHelper = null;
40 
Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA)41     public Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) {
42         m_xMSF = xMSF;
43         m_xSFA = xSFA;
44         m_aTestHelper = new TestHelper( "Test01: ");
45     }
46 
test()47     public boolean test() {
48         XTempFile xTempFile = null;
49         XTruncate xTruncate = null;
50         String sFileURL = null;
51         String sFileName = null;
52         //create a temporary file.
53         try {
54             Object oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" );
55             xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile);
56             m_aTestHelper.Message( "Tempfile created." );
57             xTruncate = UnoRuntime.queryInterface(XTruncate.class, oTempFile);
58         } catch( Exception e ) {
59             m_aTestHelper.Error( "Cannot create TempFile. exception: " + e );
60             return false;
61         }
62 
63         //retrieve the tempfile URL
64         if ( xTempFile == null ) {
65             m_aTestHelper.Error( "Cannot get XTempFile interface." );
66             return false;
67         }
68 
69         try {
70             //compare the file name with the name in the URL.
71             sFileURL = m_aTestHelper.GetTempFileURL( xTempFile );
72             sFileName = m_aTestHelper.GetTempFileName( xTempFile );
73             m_aTestHelper.CompareFileNameAndURL( sFileName, sFileURL );
74 
75             //write to the stream using the service.
76             byte pBytesIn[] = new byte[9];
77             byte pBytesOut1[][] = new byte [1][9];
78             byte pBytesOut2[][] = new byte [1][9];
79             Random oRandom = new Random();
80             oRandom.nextBytes( pBytesIn );
81             m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile );
82 
83             //check the result by reading from the service.
84             xTempFile.seek(0);
85             m_aTestHelper.ReadBytesWithStream( pBytesOut1, pBytesIn.length + 1, xTempFile );
86             for ( int i = 0; i < pBytesIn.length ; i++ ) {
87                 if ( pBytesOut1[0][i] != pBytesIn[i] ) {
88                     m_aTestHelper.Error( "Tempfile outputs false data!" );
89                 }
90             }
91 
92             //check the result by reading from the file directly.
93             m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut2, pBytesIn.length + 1, m_xSFA, sFileURL );
94             for ( int i = 0; i < pBytesIn.length; i++ ) {
95                 if ( pBytesOut2[0][i] != pBytesIn[i] ) {
96                     m_aTestHelper.Error( "Tempfile contains false data!" );
97                 }
98             }
99 
100             //close the object(by closing input and output), check that the file was removed.
101             xTempFile.setRemoveFile( false );
102             m_aTestHelper.CloseTempFile( xTempFile );
103             if( !m_aTestHelper.IfTempFileExists( m_xSFA, sFileURL ) ) {
104                 m_aTestHelper.Error( "TempFile mistakenly removed. " );
105             } else {
106                 m_aTestHelper.KillTempFile( sFileURL, m_xSFA );
107             }
108         } catch ( Exception e ) {
109             m_aTestHelper.Error( "Exception: " + e );
110             return false;
111         }
112         return true;
113     }
114 }
115