Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / qa / complex / tempfile / Test02.java
blobafef403fbfe460ef46c96cf731505c239a2366ed
1 /*
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;
21 import com.sun.star.lang.XMultiServiceFactory;
22 import com.sun.star.ucb.XSimpleFileAccess;
23 import com.sun.star.io.*;
25 import com.sun.star.uno.UnoRuntime;
26 import java.util.Random;
28 public class Test02 {
30 private XMultiServiceFactory m_xMSF;
31 private XSimpleFileAccess m_xSFA;
32 private TestHelper m_aTestHelper;
34 public Test02(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) {
35 m_xMSF = xMSF;
36 m_xSFA = xSFA;
37 m_aTestHelper = new TestHelper( "Test02: ");
40 public boolean test() {
41 Object oTempFile = null;
42 XTempFile xTempFile = null;
43 String sFileURL = null;
44 //create a temporary file.
45 try {
46 oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" );
47 xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile);
48 m_aTestHelper.Message( "Tempfile created." );
49 UnoRuntime.queryInterface(XTruncate.class, oTempFile);
50 } catch(Exception e) {
51 m_aTestHelper.Error( "Cannot create TempFile. exception: " + e );
52 return false;
54 try {
55 //write something.
56 byte pBytesIn[] = new byte[9];
57 byte pBytesOut[][] = new byte[1][9];
58 Random oRandom = new Random();
59 oRandom.nextBytes( pBytesIn );
60 m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile );
62 //get the URL.
63 sFileURL = m_aTestHelper.GetTempFileURL( xTempFile );
65 //let the service not to remove the URL.
66 m_aTestHelper.SetTempFileRemove( xTempFile, false );
68 //close the tempfile by closing input and output.
69 m_aTestHelper.CloseTempFile( xTempFile );
71 //check that the file is still available.
72 m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut, pBytesIn.length + 1, m_xSFA, sFileURL );
73 for ( int i = 0; i < pBytesIn.length; i++ ) {
74 if ( pBytesOut[0][i] != pBytesIn[i] ) {
75 m_aTestHelper.Error( "Tempfile contains false data!" );
78 } catch ( Exception e) {
79 m_aTestHelper.Error("Exception: " + e);
80 return false;
82 return true;