1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "xml_parser.h"
17 #include "gtest/gtest.h"
18
19 using namespace std;
20 using namespace testing::ext;
21
22 namespace OHOS {
23 namespace Media {
24 namespace Plugins {
25 namespace HttpPlugin {
26 class XmlParserUnitTest : public testing::Test {
27 public:
SetUpTestCase(void)28 static void SetUpTestCase(void) {}
TearDownTestCase(void)29 static void TearDownTestCase(void) {}
30 void SetUp(void);
31 void TearDown(void);
32 protected:
33 std::shared_ptr<XmlParser> xmlParser_;
34 };
35
SetUp(void)36 void XmlParserUnitTest::SetUp(void)
37 {
38 xmlParser_ = std::make_shared<XmlParser>();
39 }
40
TearDown(void)41 void XmlParserUnitTest::TearDown(void)
42 {
43 if (xmlParser_ != nullptr) {
44 xmlParser_->DestroyDoc();
45 xmlParser_.reset();
46 }
47 }
48
49 /**
50 * @tc.name : Test ParserFromFile API
51 * @tc.number: ParserFromFile_001
52 * @tc.desc : Test filePath.empty() == true
53 */
54 HWTEST_F(XmlParserUnitTest, ParserFromFile_001, TestSize.Level1)
55 {
56 ASSERT_NE(nullptr, xmlParser_);
57 std::string mockStr;
58 ASSERT_EQ(-1, xmlParser_->ParseFromFile(mockStr));
59 }
60
61 /**
62 * @tc.name : Test GetRootElement API
63 * @tc.number: GetRootElement_001
64 * @tc.desc : Test xmlDocPtr_ == nullptr
65 */
66 HWTEST_F(XmlParserUnitTest, GetRootElement_001, TestSize.Level1)
67 {
68 ASSERT_NE(nullptr, xmlParser_);
69 ASSERT_EQ(nullptr, xmlParser_->xmlDocPtr_);
70 ASSERT_EQ(nullptr, xmlParser_->GetRootElement());
71 }
72
73 /**
74 * @tc.name : Test GetRootElement API
75 * @tc.number: GetRootElement_002
76 * @tc.desc : Test xmlDocPtr_ != nullptr && rootNode == nullptr
77 */
78 HWTEST_F(XmlParserUnitTest, GetRootElement_002, TestSize.Level1)
79 {
80 ASSERT_NE(nullptr, xmlParser_);
81 xmlDoc mockDoc;
82 xmlParser_->xmlDocPtr_ = &mockDoc;
83 ASSERT_EQ(nullptr, xmlParser_->GetRootElement());
84 xmlParser_->xmlDocPtr_ = nullptr;
85 }
86
87 /**
88 * @tc.name : Test GetAttribute API
89 * @tc.number: GetAttribute_001
90 * @tc.desc : Test element == nullptr
91 */
92 HWTEST_F(XmlParserUnitTest, GetAttribute_001, TestSize.Level1)
93 {
94 ASSERT_NE(nullptr, xmlParser_);
95 std::shared_ptr<XmlElement> element;
96 std::string value;
97 ASSERT_EQ(-1, xmlParser_->GetAttribute(element, "", value));
98 }
99 }
100 }
101 }
102 }