/
githubmirror
/
guava
Обзор
Документация
Войти
/
githubmirror
/
guava
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
android/guava-testlib/test/com/google/common/testing/SerializableTesterTest.java
137 строк
4 KB
Kurt Alfred Kluever
Migrate parts of `javatests/com/google/common/testing/...` from JUnit3 to JUnit4, so that we can use `TestParameterInjector` in an upcoming CL.
26 июн 2026, 02:12
26 июн 2026, 02:12
6dc6797
Код
Авторство
О чём код?
/* * Copyright (C) 2009 The Guava Authors * * 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.common.testing; import static com.google.common.testing.SerializableTester.reserializeAndAssert; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import java.io.Serializable; import junit.framework.AssertionFailedError; import org.jspecify.annotations.NullUnmarked; import org.jspecify.annotations.Nullable; import org.junit.Test; /** * Tests for {@link SerializableTester}. * * @author Nick Kralevich */ @NullUnmarked public class SerializableTesterTest { @Test public void stringAssertions() { String original = "hello world"; String copy = reserializeAndAssert(original); assertThat(copy).isNotSameInstanceAs(original); } @Test public void classWhichDoesNotImplementEquals() { ClassWhichDoesNotImplementEquals orig = new ClassWhichDoesNotImplementEquals(); boolean errorNotThrown = false; try { reserializeAndAssert(orig); errorNotThrown = true; } catch (AssertionFailedError error) { // expected assertContains("must be Object#equals to", error.getMessage()); } assertFalse(errorNotThrown); } @Test public void classWhichIsAlwaysEqualButHasDifferentHashcodes() { ClassWhichIsAlwaysEqualButHasDifferentHashcodes orig = new ClassWhichIsAlwaysEqualButHasDifferentHashcodes(); boolean errorNotThrown = false; try { reserializeAndAssert(orig); errorNotThrown = true; } catch (AssertionFailedError error) { // expected assertContains("must be equal to the Object#hashCode", error.getMessage()); } assertFalse(errorNotThrown); } @Test public void objectWhichIsEqualButChangesClass() { ObjectWhichIsEqualButChangesClass orig = new ObjectWhichIsEqualButChangesClass(); boolean errorNotThrown = false; try { reserializeAndAssert(orig); errorNotThrown = true; } catch (AssertionFailedError error) { // expected assertContains("expected:<class ", error.getMessage()); } assertFalse(errorNotThrown); } private static class ClassWhichDoesNotImplementEquals implements Serializable { private static final long serialVersionUID = 1L; } private static class ClassWhichIsAlwaysEqualButHasDifferentHashcodes implements Serializable { private static final long serialVersionUID = 2L; @SuppressWarnings("EqualsHashCode") @Override public boolean equals(@Nullable Object other) { return (other instanceof ClassWhichIsAlwaysEqualButHasDifferentHashcodes); } } private static class ObjectWhichIsEqualButChangesClass implements Serializable { private static final long serialVersionUID = 1L; @Override public boolean equals(@Nullable Object other) { return (other instanceof ObjectWhichIsEqualButChangesClass || other instanceof OtherForm); } @Override public int hashCode() { return 1; } private Object writeReplace() { return new OtherForm(); } private static class OtherForm implements Serializable { @Override public boolean equals(@Nullable Object other) { return (other instanceof ObjectWhichIsEqualButChangesClass || other instanceof OtherForm); } @Override public int hashCode() { return 1; } } } private static void assertContains(String expectedSubstring, String actual) { // TODO(kevinb): use a Truth assertion here if (!actual.contains(expectedSubstring)) { fail("expected <" + actual + "> to contain <" + expectedSubstring + ">"); } } }