### Question:
SchemaObjectSorter { public static Iterable<ObjectClass> sortObjectClasses( List<ObjectClass> objectClasses ) { return new SchemaObjectIterable<>( objectClasses, new ReferenceCallback<ObjectClass>() { @Override public Collection<String> getSuperiorOids( ObjectClass oc ) { return oc.getSuperiorOids(); } } ); } private  SchemaObjectSorter(); static Iterable<AttributeType> hierarchicalOrdered( List<AttributeType> attributeTypes ); static Iterable<ObjectClass> sortObjectClasses( List<ObjectClass> objectClasses );  }### Answer:
@Test public void testSortObjectClassesAlreadySorted() { List<ObjectClass> objectClasses = new ArrayList<ObjectClass>(); addObjectClass( objectClasses, "1.2.1", "oc1" ); addObjectClass( objectClasses, "1.2.2", "OC2", "oc1" ); addObjectClass( objectClasses, "1.2.3", "oc3", "oC2" ); addObjectClass( objectClasses, "1.2.4", "oc4" ); addObjectClass( objectClasses, "1.2.5", "oc5", "Oc2", "oC4" ); addObjectClass( objectClasses, "1.2.6", "oc6", "other" ); Iterable<ObjectClass> sorted = SchemaObjectSorter.sortObjectClasses( objectClasses ); assertHierarchicalOrderOC( sorted ); }
@Test public void testSortObjectClassesShuffled() { List<String> oids = Arrays.asList( "1.1.1", "1.1.2", "1.1.3", "1.1.4", "1.1.5", "1.1.6" ); for ( int i = 0; i < 1000; i++ ) { Collections.shuffle( oids ); Iterator<String> oidIterator = oids.iterator(); List<ObjectClass> objectClasses = new ArrayList<ObjectClass>(); addObjectClass( objectClasses, oidIterator.next(), "oc1" ); addObjectClass( objectClasses, oidIterator.next(), "OC2", "oc1" ); addObjectClass( objectClasses, oidIterator.next(), "oc3", "Oc2" ); addObjectClass( objectClasses, oidIterator.next(), "oc4" ); addObjectClass( objectClasses, oidIterator.next(), "oc5", "oC2", "OC4" ); addObjectClass( objectClasses, oidIterator.next(), "oc6", "other" ); Iterable<ObjectClass> sorted = SchemaObjectSorter.sortObjectClasses( objectClasses ); assertHierarchicalOrderOC( sorted ); } }
@Test public void testSortObjectClassesLoop() { List<ObjectClass> objectClasses = new ArrayList<ObjectClass>(); addObjectClass( objectClasses, "1.2.1", "oc1", "oc3" ); addObjectClass( objectClasses, "1.2.2", "oc2", "oc1" ); addObjectClass( objectClasses, "1.2.3", "oc3", "oc2" ); Iterable<ObjectClass> sorted = SchemaObjectSorter.sortObjectClasses( objectClasses ); assertThrows( IllegalStateException.class, () -> { sorted.iterator().next(); } ); }