# Copyright (c) Huawei Technologies Co., Ltd. 2026-2026. All rights reserved.
# OpenOLC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         `http://license.coscl.org.cn/MulanPSL2`
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

import os
import tempfile
import unittest
from olc.utils.file_utils import FileUtils


class TestFileUtils(unittest.TestCase):
    def setUp(self):
        # 创建临时文件用于测试
        self.temp_dir = tempfile.mkdtemp()
        
        # 创建properties文件
        self.properties_file = os.path.join(self.temp_dir, "test.properties")
        with open(self.properties_file, "w", encoding="utf-8") as f:
            f.write("# Test properties file\n")
            f.write("key1=value1\n")
            f.write("key2=value2\n")
        
        # 创建JSON文件
        self.json_file = os.path.join(self.temp_dir, "test.json")
        with open(self.json_file, "w", encoding="utf-8") as f:
            f.write('{"key1": "value1", "key2": "value2"}\n')
    
    def tearDown(self):
        # 清理临时文件
        for file_path in [self.properties_file, self.json_file]:
            if os.path.exists(file_path):
                os.remove(file_path)
        os.rmdir(self.temp_dir)
    
    def test_is_valid_path(self):
        # 测试有效路径
        self.assertTrue(FileUtils.is_valid_path("/valid/path/file.txt"))
        self.assertTrue(FileUtils.is_valid_path("C:\\valid\\path\\file.txt"))
        
        # 测试无效路径
        self.assertFalse(FileUtils.is_valid_path(""))
        self.assertFalse(FileUtils.is_valid_path("/invalid<path/file.txt"))
        self.assertFalse(FileUtils.is_valid_path("/invalid>path/file.txt"))
        self.assertFalse(FileUtils.is_valid_path("/invalid\"path/file.txt"))
        self.assertFalse(FileUtils.is_valid_path("/invalid|path/file.txt"))
        self.assertFalse(FileUtils.is_valid_path("/invalid?path/file.txt"))
        self.assertFalse(FileUtils.is_valid_path("/invalid*path/file.txt"))
    
    def test_exists(self):
        # 测试存在的文件
        self.assertTrue(FileUtils.exists(self.properties_file))
        
        # 测试不存在的文件
        non_existent_file = os.path.join(self.temp_dir, "non_existent.txt")
        self.assertFalse(FileUtils.exists(non_existent_file))
        
        # 测试目录
        self.assertFalse(FileUtils.exists(self.temp_dir))
    
    def test_read_properties_file(self):
        # 测试读取存在的properties文件
        props = FileUtils.read_properties_file(self.properties_file)
        self.assertEqual(props["key1"], "value1")
        self.assertEqual(props["key2"], "value2")
        
        # 测试读取不存在的properties文件
        non_existent_file = os.path.join(self.temp_dir, "non_existent.properties")
        props = FileUtils.read_properties_file(non_existent_file)
        self.assertEqual(props, {})

if __name__ == "__main__":
    unittest.main()