1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.io.euclidean.threed.txt;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.InputStream;
21 import java.nio.charset.Charset;
22 import java.nio.charset.StandardCharsets;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.apache.commons.geometry.euclidean.threed.Vector3D;
27 import org.apache.commons.geometry.io.core.input.StreamGeometryInput;
28 import org.apache.commons.geometry.io.core.test.CloseCountInputStream;
29 import org.apache.commons.geometry.io.euclidean.EuclideanIOTestUtils;
30 import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
31 import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
32 import org.apache.commons.geometry.io.euclidean.threed.GeometryFormat3D;
33 import org.junit.jupiter.api.Assertions;
34 import org.junit.jupiter.api.Test;
35
36 class TextBoundaryReadHandler3DTest {
37
38 private static final double TEST_EPS = 1e-10;
39
40 private final TextBoundaryReadHandler3D handler = new TextBoundaryReadHandler3D();
41
42 @Test
43 void testProperties() {
44
45 Assertions.assertEquals(GeometryFormat3D.TXT, handler.getFormat());
46 Assertions.assertEquals(StandardCharsets.UTF_8, handler.getDefaultCharset());
47 }
48
49 @Test
50 void testFacetDefinitionReader() {
51
52 final InputStream in = input("0 0 0; 1 1 0; 0 1 0", StandardCharsets.UTF_8);
53
54
55 final FacetDefinitionReader reader = handler.facetDefinitionReader(new StreamGeometryInput(in));
56
57
58 final List<FacetDefinition> facets = EuclideanIOTestUtils.readAll(reader);
59
60 Assertions.assertEquals(1, facets.size());
61 EuclideanIOTestUtils.assertFacetVertices(facets.get(0),
62 Arrays.asList(Vector3D.ZERO, Vector3D.of(1, 1, 0), Vector3D.of(0, 1, 0)), TEST_EPS);
63 }
64
65 @Test
66 void testFacetDefinitionReader_usesInputCharset() {
67
68 final InputStream in = input("0 0 0; 1 1 0; 0 1 0", StandardCharsets.UTF_16);
69
70
71 final FacetDefinitionReader reader = handler.facetDefinitionReader(new StreamGeometryInput(in, null, StandardCharsets.UTF_16));
72
73
74 final List<FacetDefinition> facets = EuclideanIOTestUtils.readAll(reader);
75
76 Assertions.assertEquals(1, facets.size());
77 EuclideanIOTestUtils.assertFacetVertices(facets.get(0),
78 Arrays.asList(Vector3D.ZERO, Vector3D.of(1, 1, 0), Vector3D.of(0, 1, 0)), TEST_EPS);
79 }
80
81 @Test
82 void testFacetDefinitionReader_setDefaultCharset() {
83
84 handler.setDefaultCharset(StandardCharsets.UTF_16);
85 final InputStream in = input("0 0 0; 1 1 0; 0 1 0", StandardCharsets.UTF_16);
86
87
88 final FacetDefinitionReader reader = handler.facetDefinitionReader(new StreamGeometryInput(in));
89
90
91 final List<FacetDefinition> facets = EuclideanIOTestUtils.readAll(reader);
92
93 Assertions.assertEquals(1, facets.size());
94 EuclideanIOTestUtils.assertFacetVertices(facets.get(0),
95 Arrays.asList(Vector3D.ZERO, Vector3D.of(1, 1, 0), Vector3D.of(0, 1, 0)), TEST_EPS);
96 }
97
98 @Test
99 void testFacetDefinitionReader_close() {
100
101 final CloseCountInputStream in = new CloseCountInputStream(input("", StandardCharsets.UTF_8));
102
103
104 try (FacetDefinitionReader reader = handler.facetDefinitionReader(new StreamGeometryInput(in))) {
105 Assertions.assertEquals(0, in.getCloseCount());
106 }
107
108 Assertions.assertEquals(1, in.getCloseCount());
109 }
110
111 private static ByteArrayInputStream input(final String str, final Charset charset) {
112 return new ByteArrayInputStream(str.getBytes(charset));
113 }
114 }