bump product version to 7.6.3.2-android
[LibreOffice.git] / unotools / qa / python / test_tempfile.py
blob0d417dbbe9232ddc9471a9c4b70ebc5e5ea87e4d
1 #! /usr/bin/env python
2 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 import unittest
13 import uno
14 from org.libreoffice.unotest import UnoInProcess
17 class TestTempFile(unittest.TestCase):
18 """Test temporary file object created from com.sun.star.io.TempFile"""
20 @classmethod
21 def setUpClass(cls):
22 cls._uno = UnoInProcess()
23 cls._uno.setUp()
25 @classmethod
26 def tearDownClass(cls):
27 cls._uno.tearDown()
29 def setUp(self):
30 self.file_data = uno.ByteSequence(b"some data")
32 service_manager = self._uno.getContext().getServiceManager()
33 if service_manager is None:
34 raise RuntimeError("Cannot create service factory!")
36 try:
37 self.file_access = service_manager.createInstance("com.sun.star.ucb.SimpleFileAccess")
38 if self.file_access is None:
39 raise RuntimeError("Cannot get simple access!")
40 except Exception as e:
41 raise RuntimeError(f"Cannot get simple file access! {e}")
43 self.temp_file = service_manager.createInstance("com.sun.star.io.TempFile")
44 has_xtempfile_if = bool([
45 1 for type_info in self.temp_file.getTypes()
46 if type_info.typeName == "com.sun.star.io.XTempFile"
48 if not has_xtempfile_if:
49 raise RuntimeError("Cannot get XTempFile interface.")
51 def close_temp_file(self) -> None:
52 stream = self.temp_file.getOutputStream()
53 if stream is None:
54 raise RuntimeError("Cannot get output stream")
55 stream.closeOutput()
56 stream = self.temp_file.getInputStream()
57 if stream is None:
58 raise RuntimeError("Cannot get input stream")
59 stream.closeInput()
60 print("Tempfile closed successfully.")
62 def read_bytes_with_stream(self) -> uno.ByteSequence:
63 input_stream = self.temp_file.getInputStream()
64 if input_stream is None:
65 raise RuntimeError("Cannot get input stream from tempfile.")
66 nbytes, read_data = input_stream.readBytes(None, len(self.file_data))
67 print("Read", nbytes, "bytes from tempfile successfully.")
68 return read_data
70 def read_directly_from_temp_file(self, file_url: str) -> uno.ByteSequence:
71 print("Attempting to read directly from", file_url)
72 input_stream = self.file_access.openFileRead(file_url)
73 if input_stream is None:
74 raise RuntimeError("Cannot create input stream from URL.")
75 nbytes, read_data = input_stream.readBytes(None, len(self.file_data))
76 print("Read", nbytes, "bytes directly from tempfile successfully.")
77 return read_data
79 def write_bytes_with_stream(self) -> None:
80 output_stream = self.temp_file.getOutputStream()
81 if output_stream is None:
82 raise RuntimeError("Cannot get output stream.")
83 output_stream.writeBytes(self.file_data)
84 output_stream.flush()
85 print("Write", len(self.file_data), "bytes to tempfile successfully.")
87 def get_temp_file_url(self) -> str:
88 uri = self.temp_file.Uri
89 if not uri:
90 raise RuntimeError("Temporary file not valid.")
91 return uri
93 def get_temp_file_name(self) -> str:
94 file_name = self.temp_file.ResourceName
95 if not file_name:
96 raise RuntimeError("Temporary file not valid.")
97 return file_name
99 def test_01(self):
100 file_uri = self.get_temp_file_url()
101 file_name = self.get_temp_file_name()
102 print("Tempfile URL:", file_uri)
103 print("Tempfile name:", file_name)
104 self.assertTrue(
105 file_uri.endswith(file_name.replace("\\", "/")),
106 "FILE NAME AND URL DO NOT MATCH.",
109 # write to the stream using the service.
110 self.write_bytes_with_stream()
112 # check the result by reading from the service.
113 self.temp_file.seek(0)
114 read_data = self.read_bytes_with_stream()
115 self.assertEqual(self.file_data, read_data, "Tempfile outputs false data!")
117 # check the result by reading from the file directly.
118 read_data = self.read_directly_from_temp_file(file_uri)
119 self.assertEqual(self.file_data, read_data, "Tempfile contains false data!")
121 # close the object(by closing input and output), check that the file
122 # was removed.
123 self.temp_file.RemoveFile = False
124 # After tempfile is closed, file name cannot be got from a TempFile object.
125 file_name = self.temp_file.ResourceName
126 self.close_temp_file()
127 self.assertTrue(
128 self.file_access.exists(file_name), "TempFile mistakenly removed.",
131 # Finally, cleanup this temp file.
132 self.file_access.kill(file_name)
134 def test_02(self):
135 self.write_bytes_with_stream()
136 file_url = self.get_temp_file_url()
137 # let the service not to remove the URL.
138 self.temp_file.RemoveFile = False
139 # close the tempfile by closing input and output.
140 self.close_temp_file()
141 # check that the file is still available.
142 read_data = self.read_directly_from_temp_file(file_url)
143 self.assertEqual(self.file_data, read_data, "Tempfile contains false data!")
146 if __name__ == '__main__':
147 unittest.main()
149 # vim: set shiftwidth=4 softtabstop=4 expandtab: