Merge changes Ie6474dfc,I0ed9b031

* changes:
  Add missing serialized class field tests for TagSet types
  Add ObjectIdCacheSerializer
This commit is contained in:
Alice Kober-Sotzek
2018-08-23 10:55:56 +00:00
committed by Gerrit Code Review
5 changed files with 153 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.cache.serialize;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
public enum ObjectIdCacheSerializer implements CacheSerializer<ObjectId> {
INSTANCE;
@Override
public byte[] serialize(ObjectId object) {
byte[] buf = new byte[Constants.OBJECT_ID_LENGTH];
object.copyRawTo(buf, 0);
return buf;
}
@Override
public ObjectId deserialize(byte[] in) {
if (in == null || in.length != Constants.OBJECT_ID_LENGTH) {
throw new IllegalArgumentException("Failed to deserialize ObjectId");
}
return ObjectId.fromRaw(in);
}
}

View File

@@ -100,4 +100,11 @@ public class SerializedClassSubject extends Subject<SerializedClassSubject, Clas
.named("no-argument abstract methods on %s", actual().getName())
.isEqualTo(expectedMethods);
}
public void extendsClass(Type superclassType) {
isNotNull();
assertThat(actual().getGenericSuperclass())
.named("superclass of %s", actual().getName())
.isEqualTo(superclassType);
}
}

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.cache.serialize;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assert_;
import static com.google.gerrit.server.cache.testing.CacheSerializerTestUtil.byteArray;
import org.eclipse.jgit.lib.ObjectId;
import org.junit.Test;
public class ObjectIdCacheSerializerTest {
@Test
public void serialize() {
ObjectId id = ObjectId.fromString("aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
byte[] serialized = ObjectIdCacheSerializer.INSTANCE.serialize(id);
assertThat(serialized)
.isEqualTo(
byteArray(
0xaa, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb));
assertThat(ObjectIdCacheSerializer.INSTANCE.deserialize(serialized)).isEqualTo(id);
}
@Test
public void deserializeInvalid() {
assertDeserializeFails(null);
assertDeserializeFails(byteArray());
assertDeserializeFails(byteArray(0xaa));
assertDeserializeFails(
byteArray(
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa));
}
private void assertDeserializeFails(byte[] bytes) {
try {
ObjectIdCacheSerializer.INSTANCE.deserialize(bytes);
assert_().fail("expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected.
}
}
}

View File

@@ -16,7 +16,9 @@ package com.google.gerrit.server.git;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
import static com.google.gerrit.server.cache.testing.SerializedClassSubject.assertThatSerializedClass;
import com.google.common.collect.ImmutableMap;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.cache.proto.Cache.TagSetHolderProto;
import org.junit.Test;
@@ -54,4 +56,14 @@ public class TagSetHolderTest {
assertThat(deserialized.getProjectName()).isEqualTo(holder.getProjectName());
TagSetTest.assertEqual(holder.getTagSet(), deserialized.getTagSet());
}
@Test
public void fields() {
assertThatSerializedClass(TagSetHolder.class)
.hasFields(
ImmutableMap.of(
"buildLock", Object.class,
"projectName", Project.NameKey.class,
"tags", TagSet.class));
}
}

View File

@@ -18,7 +18,9 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
import static com.google.gerrit.server.cache.testing.CacheSerializerTestUtil.byteString;
import static com.google.gerrit.server.cache.testing.SerializedClassSubject.assertThatSerializedClass;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Streams;
import com.google.gerrit.common.Nullable;
@@ -28,11 +30,14 @@ import com.google.gerrit.server.cache.proto.Cache.TagSetHolderProto.TagSetProto.
import com.google.gerrit.server.cache.proto.Cache.TagSetHolderProto.TagSetProto.TagProto;
import com.google.gerrit.server.git.TagSet.CachedRef;
import com.google.gerrit.server.git.TagSet.Tag;
import com.google.inject.TypeLiteral;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectIdOwnerMap;
import org.junit.Test;
@@ -101,6 +106,42 @@ public class TagSetTest {
assertEqual(tagSet, TagSet.fromProto(proto));
}
@Test
public void tagSetFields() {
assertThatSerializedClass(TagSet.class)
.hasFields(
ImmutableMap.of(
"projectName", Project.NameKey.class,
"refs", new TypeLiteral<Map<String, CachedRef>>() {}.getType(),
"tags", new TypeLiteral<ObjectIdOwnerMap<Tag>>() {}.getType()));
}
@Test
public void cachedRefFields() {
assertThatSerializedClass(CachedRef.class)
.extendsClass(new TypeLiteral<AtomicReference<ObjectId>>() {}.getType());
assertThatSerializedClass(CachedRef.class)
.hasFields(
ImmutableMap.of(
"flag", int.class, "value", AtomicReference.class.getTypeParameters()[0]));
}
@Test
public void tagFields() {
assertThatSerializedClass(Tag.class).extendsClass(ObjectIdOwnerMap.Entry.class);
assertThatSerializedClass(Tag.class)
.hasFields(
ImmutableMap.<String, Type>builder()
.put("refFlags", BitSet.class)
.put("next", ObjectIdOwnerMap.Entry.class)
.put("w1", int.class)
.put("w2", int.class)
.put("w3", int.class)
.put("w4", int.class)
.put("w5", int.class)
.build());
}
// TODO(dborowitz): Find some more common place to put this method, which requires access to
// package-private TagSet details.
static void assertEqual(@Nullable TagSet a, @Nullable TagSet b) {