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
    18  package org.apache.any23.writer;
    19
    20  import java.io.ByteArrayOutputStream;
    21  import java.util.Collection;
    22  import java.util.HashSet;
    23  import java.util.List;
    24  import java.util.Set;
    25
    26  import org.apache.any23.configuration.Settings;
    27  import org.junit.Assert;
    28  import org.junit.Test;
    29
    30  /**
    31   * Test case for {@link WriterFactoryRegistry}.
    32   *
    33   * @author Michele Mostarda (mostarda@fbk.eu)
    34   */
    35  public class WriterRegistryTest {
    36
    37      private static final int NUM_OF_WRITERS = 8;
    38
    39      private final WriterFactoryRegistry target = WriterFactoryRegistry.getInstance();
    40
    41      @Test
    42      public void testGetIdentifiers() {
    43          final List<String> ids = target.getIdentifiers();
    44          Assert.assertTrue(ids.size() >= NUM_OF_WRITERS);
    45          assertUnique(ids);
    46      }
    47
    48      @Test
    49      public void testHasIdentifier() {
    50          Assert.assertTrue(target.hasIdentifier(target.getIdentifiers().get(0)));
    51      }
    52
    53      @Test
    54      public void testGetMimeTypes() {
    55          final Collection<String> mimeTypes = target.getMimeTypes();
    56          Assert.assertTrue(mimeTypes.size() > 0);
    57      }
    58
    59      @Test
    60      public void testGetWriters() {
    61          Assert.assertTrue(target.getWriters().size() >= NUM_OF_WRITERS);
    62      }
    63
    64      @Test
    65      public void testGetWriterByIdentifier() {
    66          final List<String> ids = target.getIdentifiers();
    67          for (String id : ids) {
    68              Assert.assertNotNull(target.getWriterByIdentifier(id));
    69          }
    70      }
    71
    72      @Test
    73      public void testGetWriterInstanceByIdentifier() {
    74          final List<String> ids = target.getIdentifiers();
    75          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    76          final CompositeTripleHandler delegate = new CompositeTripleHandler();
    77          for (String id : ids) {
    78              WriterFactory f = target.getWriterByIdentifier(id);
    79              if (f instanceof TripleWriterFactory) {
    80                  Assert.assertNotNull(((TripleWriterFactory) f).getTripleWriter(baos, Settings.of()));
    81              } else if (f instanceof DecoratingWriterFactory) {
    82                  Assert.assertNotNull(((DecoratingWriterFactory) f).getTripleWriter(delegate, Settings.of()));
    83              } else {
    84                  Assert.fail(id + " is not a valid writer factory");
    85              }
    86          }
    87      }
    88
    89      @Test
    90      public void testGetWritersByMimeType() {
    91          final Set<WriterFactory> set = new HashSet<WriterFactory>();
    92          final Collection<String> mimeTypes = target.getMimeTypes();
    93          for (String mimeType : mimeTypes) {
    94              set.addAll(target.getWritersByMimeType(mimeType));
    95          }
    96          Assert.assertEquals(NUM_OF_WRITERS, set.size());
    97      }
    98
    99      private void assertUnique(List<String> list) {
    100         final Set<String> set = new HashSet<String>();
    101         for (String elem : list) {
    102             if (set.contains(elem))
    103                 Assert.fail("Element " + elem + " already defined.");
    104             set.add(elem);
    105         }
    106     }
    107
    108 }