diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /unotools/qa/complex/tempfile | |
parent | Initial commit. (diff) | |
download | libreoffice-1ad18e38974bb28c3d98d0be8f7d8c18fc56de29.tar.xz libreoffice-1ad18e38974bb28c3d98d0be8f7d8c18fc56de29.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'unotools/qa/complex/tempfile')
-rw-r--r-- | unotools/qa/complex/tempfile/TempFileUnitTest.java | 91 | ||||
-rw-r--r-- | unotools/qa/complex/tempfile/Test01.java | 103 | ||||
-rw-r--r-- | unotools/qa/complex/tempfile/Test02.java | 84 | ||||
-rw-r--r-- | unotools/qa/complex/tempfile/TestHelper.java | 200 |
4 files changed, 478 insertions, 0 deletions
diff --git a/unotools/qa/complex/tempfile/TempFileUnitTest.java b/unotools/qa/complex/tempfile/TempFileUnitTest.java new file mode 100644 index 000000000..2d23db0db --- /dev/null +++ b/unotools/qa/complex/tempfile/TempFileUnitTest.java @@ -0,0 +1,91 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package complex.tempfile; + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.ucb.XSimpleFileAccess; +import com.sun.star.uno.UnoRuntime; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openoffice.test.OfficeConnection; +import static org.junit.Assert.*; + +public class TempFileUnitTest +{ + private XMultiServiceFactory m_xMSF = null; + private XSimpleFileAccess m_xSFA = null; + + @Before public void before() { + m_xMSF = getMSF(); + if ( m_xMSF == null ) { + fail ( "Cannot create service factory!" ); + } + try + { + Object oSFA = m_xMSF.createInstance( "com.sun.star.ucb.SimpleFileAccess" ); + m_xSFA = UnoRuntime.queryInterface( XSimpleFileAccess.class, oSFA ); + } + catch ( Exception e ) + { + fail ( "Cannot get simple file access! Exception: " + e); + } + if ( m_xSFA == null ) { + fail ( "Cannot get simple file access!" ); + } + } + + @After public void after() { + m_xMSF = null; + m_xSFA = null; + } + + @Test public void ExecuteTest01() { + Test01 aTest = new Test01( m_xMSF, m_xSFA ); + assertTrue( "Test01 failed!", aTest.test() ); + } + + @Test public void ExecuteTest02() { + Test02 aTest = new Test02( m_xMSF, m_xSFA ); + assertTrue( "Test02 failed!", aTest.test() ); + } + + private XMultiServiceFactory getMSF() + { + return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); + } + + // setup and close connections + @BeforeClass public static void setUpConnection() throws Exception { + System.out.println("setUpConnection()"); + connection.setUp(); + } + + @AfterClass public static void tearDownConnection() + throws InterruptedException, com.sun.star.uno.Exception + { + System.out.println("tearDownConnection()"); + connection.tearDown(); + } + + private static final OfficeConnection connection = new OfficeConnection(); +} + diff --git a/unotools/qa/complex/tempfile/Test01.java b/unotools/qa/complex/tempfile/Test01.java new file mode 100644 index 000000000..ed8e72985 --- /dev/null +++ b/unotools/qa/complex/tempfile/Test01.java @@ -0,0 +1,103 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package complex.tempfile; + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.ucb.XSimpleFileAccess; +import com.sun.star.io.*; +import com.sun.star.uno.UnoRuntime; +import java.util.Random; + +public class Test01 { + private XMultiServiceFactory m_xMSF = null; + private XSimpleFileAccess m_xSFA = null; + private TestHelper m_aTestHelper = null; + + public Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) { + m_xMSF = xMSF; + m_xSFA = xSFA; + m_aTestHelper = new TestHelper( "Test01: "); + } + + public boolean test() { + XTempFile xTempFile = null; + String sFileURL = null; + String sFileName = null; + //create a temporary file. + try { + Object oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" ); + xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile); + m_aTestHelper.Message( "Tempfile created." ); + UnoRuntime.queryInterface(XTruncate.class, oTempFile); + } catch( Exception e ) { + m_aTestHelper.Error( "Cannot create TempFile. exception: " + e ); + return false; + } + + //retrieve the tempfile URL + if ( xTempFile == null ) { + m_aTestHelper.Error( "Cannot get XTempFile interface." ); + return false; + } + + try { + //compare the file name with the name in the URL. + sFileURL = m_aTestHelper.GetTempFileURL( xTempFile ); + sFileName = m_aTestHelper.GetTempFileName( xTempFile ); + m_aTestHelper.CompareFileNameAndURL( sFileName, sFileURL ); + + //write to the stream using the service. + byte pBytesIn[] = new byte[9]; + byte pBytesOut1[][] = new byte [1][9]; + byte pBytesOut2[][] = new byte [1][9]; + Random oRandom = new Random(); + oRandom.nextBytes( pBytesIn ); + m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile ); + + //check the result by reading from the service. + xTempFile.seek(0); + m_aTestHelper.ReadBytesWithStream( pBytesOut1, pBytesIn.length + 1, xTempFile ); + for ( int i = 0; i < pBytesIn.length ; i++ ) { + if ( pBytesOut1[0][i] != pBytesIn[i] ) { + m_aTestHelper.Error( "Tempfile outputs false data!" ); + } + } + + //check the result by reading from the file directly. + m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut2, pBytesIn.length + 1, m_xSFA, sFileURL ); + for ( int i = 0; i < pBytesIn.length; i++ ) { + if ( pBytesOut2[0][i] != pBytesIn[i] ) { + m_aTestHelper.Error( "Tempfile contains false data!" ); + } + } + + //close the object(by closing input and output), check that the file was removed. + xTempFile.setRemoveFile( false ); + m_aTestHelper.CloseTempFile( xTempFile ); + if( !m_aTestHelper.IfTempFileExists( m_xSFA, sFileURL ) ) { + m_aTestHelper.Error( "TempFile mistakenly removed. " ); + } else { + m_aTestHelper.KillTempFile( sFileURL, m_xSFA ); + } + } catch ( Exception e ) { + m_aTestHelper.Error( "Exception: " + e ); + return false; + } + return true; + } +} diff --git a/unotools/qa/complex/tempfile/Test02.java b/unotools/qa/complex/tempfile/Test02.java new file mode 100644 index 000000000..afef403fb --- /dev/null +++ b/unotools/qa/complex/tempfile/Test02.java @@ -0,0 +1,84 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package complex.tempfile; + + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.ucb.XSimpleFileAccess; +import com.sun.star.io.*; + +import com.sun.star.uno.UnoRuntime; +import java.util.Random; + +public class Test02 { + + private XMultiServiceFactory m_xMSF; + private XSimpleFileAccess m_xSFA; + private TestHelper m_aTestHelper; + + public Test02(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) { + m_xMSF = xMSF; + m_xSFA = xSFA; + m_aTestHelper = new TestHelper( "Test02: "); + } + + public boolean test() { + Object oTempFile = null; + XTempFile xTempFile = null; + String sFileURL = null; + //create a temporary file. + try { + oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" ); + xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile); + m_aTestHelper.Message( "Tempfile created." ); + UnoRuntime.queryInterface(XTruncate.class, oTempFile); + } catch(Exception e) { + m_aTestHelper.Error( "Cannot create TempFile. exception: " + e ); + return false; + } + try { + //write something. + byte pBytesIn[] = new byte[9]; + byte pBytesOut[][] = new byte[1][9]; + Random oRandom = new Random(); + oRandom.nextBytes( pBytesIn ); + m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile ); + + //get the URL. + sFileURL = m_aTestHelper.GetTempFileURL( xTempFile ); + + //let the service not to remove the URL. + m_aTestHelper.SetTempFileRemove( xTempFile, false ); + + //close the tempfile by closing input and output. + m_aTestHelper.CloseTempFile( xTempFile ); + + //check that the file is still available. + m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut, pBytesIn.length + 1, m_xSFA, sFileURL ); + for ( int i = 0; i < pBytesIn.length; i++ ) { + if ( pBytesOut[0][i] != pBytesIn[i] ) { + m_aTestHelper.Error( "Tempfile contains false data!" ); + } + } + } catch ( Exception e) { + m_aTestHelper.Error("Exception: " + e); + return false; + } + return true; + } +} diff --git a/unotools/qa/complex/tempfile/TestHelper.java b/unotools/qa/complex/tempfile/TestHelper.java new file mode 100644 index 000000000..237c95b9e --- /dev/null +++ b/unotools/qa/complex/tempfile/TestHelper.java @@ -0,0 +1,200 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package complex.tempfile; + + + +import com.sun.star.io.*; + +import com.sun.star.uno.AnyConverter; +import com.sun.star.ucb.XSimpleFileAccess; + + +public class TestHelper { + + private String m_sTestPrefix; + + public TestHelper( String sTestPrefix ) { + + m_sTestPrefix = sTestPrefix; + } + public void SetTempFileRemove( XTempFile xTempFile, boolean b ) { + try { + xTempFile.setRemoveFile( b ); + } catch( Exception e ) { + Error( "Cannot set TempFileRemove. exception: " + e ); + } + } + + public String GetTempFileURL ( XTempFile xTempFile ) { + String sTempFileURL = null; + try { + sTempFileURL = AnyConverter.toString( xTempFile.getUri() ); + } catch (Exception e) { + Error ( "Cannot get TempFileURL. exception: " + e ); + } + if ( sTempFileURL == null || sTempFileURL.equals("") ) { + Error ( "Temporary file not valid." ); + } + return sTempFileURL; + } + + public String GetTempFileName( XTempFile xTempFile ) { + String sTempFileName = null; + try { + sTempFileName = AnyConverter.toString( xTempFile.getResourceName() ); + } catch ( Exception e ) { + Error( "Cannot get TempFileName. exception: " + e ); + } + if ( sTempFileName == null || sTempFileName.equals("") ) { + Error( "Temporary file not valid." ); + } + return sTempFileName; + } + + public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) { + boolean bRet = false; + try { + bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) ); + Message ( "Compare file name and URL: " + + ( bRet ? "OK." : "ERROR: FILE NAME AND URL DO NOT MATCH." ) ); + } + catch ( Exception e ) { + Error ( "exception: " + e); + } + return bRet; + } + + public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) { + try { + XOutputStream xOutTemp = xTempFile.getOutputStream(); + if ( xOutTemp == null ) { + Error( "Cannot get output stream." ); + } else { + xOutTemp.writeBytes( pBytes ); + Message ( "Write to tempfile successfully." ); + } + } catch ( Exception e ) { + Error( "Cannot write to stream. exception: " + e ); + } + } + + public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) { + try { + XInputStream xInTemp = xTempFile.getInputStream(); + if ( xInTemp == null ) { + Error( "Cannot get input stream from tempfile." ); + } else { + xInTemp.readBytes( pBytes, nBytes ); + Message ( "Read from tempfile successfully." ); + } + } catch ( Exception e ) { + Error( "Cannot read from stream. exception: " + e ); + } + } + public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL ) + { + try + { + if ( xSFA != null ) { + XInputStream xInTemp = xSFA.openFileRead( sTempFileURL ); + if ( xInTemp != null ) + { + xInTemp.readBytes( pBytes, nBytes ); + xInTemp.closeInput(); + Message ( "Read directly from tempfile successfully." ); + } else { + Error ( "Cannot create input stream from URL." ); + } + } + } + catch ( Exception e) + { + Error( "Exception caught in TestHelper." + + "ReadDirectlyFromTempFile(). exception: " + e ); + } + } + + public void CloseTempFile( XTempFile xTempFile ) { + XOutputStream xOutTemp = null; + XInputStream xInTemp = null; + try { + xOutTemp = xTempFile.getOutputStream(); + if ( xOutTemp == null ) { + Error( "Cannot get output stream." ); + } + } catch ( Exception e ) { + Error( "Cannot get output stream. exception:" + e ); + } + try { + xOutTemp.closeOutput(); + } catch( Exception e ) { + Error( "Cannot close output stream. exception:" + e ); + } + try { + xInTemp = xTempFile.getInputStream(); + if ( xInTemp == null ) { + Error( "Cannot get input stream." ); + } + } catch ( Exception e ) { + Error( "Cannot get input stream. exception:" + e ); + } + try { + xInTemp.closeInput(); + Message ( "Tempfile closed successfully." ); + } catch( Exception e ) { + Error( "Cannot close input stream. exception:" + e ); + } + } + + public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) { + try { + if ( sTempFileURL != null && xSFA != null ) { + xSFA.kill( sTempFileURL ); + Message ( "Tempfile killed successfully." ); + } + } + catch ( Exception e ) { + Error ( "Exception caught in TestHelper." + + "KillTempFile(): " + e); + } + } + + public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL ) { + boolean bRet = false; + try { + if ( sTempFileURL != null && xSFA != null ) { + bRet = xSFA.exists( sTempFileURL ); + Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." ); + } + } + catch( Exception e ) { + Error( "Exception caught in TestHelper." + + "IfTempFileExists(): " + e ); + } + return bRet; + } + + public void Error( String sError ) { + System.out.println( m_sTestPrefix + "Error: " + sError ); + } + + public void Message( String sMessage ) { + System.out.println( m_sTestPrefix + sMessage ); + } +} |