3
3
// SPDX-License-Identifier: Apache-2.0
4
4
package org .lfenergy .compas .scl .auto .alignment ;
5
5
6
+ import javanet .staxutils .SimpleNamespaceContext ;
6
7
import org .lfenergy .compas .core .commons .ElementConverter ;
7
8
import org .w3c .dom .Element ;
9
+ import org .w3c .dom .NodeList ;
8
10
11
+ import javax .xml .xpath .XPathConstants ;
12
+ import javax .xml .xpath .XPathFactory ;
9
13
import java .io .*;
10
14
import java .nio .charset .StandardCharsets ;
11
15
12
- import static org .lfenergy .compas .scl .auto .alignment .SclAutoAlignmentConstants .SCL_ELEMENT_NAME ;
13
- import static org .lfenergy .compas .scl .auto .alignment .SclAutoAlignmentConstants .SCL_NS_URI ;
16
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
17
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
18
+ import static org .lfenergy .compas .scl .auto .alignment .SclAutoAlignmentConstants .*;
14
19
15
20
public final class TestUtil {
16
21
private TestUtil () {
17
22
// Only static methods.
18
23
}
19
24
20
- public static Element readSCLElement (String filename ) throws IOException {
21
- var sclData = readSCL (filename );
25
+ public static Element toElement (String sclData ) throws IOException {
22
26
var converter = new ElementConverter ();
27
+ try (var inputStream = new BufferedInputStream (
28
+ new ByteArrayInputStream (sclData .getBytes (StandardCharsets .UTF_8 )))) {
29
+ return converter .convertToElement (inputStream , SCL_ELEMENT_NAME , SCL_NS_URI );
30
+ }
31
+ }
23
32
24
- return converter .convertToElement (new BufferedInputStream (
25
- new ByteArrayInputStream (sclData .getBytes (StandardCharsets .UTF_8 ))), SCL_ELEMENT_NAME , SCL_NS_URI );
33
+ public static Element readSCLElement (String filename ) throws IOException {
34
+ var sclData = readSCL (filename );
35
+ return toElement (sclData );
26
36
}
27
37
28
38
public static String readSCL (String filename ) throws IOException {
29
39
return readFile ("/scl/" + filename );
30
40
}
31
41
32
42
public static String readFile (String filename ) throws IOException {
33
- var inputStream = TestUtil .class .getResourceAsStream (filename );
34
- assert inputStream != null ;
43
+ try ( var inputStream = TestUtil .class .getResourceAsStream (filename )) {
44
+ assert inputStream != null ;
35
45
36
- return new String (inputStream .readAllBytes ());
46
+ return new String (inputStream .readAllBytes ());
47
+ }
37
48
}
38
49
39
- @ Deprecated
40
50
public static void writeFile (String fileName , String data ) {
41
51
var file = new File ("target" , fileName );
42
52
try (var fw = new OutputStreamWriter (new FileOutputStream (file ), StandardCharsets .UTF_8 )) {
@@ -45,4 +55,20 @@ public static void writeFile(String fileName, String data) {
45
55
throw new UncheckedIOException (e );
46
56
}
47
57
}
58
+
59
+ public static void assertXYCoordinates (Element rootElement , String xPathExpression , Integer x , Integer y ) throws Exception {
60
+ var xPath = XPathFactory .newInstance ().newXPath ();
61
+
62
+ SimpleNamespaceContext nsCtx = new SimpleNamespaceContext ();
63
+ nsCtx .setPrefix ("scl" , SCL_NS_URI );
64
+ xPath .setNamespaceContext (nsCtx );
65
+
66
+ var nodeList = (NodeList ) xPath .compile (xPathExpression ).evaluate (rootElement .getOwnerDocument (), XPathConstants .NODESET );
67
+ assertEquals (1 , nodeList .getLength ());
68
+
69
+ var element = (Element ) nodeList .item (0 );
70
+ assertNotNull (element );
71
+ assertEquals (x .toString (), element .getAttributeNS (SCLXY_NS_URI , "x" ));
72
+ assertEquals (y .toString (), element .getAttributeNS (SCLXY_NS_URI , "y" ));
73
+ }
48
74
}
0 commit comments