Skip to content

Commit 04262b6

Browse files
committed
Make CodeListTest robust to code list values created in other tests.
1 parent 0de6953 commit 04262b6

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

geoapi/src/test/java/org/opengis/geoapi/CodeListTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
* @since 2.0
4242
*/
4343
public final class CodeListTest {
44+
/**
45+
* Suffix of code list values created by other tests.
46+
* Those values need to be ignored by {@code CodeListTest}.
47+
* We use the {@value} prefix in their name for identifying them.
48+
*/
49+
public static final String IGNORABLE_NAME_SUFFIX = "Test";
50+
4451
/**
4552
* Creates a new test case.
4653
*/
@@ -72,6 +79,7 @@ public void testAll() throws NoSuchFieldException, NoSuchMethodException,
7279
assertTrue(Modifier.isStatic(valuesMethod.getModifiers()), () -> className + ".values() is not static.");
7380
final ControlledVocabulary[] values = (ControlledVocabulary[]) valuesMethod.invoke(null, (Object[]) null);
7481
assertNotNull(values, () -> className + ".values() returned null.");
82+
int ignored = 0;
7583
/*
7684
* Tests every CodeList instances returned by values().
7785
* Every field should be public, static and final.
@@ -81,6 +89,10 @@ public void testAll() throws NoSuchFieldException, NoSuchMethodException,
8189
final String valueName = value.name();
8290
final String fullName = className + '.' + valueName;
8391
assertTrue(codeClass.isInstance(value), () -> fullName + " is of unexpected type.");
92+
if (valueName.endsWith(IGNORABLE_NAME_SUFFIX)) {
93+
ignored++;
94+
continue; // Skip values created in other tests.
95+
}
8496
final Field field = codeClass.getDeclaredField(valueName);
8597
final int modifiers = field.getModifiers();
8698
if (field.isAnnotationPresent(UML.class)) {
@@ -110,7 +122,7 @@ public void testAll() throws NoSuchFieldException, NoSuchMethodException,
110122
if (CodeList.class.isAssignableFrom(codeClass)) {
111123
final Vocabulary desc = codeClass.getAnnotation(Vocabulary.class);
112124
assertNotNull(desc, () -> className + " has no @Vocabulary annotation.");
113-
assertEquals(values.length, desc.capacity(), () -> className + " is not properly sized.");
125+
assertEquals(values.length - ignored, desc.capacity(), () -> className + " is not properly sized.");
114126
/*
115127
* Tests valueOf(String).
116128
*/

geoapi/src/test/java/org/opengis/referencing/ReferenceSystemTypeTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.opengis.referencing.crs.VerticalCRS;
4343
import org.junit.jupiter.api.Test;
4444

45+
import static org.opengis.geoapi.CodeListTest.IGNORABLE_NAME_SUFFIX;
4546
import static org.junit.jupiter.api.Assertions.*;
4647

4748

@@ -81,16 +82,17 @@ public void testCompound() {
8182
"COMPOUND_ENGINEERING_TEMPORAL", ReferenceSystemType.TEMPORAL);
8283
assertSame(ReferenceSystemType.COMPOUND_ENGINEERING_TEMPORAL, base);
8384

84-
final ReferenceSystemType compound = base.compound("MyCustomCompound", ReferenceSystemType.PARAMETRIC);
85-
assertSame(compound, base.compound("MyCustomCompound", ReferenceSystemType.PARAMETRIC));
86-
assertEquals("MyCustomCompound", compound.name());
85+
final String name = "Compound" + IGNORABLE_NAME_SUFFIX;
86+
final ReferenceSystemType compound = base.compound(name, ReferenceSystemType.PARAMETRIC);
87+
assertSame(compound, base.compound(name, ReferenceSystemType.PARAMETRIC));
88+
assertEquals(name, compound.name());
8789

8890
RuntimeException e;
8991
e = assertThrows(IllegalStateException.class, () -> base.compound("Dummy", ReferenceSystemType.ENGINEERING_DESIGN));
9092
assertEquals("ENGINEERING_DESIGN", e.getMessage());
9193

92-
e = assertThrows(IllegalArgumentException.class, () -> base.compound("MyCustomCompound", ReferenceSystemType.VERTICAL));
93-
assertTrue(e.getMessage().contains("MyCustomCompound"));
94+
e = assertThrows(IllegalArgumentException.class, () -> base.compound(name, ReferenceSystemType.VERTICAL));
95+
assertTrue(e.getMessage().contains(name));
9496
}
9597

9698
/**

0 commit comments

Comments
 (0)