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.examples.io.threed;
    18
    19  import java.io.ByteArrayInputStream;
    20  import java.io.ByteArrayOutputStream;
    21  import java.io.File;
    22  import java.io.InputStream;
    23  import java.io.OutputStream;
    24  import java.util.Arrays;
    25  import java.util.List;
    26
    27  import org.apache.commons.geometry.core.GeometryTestUtils;
    28  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
    29  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
    30  import org.apache.commons.geometry.euclidean.threed.BoundarySource3D;
    31  import org.junit.Assert;
    32  import org.junit.Test;
    33
    34  public class ModelIOHandlerRegistryTest {
    35
    36      private static final double TEST_EPS = 1e-10;
    37
    38      private static final DoublePrecisionContext TEST_PRECISION =
    39              new EpsilonDoublePrecisionContext(TEST_EPS);
    40
    41      private static final BoundarySource3D SRC_A = BoundarySource3D.from();
    42
    43      private static final BoundarySource3D SRC_B = BoundarySource3D.from();
    44
    45      private ModelIOHandlerRegistry registry = new ModelIOHandlerRegistry();
    46
    47      @Test
    48      public void testGetSetHandlers() {
    49          // arrange
    50          StubHandler handlerA = new StubHandler("a", SRC_A);
    51          StubHandler handlerB = new StubHandler("b", SRC_B);
    52
    53          List<ModelIOHandler> handlers = Arrays.asList(handlerA, handlerB);
    54
    55          // act
    56          registry.setHandlers(handlers);
    57
    58          // assert
    59          List<ModelIOHandler> resultHandlers = registry.getHandlers();
    60          Assert.assertNotSame(handlers, resultHandlers);
    61          Assert.assertEquals(2, resultHandlers.size());
    62
    63          Assert.assertSame(handlerA, resultHandlers.get(0));
    64          Assert.assertSame(handlerB, resultHandlers.get(1));
    65      }
    66
    67      @Test
    68      public void testSetHandlers_null() {
    69          // arrange
    70          StubHandler handlerA = new StubHandler("a", SRC_A);
    71          StubHandler handlerB = new StubHandler("b", SRC_B);
    72
    73          registry.setHandlers(Arrays.asList(handlerA, handlerB));
    74
    75          // act
    76          registry.setHandlers(null);
    77
    78          // assert
    79          Assert.assertEquals(0, registry.getHandlers().size());
    80      }
    81
    82      @Test
    83      public void testGetHandlerForType() {
    84          // arrange
    85          StubHandler handlerA = new StubHandler("a", SRC_A);
    86          StubHandler handlerB = new StubHandler("b", SRC_B);
    87
    88          registry.setHandlers(Arrays.asList(handlerA, handlerB));
    89
    90          // act/assert
    91          Assert.assertSame(handlerA, registry.getHandlerForType("a"));
    92          Assert.assertSame(handlerB, registry.getHandlerForType("b"));
    93
    94          Assert.assertNull(registry.getHandlerForType(null));
    95          Assert.assertNull(registry.getHandlerForType(""));
    96          Assert.assertNull(registry.getHandlerForType(" "));
    97          Assert.assertNull(registry.getHandlerForType("nope"));
    98      }
    99
    100     @Test
    101     public void testHandlesType() {
    102         // arrange
    103         StubHandler handlerA = new StubHandler("a", SRC_A);
    104         StubHandler handlerB = new StubHandler("b", SRC_B);
    105
    106         registry.setHandlers(Arrays.asList(handlerA, handlerB));
    107
    108         // act/assert
    109         Assert.assertTrue(registry.handlesType("a"));
    110         Assert.assertTrue(registry.handlesType("b"));
    111
    112         Assert.assertFalse(registry.handlesType(null));
    113         Assert.assertFalse(registry.handlesType(""));
    114         Assert.assertFalse(registry.handlesType(" "));
    115         Assert.assertFalse(registry.handlesType("nope"));
    116     }
    117
    118     @Test
    119     public void testRead_typeFromFileExtension() {
    120         // arrange
    121         StubHandler handlerA = new StubHandler("a", SRC_A);
    122         StubHandler handlerB = new StubHandler("b", SRC_B);
    123
    124         registry.setHandlers(Arrays.asList(handlerA, handlerB));
    125
    126         File file = new File("file.B");
    127
    128         // act
    129         BoundarySource3D src = registry.read(file, TEST_PRECISION);
    130
    131         // assert
    132         Assert.assertSame(SRC_B, src);
    133     }
    134
    135     @Test
    136     public void testRead_typeFromFileExtension_unknownType() {
    137         // arrange
    138         File file = new File("file.B");
    139
    140         // act/assert
    141         GeometryTestUtils.assertThrows(() -> {
    142             registry.read(file, TEST_PRECISION);
    143         }, IllegalArgumentException.class, "No handler found for type \"b\"");
    144     }
    145
    146     @Test
    147     public void testRead_typeFromFileExtension_noFileExtension() {
    148         // arrange
    149         File file = new File("file");
    150
    151         // act/assert
    152         GeometryTestUtils.assertThrows(() -> {
    153             registry.read(file, TEST_PRECISION);
    154         }, IllegalArgumentException.class,
    155                 "Cannot determine target file type: \"file\" does not have a file extension");
    156     }
    157
    158     @Test
    159     public void testRead_typeAndFile() {
    160         // arrange
    161         StubHandler handlerA = new StubHandler("a", SRC_A);
    162         StubHandler handlerB = new StubHandler("b", SRC_B);
    163
    164         registry.setHandlers(Arrays.asList(handlerA, handlerB));
    165
    166         // act
    167         BoundarySource3D src = registry.read("a", new File("file"), TEST_PRECISION);
    168
    169         // assert
    170         Assert.assertSame(SRC_A, src);
    171     }
    172
    173     @Test
    174     public void testRead_typeAndFile_unknownType() {
    175         // arrange
    176         File file = new File("file");
    177
    178         // act/assert
    179         GeometryTestUtils.assertThrows(() -> {
    180             registry.read("nope", file, TEST_PRECISION);
    181         }, IllegalArgumentException.class, "No handler found for type \"nope\"");
    182     }
    183
    184     @Test
    185     public void testRead_typeAndInputStream() {
    186         // arrange
    187         StubHandler handlerA = new StubHandler("a", SRC_A);
    188         StubHandler handlerB = new StubHandler("b", SRC_B);
    189
    190         registry.setHandlers(Arrays.asList(handlerA, handlerB));
    191
    192         // act
    193         BoundarySource3D src = registry.read("a", new ByteArrayInputStream(new byte[0]), TEST_PRECISION);
    194
    195         // assert
    196         Assert.assertSame(SRC_A, src);
    197     }
    198
    199     @Test
    200     public void testRead_typeAndInputStream_unknownType() {
    201         // act/assert
    202         GeometryTestUtils.assertThrows(() -> {
    203             registry.read("nope", new ByteArrayInputStream(new byte[0]), TEST_PRECISION);
    204         }, IllegalArgumentException.class, "No handler found for type \"nope\"");
    205     }
    206
    207     @Test
    208     public void testWrite_typeFromFileExtension() {
    209         // arrange
    210         StubHandler handlerA = new StubHandler("a", SRC_A);
    211         StubHandler handlerB = new StubHandler("b", SRC_B);
    212
    213         registry.setHandlers(Arrays.asList(handlerA, handlerB));
    214
    215         File file = new File("file.B");
    216
    217         // act
    218         registry.write(SRC_B, file);
    219
    220         // assert
    221         Assert.assertNull(handlerA.outputBoundarySrc);
    222         Assert.assertSame(SRC_B, handlerB.outputBoundarySrc);
    223     }
    224
    225     @Test
    226     public void testWrite_typeFromFileExtension_unknownType() {
    227         // arrange
    228         File file = new File("file.B");
    229
    230         // act/assert
    231         GeometryTestUtils.assertThrows(() -> {
    232             registry.write(SRC_A, file);
    233         }, IllegalArgumentException.class, "No handler found for type \"b\"");
    234     }
    235
    236     @Test
    237     public void testWrite_typeFromFileExtension_noFileExtension() {
    238         // arrange
    239         File file = new File("file");
    240
    241         // act/assert
    242         GeometryTestUtils.assertThrows(() -> {
    243             registry.write(SRC_A, file);
    244         }, IllegalArgumentException.class,
    245                 "Cannot determine target file type: \"file\" does not have a file extension");
    246     }
    247
    248     @Test
    249     public void testWrite_typeAndFile() {
    250         // arrange
    251         StubHandler handlerA = new StubHandler("a", SRC_A);
    252         StubHandler handlerB = new StubHandler("b", SRC_B);
    253
    254         registry.setHandlers(Arrays.asList(handlerA, handlerB));
    255
    256         File file = new File("file.B");
    257
    258         // act
    259         registry.write(SRC_B, "a", file);
    260
    261         // assert
    262         Assert.assertSame(SRC_B, handlerA.outputBoundarySrc);
    263         Assert.assertNull(handlerB.outputBoundarySrc);
    264     }
    265
    266     @Test
    267     public void testWrite_typeAndFile_unknownType() {
    268         // arrange
    269         File file = new File("file");
    270
    271         // act/assert
    272         GeometryTestUtils.assertThrows(() -> {
    273             registry.write(SRC_A, "nope", file);
    274         }, IllegalArgumentException.class, "No handler found for type \"nope\"");
    275     }
    276
    277     @Test
    278     public void testWrite_typeAndOutputStream() {
    279         // arrange
    280         StubHandler handlerA = new StubHandler("a", SRC_A);
    281         StubHandler handlerB = new StubHandler("b", SRC_B);
    282
    283         registry.setHandlers(Arrays.asList(handlerA, handlerB));
    284
    285         // act
    286         registry.write(SRC_B, "a", new ByteArrayOutputStream());
    287
    288         // assert
    289         Assert.assertSame(SRC_B, handlerA.outputBoundarySrc);
    290         Assert.assertNull(handlerB.outputBoundarySrc);
    291     }
    292
    293     @Test
    294     public void testWrite_typeAndOutputStream_unknownType() {
    295         // act/assert
    296         GeometryTestUtils.assertThrows(() -> {
    297             registry.write(SRC_A, "nope", new ByteArrayOutputStream());
    298         }, IllegalArgumentException.class, "No handler found for type \"nope\"");
    299     }
    300
    301     private static final class StubHandler implements ModelIOHandler {
    302
    303         private final String handlerType;
    304
    305         private final BoundarySource3D boundarySrc;
    306
    307         private BoundarySource3D outputBoundarySrc;
    308
    309         StubHandler(final String type, final BoundarySource3D boundarySrc) {
    310             this.handlerType = type;
    311             this.boundarySrc = boundarySrc;
    312         }
    313
    314         @Override
    315         public boolean handlesType(String type) {
    316             return this.handlerType.equals(type);
    317         }
    318
    319         @Override
    320         public BoundarySource3D read(String type, File in, DoublePrecisionContext precision) {
    321             return boundarySrc;
    322         }
    323
    324         @Override
    325         public BoundarySource3D read(String type, InputStream in, DoublePrecisionContext precision) {
    326             return boundarySrc;
    327         }
    328
    329         @Override
    330         public void write(BoundarySource3D model, String type, File out) {
    331             outputBoundarySrc = model;
    332         }
    333
    334         @Override
    335         public void write(BoundarySource3D model, String type, OutputStream out) {
    336             outputBoundarySrc = model;
    337         }
    338     }
    339 }