PackageValidator { public static void validate(String name) { Preconditions.checkArgument( VALID_PACKAGE.matcher(name).matches(), "Conjure package names must match pattern %s: %s", VALID_PACKAGE.pattern(), name); } private  PackageValidator(); static List<String> components(String name); static void validate(String name); static String conjurePackage(Iterable<String> components); static final String PRIMITIVE; }
@Test public void testValidPackageNames() { PackageValidator.validate("foo"); PackageValidator.validate("foo.bar"); PackageValidator.validate("foo.bar.baz"); PackageValidator.validate("ab.c.d"); PackageValidator.validate("a1.b2.c3"); }
@Test public void testInvalidPackageNames() { for (String illegal : new String[] {".", "foo-bar", "foo_bar", "1a", "a.foo"}) { assertThatThrownBy(() -> PackageValidator.validate(illegal)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Conjure package names must match pattern ^([a-z][a-z0-9]+(\\.[a-z][a-z0-9]*)*)?$: " + illegal); } }