Skip to content

Commit 4793ba9

Browse files
authored
refactor: move test xml to resources dir (#19)
Signed-off-by: zhiheng123 <[email protected]>
1 parent 805663d commit 4793ba9

File tree

10 files changed

+208
-191
lines changed

10 files changed

+208
-191
lines changed

mtconnect-common/src/main/java/io/github/protocol/mtconnect/common/XmlUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
44

5+
import java.io.InputStream;
6+
57
public class XmlUtil {
68

79
private static final XmlMapper xmlMapper = new XmlMapper();
@@ -10,6 +12,10 @@ public static <T> T fromXml(String xml, Class<T> clazz) throws Exception {
1012
return xmlMapper.readValue(xml, clazz);
1113
}
1214

15+
public static <T> T fromXml(InputStream xml, Class<T> clazz) throws Exception {
16+
return xmlMapper.readValue(xml, clazz);
17+
}
18+
1319
public static String toXml(Object obj) throws Exception {
1420
return xmlMapper.writeValueAsString(obj);
1521
}

mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,15 @@
66
import org.junit.jupiter.api.Assertions;
77
import org.junit.jupiter.api.Test;
88

9+
import java.io.InputStream;
10+
911
class MTConnectAssetsDecodeTest {
1012

1113
@Test
1214
void testMTConnectAssetsDecode() throws Exception {
13-
String xml = """
14-
<?xml version="1.0" encoding="UTF-8"?>
15-
<MTConnectAssets xmlns="urn:mtconnect.org:MTConnectAssets:1.2"
16-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17-
xsi:schemaLocation="urn:mtconnect.org:MTConnectAssets:1.2 ../MTConnectAssets_1.2.xsd">
18-
19-
<Header creationTime="2001-12-17T09:30:47Z" sender="localhost"
20-
version="1.2" bufferSize="131000" instanceId="1" />
21-
22-
<Assets>
23-
<CuttingTool serialNumber="1234" timestamp="2001-12-17T09:30:47Z" assetId="1234-112233">
24-
<Description>Cutting Tool</Description>
25-
<ToolDefinition>...</ToolDefinition>
26-
<ToolLifeCycle deviceUuid="1222" toolId="1234">...</ToolLifeCycle>
27-
</CuttingTool>
28-
</Assets>
29-
</MTConnectAssets>
30-
""";
31-
32-
MTConnectAssets mtConnectAssets = XmlUtil.fromXml(xml, MTConnectAssets.class);
15+
16+
InputStream xmlInputStream = MTConnectFileUtil.readFile("mtconnect_assets.xml");
17+
MTConnectAssets mtConnectAssets = XmlUtil.fromXml(xmlInputStream, MTConnectAssets.class);
3318

3419
// Validate MTConnectAssets object
3520
Assertions.assertNotNull(mtConnectAssets);

mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java

Lines changed: 4 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -6,126 +6,14 @@
66
import org.junit.jupiter.api.Assertions;
77
import org.junit.jupiter.api.Test;
88

9+
import java.io.InputStream;
10+
911
class MTConnectDevicesDecodeTest {
1012
@Test
1113
void testMTConnectDevicesDecode() throws Exception {
12-
String xml = """
13-
<?xml version="1.0" encoding="UTF-8"?>
14-
<MTConnectDevices xmlns:m="urn:mtconnect.org:MTConnectDevices:1.1"
15-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16-
xmlns="urn:mtconnect.org:MTConnectDevices:1.1"
17-
xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:1.1
18-
http://www.mtconnect.org/schemas/MTConnectDevices_1.1.xsd">
19-
20-
<Header creationTime="2010-03-13T08:02:38+00:00" sender="localhost"
21-
instanceId="1268463594" bufferSize="131072" version="1.1" />
22-
23-
<Devices>
24-
<Device id="dev" name="VMC-4Axis" uuid="XXX111">
25-
<DataItems>
26-
<DataItem category="EVENT" id="avail" type="AVAILABILITY" />
27-
</DataItems>
28-
<Components>
29-
<Axes id="axes" name="axes">
30-
<Components>
31-
<Linear id="x" name="X">
32-
<DataItems>
33-
<DataItem category="SAMPLE" id="Xact" nativeUnits="MILLIMETER"
34-
subType="ACTUAL" type="POSITION" units="MILLIMETER" />
35-
<DataItem category="SAMPLE" id="Xload" nativeUnits="PERCENT"
36-
type="LOAD" units="PERCENT" />
37-
<DataItem category="CONDITION" id="Xtravel" type="POSITION" />
38-
<DataItem category="CONDITION" id="Xovertemp" type="TEMPERATURE" />
39-
<DataItem category="CONDITION" id="Xservo" type="ACTUATOR" />
40-
</DataItems>
41-
</Linear>
42-
<Linear id="y" name="Y">
43-
<DataItems>
44-
<DataItem category="SAMPLE" id="Yact" nativeUnits="MILLIMETER"
45-
subType="ACTUAL" type="POSITION" units="MILLIMETER" />
46-
<DataItem category="SAMPLE" id="Yload" nativeUnits="PERCENT"
47-
type="LOAD" units="PERCENT" />
48-
<DataItem category="CONDITION" id="Ytravel" type="POSITION" />
49-
<DataItem category="CONDITION" id="Yovertemp" type="TEMPERATURE" />
50-
<DataItem category="CONDITION" id="Yservo" type="ACTUATOR" />
51-
</DataItems>
52-
</Linear>
53-
<Linear id="z" name="Z">
54-
<DataItems>
55-
<DataItem category="SAMPLE" id="Zact" nativeUnits="MILLIMETER"
56-
subType="ACTUAL" type="POSITION" units="MILLIMETER" />
57-
<DataItem category="SAMPLE" id="Zload" nativeUnits="PERCENT"
58-
type="LOAD" units="PERCENT" />
59-
<DataItem category="CONDITION" id="Ztravel" type="POSITION" />
60-
<DataItem category="CONDITION" id="Zovertemp" type="TEMPERATURE" />
61-
<DataItem category="CONDITION" id="Zservo" type="ACTUATOR" />
62-
</DataItems>
63-
</Linear>
64-
<Rotary id="a" name="A">
65-
<DataItems>
66-
<DataItem category="SAMPLE" id="Aact" nativeUnits="DEGREE"
67-
subType="ACTUAL" type="ANGLE" units="DEGREE" />
68-
<DataItem category="SAMPLE" id="Aload" nativeUnits="PERCENT"
69-
type="LOAD" units="PERCENT" />
70-
<DataItem category="CONDITION" id="Atravel" type="POSITION" />
71-
<DataItem category="CONDITION" id="Aovertemp" type="TEMPERATURE" />
72-
<DataItem category="CONDITION" id="Aservo" type="ACTUATOR" />
73-
</DataItems>
74-
</Rotary>
75-
<Rotary id="c" name="C" nativeName="S1">
76-
<DataItems>
77-
<DataItem category="SAMPLE" id="S1speed" nativeUnits="REVOLUTION/MINUTE"
78-
type="SPINDLE_SPEED" units="REVOLUTION/MINUTE" />
79-
<DataItem category="EVENT" id="S1mode" type="ROTARY_MODE">
80-
<Constraints>
81-
<Value>SPINDLE</Value>
82-
</Constraints>
83-
</DataItem>
84-
<DataItem category="SAMPLE" id="S1load" nativeUnits="PERCENT"
85-
type="LOAD" units="PERCENT" />
86-
<DataItem category="CONDITION" id="spindle" type="SYSTEM" />
87-
</DataItems>
88-
</Rotary>
89-
</Components>
90-
</Axes>
91-
<Controller id="cont" name="controller">
92-
<DataItems>
93-
<DataItem category="CONDITION" id="logic" type="LOGIC_PROGRAM" />
94-
<DataItem category="EVENT" id="estop" type="EMERGENCY_STOP" />
95-
<DataItem category="CONDITION" id="servo" type="ACTUATOR" />
96-
<DataItem category="EVENT" id="message" type="MESSAGE" />
97-
<DataItem category="CONDITION" id="comms" type="COMMUNICATIONS" />
98-
</DataItems>
99-
<Components>
100-
<Path id="path" name="path">
101-
<DataItems>
102-
<DataItem category="SAMPLE" id="SspeedOvr" nativeUnits="PERCENT"
103-
subType="OVERRIDE" type="SPINDLE_SPEED" units="PERCENT" />
104-
<DataItem category="EVENT" id="block" type="BLOCK" />
105-
<DataItem category="EVENT" id="execution" type="EXECUTION" />
106-
<DataItem category="EVENT" id="program" type="PROGRAM" />
107-
<DataItem category="SAMPLE" id="path_feedrate" nativeUnits="MILLIMETER/SECOND"
108-
type="PATH_FEEDRATE" units="MILLIMETER/SECOND" />
109-
<DataItem category="EVENT" id="mode" type="CONTROLLER_MODE" />
110-
<DataItem category="EVENT" id="line" type="LINE" />
111-
<DataItem category="SAMPLE" id="path_pos" nativeUnits="MILLIMETER_3D"
112-
subType="ACTUAL" type="PATH_POSITION" units="MILLIMETER_3D" />
113-
<DataItem category="SAMPLE" id="probe" nativeUnits="MILLIMETER_3D"
114-
subType="PROBE" type="PATH_POSITION" units="MILLIMETER_3D" />
115-
<DataItem category="EVENT" id="part" type="PART_ID" />
116-
<DataItem category="CONDITION" id="motion" type="MOTION_PROGRAM" />
117-
<DataItem category="CONDITION" id="system" type="SYSTEM" />
118-
</DataItems>
119-
</Path>
120-
</Components>
121-
</Controller>
122-
</Components>
123-
</Device>
124-
</Devices>
125-
</MTConnectDevices>
126-
""";
12714

128-
MTConnectDevices mtConnectDevices = XmlUtil.fromXml(xml, MTConnectDevices.class);
15+
InputStream xmlInputStream = MTConnectFileUtil.readFile("mtconnect_devices.xml");
16+
MTConnectDevices mtConnectDevices = XmlUtil.fromXml(xmlInputStream, MTConnectDevices.class);
12917

13018
Assertions.assertNotNull(mtConnectDevices);
13119
Assertions.assertNotNull(mtConnectDevices.getHeader());

mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectErrorDecodeTest.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,14 @@
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.Test;
77

8+
import java.io.InputStream;
9+
810
class MTConnectErrorDecodeTest {
911

1012
@Test
1113
void testMTConnectErrorDecode() throws Exception {
12-
// The raw XML string
13-
String xml = """
14-
<?xml version="1.0" encoding="UTF-8"?>
15-
<MTConnectError xmlns="urn:mtconnect.org:MTConnectError:1.1"
16-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17-
xsi:schemaLocation="urn:mtconnect.org:MTConnectError:1.1 http://www.mtconnect.org/schemas/MTConnectError_1.1.xsd">
18-
19-
<Header creationTime="2010-03-12T12:33:01" sender="localhost"
20-
version="1.1" bufferSize="131072" instanceId="1268463594" />
21-
22-
<Errors>
23-
<Error errorCode="OUT_OF_RANGE">Argument was out of range</Error>
24-
<Error errorCode="INVALID_XPATH">Bad path</Error>
25-
</Errors>
26-
</MTConnectError>
27-
""";
28-
29-
MTConnectError mtConnectError = XmlUtil.fromXml(xml, MTConnectError.class);
14+
InputStream xmlInputStream = MTConnectFileUtil.readFile("mtconnect_error.xml");
15+
MTConnectError mtConnectError = XmlUtil.fromXml(xmlInputStream, MTConnectError.class);
3016

3117
// Validate MTConnectError object
3218
Assertions.assertNotNull(mtConnectError);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.protocol.mtconnect.server;
2+
3+
import java.io.InputStream;
4+
5+
public class MTConnectFileUtil {
6+
public static InputStream readFile(String filePath) {
7+
return MTConnectFileUtil.class.getClassLoader().getResourceAsStream(filePath);
8+
}
9+
}

mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectStreamsDecodeTest.java

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,16 @@
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.Test;
77

8+
import java.io.InputStream;
9+
810
class MTConnectStreamsDecodeTest {
911

1012
@Test
1113
void testMTConnectStreamsParsing() throws Exception {
12-
String xml = """
13-
<?xml version="1.0" encoding="UTF-8"?>
14-
<MTConnectStreams xmlns:m="urn:mtconnect.org:MTConnectStreams:1.1"
15-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16-
xmlns="urn:mtconnect.org:MTConnectStreams:1.1"
17-
xsi:schemaLocation="urn:mtconnect.org:MTConnectStreams:1.1 http://www.mtconnect.org/schemas/MTConnectStreams_1.1.xsd">
18-
19-
<Header creationTime="2010-04-06T06:53:34+00:00" sender="localhost"
20-
instanceId="1270534765" bufferSize="16" version="1.1" nextSequence="19"
21-
firstSequence="3" lastSequence="18" />
22-
23-
<Streams>
24-
<DeviceStream name="minimal" uuid="1">
25-
<ComponentStream component="Device" name="minimal" componentId="d">
26-
<Events>
27-
<Availability dataItemId="avail" sequence="5" timestamp="2010-04-06T06:19:35.153141">AVAILABLE</Availability>
28-
</Events>
29-
</ComponentStream>
30-
31-
<ComponentStream component="Controller" name="controller" componentId="c1">
32-
<Events>
33-
<EmergencyStop dataItemId="estop" sequence="9" timestamp="2010-04-06T06:20:05.153230">RESET</EmergencyStop>
34-
</Events>
35-
<Condition>
36-
<Normal dataItemId="system" sequence="13" timestamp="2010-04-06T06:21:35.153784" type="SYSTEM" />
37-
</Condition>
38-
</ComponentStream>
3914

40-
<ComponentStream component="Path" name="path" componentId="p1">
41-
<Events>
42-
<Execution dataItemId="execution" sequence="14" timestamp="2010-04-06T06:22:05.153741">ACTIVE</Execution>
43-
</Events>
44-
</ComponentStream>
45-
</DeviceStream>
46-
</Streams>
47-
</MTConnectStreams>
48-
""";
15+
InputStream xmlInputStream = MTConnectFileUtil.readFile("mtconnect_streams.xml");
4916

50-
MTConnectStreams mtConnectStreams = XmlUtil.fromXml(xml, MTConnectStreams.class);
17+
MTConnectStreams mtConnectStreams = XmlUtil.fromXml(xmlInputStream, MTConnectStreams.class);
5118

5219
// Validate MTConnectStreams object
5320
Assertions.assertNotNull(mtConnectStreams);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<MTConnectAssets xmlns="urn:mtconnect.org:MTConnectAssets:1.2"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="urn:mtconnect.org:MTConnectAssets:1.2 ../MTConnectAssets_1.2.xsd">
5+
6+
<Header creationTime="2001-12-17T09:30:47Z" sender="localhost"
7+
version="1.2" bufferSize="131000" instanceId="1" />
8+
9+
<Assets>
10+
<CuttingTool serialNumber="1234" timestamp="2001-12-17T09:30:47Z" assetId="1234-112233">
11+
<Description>Cutting Tool</Description>
12+
<ToolDefinition>...</ToolDefinition>
13+
<ToolLifeCycle deviceUuid="1222" toolId="1234">...</ToolLifeCycle>
14+
</CuttingTool>
15+
</Assets>
16+
</MTConnectAssets>

0 commit comments

Comments
 (0)