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 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.ucb.XSimpleFileAccess;
28 import com.sun.star.io.*;
29 
30 import com.sun.star.uno.UnoRuntime;
31 import java.util.Random;
32 
33 public class Test02 implements TempFileTest {
34 
35     XMultiServiceFactory m_xMSF;
36     XSimpleFileAccess m_xSFA;
37     TestHelper m_aTestHelper;
38 
Test02(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA)39     public Test02(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) {
40         m_xMSF = xMSF;
41         m_xSFA = xSFA;
42         m_aTestHelper = new TestHelper( "Test02: ");
43     }
44 
test()45     public boolean test() {
46         Object oTempFile = null;
47         XTempFile xTempFile = null;
48         XTruncate xTruncate = null;
49         String sFileURL = null;
50         String sFileName = null;
51         //create a temporary file.
52         try {
53             oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" );
54             xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile);
55             m_aTestHelper.Message( "Tempfile created." );
56             xTruncate = UnoRuntime.queryInterface(XTruncate.class, oTempFile);
57         } catch(Exception e) {
58             m_aTestHelper.Error( "Cannot create TempFile. exception: " + e );
59             return false;
60         }
61         try {
62             //write something.
63             byte pBytesIn[] = new byte[9];
64             byte pBytesOut[][] = new byte[1][9];
65             Random oRandom = new Random();
66             oRandom.nextBytes( pBytesIn );
67             m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile );
68 
69             //get the URL.
70             sFileURL = m_aTestHelper.GetTempFileURL( xTempFile );
71 
72             //let the service not to remove the URL.
73             m_aTestHelper.SetTempFileRemove( xTempFile, false );
74 
75             //close the tempfile by closing input and output.
76             m_aTestHelper.CloseTempFile( xTempFile );
77 
78             //check that the file is still available.
79             //xTempFile.seek(0);
80             m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut, pBytesIn.length + 1, m_xSFA, sFileURL );
81             for ( int i = 0; i < pBytesIn.length; i++ ) {
82                 if ( pBytesOut[0][i] != pBytesIn[i] ) {
83                     m_aTestHelper.Error( "Tempfile contains false data!" );
84                 }
85             }
86         } catch ( Exception e) {
87             m_aTestHelper.Error("Exception: " + e);
88             return false;
89         }
90         return true;
91     }
92 }
93