2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
18 package complex
.tempfile
;
20 // import complexlib.ComplexTestCase;
22 import com
.sun
.star
.lang
.XMultiServiceFactory
;
23 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
24 import com
.sun
.star
.io
.*;
25 import com
.sun
.star
.uno
.UnoRuntime
;
26 import java
.util
.Random
;
28 import share
.LogWriter
;
30 public class Test01
implements TempFileTest
{
31 LogWriter m_aLogWriter
;
32 XMultiServiceFactory m_xMSF
= null;
33 XSimpleFileAccess m_xSFA
= null;
34 TestHelper m_aTestHelper
= null;
36 public Test01(XMultiServiceFactory xMSF
, XSimpleFileAccess xSFA
) {
39 m_aTestHelper
= new TestHelper( "Test01: ");
42 public boolean test() {
43 XTempFile xTempFile
= null;
44 XTruncate xTruncate
= null;
45 String sFileURL
= null;
46 String sFileName
= null;
47 //create a temporary file.
49 Object oTempFile
= m_xMSF
.createInstance( "com.sun.star.io.TempFile" );
50 xTempFile
= UnoRuntime
.queryInterface(XTempFile
.class, oTempFile
);
51 m_aTestHelper
.Message( "Tempfile created." );
52 xTruncate
= UnoRuntime
.queryInterface(XTruncate
.class, oTempFile
);
53 } catch( Exception e
) {
54 m_aTestHelper
.Error( "Cannot create TempFile. exception: " + e
);
58 //retrieve the tempfile URL
59 if ( xTempFile
== null ) {
60 m_aTestHelper
.Error( "Cannot get XTempFile interface." );
65 //compare the file name with the name in the URL.
66 sFileURL
= m_aTestHelper
.GetTempFileURL( xTempFile
);
67 sFileName
= m_aTestHelper
.GetTempFileName( xTempFile
);
68 m_aTestHelper
.CompareFileNameAndURL( sFileName
, sFileURL
);
70 //write to the stream using the service.
71 byte pBytesIn
[] = new byte[9];
72 byte pBytesOut1
[][] = new byte [1][9];
73 byte pBytesOut2
[][] = new byte [1][9];
74 Random oRandom
= new Random();
75 oRandom
.nextBytes( pBytesIn
);
76 m_aTestHelper
.WriteBytesWithStream( pBytesIn
, xTempFile
);
78 //check the result by reading from the service.
80 m_aTestHelper
.ReadBytesWithStream( pBytesOut1
, pBytesIn
.length
+ 1, xTempFile
);
81 for ( int i
= 0; i
< pBytesIn
.length
; i
++ ) {
82 if ( pBytesOut1
[0][i
] != pBytesIn
[i
] ) {
83 m_aTestHelper
.Error( "Tempfile outputs false data!" );
87 //check the result by reading from the file directly.
88 m_aTestHelper
.ReadDirectlyFromTempFile( pBytesOut2
, pBytesIn
.length
+ 1, m_xSFA
, sFileURL
);
89 for ( int i
= 0; i
< pBytesIn
.length
; i
++ ) {
90 if ( pBytesOut2
[0][i
] != pBytesIn
[i
] ) {
91 m_aTestHelper
.Error( "Tempfile contains false data!" );
95 //close the object(by closing input and output), check that the file was removed.
96 xTempFile
.setRemoveFile( false );
97 m_aTestHelper
.CloseTempFile( xTempFile
);
98 if( !m_aTestHelper
.IfTempFileExists( m_xSFA
, sFileURL
) ) {
99 m_aTestHelper
.Error( "TempFile mistakenly removed. " );
101 m_aTestHelper
.KillTempFile( sFileURL
, m_xSFA
);
103 } catch ( Exception e
) {
104 m_aTestHelper
.Error( "Exception: " + e
);