IdUtil { public static void validateSpaceId(String spaceID) throws InvalidIdException { if (spaceID == null || spaceID.trim().length() < 3 || spaceID.trim().length() > 42) { String err = "Space ID must be between 3 and 42 characters long"; throw new InvalidIdException(err); } if (!spaceID.matches("[a-z0-9.-]*")) { String err = "Only lowercase letters, numbers, periods, " + "and dashes may be used in a Space ID"; throw new InvalidIdException(err); } if (spaceID.startsWith(".") || spaceID.startsWith("-")) { String err = "A Space ID must begin with a lowercase letter."; throw new InvalidIdException(err); } if (spaceID.matches("[0-9]+[a-z0-9.-]*")) { String err = "A Space ID must begin with a lowercase letter."; throw new InvalidIdException(err); } if (spaceID.matches("[a-z0-9.-]*[.][0-9]+[a-z0-9-]*")) { String err = "The last period in a Space ID may not be " + "immediately followed by a number."; throw new InvalidIdException(err); } if (spaceID.endsWith("-")) { String err = "A Space ID must end with a lowercase letter, " + "number, or period"; throw new InvalidIdException(err); } if (spaceID.contains("..") || spaceID.contains("-.") || spaceID.contains(".-")) { String err = "A Space ID must not contain '..' '-.' or '.-'"; throw new InvalidIdException(err); } if (spaceID.matches("[0-9]+.[0-9]+.[0-9]+.[0-9]+")) { String err = "A Space ID must not be formatted as an IP address"; throw new InvalidIdException(err); } } private  IdUtil(); static void validateSpaceId(String spaceID); static void validateContentId(String contentID); static void validateStoreId(String storeId);  }
@Test public void testSpaceIDs() throws Exception { List<String> invalidIds = new ArrayList<String>(); invalidIds.add("Test-Space"); invalidIds.add("test-space!"); invalidIds.add("test..space"); invalidIds.add("-test-space"); invalidIds.add("test-space-"); invalidIds.add("test-.space"); invalidIds.add("te"); invalidIds.add("test-space-test-space-test-space-" + "test-space)"); invalidIds.add("127.0.0.1"); invalidIds.add("1-test-space"); invalidIds.add("1000-test-space"); invalidIds.add("1000"); invalidIds.add("test.space.1"); invalidIds.add("a0.1b.2c.3d.4e"); for (String id : invalidIds) { checkInvalidSpaceId(id); } IdUtil.validateSpaceId("test-space.test.space"); IdUtil.validateSpaceId("test-space."); IdUtil.validateSpaceId("test.1.space"); IdUtil.validateSpaceId("test-1-space"); IdUtil.validateSpaceId("test-1-space"); IdUtil.validateSpaceId("a.1.2.3.4.b"); IdUtil.validateSpaceId("a0.1b.2c.3d.4e.f6"); IdUtil.validateSpaceId("tes"); IdUtil.validateSpaceId("test-space-test-space-test-space-" + "test-spac"); }