View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // act/assert
45          Assertions.assertEquals(GeometryFormat3D.TXT, handler.getFormat());
46          Assertions.assertEquals(StandardCharsets.UTF_8, handler.getDefaultCharset());
47      }
48  
49      @Test
50      void testFacetDefinitionReader() {
51          // arrange
52          final InputStream in = input("0 0 0; 1 1 0; 0 1 0", StandardCharsets.UTF_8);
53  
54          // act
55          final FacetDefinitionReader reader = handler.facetDefinitionReader(new StreamGeometryInput(in));
56  
57          // assert
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          // arrange
68          final InputStream in = input("0 0 0; 1 1 0; 0 1 0", StandardCharsets.UTF_16);
69  
70          // act
71          final FacetDefinitionReader reader = handler.facetDefinitionReader(new StreamGeometryInput(in, null, StandardCharsets.UTF_16));
72  
73          // assert
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          // arrange
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          // act
88          final FacetDefinitionReader reader = handler.facetDefinitionReader(new StreamGeometryInput(in));
89  
90          // assert
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         // arrange
101         final CloseCountInputStream in = new CloseCountInputStream(input("", StandardCharsets.UTF_8));
102 
103         // act/assert
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 }