ProjectChangeId { public static ProjectChangeId create(String token) { String mutableToken = token; int delimiter = mutableToken.indexOf(PageLinks.PROJECT_CHANGE_DELIMITER); Project.NameKey project = null; if (delimiter > 0) { project = new Project.NameKey(token.substring(0, delimiter)); mutableToken = mutableToken.substring(delimiter + PageLinks.PROJECT_CHANGE_DELIMITER.length()); } int s = mutableToken.indexOf('/'); if (s > 0) { mutableToken = mutableToken.substring(0, s); } s = mutableToken.indexOf(",edit"); if (s > 0) { mutableToken = mutableToken.substring(0, s); } Integer cId = tryParse(mutableToken); if (cId != null) { return new ProjectChangeId(project, new Change.Id(cId)); } throw new IllegalArgumentException(token + " is not a valid change identifier"); } @VisibleForTesting  ProjectChangeId(@Nullable Project.NameKey project, Change.Id changeId); static ProjectChangeId create(String token); @Nullable Project.NameKey getProject(); Change.Id getChangeId(); int identifierLength(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString();  }
@Test public void emptyStringThrowsException() { exception.expect(IllegalArgumentException.class); exception.expectMessage(" is not a valid change identifier"); ProjectChangeId.create(""); }
@Test public void noChangeIdThrowsException() { exception.expect(IllegalArgumentException.class); exception.expectMessage("some/path is not a valid change identifier"); ProjectChangeId.create("some/path"); }
@Test public void noChangeButProjectIdThrowsException() { exception.expect(IllegalArgumentException.class); exception.expectMessage("some/+/path is not a valid change identifier"); ProjectChangeId.create("some/+/path"); }
@Test public void project() { assertThat(ProjectChangeId.create("test/+/123/some/path")).isEqualTo(result("test", 123)); assertThat(ProjectChangeId.create("test/+/123/some/path/")).isEqualTo(result("test", 123)); assertThat(ProjectChangeId.create("test/+/123/")).isEqualTo(result("test", 123)); assertThat(ProjectChangeId.create("test/+/123")).isEqualTo(result("test", 123)); assertThat(ProjectChangeId.create("123/+/123")).isEqualTo(result("123", 123)); assertThat(ProjectChangeId.create("123,edit/+/123")).isEqualTo(result("123,edit", 123)); }
@Test public void noProject() { assertThat(ProjectChangeId.create("123/some/path")).isEqualTo(result(null, 123)); assertThat(ProjectChangeId.create("123/some/path/")).isEqualTo(result(null, 123)); assertThat(ProjectChangeId.create("123/")).isEqualTo(result(null, 123)); assertThat(ProjectChangeId.create("123")).isEqualTo(result(null, 123)); }
@Test public void editSuffix() { assertThat(ProjectChangeId.create("123,edit/some/path")).isEqualTo(result(null, 123)); assertThat(ProjectChangeId.create("123,edit/")).isEqualTo(result(null, 123)); assertThat(ProjectChangeId.create("123,edit")).isEqualTo(result(null, 123)); assertThat(ProjectChangeId.create("test/+/123,edit/some/path")).isEqualTo(result("test", 123)); assertThat(ProjectChangeId.create("test/+/123,edit/")).isEqualTo(result("test", 123)); assertThat(ProjectChangeId.create("test/+/123,edit")).isEqualTo(result("test", 123)); }