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 java.net.URI;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.junit.jupiter.api.Test;
023
024import static org.junit.jupiter.api.Assertions.assertEquals;
025import static org.junit.jupiter.api.Assertions.assertNotEquals;
026import static org.junit.jupiter.api.Assertions.assertThrows;
027
028public class UnknownTermTest {
029
030  @Test
031  public void testBuild() {
032    UnknownTerm.build("http://me.com/#me");
033    UnknownTerm.build("http://me.com/me");
034    UnknownTerm.build("http://me.org/me");
035
036
037    assertEquals(UnknownTerm.build("http://me.org/me"), UnknownTerm.build("http://me.org/me"));
038    assertEquals(UnknownTerm.build("me"), UnknownTerm.build("http://unknown.org/me"));
039  }
040
041  @Test
042  public void testNamespace() {
043    assertEquals(URI.create("http://me.com"), UnknownTerm.build("http://me.com/me").namespace());
044    assertEquals(URI.create("https://me.com"), UnknownTerm.build("https://me.com/me").namespace());
045    assertEquals(URI.create("http://unknown.org"), UnknownTerm.build("me").namespace());
046  }
047
048  @Test
049  public void testBuildError() {
050    assertThrows(IllegalArgumentException.class, () -> UnknownTerm.build("http://me.com/"));
051  }
052
053  @Test
054  public void testBuildError2() {
055    Term u = UnknownTerm.build("gbif.org/verbatimLabel");
056    assertEquals("http://unknown.org/gbif.org/verbatimLabel", u.qualifiedName());
057    assertEquals("verbatimLabel", u.simpleName());
058  }
059
060  @Test
061  public void testPrefixedName() {
062    assertEquals("gbif:verbatimLabel", UnknownTerm.build("gbif:verbatimLabel").prefixedName());
063    assertEquals("unknown:verbatimLabel", UnknownTerm.build("unknown:verbatimLabel").prefixedName());
064    assertEquals("unknown:fooBar", UnknownTerm.build("fooBar").prefixedName());
065  }
066
067  @Test
068  public void testQualifiedName() {
069    Term me = UnknownTerm.build("http://me.com/#me");
070    assertEquals("http://me.com/#me", me.qualifiedName());
071
072    me = UnknownTerm.build("http://me.com/me");
073    assertEquals("http://me.com/me", me.qualifiedName());
074
075    me = UnknownTerm.build("http://me.org/me");
076    assertEquals("http://me.org/me", me.qualifiedName());
077
078    me = new UnknownTerm(URI.create("http://me.org/me"), "me", "me", false);
079    assertEquals("http://me.org/me", me.qualifiedName());
080  }
081
082  @Test
083  public void testSimpleName() {
084    Term me1 = UnknownTerm.build("http://me.com/#me");
085    Term me2 = UnknownTerm.build("http://me.com/me");
086    Term me3 = UnknownTerm.build("http://me.org/me");
087
088    assertEquals("me", me1.simpleName());
089    assertEquals("me", me2.simpleName());
090    assertEquals("me", me3.simpleName());
091  }
092
093  @Test
094  public void testEquals() {
095    Term me1 = UnknownTerm.build("http://me.com/#me");
096    Term me2 = UnknownTerm.build("http://me.com/me");
097    Term me3 = UnknownTerm.build("http://me.org/me");
098
099    assertNotEquals(me1, me2);
100    assertNotEquals(me1.hashCode(), me2.hashCode());
101    assertNotEquals(me1, me3);
102    assertNotEquals(me1.hashCode(), me3.hashCode());
103    assertNotEquals(me2, me3);
104    assertNotEquals(me2.hashCode(), me3.hashCode());
105
106    // only qualified names are relevant when deciding if a term is equal
107    Term me4 = UnknownTerm.build("http://me.org/me", "oscar");
108    assertNotEquals(me1, me4);
109    assertNotEquals(me1.hashCode(), me4.hashCode());
110    assertNotEquals(me2, me4);
111    assertNotEquals(me2.hashCode(), me4.hashCode());
112    assertEquals(me3, me4);
113    assertEquals(me3.hashCode(), me4.hashCode());
114  }
115
116  @Test
117  public void testSet() {
118    Set<Term> terms = new HashSet<>();
119    terms.add(UnknownTerm.build("http://me.com/#me"));
120    terms.add(UnknownTerm.build("http://me.com/me"));
121    terms.add(UnknownTerm.build("http://me.org/me"));
122    // duplicate
123    terms.add(UnknownTerm.build("http://me.org/me"));
124    // same qualified name, so it should be ignored!
125    terms.add(UnknownTerm.build("http://me.org/me", "oscar"));
126    terms.add(DcTerm.title);
127    terms.add(DcElement.title);
128    terms.add(DwcTerm.bed);
129
130    assertEquals(6, terms.size());
131  }
132}