Parse Account.Ids out of ref names

Change-Id: I8391ff4cf9c99947f9c50e881df7a30c5a1304aa
This commit is contained in:
Dave Borowitz
2014-09-10 12:23:27 +02:00
parent 51e72b8809
commit 4542809163
2 changed files with 40 additions and 0 deletions

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.reviewdb.client; 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.Column;
import com.google.gwtorm.client.IntKey; import com.google.gwtorm.client.IntKey;
@@ -105,6 +107,16 @@ public final class Account {
return r; 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. * 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; import org.junit.Test;
public class AccountTest { 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 @Test
public void parseRefNameParts() { public void parseRefNameParts() {
assertRefPart(1, "01/1"); assertRefPart(1, "01/1");
@@ -43,6 +63,14 @@ public class AccountTest {
assertNotRefPart("1/1"); 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) { private static void assertRefPart(int accountId, String refName) {
assertEquals(new Account.Id(accountId), Account.Id.fromRefPart(refName)); assertEquals(new Account.Id(accountId), Account.Id.fromRefPart(refName));
} }