1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.tempfile;
28 
29 // import complexlib.ComplexTestCase;
30 
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.ucb.XSimpleFileAccess;
33 import com.sun.star.io.*;
34 import com.sun.star.uno.UnoRuntime;
35 import java.util.Random;
36 
37 import share.LogWriter;
38 
39 public class Test01 implements TempFileTest {
40     LogWriter m_aLogWriter;
41     XMultiServiceFactory m_xMSF = null;
42     XSimpleFileAccess m_xSFA = null;
43     TestHelper m_aTestHelper = null;
44 
45     public Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) {
46         m_xMSF = xMSF;
47         m_xSFA = xSFA;
48         m_aTestHelper = new TestHelper( "Test01: ");
49     }
50 
51     public boolean test() {
52         XTempFile xTempFile = null;
53         XTruncate xTruncate = null;
54         String sFileURL = null;
55         String sFileName = null;
56         //create a temporary file.
57         try {
58             Object oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" );
59             xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile);
60             m_aTestHelper.Message( "Tempfile created." );
61             xTruncate = UnoRuntime.queryInterface(XTruncate.class, oTempFile);
62         } catch( Exception e ) {
63             m_aTestHelper.Error( "Cannot create TempFile. exception: " + e );
64             return false;
65         }
66 
67         //retrieve the tempfile URL
68         if ( xTempFile == null ) {
69             m_aTestHelper.Error( "Cannot get XTempFile interface." );
70             return false;
71         }
72 
73         try {
74             //compare the file name with the name in the URL.
75             sFileURL = m_aTestHelper.GetTempFileURL( xTempFile );
76             sFileName = m_aTestHelper.GetTempFileName( xTempFile );
77             m_aTestHelper.CompareFileNameAndURL( sFileName, sFileURL );
78 
79             //write to the stream using the service.
80             byte pBytesIn[] = new byte[9];
81             byte pBytesOut1[][] = new byte [1][9];
82             byte pBytesOut2[][] = new byte [1][9];
83             Random oRandom = new Random();
84             oRandom.nextBytes( pBytesIn );
85             m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile );
86 
87             //check the result by reading from the service.
88             xTempFile.seek(0);
89             m_aTestHelper.ReadBytesWithStream( pBytesOut1, pBytesIn.length + 1, xTempFile );
90             for ( int i = 0; i < pBytesIn.length ; i++ ) {
91                 if ( pBytesOut1[0][i] != pBytesIn[i] ) {
92                     m_aTestHelper.Error( "Tempfile outputs false data!" );
93                 }
94             }
95 
96             //check the result by reading from the file directly.
97             m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut2, pBytesIn.length + 1, m_xSFA, sFileURL );
98             for ( int i = 0; i < pBytesIn.length; i++ ) {
99                 if ( pBytesOut2[0][i] != pBytesIn[i] ) {
100                     m_aTestHelper.Error( "Tempfile contains false data!" );
101                 }
102             }
103 
104             //close the object(by closing input and output), check that the file was removed.
105             xTempFile.setRemoveFile( false );
106             m_aTestHelper.CloseTempFile( xTempFile );
107             if( !m_aTestHelper.IfTempFileExists( m_xSFA, sFileURL ) ) {
108                 m_aTestHelper.Error( "TempFile mistakenly removed. " );
109             } else {
110                 m_aTestHelper.KillTempFile( sFileURL, m_xSFA );
111             }
112         } catch ( Exception e ) {
113             m_aTestHelper.Error( "Exception: " + e );
114             return false;
115         }
116         return true;
117     }
118 }
119