From 42b2ea820773478ca6018093937e2611635541d4 Mon Sep 17 00:00:00 2001 From: zhiheng123 <903292776@qq.com> Date: Sat, 16 Nov 2024 22:01:51 +0800 Subject: [PATCH] refactor: move test xml to resources dir Signed-off-by: zhiheng123 <903292776@qq.com> --- .../protocol/mtconnect/common/XmlUtil.java | 6 + .../server/MTConnectAssetsDecodeTest.java | 25 +--- .../server/MTConnectDevicesDecodeTest.java | 120 +----------------- .../server/MTConnectErrorDecodeTest.java | 22 +--- .../mtconnect/server/MTConnectFileUtil.java | 9 ++ .../server/MTConnectStreamsDecodeTest.java | 41 +----- .../src/test/resources/mtconnect_assets.xml | 16 +++ .../src/test/resources/mtconnect_devices.xml | 112 ++++++++++++++++ .../src/test/resources/mtconnect_error.xml | 13 ++ .../src/test/resources/mtconnect_streams.xml | 35 +++++ 10 files changed, 208 insertions(+), 191 deletions(-) create mode 100644 mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectFileUtil.java create mode 100644 mtconnect-server/src/test/resources/mtconnect_assets.xml create mode 100644 mtconnect-server/src/test/resources/mtconnect_devices.xml create mode 100644 mtconnect-server/src/test/resources/mtconnect_error.xml create mode 100644 mtconnect-server/src/test/resources/mtconnect_streams.xml diff --git a/mtconnect-common/src/main/java/io/github/protocol/mtconnect/common/XmlUtil.java b/mtconnect-common/src/main/java/io/github/protocol/mtconnect/common/XmlUtil.java index a192bb9..48d711a 100644 --- a/mtconnect-common/src/main/java/io/github/protocol/mtconnect/common/XmlUtil.java +++ b/mtconnect-common/src/main/java/io/github/protocol/mtconnect/common/XmlUtil.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import java.io.InputStream; + public class XmlUtil { private static final XmlMapper xmlMapper = new XmlMapper(); @@ -10,6 +12,10 @@ public static T fromXml(String xml, Class clazz) throws Exception { return xmlMapper.readValue(xml, clazz); } + public static T fromXml(InputStream xml, Class clazz) throws Exception { + return xmlMapper.readValue(xml, clazz); + } + public static String toXml(Object obj) throws Exception { return xmlMapper.writeValueAsString(obj); } diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java index 276bb03..8b40fb3 100644 --- a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java @@ -6,30 +6,15 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.InputStream; + class MTConnectAssetsDecodeTest { @Test void testMTConnectAssetsDecode() throws Exception { - String xml = """ - - - -
- - - - Cutting Tool - ... - ... - - - - """; - - MTConnectAssets mtConnectAssets = XmlUtil.fromXml(xml, MTConnectAssets.class); + + InputStream xmlInputStream = MTConnectFileUtil.readFile("mtconnect_assets.xml"); + MTConnectAssets mtConnectAssets = XmlUtil.fromXml(xmlInputStream, MTConnectAssets.class); // Validate MTConnectAssets object Assertions.assertNotNull(mtConnectAssets); diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java index 94287e0..23355db 100644 --- a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java @@ -6,126 +6,14 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.InputStream; + class MTConnectDevicesDecodeTest { @Test void testMTConnectDevicesDecode() throws Exception { - String xml = """ - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SPINDLE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - """; - MTConnectDevices mtConnectDevices = XmlUtil.fromXml(xml, MTConnectDevices.class); + InputStream xmlInputStream = MTConnectFileUtil.readFile("mtconnect_devices.xml"); + MTConnectDevices mtConnectDevices = XmlUtil.fromXml(xmlInputStream, MTConnectDevices.class); Assertions.assertNotNull(mtConnectDevices); Assertions.assertNotNull(mtConnectDevices.getHeader()); diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectErrorDecodeTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectErrorDecodeTest.java index 0a37234..ff1830a 100644 --- a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectErrorDecodeTest.java +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectErrorDecodeTest.java @@ -5,28 +5,14 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.InputStream; + class MTConnectErrorDecodeTest { @Test void testMTConnectErrorDecode() throws Exception { - // The raw XML string - String xml = """ - - - -
- - - Argument was out of range - Bad path - - - """; - - MTConnectError mtConnectError = XmlUtil.fromXml(xml, MTConnectError.class); + InputStream xmlInputStream = MTConnectFileUtil.readFile("mtconnect_error.xml"); + MTConnectError mtConnectError = XmlUtil.fromXml(xmlInputStream, MTConnectError.class); // Validate MTConnectError object Assertions.assertNotNull(mtConnectError); diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectFileUtil.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectFileUtil.java new file mode 100644 index 0000000..5b3ac5d --- /dev/null +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectFileUtil.java @@ -0,0 +1,9 @@ +package io.github.protocol.mtconnect.server; + +import java.io.InputStream; + +public class MTConnectFileUtil { + public static InputStream readFile(String filePath) { + return MTConnectFileUtil.class.getClassLoader().getResourceAsStream(filePath); + } +} diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectStreamsDecodeTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectStreamsDecodeTest.java index 5014520..e82e7ff 100644 --- a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectStreamsDecodeTest.java +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectStreamsDecodeTest.java @@ -5,49 +5,16 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.InputStream; + class MTConnectStreamsDecodeTest { @Test void testMTConnectStreamsParsing() throws Exception { - String xml = """ - - - -
- - - - - - AVAILABLE - - - - - - RESET - - - - - - - - ACTIVE - - - - - - """; + InputStream xmlInputStream = MTConnectFileUtil.readFile("mtconnect_streams.xml"); - MTConnectStreams mtConnectStreams = XmlUtil.fromXml(xml, MTConnectStreams.class); + MTConnectStreams mtConnectStreams = XmlUtil.fromXml(xmlInputStream, MTConnectStreams.class); // Validate MTConnectStreams object Assertions.assertNotNull(mtConnectStreams); diff --git a/mtconnect-server/src/test/resources/mtconnect_assets.xml b/mtconnect-server/src/test/resources/mtconnect_assets.xml new file mode 100644 index 0000000..d302b34 --- /dev/null +++ b/mtconnect-server/src/test/resources/mtconnect_assets.xml @@ -0,0 +1,16 @@ + + + +
+ + + + Cutting Tool + ... + ... + + + diff --git a/mtconnect-server/src/test/resources/mtconnect_devices.xml b/mtconnect-server/src/test/resources/mtconnect_devices.xml new file mode 100644 index 0000000..68e9a1f --- /dev/null +++ b/mtconnect-server/src/test/resources/mtconnect_devices.xml @@ -0,0 +1,112 @@ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + SPINDLE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mtconnect-server/src/test/resources/mtconnect_error.xml b/mtconnect-server/src/test/resources/mtconnect_error.xml new file mode 100644 index 0000000..9b71534 --- /dev/null +++ b/mtconnect-server/src/test/resources/mtconnect_error.xml @@ -0,0 +1,13 @@ + + + +
+ + + Argument was out of range + Bad path + + diff --git a/mtconnect-server/src/test/resources/mtconnect_streams.xml b/mtconnect-server/src/test/resources/mtconnect_streams.xml new file mode 100644 index 0000000..05aa321 --- /dev/null +++ b/mtconnect-server/src/test/resources/mtconnect_streams.xml @@ -0,0 +1,35 @@ + + + +
+ + + + + + AVAILABLE + + + + + + RESET + + + + + + + + + ACTIVE + + + + +