diff options
author | janakr <janakr@google.com> | 2018-03-20 13:52:00 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-03-20 13:53:17 -0700 |
commit | 3b3c7cd679cd962994b559ea79b0d622b0bfbc5f (patch) | |
tree | 278a86260ddd10458bde0607980149b7627489f2 /src/test | |
parent | a0f66c4077d7c13c06e21ddbb22f0f54b3767be7 (diff) |
Add EnumMapCodec. Some hackery necessary to get Class for Enum when map is empty.
Since EnumMap.equals() ignores the key type when the maps are empty [1], we could just return a canonical empty EnumMap, but that seems to depend on implementation details in a hacky and potentially subtly broken way. The Unsafe method will hopefully break spectacularly if it goes wrong.
[1] http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/EnumMap.java#l685
PiperOrigin-RevId: 189803280
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/skyframe/serialization/EnumMapCodecTest.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/EnumMapCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/EnumMapCodecTest.java new file mode 100644 index 0000000000..93400620cb --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/EnumMapCodecTest.java @@ -0,0 +1,64 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// 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.devtools.build.lib.skyframe.serialization; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; + +import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester; +import com.google.devtools.build.lib.skyframe.serialization.testutils.TestUtils; +import java.util.EnumMap; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link EnumMapCodec}. */ +@RunWith(JUnit4.class) +public class EnumMapCodecTest { + @Test + public void smoke() throws Exception { + new SerializationTester( + new EnumMap<>( + ImmutableMap.of( + TestEnum.FIRST, "first", TestEnum.THIRD, "third", TestEnum.SECOND, "second")), + new EnumMap<>(TestEnum.class), + new EnumMap<>(EmptyEnum.class)) + .runTests(); + } + + @Test + public void throwsOnSubclass() { + SerializationException exception = + assertThrows( + SerializationException.class, + () -> TestUtils.toBytes(new SubEnum<>(TestEnum.class), ImmutableMap.of())); + assertThat(exception).hasMessageThat().contains("Cannot serialize subclasses of EnumMap"); + } + + private enum TestEnum { + FIRST, + SECOND, + THIRD + } + + private enum EmptyEnum {} + + private static class SubEnum<E extends Enum<E>, V> extends EnumMap<E, V> { + public SubEnum(Class<E> keyType) { + super(keyType); + } + } +} |