001/*
002 * Copyright 2021 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.dwc.terms;
017
018import org.junit.jupiter.api.Test;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022
023/**
024 * Generic tests for all Term implementing classes.
025 */
026public abstract class TermBaseTest<T extends Term> {
027
028  private static final TermFactory TERM_FACTORY = TermFactory.instance();
029
030  private final Class<T> clazz;
031  private final T[] values;
032  private final String[] forbiddenChars;
033  private final boolean skipSimple;
034
035  public TermBaseTest(Class<T> clazz) {
036    this(clazz, new String[]{"_","-"}, false);
037  }
038
039  public TermBaseTest(Class<T> clazz, String[] forbiddenChars, boolean skipSimple) {
040    this.clazz = clazz;
041    this.forbiddenChars = forbiddenChars;
042    values = clazz.getEnumConstants();
043    this.skipSimple = skipSimple;
044  }
045
046  @Test
047  public void testNames() {
048    for (T t : values) {
049      for (String c : forbiddenChars) {
050        assertFalse(t.simpleName().contains(c), "Term contains forbidden character " + c + " : " + t.simpleName());
051        assertFalse(t.qualifiedName().contains(c), "Term contains forbidden character " + c + " : " + t.qualifiedName());
052      }
053    }
054  }
055
056  /**
057   * Test that iterates over all term values and uses the simple name alone as input to the factory and then verifies
058   * the Term returned is the same as the original one.
059   */
060  @Test
061  public void testFindSimpleTerm() {
062    if (!skipSimple) {
063      for (T t : values) {
064          assertEquals(t, TERM_FACTORY.findTerm(t.simpleName(), t.isClass()));
065      }
066    }
067  }
068
069  @Test
070  public void testFindPrefixedTerms() {
071    for (T t : values) {
072      Term found = TERM_FACTORY.findTerm(t.prefixedName(), t.isClass());
073      assertEquals(t, found);
074    }
075  }
076
077  @Test
078  public void testFindQualifiedTerm() {
079    for (T t : values) {
080      assertEquals(t, TERM_FACTORY.findTerm(t.qualifiedName(), t.isClass()));
081    }
082  }
083}