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 com
.sun
.star
.lang
.XMultiServiceFactory
;
21 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
22 import com
.sun
.star
.io
.*;
23 import com
.sun
.star
.uno
.UnoRuntime
;
24 import java
.util
.Random
;
27 private XMultiServiceFactory m_xMSF
= null;
28 private XSimpleFileAccess m_xSFA
= null;
29 private TestHelper m_aTestHelper
= null;
31 public Test01(XMultiServiceFactory xMSF
, XSimpleFileAccess xSFA
) {
34 m_aTestHelper
= new TestHelper( "Test01: ");
37 public boolean test() {
38 XTempFile xTempFile
= null;
39 String sFileURL
= null;
40 String sFileName
= null;
41 //create a temporary file.
43 Object oTempFile
= m_xMSF
.createInstance( "com.sun.star.io.TempFile" );
44 xTempFile
= UnoRuntime
.queryInterface(XTempFile
.class, oTempFile
);
45 m_aTestHelper
.Message( "Tempfile created." );
46 UnoRuntime
.queryInterface(XTruncate
.class, oTempFile
);
47 } catch( Exception e
) {
48 m_aTestHelper
.Error( "Cannot create TempFile. exception: " + e
);
52 //retrieve the tempfile URL
53 if ( xTempFile
== null ) {
54 m_aTestHelper
.Error( "Cannot get XTempFile interface." );
59 //compare the file name with the name in the URL.
60 sFileURL
= m_aTestHelper
.GetTempFileURL( xTempFile
);
61 sFileName
= m_aTestHelper
.GetTempFileName( xTempFile
);
62 m_aTestHelper
.CompareFileNameAndURL( sFileName
, sFileURL
);
64 //write to the stream using the service.
65 byte pBytesIn
[] = new byte[9];
66 byte pBytesOut1
[][] = new byte [1][9];
67 byte pBytesOut2
[][] = new byte [1][9];
68 Random oRandom
= new Random();
69 oRandom
.nextBytes( pBytesIn
);
70 m_aTestHelper
.WriteBytesWithStream( pBytesIn
, xTempFile
);
72 //check the result by reading from the service.
74 m_aTestHelper
.ReadBytesWithStream( pBytesOut1
, pBytesIn
.length
+ 1, xTempFile
);
75 for ( int i
= 0; i
< pBytesIn
.length
; i
++ ) {
76 if ( pBytesOut1
[0][i
] != pBytesIn
[i
] ) {
77 m_aTestHelper
.Error( "Tempfile outputs false data!" );
81 //check the result by reading from the file directly.
82 m_aTestHelper
.ReadDirectlyFromTempFile( pBytesOut2
, pBytesIn
.length
+ 1, m_xSFA
, sFileURL
);
83 for ( int i
= 0; i
< pBytesIn
.length
; i
++ ) {
84 if ( pBytesOut2
[0][i
] != pBytesIn
[i
] ) {
85 m_aTestHelper
.Error( "Tempfile contains false data!" );
89 //close the object(by closing input and output), check that the file was removed.
90 xTempFile
.setRemoveFile( false );
91 m_aTestHelper
.CloseTempFile( xTempFile
);
92 if( !m_aTestHelper
.IfTempFileExists( m_xSFA
, sFileURL
) ) {
93 m_aTestHelper
.Error( "TempFile mistakenly removed. " );
95 m_aTestHelper
.KillTempFile( sFileURL
, m_xSFA
);
97 } catch ( Exception e
) {
98 m_aTestHelper
.Error( "Exception: " + e
);