This repository was archived by the owner on Feb 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
133 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,6 @@ | |
|
||
/** | ||
* XML 处理工具类 | ||
* | ||
* <a href="https://blog.csdn.net/axman/article/details/420910">...</a> | ||
* | ||
* @author sp42 [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 104 additions & 9 deletions
113
aj-backend/aj-framework/aj-util/src/test/java/com/ajaxjs/util/TestXmlHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,112 @@ | ||
package com.ajaxjs.util; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import com.ajaxjs.util.io.Resources; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class TestXmlHelper { | ||
@Test | ||
public void test() { | ||
String xml = "C:\\project\\doctor\\WebContent\\META-INF\\context.xml"; | ||
Map<String, String> map = XmlHelper.nodeAsMap(xml, "//Resource[@name='jdbc/mys2ql']"); | ||
assertNotNull(map); | ||
} | ||
@Test | ||
public void testInitBuilder() { | ||
// 测试 initBuilder 方法是否能够返回非空的 DocumentBuilder 实例 | ||
DocumentBuilder builder = XmlHelper.initBuilder(); | ||
assertNotNull(builder); | ||
|
||
// 验证返回的 DocumentBuilder 是否是有效的实例 | ||
assertTrue(builder instanceof DocumentBuilder); | ||
|
||
// 测试 initBuilder 在异常情况下的行为,是否返回 null | ||
// 我们将通过反射禁用工厂来模拟 ParserConfigurationException | ||
try { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
Method method = DocumentBuilderFactory.class.getDeclaredMethod("setValidating", boolean.class); | ||
method.setAccessible(true); | ||
method.invoke(factory, true); // 设置为验证状态,这将导致 ParserConfigurationException | ||
|
||
DocumentBuilder invalidBuilder = factory.newDocumentBuilder(); | ||
System.out.println("Expected ParserConfigurationException to be thrown"); | ||
} catch (Exception e) { | ||
// 我们期望在调用 factory.newDocumentBuilder() 时捕获异常 | ||
assertTrue(e.getCause() instanceof ParserConfigurationException); | ||
} | ||
} | ||
|
||
private Consumer<Node> mockConsumer = mock(Consumer.class); | ||
; | ||
|
||
@Test | ||
public void testXPathWithValidXmlAndXPath() { | ||
String xpath = "/root/element"; | ||
// 执行测试方法 | ||
XmlHelper.xPath(Resources.getResourcesFromClasspath("test.xml"), xpath, mockConsumer); | ||
|
||
// 验证 Consumer 是否被正确调用 | ||
verify(mockConsumer, times(1)).accept(any(Node.class)); | ||
} | ||
|
||
@Test | ||
public void testXPathWithInvalidXPath() { | ||
String xpath = "/root/invalidElement"; | ||
|
||
// 执行测试方法,期待不抛出异常 | ||
XmlHelper.xPath(Resources.getResourcesFromClasspath("test.xml"), xpath, mockConsumer); | ||
|
||
// 由于 XPath 不匹配,Consumer 不应该被调用 | ||
verify(mockConsumer, never()).accept(any(Node.class)); | ||
} | ||
|
||
@Test | ||
public void testParseXML() { | ||
// 假设的 XML 内容 | ||
String xmlContent = "<root><child>Content</child></root>"; | ||
// 创建一个模拟的 BiConsumer | ||
BiConsumer<Node, NodeList> consumerMock = Mockito.mock(BiConsumer.class); | ||
// 创建模拟的节点和子节点列表 | ||
Node mockNode = Mockito.mock(Node.class); | ||
NodeList mockNodeList = Mockito.mock(NodeList.class); | ||
// 当调用 getLength() 方法时返回 1 | ||
when(mockNodeList.getLength()).thenReturn(1); | ||
// 当调用 item(0) 方法时返回 mockNode | ||
when(mockNodeList.item(0)).thenReturn(mockNode); | ||
|
||
// 验证 initBuilder() 方法至少被调用一次 | ||
Mockito.doReturn(mock(DocumentBuilder.class)).when(XmlHelper.class); | ||
XmlHelper.initBuilder(); | ||
|
||
// 调用待测试的 parseXML 方法 | ||
XmlHelper.parseXML(xmlContent, consumerMock); | ||
|
||
// 验证 consumerMock 被正确调用 | ||
verify(consumerMock, times(1)).accept(any(Node.class), any(NodeList.class)); | ||
} | ||
|
||
@Test | ||
public void testNodeAsMapWithValidXmlAndXPath() { | ||
String xpath = "/root/element"; | ||
Map<String, String> expectedMap = ObjectHelper.hashMap("attr1", "value1", "attr2", "value2"); | ||
|
||
Map<String, String> result = XmlHelper.nodeAsMap(Resources.getResourcesFromClasspath("test2.xml"), xpath); | ||
|
||
assertNotNull(result); | ||
assertEquals(expectedMap, result); | ||
} | ||
|
||
@Test | ||
public void testNodeAsMapWithInvalidXPath() { | ||
String xpath = "/root/invalidElement"; | ||
Map<String, String> result = XmlHelper.nodeAsMap(Resources.getResourcesFromClasspath("test2.xml"), xpath); | ||
assertNull(result); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<root> | ||
<element>Content</element> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<root><element attr1="value1" attr2="value2" /></root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters