Convert FolderNameCodec
to Kotlin
This commit is contained in:
parent
58ce09c43f
commit
5f4b24d5fe
4 changed files with 35 additions and 82 deletions
|
@ -1,43 +1,27 @@
|
|||
package com.fsck.k9.mail.store.imap;
|
||||
package com.fsck.k9.mail.store.imap
|
||||
|
||||
import com.beetstra.jutf7.CharsetProvider
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.charset.CodingErrorAction
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
internal class FolderNameCodec {
|
||||
private val modifiedUtf7Charset = CharsetProvider().charsetForName("X-RFC-3501")
|
||||
private val asciiCharset = StandardCharsets.US_ASCII
|
||||
|
||||
import com.beetstra.jutf7.CharsetProvider;
|
||||
fun encode(folderName: String): String {
|
||||
val byteBuffer = modifiedUtf7Charset.encode(folderName)
|
||||
val bytes = ByteArray(byteBuffer.limit())
|
||||
byteBuffer.get(bytes)
|
||||
|
||||
|
||||
class FolderNameCodec {
|
||||
private final Charset modifiedUtf7Charset;
|
||||
private final Charset asciiCharset;
|
||||
|
||||
|
||||
public static FolderNameCodec newInstance() {
|
||||
return new FolderNameCodec();
|
||||
return String(bytes, asciiCharset)
|
||||
}
|
||||
|
||||
private FolderNameCodec() {
|
||||
modifiedUtf7Charset = new CharsetProvider().charsetForName("X-RFC-3501");
|
||||
asciiCharset = Charset.forName("US-ASCII");
|
||||
}
|
||||
fun decode(encodedFolderName: String): String {
|
||||
val decoder = modifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
|
||||
val byteBuffer = ByteBuffer.wrap(encodedFolderName.toByteArray(asciiCharset))
|
||||
val charBuffer = decoder.decode(byteBuffer)
|
||||
|
||||
public String encode(String folderName) {
|
||||
ByteBuffer byteBuffer = modifiedUtf7Charset.encode(folderName);
|
||||
byte[] bytes = new byte[byteBuffer.limit()];
|
||||
byteBuffer.get(bytes);
|
||||
|
||||
return new String(bytes, asciiCharset);
|
||||
}
|
||||
|
||||
public String decode(String encodedFolderName) throws CharacterCodingException {
|
||||
CharsetDecoder decoder = modifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT);
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(encodedFolderName.getBytes(asciiCharset));
|
||||
CharBuffer charBuffer = decoder.decode(byteBuffer);
|
||||
|
||||
return charBuffer.toString();
|
||||
return charBuffer.toString()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ internal open class RealImapStore(
|
|||
private val trustedSocketFactory: TrustedSocketFactory,
|
||||
private val oauthTokenProvider: OAuth2TokenProvider?
|
||||
) : ImapStore, ImapConnectionManager, InternalImapStore {
|
||||
private val folderNameCodec: FolderNameCodec = FolderNameCodec.newInstance()
|
||||
private val folderNameCodec: FolderNameCodec = FolderNameCodec()
|
||||
|
||||
private val host: String = checkNotNull(serverSettings.host)
|
||||
|
||||
|
|
|
@ -1,59 +1,28 @@
|
|||
package com.fsck.k9.mail.store.imap;
|
||||
package com.fsck.k9.mail.store.imap
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
class FolderNameCodecTest {
|
||||
private var folderNameCode = FolderNameCodec()
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
|
||||
public class FolderNameCodecTest {
|
||||
private FolderNameCodec folderNameCode;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
folderNameCode = FolderNameCodec.newInstance();
|
||||
@Test
|
||||
fun `encode() with ASCII argument should return input`() {
|
||||
assertThat(folderNameCode.encode("ASCII")).isEqualTo("ASCII")
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode_withAsciiArgument_shouldReturnInput() throws Exception {
|
||||
String folderName = "ASCII";
|
||||
|
||||
String result = folderNameCode.encode(folderName);
|
||||
|
||||
assertEquals(folderName, result);
|
||||
fun `encode() with non-ASCII argument should return encoded string`() {
|
||||
assertThat(folderNameCode.encode("über")).isEqualTo("&APw-ber")
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode_withNonAsciiArgument_shouldReturnEncodedString() throws Exception {
|
||||
String folderName = "über";
|
||||
|
||||
String result = folderNameCode.encode(folderName);
|
||||
|
||||
assertEquals("&APw-ber", result);
|
||||
fun `decode() with encoded argument should return decoded string`() {
|
||||
assertThat(folderNameCode.decode("&ANw-bergr&APYA3w-entr&AOQ-ger")).isEqualTo("Übergrößenträger")
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode_withEncodedArgument_shouldReturnDecodedString() throws Exception {
|
||||
String encodedFolderName = "&ANw-bergr&APYA3w-entr&AOQ-ger";
|
||||
|
||||
String result = folderNameCode.decode(encodedFolderName);
|
||||
|
||||
assertEquals("Übergrößenträger", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode_withInvalidEncodedArgument_shouldThrow() throws Exception {
|
||||
String encodedFolderName = "&12-foo";
|
||||
|
||||
try {
|
||||
folderNameCode.decode(encodedFolderName);
|
||||
fail("Expected exception");
|
||||
} catch (CharacterCodingException ignored) {
|
||||
}
|
||||
@Test(expected = CharacterCodingException::class)
|
||||
fun `decode() with invalid encoded argument should throw`() {
|
||||
folderNameCode.decode("&12-foo")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1096,7 +1096,7 @@ class RealImapFolderTest {
|
|||
private fun extractMessageUids(messages: List<ImapMessage>) = messages.map { it.uid }.toSet()
|
||||
|
||||
private fun createFolder(folderName: String): RealImapFolder {
|
||||
return RealImapFolder(internalImapStore, testConnectionManager, folderName, FolderNameCodec.newInstance())
|
||||
return RealImapFolder(internalImapStore, testConnectionManager, folderName, FolderNameCodec())
|
||||
}
|
||||
|
||||
private fun createImapMessage(uid: String): ImapMessage {
|
||||
|
|
Loading…
Reference in a new issue