From 04f79f207d77abc1d28043fa22f4c4ad946dd1ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 27 Sep 2021 14:34:26 +0200 Subject: [PATCH] Fix incorrect comparison The result of Object.equals(Object) will _always_ be non-null because a primitive is always non-null. So once we use the correct method to check the result of the equals call everything will be fine. --- src/main/java/net/pterodactylus/sone/data/impl/ImageImpl.java | 2 +- .../kotlin/net/pterodactylus/sone/data/impl/ImageImplTest.kt | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/pterodactylus/sone/data/impl/ImageImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/ImageImpl.java index 0dd84fe..9446cec 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/ImageImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/ImageImpl.java @@ -95,7 +95,7 @@ public class ImageImpl implements Image { @Override public Image setAlbum(Album album) { checkNotNull(album, "album must not be null"); - checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image"); + checkState(album.getSone().equals(getSone()), "album must belong to the same Sone as this image"); this.album = album; return this; } diff --git a/src/test/kotlin/net/pterodactylus/sone/data/impl/ImageImplTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/impl/ImageImplTest.kt index 022bde0..e1551e1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/data/impl/ImageImplTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/data/impl/ImageImplTest.kt @@ -15,4 +15,13 @@ class ImageImplTest { image.modify().setTitle("").update() } + @Test(expected = IllegalStateException::class) + fun `album cannot be changed to album of different Sone`() { + val sone1 = IdOnlySone("Sone1") + val sone2 = IdOnlySone("Sone2") + image.modify().setSone(sone1).update() + val album = AlbumImpl(sone2) + image.album = album + } + } -- 2.7.4