package com.fsck.k9.activity;

import android.support.test.runner.AndroidJUnit4;

import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.MessagingException;
import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
public class MessageReferenceTest
{
    /**
     * Typically happens during forwards.  (You have a reference, but no flag since we don't currently consider FORWARDED a flag.)
     */
    @Test
    public void testIdentityStringNoFlag()
    {
        MessageReference mr = new MessageReference();
        mr.accountUuid = "o hai!";
        mr.folderName = "folder";
        mr.uid = "10101010";

        assertEquals("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=", mr.toIdentityString());
    }

    /**
     * Typically happens during replies.
     */
    @Test
    public void testIdentityString()
    {
        MessageReference mr = new MessageReference();
        mr.accountUuid = "o hai!";
        mr.folderName = "folder";
        mr.uid = "10101010";
        mr.flag = Flag.ANSWERED;

        assertEquals("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED", mr.toIdentityString());
    }

    @Test
    public void testParseIdentityStringNoFlag() throws MessagingException
    {
        MessageReference mr = new MessageReference("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=");
        assertEquals("o hai!", mr.accountUuid);
        assertEquals("folder", mr.folderName);
        assertEquals("10101010", mr.uid);
        assertNull(mr.flag);
    }

    @Test
    public void testParseIdentityString() throws MessagingException
    {
        MessageReference mr = new MessageReference("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED");
        assertEquals("o hai!", mr.accountUuid);
        assertEquals("folder", mr.folderName);
        assertEquals("10101010", mr.uid);
        assertEquals(Flag.ANSWERED, mr.flag);
    }

    @Test
    public void testBadVersion() throws MessagingException
    {
        MessageReference mr = new MessageReference("@:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED");
        assertNull(mr.accountUuid);
    }

    @Test
    public void testNull() throws MessagingException
    {
        try
        {
            new MessageReference(null);
            assertTrue(false);
        } catch (MessagingException e)
        {
            assertTrue(true);
        }
    }

    @Test
    public void testCorruption() throws MessagingException
    {
        MessageReference mr = new MessageReference("!:%^&%^*$&$by&(BYWkh:Zm9%^@sZGVy:MT-35#$AxMDEwMTA=:ANSWERED");
        // No idea what this is going to generate, but it should be non-null.
        assertNotNull(mr.accountUuid);
        assertNotNull(mr.folderName);
        assertNotNull(mr.uid);

        // Corruption in the Flag should throw MessagingException.
        try
        {
            new MessageReference("!:%^&%^*$&$by&(BYWkh:Zm9%^@sZGVy:MT-35#$AxMDEwMTA=:ANSWE!RED");
            assertTrue(false);
        } catch (MessagingException e)
        {
            assertTrue(true);
        }
    }
}

@@ -361,57 +361,8 @@
 ull;
-%0Aimport static junit.framework.Assert.assertTrue;
 %0A%0A@R
@@ -2203,32 +2203,69 @@
     %7D%0A%0A    @Test
+(expected = MessagingException.class)
 %0A    public void
@@ -2312,34 +2312,8 @@
   %7B%0A
-        try%0A        %7B%0A    
     
@@ -2352,140 +2352,57 @@
     
-        assertTrue(false);%0A        %7D catch (MessagingException e)%0A        %7B%0A            assertTrue(true);%0A        %7D%0A    %7D%0A%0A    @Test
+%7D%0A%0A    @Test(expected = MessagingException.class)
 %0A   
@@ -2836,34 +2836,8 @@
 on.%0A
-        try%0A        %7B%0A    
     
@@ -2930,128 +2930,8 @@
 %22);%0A
-            assertTrue(false);%0A        %7D catch (MessagingException e)%0A        %7B%0A            assertTrue(true);%0A        %7D%0A
     

use `@Test(expected =`