1*bc3375a3SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*bc3375a3SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*bc3375a3SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*bc3375a3SAndrew Rist  * distributed with this work for additional information
6*bc3375a3SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*bc3375a3SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*bc3375a3SAndrew Rist  * "License"); you may not use this file except in compliance
9*bc3375a3SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*bc3375a3SAndrew Rist  *
11*bc3375a3SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*bc3375a3SAndrew Rist  *
13*bc3375a3SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*bc3375a3SAndrew Rist  * software distributed under the License is distributed on an
15*bc3375a3SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*bc3375a3SAndrew Rist  * KIND, either express or implied.  See the License for the
17*bc3375a3SAndrew Rist  * specific language governing permissions and limitations
18*bc3375a3SAndrew Rist  * under the License.
19*bc3375a3SAndrew Rist  *
20*bc3375a3SAndrew Rist  *************************************************************/
21*bc3375a3SAndrew Rist 
22*bc3375a3SAndrew Rist 
23cdf0e10cSrcweir package complex.tempfile;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
27cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
28cdf0e10cSrcweir import com.sun.star.io.*;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
31cdf0e10cSrcweir import java.util.Random;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir public class Test02 implements TempFileTest {
34cdf0e10cSrcweir 
35cdf0e10cSrcweir     XMultiServiceFactory m_xMSF;
36cdf0e10cSrcweir     XSimpleFileAccess m_xSFA;
37cdf0e10cSrcweir     TestHelper m_aTestHelper;
38cdf0e10cSrcweir 
Test02(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA)39cdf0e10cSrcweir     public Test02(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) {
40cdf0e10cSrcweir         m_xMSF = xMSF;
41cdf0e10cSrcweir         m_xSFA = xSFA;
42cdf0e10cSrcweir         m_aTestHelper = new TestHelper( "Test02: ");
43cdf0e10cSrcweir     }
44cdf0e10cSrcweir 
test()45cdf0e10cSrcweir     public boolean test() {
46cdf0e10cSrcweir         Object oTempFile = null;
47cdf0e10cSrcweir         XTempFile xTempFile = null;
48cdf0e10cSrcweir         XTruncate xTruncate = null;
49cdf0e10cSrcweir         String sFileURL = null;
50cdf0e10cSrcweir         String sFileName = null;
51cdf0e10cSrcweir         //create a temporary file.
52cdf0e10cSrcweir         try {
53cdf0e10cSrcweir             oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" );
54cdf0e10cSrcweir             xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile);
55cdf0e10cSrcweir             m_aTestHelper.Message( "Tempfile created." );
56cdf0e10cSrcweir             xTruncate = UnoRuntime.queryInterface(XTruncate.class, oTempFile);
57cdf0e10cSrcweir         } catch(Exception e) {
58cdf0e10cSrcweir             m_aTestHelper.Error( "Cannot create TempFile. exception: " + e );
59cdf0e10cSrcweir             return false;
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir         try {
62cdf0e10cSrcweir             //write something.
63cdf0e10cSrcweir             byte pBytesIn[] = new byte[9];
64cdf0e10cSrcweir             byte pBytesOut[][] = new byte[1][9];
65cdf0e10cSrcweir             Random oRandom = new Random();
66cdf0e10cSrcweir             oRandom.nextBytes( pBytesIn );
67cdf0e10cSrcweir             m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir             //get the URL.
70cdf0e10cSrcweir             sFileURL = m_aTestHelper.GetTempFileURL( xTempFile );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir             //let the service not to remove the URL.
73cdf0e10cSrcweir             m_aTestHelper.SetTempFileRemove( xTempFile, false );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir             //close the tempfile by closing input and output.
76cdf0e10cSrcweir             m_aTestHelper.CloseTempFile( xTempFile );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir             //check that the file is still available.
79cdf0e10cSrcweir             //xTempFile.seek(0);
80cdf0e10cSrcweir             m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut, pBytesIn.length + 1, m_xSFA, sFileURL );
81cdf0e10cSrcweir             for ( int i = 0; i < pBytesIn.length; i++ ) {
82cdf0e10cSrcweir                 if ( pBytesOut[0][i] != pBytesIn[i] ) {
83cdf0e10cSrcweir                     m_aTestHelper.Error( "Tempfile contains false data!" );
84cdf0e10cSrcweir                 }
85cdf0e10cSrcweir             }
86cdf0e10cSrcweir         } catch ( Exception e) {
87cdf0e10cSrcweir             m_aTestHelper.Error("Exception: " + e);
88cdf0e10cSrcweir             return false;
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir         return true;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir }
93