Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit

Permalink
ci
Browse files Browse the repository at this point in the history
  • Loading branch information
sp42 committed Apr 15, 2024
1 parent 5751048 commit 20c9b28
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 23 deletions.
4 changes: 2 additions & 2 deletions aj-backend/aj-framework/aj-common-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<relativePath />
</parent>
<artifactId>aj-common-parent</artifactId>
<version>1.8</version>
<version>1.9</version>
<packaging>pom</packaging>
<description>常见项目的依赖</description>

Expand Down Expand Up @@ -148,7 +148,7 @@
<dependency>
<groupId>com.ajaxjs</groupId>
<artifactId>ajaxjs-framework</artifactId>
<version>1.1.8</version>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>
6 changes: 3 additions & 3 deletions aj-backend/aj-framework/aj-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ajaxjs-framework</artifactId>
<version>1.1.8</version>
<version>1.2.0</version>
<name>aj-framework</name>
<description>轻量级的开发框架</description>
<packaging>jar</packaging>
Expand Down Expand Up @@ -38,14 +38,14 @@
<dependency>
<groupId>com.ajaxjs</groupId>
<artifactId>ajaxjs-util</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
</dependency>

<!-- 本项目不依赖 net,但为方便其他项目故集成 -->
<dependency>
<groupId>com.ajaxjs</groupId>
<artifactId>ajaxjs-net</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions aj-backend/aj-framework/aj-net/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>ajaxjs-net</artifactId>
<name>aj-net</name>
<version>1.0.2</version>
<version>1.0.3</version>
<description>网络组件</description>
<packaging>jar</packaging>

Expand All @@ -19,7 +19,7 @@
<dependency>
<groupId>com.ajaxjs</groupId>
<artifactId>ajaxjs-util</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
</dependency>

<!-- 编译为 jar 包时依赖的 -->
Expand Down
2 changes: 1 addition & 1 deletion aj-backend/aj-framework/aj-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>ajaxjs-util</artifactId>
<description>Small Java Utils Library with many powerful components</description>
<version>1.1.3</version>
<version>1.1.4</version>
<packaging>jar</packaging>

<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

/**
* XML 处理工具类
*
* <a href="https://blog.csdn.net/axman/article/details/420910">...</a>
*
* @author sp42 [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@Slf4j
public class Resources {
/**
* 获取 Classpath 根目录下的资源文件
* 获取 Classpath 根目录下的资源文件的路径
*
* @param resource 文件名称,输入空字符串这返回 Classpath 根目录
* @param isDecode 是否解码
Expand Down Expand Up @@ -179,7 +179,7 @@ static void listResourceFile() {
}

/**
* 执行 OS 命令
* 执行 OS 命令
*
* @param cmd 命令
* @param fn 回调函数
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,22 @@ public static String byteStream2string_Charset(InputStream in, Charset encode) {
return result.toString();
}

/**
* 从输入流中读取数据,并对每行数据应用提供的消费函数。
*
* @param in 输入流,从中读取数据。
* @param fn 消费函数,用于处理读取到的每行数据。
*/
public static void read(InputStream in, Consumer<String> fn) {
read(new InputStreamReader(in, StandardCharsets.UTF_8), fn);
}

/**
* 从 InputStreamReader 中读取数据,并逐行消费。
*
* @param inReader 输入流读取器,用于读取数据。
* @param fn 消费函数,接收一行数据作为参数,对每行数据进行处理。
*/
public static void read(InputStreamReader inReader, Consumer<String> fn) {
try (
/*
Expand Down Expand Up @@ -296,5 +308,4 @@ public static byte[] charToByte(char[] chars) {

return bytes;
}

}
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);
}
}
4 changes: 4 additions & 0 deletions aj-backend/aj-framework/aj-util/src/test/resources/test.xml
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root><element attr1="value1" attr2="value2" /></root>
2 changes: 1 addition & 1 deletion aj-backend/aj-iam/aj-iam-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.ajaxjs</groupId>
<artifactId>aj-common-parent</artifactId>
<version>1.8</version>
<version>1.9</version>
<relativePath/>
</parent>
<artifactId>aj-iam-server</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion aj-backend/aj-playground/aj-s3client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.ajaxjs</groupId>
<artifactId>ajaxjs-net</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</dependency>

<dependency>
Expand Down

0 comments on commit 20c9b28

Please sign in to comment.