001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package org.basepom.mojo.dvc; 016 017import static org.junit.jupiter.api.Assertions.assertEquals; 018import static org.junit.jupiter.api.Assertions.assertFalse; 019import static org.junit.jupiter.api.Assertions.assertThrows; 020import static org.junit.jupiter.api.Assertions.assertTrue; 021 022import org.junit.jupiter.api.Test; 023 024public class QualifiedNameTest { 025 026 @Test 027 public void testSimple() { 028 QualifiedName name = new QualifiedName("groupId", "artifactId", "demo", "none"); 029 assertEquals("groupId", name.getGroupId()); 030 assertEquals("artifactId", name.getArtifactId()); 031 032 assertTrue(name.getType().isPresent()); 033 assertEquals("demo", name.getType().get()); 034 035 assertTrue(name.getClassifier().isPresent()); 036 assertEquals("none", name.getClassifier().get()); 037 038 assertEquals("groupId:artifactId:demo:none", name.getFullName()); 039 } 040 041 @Test 042 public void testNoType() { 043 IllegalStateException e = assertThrows(IllegalStateException.class, () -> { 044 new QualifiedName("groupId", "artifactId", null, "none"); 045 }); 046 047 assertEquals("Classifier must be null if type is null", e.getMessage()); 048 049 QualifiedName noType = new QualifiedName("groupId", "artifactId", null, null); 050 051 assertEquals("groupId", noType.getGroupId()); 052 assertEquals("artifactId", noType.getArtifactId()); 053 054 assertFalse(noType.getType().isPresent()); 055 assertFalse(noType.getClassifier().isPresent()); 056 assertEquals("groupId:artifactId", noType.getFullName()); 057 } 058 059 @Test 060 public void testNoClassifier() { 061 QualifiedName noClassifier = new QualifiedName("groupId", "artifactId", "demo", null); 062 assertEquals("groupId", noClassifier.getGroupId()); 063 assertEquals("artifactId", noClassifier.getArtifactId()); 064 065 assertTrue(noClassifier.getType().isPresent()); 066 assertEquals("demo", noClassifier.getType().get()); 067 068 assertFalse(noClassifier.getClassifier().isPresent()); 069 070 assertEquals("groupId:artifactId:demo", noClassifier.getFullName()); 071 } 072 073 @Test 074 public void testNoTypeAndClassifier() { 075 QualifiedName noClassifier = new QualifiedName("groupId", "artifactId", null, null); 076 assertEquals("groupId", noClassifier.getGroupId()); 077 assertEquals("artifactId", noClassifier.getArtifactId()); 078 079 assertFalse(noClassifier.getType().isPresent()); 080 assertFalse(noClassifier.getClassifier().isPresent()); 081 082 assertEquals("groupId:artifactId", noClassifier.getFullName()); 083 } 084 085 @Test 086 public void testEquality() { 087 QualifiedName n1 = new QualifiedName("groupId", "artifactId", "demo", "none"); 088 QualifiedName n2 = new QualifiedName("groupId", "artifactId", "demo", "none"); 089 090 assertEquals(n1, n2); 091 assertEquals(n2, n1); 092 assertEquals(n1.hashCode(), n2.hashCode()); 093 } 094 095 @Test 096 public void testTestJarEquality() { 097 // test jar equality 098 QualifiedName t1 = new QualifiedName("groupId", "artifactId", "test-jar", null); 099 QualifiedName t2 = new QualifiedName("groupId", "artifactId", "jar", "tests"); 100 assertEquals(t1, t2); 101 assertEquals(t2, t1); 102 assertEquals(t1.hashCode(), t2.hashCode()); 103 } 104 105 @Test 106 public void testNullTypeEquality() { 107 // type null and jar equality 108 QualifiedName j1 = new QualifiedName("groupId", "artifactId", "jar", null); 109 QualifiedName j2 = new QualifiedName("groupId", "artifactId", null, null); 110 assertEquals(j1, j2); 111 assertEquals(j2, j1); 112 assertEquals(j1.hashCode(), j2.hashCode()); 113 } 114 115 @Test 116 public void testNullClassifierEquality() { 117 // classifier null and empty 118 QualifiedName c1 = new QualifiedName("groupId", "artifactId", "jar", null); 119 QualifiedName c2 = new QualifiedName("groupId", "artifactId", "jar", ""); 120 assertEquals(c1, c2); 121 assertEquals(c2, c1); 122 assertEquals(c1.hashCode(), c2.hashCode()); 123 } 124 125 @Test 126 public void testTestJars() { 127 QualifiedName c1 = new QualifiedName("groupId", "artifactId", "test-jar", null); 128 QualifiedName c2 = new QualifiedName("groupId", "artifactId", "jar", "tests"); 129 130 assertEquals(c1, c2); 131 132 assertEquals("groupId:artifactId:test-jar", c1.getFullName()); 133 assertEquals("groupId:artifactId:jar:tests", c2.getFullName()); 134 135 assertEquals(0, c1.compareTo(c2)); 136 assertEquals(0, c2.compareTo(c1)); 137 } 138}