diff --git a/mail/common/src/test/java/com/fsck/k9/mail/internet/MimeParameterDecoderTest.kt b/mail/common/src/test/java/com/fsck/k9/mail/internet/MimeParameterDecoderTest.kt index a5cc35e23..def1fe989 100644 --- a/mail/common/src/test/java/com/fsck/k9/mail/internet/MimeParameterDecoderTest.kt +++ b/mail/common/src/test/java/com/fsck/k9/mail/internet/MimeParameterDecoderTest.kt @@ -1,7 +1,8 @@ package com.fsck.k9.mail.internet -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue +import com.google.common.truth.MapSubject +import com.google.common.truth.Ordered +import com.google.common.truth.Truth.assertThat import org.junit.Test class MimeParameterDecoderTest { @@ -9,18 +10,18 @@ class MimeParameterDecoderTest { fun rfc2045_example1() { val mimeValue = MimeParameterDecoder.decode("text/plain; charset=us-ascii (Plain text)") - assertEquals("text/plain", mimeValue.value) - assertParametersEquals(mimeValue, "charset" to "us-ascii") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.value).isEqualTo("text/plain") + assertThat(mimeValue.parameters).containsExactlyEntries("charset" to "us-ascii") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun rfc2045_example2() { val mimeValue = MimeParameterDecoder.decode("text/plain; charset=\"us-ascii\"") - assertEquals("text/plain", mimeValue.value) - assertParametersEquals(mimeValue, "charset" to "us-ascii") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.value).isEqualTo("text/plain") + assertThat(mimeValue.parameters).containsExactlyEntries("charset" to "us-ascii") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -31,13 +32,12 @@ class MimeParameterDecoderTest { " URL*1=\"cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar\"" ) - assertEquals("message/external-body", mimeValue.value) - assertParametersEquals( - mimeValue, + assertThat(mimeValue.value).isEqualTo("message/external-body") + assertThat(mimeValue.parameters).containsExactlyEntries( "access-type" to "URL", "url" to "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar" ) - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -47,13 +47,12 @@ class MimeParameterDecoderTest { " URL=\"ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar\"" ) - assertEquals("message/external-body", mimeValue.value) - assertParametersEquals( - mimeValue, + assertThat(mimeValue.value).isEqualTo("message/external-body") + assertThat(mimeValue.parameters).containsExactlyEntries( "access-type" to "URL", "url" to "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar" ) - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -63,9 +62,9 @@ class MimeParameterDecoderTest { " name*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A" ) - assertEquals("application/x-stuff", mimeValue.value) - assertParametersEquals(mimeValue, "name" to "This is ***fun***") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.value).isEqualTo("application/x-stuff") + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "This is ***fun***") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -77,9 +76,9 @@ class MimeParameterDecoderTest { " name*2=\"isn't it!\"" ) - assertEquals("application/x-stuff", mimeValue.value) - assertParametersEquals(mimeValue, "name" to "This is even more ***fun*** isn't it!") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.value).isEqualTo("application/x-stuff") + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "This is even more ***fun*** isn't it!") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -91,8 +90,8 @@ class MimeParameterDecoderTest { " name*0=\"[one]\"" ) - assertParametersEquals(mimeValue, "name" to "[one][two][three]") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "[one][two][three]") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -100,12 +99,12 @@ class MimeParameterDecoderTest { val mimeValue = MimeParameterDecoder.decode( "application/x-stuff;\r\n" + " name*0=\"[one]\";\r\n" + - " name*1=\"[two]\";\r\n" + - " name*2=\"[three]\"" + " NAME*1=\"[two]\";\r\n" + + " nAmE*2=\"[three]\"" ) - assertParametersEquals(mimeValue, "name" to "[one][two][three]") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "[one][two][three]") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -120,8 +119,8 @@ class MimeParameterDecoderTest { " name*5=six" ) - assertParametersEquals(mimeValue, "name" to "[one][two][three][four][five]six") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "[one][two][three][four][five]six") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -132,8 +131,8 @@ class MimeParameterDecoderTest { " name*=utf-8''filen%C3%A4me.ext" ) - assertParametersEquals(mimeValue, "name" to "filenäme.ext") - assertIgnoredParametersEquals(mimeValue, "name" to "filename.ext") + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "filenäme.ext") + assertThat(mimeValue.ignoredParameters).containsExactly("name" to "filename.ext") } @Test @@ -145,8 +144,8 @@ class MimeParameterDecoderTest { " name=two" ) - assertParametersEquals(mimeValue, "extra" to "something") - assertIgnoredParametersEquals(mimeValue, "name" to "one", "name" to "two") + assertThat(mimeValue.parameters).containsExactlyEntries("extra" to "something") + assertThat(mimeValue.ignoredParameters).containsExactly("name" to "one", "name" to "two") } @Test @@ -158,26 +157,26 @@ class MimeParameterDecoderTest { " NAME=two" ) - assertParametersEquals(mimeValue, "extra" to "something") - assertIgnoredParametersEquals(mimeValue, "name" to "one", "name" to "two") + assertThat(mimeValue.parameters).containsExactlyEntries("extra" to "something") + assertThat(mimeValue.ignoredParameters).containsExactly("name" to "one", "name" to "two") } @Test fun name_only_parameter() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; parameter") - assertEquals(30, mimeValue.parserErrorIndex) - assertTrue(mimeValue.parameters.isEmpty()) - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parserErrorIndex).isEqualTo(30) + assertThat(mimeValue.parameters).isEmpty() + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun missing_parameter_value() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; parameter=") - assertEquals(31, mimeValue.parserErrorIndex) - assertTrue(mimeValue.parameters.isEmpty()) - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parserErrorIndex).isEqualTo(31) + assertThat(mimeValue.parameters).isEmpty() + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -189,72 +188,72 @@ class MimeParameterDecoderTest { " (comment) extra (comment) = (comment) something (comment)" ) - assertParametersEquals(mimeValue, "name" to "one", "extra" to "something") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "one", "extra" to "something") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun iso8859_1_charset() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*=iso-8859-1''filen%E4me.ext") - assertParametersEquals(mimeValue, "name" to "filenäme.ext") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "filenäme.ext") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun missing_charset() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*=''filen%AAme.ext") - assertParametersEquals(mimeValue, "name" to "filen%AAme.ext") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "filen%AAme.ext") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun unknown_charset() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*=foobar''filen%AAme.ext") - assertParametersEquals(mimeValue, "name" to "filen%AAme.ext") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "filen%AAme.ext") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun section_index_missing() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name**=utf-8''filename") - assertParametersEquals(mimeValue, "name**" to "utf-8''filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name**" to "utf-8''filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun section_index_not_a_number() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*x*=filename") - assertParametersEquals(mimeValue, "name*x*" to "filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*x*" to "filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun section_index_prefixed_with_plus() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*+0=filename") - assertParametersEquals(mimeValue, "name*+0" to "filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*+0" to "filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun section_index_prefixed_with_minus() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*-0=filename") - assertParametersEquals(mimeValue, "name*-0" to "filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*-0" to "filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun section_index_with_two_zeros() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*00=filename") - assertParametersEquals(mimeValue, "name*00" to "filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*00" to "filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -265,8 +264,8 @@ class MimeParameterDecoderTest { " name*01=two" ) - assertParametersEquals(mimeValue, "name" to "one", "name*01" to "two") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "one", "name*01" to "two") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -276,80 +275,80 @@ class MimeParameterDecoderTest { " name*10000000000000000000=filename" ) - assertParametersEquals(mimeValue, "name*10000000000000000000" to "filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*10000000000000000000" to "filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun extended_parameter_name_with_additional_asterisk() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*0**=utf-8''filename") - assertParametersEquals(mimeValue, "name*0**" to "utf-8''filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*0**" to "utf-8''filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun extended_parameter_name_with_additional_text() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*0*x=utf-8''filename") - assertParametersEquals(mimeValue, "name*0*x" to "utf-8''filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*0*x" to "utf-8''filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun extended_parameter_value_with_quoted_string() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*0*=\"utf-8''filename\"") - assertParametersEquals(mimeValue, "name*0*" to "utf-8''filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*0*" to "utf-8''filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun extended_initial_parameter_value_missing_single_quotes() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*0*=filename") - assertParametersEquals(mimeValue, "name*0*" to "filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*0*" to "filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun extended_initial_parameter_value_missing_second_single_quote() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*0*='") - assertParametersEquals(mimeValue, "name*0*" to "'") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*0*" to "'") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun extended_parameter_value_with_trailing_percent_sign() { val mimeValue = MimeParameterDecoder.decode("attachment; filename*=utf-8''file%") - assertParametersEquals(mimeValue, "filename*" to "utf-8''file%") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("filename*" to "utf-8''file%") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun extended_parameter_value_with_invalid_percent_encoding() { val mimeValue = MimeParameterDecoder.decode("attachment; filename*=UTF-8''f%oo.html") - assertParametersEquals(mimeValue, "filename*" to "UTF-8''f%oo.html") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("filename*" to "UTF-8''f%oo.html") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun section_0_missing() { val mimeValue = MimeParameterDecoder.decode("application/x-stuff; name*1=filename") - assertParametersEquals(mimeValue, "name*1" to "filename") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name*1" to "filename") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test fun semicolon_in_parameter_value() { val mimeValue = MimeParameterDecoder.decode("attachment; filename=\"Here's a semicolon;.txt\"") - assertParametersEquals(mimeValue, "filename" to "Here's a semicolon;.txt") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("filename" to "Here's a semicolon;.txt") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -359,8 +358,8 @@ class MimeParameterDecoderTest { " name=\"=?UTF-8?Q?filn=C3=A4me=2Eext?=\"" ) - assertParametersEquals(mimeValue, "name" to "filnäme.ext") - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.parameters).containsExactlyEntries("name" to "filnäme.ext") + assertThat(mimeValue.ignoredParameters).isEmpty() } @Test @@ -372,23 +371,14 @@ class MimeParameterDecoderTest { " =?UTF-8?Q?non-ASCII_characters=3A_=C3=A4=E2=82=AC=F0=9F=8C=9E?=\"" ) - assertParametersEquals( - mimeValue, + assertThat(mimeValue.parameters).containsExactlyEntries( "name" to "File name that is so long it likes to be wrapped " + "into multiple lines. Also non-ASCII characters: ä€\uD83C\uDF1E" ) - assertTrue(mimeValue.ignoredParameters.isEmpty()) + assertThat(mimeValue.ignoredParameters).isEmpty() } - private fun assertParametersEquals(mimeValue: MimeValue, vararg expected: Pair) { - assertEquals(expected.toSet(), mimeValue.parameters.toPairSet()) - } - - private fun assertIgnoredParametersEquals(mimeValue: MimeValue, vararg expected: Pair) { - assertEquals(expected.toSet(), mimeValue.ignoredParameters.toSet()) - } - - private fun Map.toPairSet(): Set> { - return this.map { (key, value) -> key to value }.toSet() + private fun MapSubject.containsExactlyEntries(vararg values: Pair): Ordered { + return containsExactlyEntriesIn(values.toMap()) } }