tdf#97362 Convert unotools tempfile tests to Python
Signed-off-by: Chenxiong Qi <qcxhome@gmail.com> Change-Id: I43d0881bbd2a99e018e027e166dcb7b0bffa5ff5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/144395 Tested-by: Jenkins Reviewed-by: Hossein <hossein@libreoffice.org>
This commit is contained in:
parent
bcce220531
commit
be7f3df701
@ -14,7 +14,7 @@ $(eval $(call gb_Module_add_targets,unotools,\
|
||||
))
|
||||
|
||||
$(eval $(call gb_Module_add_subsequentcheck_targets,unotools,\
|
||||
JunitTest_unotools_complex \
|
||||
PythonTest_unotools_python \
|
||||
))
|
||||
$(eval $(call gb_Module_add_check_targets,unotools,\
|
||||
CppunitTest_unotools_configpaths \
|
||||
|
16
unotools/PythonTest_unotools_python.mk
Normal file
16
unotools/PythonTest_unotools_python.mk
Normal file
@ -0,0 +1,16 @@
|
||||
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
|
||||
#
|
||||
# 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/.
|
||||
#
|
||||
|
||||
$(eval $(call gb_PythonTest_PythonTest,unotools_python))
|
||||
|
||||
$(eval $(call gb_PythonTest_add_modules,unotools_python,$(SRCDIR)/unotools/qa/python,\
|
||||
test_tempfile \
|
||||
))
|
||||
|
||||
# vim: set noet sw=4 ts=4:
|
@ -1,91 +0,0 @@
|
||||
/*
|
||||
* 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!" + 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() throws Exception {
|
||||
Test01 aTest = new Test01( m_xMSF, m_xSFA );
|
||||
assertTrue( "Test01 failed!", aTest.test() );
|
||||
}
|
||||
|
||||
@Test public void ExecuteTest02() throws Exception {
|
||||
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();
|
||||
}
|
||||
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
* 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() throws Exception {
|
||||
XTempFile xTempFile = null;
|
||||
String sFileURL = null;
|
||||
String sFileName = null;
|
||||
//create a temporary file.
|
||||
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);
|
||||
|
||||
//retrieve the tempfile URL
|
||||
if ( xTempFile == null )
|
||||
throw new java.lang.Exception( "Cannot get XTempFile interface." );
|
||||
|
||||
//compare the file name with the name in the URL.
|
||||
sFileURL = m_aTestHelper.GetTempFileURL( xTempFile );
|
||||
sFileName = m_aTestHelper.GetTempFileName( xTempFile );
|
||||
m_aTestHelper.Message( "Tempfile URL: " + sFileURL );
|
||||
m_aTestHelper.Message( "Tempfile name: " + sFileName );
|
||||
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] ) {
|
||||
throw new java.lang.Exception( "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] ) {
|
||||
throw new java.lang.Exception( "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 ) ) {
|
||||
throw new java.lang.Exception( "TempFile mistakenly removed. " );
|
||||
}
|
||||
m_aTestHelper.KillTempFile( sFileURL, m_xSFA );
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
/*
|
||||
* 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() throws Exception {
|
||||
Object oTempFile = null;
|
||||
XTempFile xTempFile = null;
|
||||
String sFileURL = null;
|
||||
//create a temporary file.
|
||||
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);
|
||||
|
||||
//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] ) {
|
||||
throw new Exception( "Tempfile contains false data!" );
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,123 +0,0 @@
|
||||
/*
|
||||
* 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.*;
|
||||
import com.sun.star.uno.AnyConverter;
|
||||
import com.sun.star.ucb.XSimpleFileAccess;
|
||||
import java.io.*;
|
||||
|
||||
public class TestHelper {
|
||||
|
||||
private String m_sTestPrefix;
|
||||
|
||||
public TestHelper( String sTestPrefix ) {
|
||||
m_sTestPrefix = sTestPrefix;
|
||||
}
|
||||
|
||||
public void SetTempFileRemove( XTempFile xTempFile, boolean b ) {
|
||||
xTempFile.setRemoveFile( b );
|
||||
}
|
||||
|
||||
public String GetTempFileURL ( XTempFile xTempFile ) throws java.lang.Exception {
|
||||
String sTempFileURL = AnyConverter.toString( xTempFile.getUri() );
|
||||
if ( sTempFileURL == null || sTempFileURL.equals("") ) {
|
||||
throw new java.lang.Exception( "Temporary file not valid." );
|
||||
}
|
||||
return sTempFileURL;
|
||||
}
|
||||
|
||||
public String GetTempFileName( XTempFile xTempFile ) throws java.lang.Exception {
|
||||
String sTempFileName = AnyConverter.toString( xTempFile.getResourceName() );
|
||||
if ( sTempFileName == null || sTempFileName.equals("") ) {
|
||||
throw new java.lang.Exception( "Temporary file not valid." );
|
||||
}
|
||||
return sTempFileName;
|
||||
}
|
||||
|
||||
public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) throws java.lang.Exception {
|
||||
boolean bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) );
|
||||
if (!bRet)
|
||||
throw new java.lang.Exception("FILE NAME AND URL DO NOT MATCH." );
|
||||
return bRet;
|
||||
}
|
||||
|
||||
public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) throws java.lang.Exception {
|
||||
XOutputStream xOutTemp = xTempFile.getOutputStream();
|
||||
if ( xOutTemp == null )
|
||||
throw new java.lang.Exception( "Cannot get output stream." );
|
||||
xOutTemp.writeBytes( pBytes );
|
||||
xOutTemp.flush();
|
||||
Message ( "Write " + pBytes.length + " bytes to tempfile successfully." );
|
||||
}
|
||||
|
||||
public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) throws java.lang.Exception {
|
||||
XInputStream xInTemp = xTempFile.getInputStream();
|
||||
if ( xInTemp == null )
|
||||
throw new java.lang.Exception( "Cannot get input stream from tempfile." );
|
||||
int n = xInTemp.readBytes( pBytes, nBytes );
|
||||
Message ( "Read " + n + " bytes from tempfile successfully." );
|
||||
}
|
||||
|
||||
public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL )
|
||||
throws java.lang.Exception
|
||||
{
|
||||
Message ( "Attempting to read directly from " + sTempFileURL );
|
||||
XInputStream xInTemp = xSFA.openFileRead( sTempFileURL );
|
||||
if ( xInTemp == null )
|
||||
throw new java.lang.Exception("Cannot create input stream from URL.");
|
||||
int n = xInTemp.readBytes( pBytes, nBytes );
|
||||
xInTemp.closeInput();
|
||||
Message ( "Read " + n + " bytes directly from tempfile successfully. " + sTempFileURL );
|
||||
}
|
||||
|
||||
public void CloseTempFile( XTempFile xTempFile ) throws java.lang.Exception {
|
||||
XOutputStream xOutTemp = null;
|
||||
XInputStream xInTemp = null;
|
||||
xOutTemp = xTempFile.getOutputStream();
|
||||
if ( xOutTemp == null ) {
|
||||
throw new java.lang.Exception( "Cannot get output stream." );
|
||||
}
|
||||
xOutTemp.closeOutput();
|
||||
xInTemp = xTempFile.getInputStream();
|
||||
if ( xInTemp == null ) {
|
||||
throw new java.lang.Exception( "Cannot get input stream." );
|
||||
}
|
||||
xInTemp.closeInput();
|
||||
Message ( "Tempfile closed successfully." );
|
||||
}
|
||||
|
||||
public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) throws com.sun.star.uno.Exception {
|
||||
xSFA.kill( sTempFileURL );
|
||||
Message ( "Tempfile killed successfully." );
|
||||
}
|
||||
|
||||
public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL )
|
||||
throws com.sun.star.uno.Exception
|
||||
{
|
||||
boolean bRet = false;
|
||||
bRet = xSFA.exists( sTempFileURL );
|
||||
Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." );
|
||||
return bRet;
|
||||
}
|
||||
|
||||
public void Message( String sMessage ) {
|
||||
System.out.println( m_sTestPrefix + sMessage );
|
||||
}
|
||||
}
|
149
unotools/qa/python/test_tempfile.py
Normal file
149
unotools/qa/python/test_tempfile.py
Normal file
@ -0,0 +1,149 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
|
||||
#
|
||||
# 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/.
|
||||
#
|
||||
|
||||
import unittest
|
||||
|
||||
import uno
|
||||
from org.libreoffice.unotest import UnoInProcess
|
||||
|
||||
|
||||
class TestTempFile(unittest.TestCase):
|
||||
"""Test temporary file object created from com.sun.star.io.TempFile"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._uno = UnoInProcess()
|
||||
cls._uno.setUp()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls._uno.tearDown()
|
||||
|
||||
def setUp(self):
|
||||
self.file_data = uno.ByteSequence(b"some data")
|
||||
|
||||
service_manager = self._uno.getContext().getServiceManager()
|
||||
if service_manager is None:
|
||||
raise RuntimeError("Cannot create service factory!")
|
||||
|
||||
try:
|
||||
self.file_access = service_manager.createInstance("com.sun.star.ucb.SimpleFileAccess")
|
||||
if self.file_access is None:
|
||||
raise RuntimeError("Cannot get simple access!")
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Cannot get simple file access! {e}")
|
||||
|
||||
self.temp_file = service_manager.createInstance("com.sun.star.io.TempFile")
|
||||
has_xtempfile_if = bool([
|
||||
1 for type_info in self.temp_file.getTypes()
|
||||
if type_info.typeName == "com.sun.star.io.XTempFile"
|
||||
])
|
||||
if not has_xtempfile_if:
|
||||
raise RuntimeError("Cannot get XTempFile interface.")
|
||||
|
||||
def close_temp_file(self) -> None:
|
||||
stream = self.temp_file.getOutputStream()
|
||||
if stream is None:
|
||||
raise RuntimeError("Cannot get output stream")
|
||||
stream.closeOutput()
|
||||
stream = self.temp_file.getInputStream()
|
||||
if stream is None:
|
||||
raise RuntimeError("Cannot get input stream")
|
||||
stream.closeInput()
|
||||
print("Tempfile closed successfully.")
|
||||
|
||||
def read_bytes_with_stream(self) -> uno.ByteSequence:
|
||||
input_stream = self.temp_file.getInputStream()
|
||||
if input_stream is None:
|
||||
raise RuntimeError("Cannot get input stream from tempfile.")
|
||||
nbytes, read_data = input_stream.readBytes(None, len(self.file_data))
|
||||
print("Read", nbytes, "bytes from tempfile successfully.")
|
||||
return read_data
|
||||
|
||||
def read_directly_from_temp_file(self, file_url: str) -> uno.ByteSequence:
|
||||
print("Attempting to read directly from", file_url)
|
||||
input_stream = self.file_access.openFileRead(file_url)
|
||||
if input_stream is None:
|
||||
raise RuntimeError("Cannot create input stream from URL.")
|
||||
nbytes, read_data = input_stream.readBytes(None, len(self.file_data))
|
||||
print("Read", nbytes, "bytes directly from tempfile successfully.")
|
||||
return read_data
|
||||
|
||||
def write_bytes_with_stream(self) -> None:
|
||||
output_stream = self.temp_file.getOutputStream()
|
||||
if output_stream is None:
|
||||
raise RuntimeError("Cannot get output stream.")
|
||||
output_stream.writeBytes(self.file_data)
|
||||
output_stream.flush()
|
||||
print("Write", len(self.file_data), "bytes to tempfile successfully.")
|
||||
|
||||
def get_temp_file_url(self) -> str:
|
||||
uri = self.temp_file.Uri
|
||||
if not uri:
|
||||
raise RuntimeError("Temporary file not valid.")
|
||||
return uri
|
||||
|
||||
def get_temp_file_name(self) -> str:
|
||||
file_name = self.temp_file.ResourceName
|
||||
if not file_name:
|
||||
raise RuntimeError("Temporary file not valid.")
|
||||
return file_name
|
||||
|
||||
def test_01(self):
|
||||
file_uri = self.get_temp_file_url()
|
||||
file_name = self.get_temp_file_name()
|
||||
print("Tempfile URL:", file_uri)
|
||||
print("Tempfile name:", file_name)
|
||||
self.assertTrue(
|
||||
file_uri.endswith(file_name.replace("\\", "/")),
|
||||
"FILE NAME AND URL DO NOT MATCH.",
|
||||
)
|
||||
|
||||
# write to the stream using the service.
|
||||
self.write_bytes_with_stream()
|
||||
|
||||
# check the result by reading from the service.
|
||||
self.temp_file.seek(0)
|
||||
read_data = self.read_bytes_with_stream()
|
||||
self.assertEqual(self.file_data, read_data, "Tempfile outputs false data!")
|
||||
|
||||
# check the result by reading from the file directly.
|
||||
read_data = self.read_directly_from_temp_file(file_uri)
|
||||
self.assertEqual(self.file_data, read_data, "Tempfile contains false data!")
|
||||
|
||||
# close the object(by closing input and output), check that the file
|
||||
# was removed.
|
||||
self.temp_file.RemoveFile = False
|
||||
# After tempfile is closed, file name cannot be got from a TempFile object.
|
||||
file_name = self.temp_file.ResourceName
|
||||
self.close_temp_file()
|
||||
self.assertTrue(
|
||||
self.file_access.exists(file_name), "TempFile mistakenly removed.",
|
||||
)
|
||||
|
||||
# Finally, cleanup this temp file.
|
||||
self.file_access.kill(file_name)
|
||||
|
||||
def test_02(self):
|
||||
self.write_bytes_with_stream()
|
||||
file_url = self.get_temp_file_url()
|
||||
# let the service not to remove the URL.
|
||||
self.temp_file.RemoveFile = False
|
||||
# close the tempfile by closing input and output.
|
||||
self.close_temp_file()
|
||||
# check that the file is still available.
|
||||
read_data = self.read_directly_from_temp_file(file_url)
|
||||
self.assertEqual(self.file_data, read_data, "Tempfile contains false data!")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
# vim: set shiftwidth=4 softtabstop=4 expandtab:
|
Loading…
x
Reference in New Issue
Block a user