Merge "Parse Account.Ids out of ref names"

This commit is contained in:
Edwin Kempin
2014-09-10 13:04:21 +00:00
committed by Gerrit Code Review
2 changed files with 40 additions and 0 deletions

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.reviewdb.client;
import static com.google.gerrit.reviewdb.client.RefNames.REFS_USER;
import com.google.gwtorm.client.Column;
import com.google.gwtorm.client.IntKey;
@@ -105,6 +107,16 @@ public final class Account {
return r;
}
public static Id fromRef(String name) {
if (name == null) {
return null;
}
if (name.startsWith(REFS_USER)) {
return fromRefPart(name.substring(REFS_USER.length()));
}
return null;
}
/**
* Parse an Account.Id out of a part of a ref-name.
*

View File

@@ -20,6 +20,26 @@ import static org.junit.Assert.assertNull;
import org.junit.Test;
public class AccountTest {
@Test
public void parseRefName() {
assertRef(1, "refs/users/01/1");
assertRef(1, "refs/users/01/1-drafts");
assertRef(1, "refs/users/01/1-drafts/2");
assertNotRef(null);
assertNotRef("");
// Invalid characters.
assertNotRef("refs/users/01a/1");
assertNotRef("refs/users/01/a1");
// Mismatched shard.
assertNotRef("refs/users/01/23");
// Shard too short.
assertNotRef("refs/users/1/1");
}
@Test
public void parseRefNameParts() {
assertRefPart(1, "01/1");
@@ -43,6 +63,14 @@ public class AccountTest {
assertNotRefPart("1/1");
}
private static void assertRef(int accountId, String refName) {
assertEquals(new Account.Id(accountId), Account.Id.fromRef(refName));
}
private static void assertNotRef(String refName) {
assertNull(Account.Id.fromRef(refName));
}
private static void assertRefPart(int accountId, String refName) {
assertEquals(new Account.Id(accountId), Account.Id.fromRefPart(refName));
}