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.core.utils;
18  
19  import java.io.IOException;
20  import java.io.StringWriter;
21  import java.io.UncheckedIOException;
22  import java.io.Writer;
23  import java.text.DecimalFormat;
24  import java.text.DecimalFormatSymbols;
25  import java.util.Locale;
26  import java.util.function.DoubleFunction;
27  
28  import org.apache.commons.geometry.core.GeometryTestUtils;
29  import org.apache.commons.geometry.io.core.test.CloseCountWriter;
30  import org.junit.jupiter.api.Assertions;
31  import org.junit.jupiter.api.Test;
32  
33  class AbstractTextFormatWriterTest {
34  
35      private StringWriter out = new StringWriter();
36  
37      @Test
38      void testDefaults() {
39          // act
40          try (TestWriter writer = new TestWriter(out)) {
41              // assert
42              Assertions.assertEquals("\n", writer.getLineSeparator());
43              Assertions.assertNotNull(writer.getDoubleFormat());
44              Assertions.assertSame(out, writer.getWriter());
45          }
46      }
47  
48      @Test
49      void testWrite_defaultConfig() {
50          // arrange
51          final double n = 20000.0 / 3.0;
52          final CloseCountWriter closeCountWriter = new CloseCountWriter(out);
53          try (TestWriter writer = new TestWriter(closeCountWriter)) {
54              // act
55              writer.write('a');
56              writer.write("bc");
57              writer.writeNewLine();
58              writer.write(n);
59              writer.writeNewLine();
60              writer.write(Double.POSITIVE_INFINITY);
61              writer.writeNewLine();
62              writer.write(5);
63  
64              // assert
65              Assertions.assertEquals("abc\n" + n + "\nInfinity\n5", out.toString());
66          }
67  
68          Assertions.assertEquals(1, closeCountWriter.getCloseCount());
69      }
70  
71      @Test
72      void testWrite_customConfig() {
73          // arrange
74          final CloseCountWriter closeCountWriter = new CloseCountWriter(out);
75          try (TestWriter writer = new TestWriter(closeCountWriter)) {
76  
77              writer.setLineSeparator("\r\n");
78  
79              final DecimalFormat fmt = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
80  
81              final DoubleFunction<String> df = fmt::format;
82              writer.setDoubleFormat(df);
83  
84              // act
85              writer.write('a');
86              writer.write("bc");
87              writer.writeNewLine();
88              writer.write(20000.0 / 3.0);
89              writer.writeNewLine();
90              writer.write(5);
91  
92              // assert
93              Assertions.assertEquals("abc\r\n6666.67\r\n5", out.toString());
94          }
95  
96          Assertions.assertEquals(1, closeCountWriter.getCloseCount());
97      }
98  
99      @Test
100     void testWrite_failure() {
101         // arrange
102         final Writer failWriter = new Writer() {
103             @Override
104             public void write(char[] cbuf, int off, int len) throws IOException {
105                 throw new IOException("test");
106             }
107 
108             @Override
109             public void flush() {
110             }
111 
112             @Override
113             public void close() {
114             }
115         };
116 
117         // act/assert
118         try (TestWriter writer = new TestWriter(failWriter)) {
119             GeometryTestUtils.assertThrowsWithMessage(
120                     () -> writer.write('a'),
121                     UncheckedIOException.class,
122                     "IOException: test");
123 
124             GeometryTestUtils.assertThrowsWithMessage(
125                     () -> writer.write("abc"),
126                     UncheckedIOException.class,
127                     "IOException: test");
128         }
129     }
130 
131     private static final class TestWriter extends AbstractTextFormatWriter {
132 
133         protected TestWriter(final Writer writer) {
134             super(writer);
135         }
136     }
137 }