Update PathGroup tests to reflect nice property: no group overlap!
@@ -2,10 +2,12 @@ package com.yahoo.vespa.hosted.controller.role;
import org.junit.Test;
+import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Pattern;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
@@ -14,27 +16,18 @@ import static org.junit.Assert.fail;
*/
public class PathGroupTest {
- @Test
- public void nonOverlappingGroups() {
- for (PathGroup pg : PathGroup.all()) {
- for (PathGroup pg2 : PathGroup.all()) {
- if (pg == pg2) continue;
- Set<String> overlapping = new LinkedHashSet<>(pg.pathSpecs);
- overlapping.retainAll(pg2.pathSpecs);
- if ( ! overlapping.isEmpty()) {
- fail("The following path specs overlap in " + pg + " and " + pg2 + ": " + overlapping);
- }
- }
- }
- }
-
@Test
public void uniqueMatches() {
// Ensure that each path group contains at most one match for any given path, to avoid undefined context extraction.
- for (PathGroup group : PathGroup.values())
- for (String path1 : group.pathSpecs)
- for (String path2 : group.pathSpecs) {
- if (path1 == path2) continue;
+ Set<String> checkedAgainstSelf = new HashSet<>();
+ for (PathGroup group1 : PathGroup.values())
+ for (PathGroup group2 : PathGroup.values())
+ for (String path1 : group1.pathSpecs)
+ for (String path2 : group2.pathSpecs) {
+ if (path1.equals(path2)) {
+ if (checkedAgainstSelf.add(path1)) continue;
+ fail("Path '" + path1 + "' appears in both '" + group1 + "' and '" + group2 + "'.");
+ }
String[] parts1 = path1.split("/");
String[] parts2 = path2.split("/");
@@ -43,6 +36,7 @@ public class PathGroupTest {
// If one path has more parts than the other ...
// and the other doesn't end with a wildcard matcher ...
// and the longest one isn't just one wildcard longer ...
+ // then one is strictly longer than the other, and it's not a match.
if (end < parts1.length && (end == 0 || ! parts2[end - 1].equals("{*}")) && ! parts1[end].equals("{*}")) continue;
if (end < parts2.length && (end == 0 || ! parts1[end - 1].equals("{*}")) && ! parts2[end].equals("{*}")) continue;
@@ -54,6 +48,9 @@ public class PathGroupTest {
if (i == end) fail("Paths '" + path1 + "' and '" + path2 + "' overlap.");
}
+
+ assertEquals(PathGroup.all().stream().mapToInt(group -> group.pathSpecs.size()).sum(),
+ checkedAgainstSelf.size());
}
@Test
