Skip to content

Commit

Permalink
Changes from PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cover jdcove2 committed Dec 23, 2024
1 parent bc8c40b commit d9411f2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/emissary/util/ShortNameComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public int compare(IBaseDataObject obj1, IBaseDataObject obj2) {
// Get character length of level.
final int nextIndex1 = s1.indexOf(Family.SEP, index1);
final int nextIndex2 = s2.indexOf(Family.SEP, index2);
final int length1 = ((nextIndex1 <= -1) ? s1.length() : nextIndex1) - index1;
final int length2 = ((nextIndex2 <= -1) ? s2.length() : nextIndex2) - index2;
final int length1 = ((nextIndex1 < 0) ? s1.length() : nextIndex1) - index1;
final int length2 = ((nextIndex2 < 0) ? s2.length() : nextIndex2) - index2;

// If the obj1's level and obj2's level are the same, then go to the next level.
if (length1 == length2 && s1.regionMatches(index1, s2, index2, length1)) {
Expand Down
73 changes: 57 additions & 16 deletions src/test/java/emissary/util/ShortNameComparatorTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package emissary.util;

import emissary.core.BaseDataObject;
import emissary.core.Family;
import emissary.core.DataObjectFactory;
import emissary.core.IBaseDataObject;
import emissary.test.core.junit5.UnitTest;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ShortNameComparatorTest {
class ShortNameComparatorTest extends UnitTest {
// @formatter: off
private static final String ABC = "abc";
private static final String DEF = "def";
private static final String PARENT = "foo";
private static final String CHILD_ABC = PARENT + Family.SEP + ABC;
private static final String CHILD_DEF = PARENT + Family.SEP + DEF;
private static final String CHILD_1 = PARENT + Family.SEP + "1";
private static final String CHILD_2 = PARENT + Family.SEP + "2";
private static final String CHILD_10 = PARENT + Family.SEP + "10";
private static final String GRANDCHILD_1_1 = CHILD_1 + Family.SEP + "1";
private static final String FOO_ATT_ABC = "foo-att-abc";
private static final String FOO_ATT_DEF = "foo-att-def";
private static final String FOO_ATT_1 = "foo-att-1";
private static final String FOO_ATT_2 = "foo-att-2";
private static final String FOO_ATT_1_ATT_1 = "foo-att-1-att-1";
private static final String FOO_ATT_1_ATT_2 = "foo-att-1-att-2";
private static final String FOO_ATT_10 = "foo-att-10";
private static final String FOO_ATT_10_ATT_1 = "foo-att-10-att-1";
// @formatter: on
private static final ShortNameComparator COMPARATOR = new ShortNameComparator();

@Test
void testParameters() {
Expand All @@ -31,19 +40,51 @@ void testParameters() {

@Test
void testComparator() {
check(ABC, DEF, 0, 0);
check(CHILD_ABC, CHILD_DEF, -3, 3);
check(CHILD_1, CHILD_2, -1, 1);
check(CHILD_1, CHILD_10, -9, 9);
check(CHILD_1, GRANDCHILD_1_1, -1, 1);
check(ABC, DEF, 0);
check(FOO_ATT_ABC, FOO_ATT_DEF, -3);
check(FOO_ATT_1, FOO_ATT_2, -1);
check(FOO_ATT_1, FOO_ATT_10, -9);
check(FOO_ATT_1, FOO_ATT_1_ATT_1, -1);
}

@Test
void testListSorting() {
// start with an unsorted list
List<String> unsorted = List.of(
FOO_ATT_1,
FOO_ATT_10,
FOO_ATT_2,
FOO_ATT_10_ATT_1,
FOO_ATT_1_ATT_2,
FOO_ATT_1_ATT_1);

// map to a list of IBDO, sort using Comparator, map back to a list of Strings
List<String> actual = unsorted
.stream()
.map(name -> DataObjectFactory.getInstance(new byte[0], name))
.sorted(COMPARATOR)
.map(IBaseDataObject::shortName)
.collect(Collectors.toList());

// list with shortnames in expected sort order
List<String> expected = List.of(
FOO_ATT_1,
FOO_ATT_1_ATT_1,
FOO_ATT_1_ATT_2,
FOO_ATT_2,
FOO_ATT_10,
FOO_ATT_10_ATT_1);

// verify that the sorted output is correct
assertIterableEquals(expected, actual, "List wasn't sorted in the expected order");
}

private static void check(final String name1, final String name2, final int forwardResult, final int reverseResult) {
private static void check(final String name1, final String name2, final int forwardResult) {
final IBaseDataObject ibdo1 = new BaseDataObject(null, name1);
final IBaseDataObject ibdo2 = new BaseDataObject(null, name2);
final ShortNameComparator snc = new ShortNameComparator();

assertEquals(forwardResult, snc.compare(ibdo1, ibdo2));
assertEquals(reverseResult, snc.compare(ibdo2, ibdo1));
assertEquals(-forwardResult, snc.compare(ibdo2, ibdo1));
}
}

0 comments on commit d9411f2

Please sign in to comment.