Project.NameKey: Implement Serializable

Prior to Ic46c4a357 the Project.NameKey class extended StringKey and
was implicitly serializable because StringKey implements Serializable.

After Ic46c4a357, Project.NameKey is no longer serializable, which
causes an IllegalStateException when attempting to bind a cache that
uses JavaCacheSerializer with Project.NameKey as key or value.

Add back the Serializable interface and add a test to make sure it is
serializable by JavaCacheSerializer.

Bug: Issue 11793
Change-Id: Id465c3a4cd810de477fb86a13185ec452d5d3a40
This commit is contained in:
David Pursehouse
2019-10-22 17:53:56 +09:00
parent 3e05a05372
commit e0ebf9d3e5
5 changed files with 15 additions and 1 deletions

View File

@@ -19,6 +19,7 @@ import static java.util.Objects.requireNonNull;
import com.google.gerrit.extensions.client.InheritableBoolean;
import com.google.gerrit.extensions.client.ProjectState;
import com.google.gerrit.extensions.client.SubmitType;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -46,7 +47,9 @@ public final class Project {
* unlike other key types in this package. However, this is strictly an implementation detail; its
* interface and semantics are otherwise analogous to the {@code @AutoValue} types.
*/
public static class NameKey implements Comparable<NameKey> {
public static class NameKey implements Serializable, Comparable<NameKey> {
private static final long serialVersionUID = 1L;
/** Parse a Project.NameKey out of a string representation. */
public static NameKey parse(String str) {
return nameKey(KeyUtil.decode(str));

View File

@@ -18,6 +18,8 @@ import com.google.gerrit.entities.Project;
/** Special name of the project that all projects derive from. */
public class AllProjectsName extends Project.NameKey {
private static final long serialVersionUID = 1L;
public AllProjectsName(String name) {
super(name);
}

View File

@@ -18,6 +18,8 @@ import com.google.gerrit.entities.Project;
/** Special name of the project in which meta data for all users is stored. */
public class AllUsersName extends Project.NameKey {
private static final long serialVersionUID = 1L;
public AllUsersName(String name) {
super(name);
}

View File

@@ -4,6 +4,7 @@ junit_tests(
name = "tests",
srcs = glob(["*.java"]),
deps = [
"//java/com/google/gerrit/entities",
"//java/com/google/gerrit/server/cache/serialize",
"//java/com/google/gerrit/server/cache/testing",
"//java/com/google/gerrit/testing:gerrit-test-util",

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.cache.serialize;
import static com.google.common.truth.Truth.assertThat;
import com.google.auto.value.AutoValue;
import com.google.gerrit.entities.Project;
import java.io.Serializable;
import org.junit.Test;
@@ -33,6 +34,11 @@ public class JavaCacheSerializerTest {
assertRoundTrip(new AutoValue_JavaCacheSerializerTest_MyType(123, "four five six"));
}
@Test
public void gerritEntities() throws Exception {
assertRoundTrip(Project.nameKey("foo"));
}
@AutoValue
abstract static class MyType implements Serializable {
private static final long serialVersionUID = 1L;