package jmail;

import static org.junit.Assert.*;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;

import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.*;

public class MailTest {
	
	private static final String MESSAGE_ID_HEADER = "message-id";
	private static final String WANTED_MESSAGE_ID = "JavaMail.Message.12345.GOTYA.COM.erewhon.slugvax";
	private Mail target;
	
	@Before
	public void setup() {
		target = new Mail();
	}

	@Test
	public void testFindMessageById() throws MessagingException {
		Folder f = mock(Folder.class);
		target.mFolder = f;
		Message m1 = mock(Message.class);
		Message m2 = mock(Message.class);
		when (m1.getHeader(MESSAGE_ID_HEADER)).thenReturn(new String[]{"bleah"});
		when (m2.getHeader(MESSAGE_ID_HEADER)).thenReturn(new String[]{WANTED_MESSAGE_ID});
		when (f.getMessages()).thenReturn(new Message[]{m1, m2});
		Message message = target.findMessageById(WANTED_MESSAGE_ID);
		assertNotNull(message);
		assertSame(message, m2);
	}

}

@@ -362,16 +362,17 @@
 E_ID = %22
+%3C
 JavaMail
@@ -411,16 +411,17 @@
 .slugvax
+%3E
 %22;%0A%09priv
@@ -1035,12 +1035,513 @@
  m2);%0A%09%7D
+%0A%09%0A%09@Test%0A%09public void testFindMessageByIdBad() throws MessagingException %7B%0A%09%09Folder f = mock(Folder.class);%0A%09%09target.mFolder = f;%0A%09%09Message m1 = mock(Message.class);%0A%09%09Message m2 = mock(Message.class);%0A%09%09when (m1.getHeader(MESSAGE_ID_HEADER)).thenReturn(new String%5B%5D%7B%22bleah%22%7D);%0A%09%09when (m2.getHeader(MESSAGE_ID_HEADER)).thenReturn(new String%5B%5D%7B%22meh%22%7D);%0A%09%09when (f.getMessages()).thenReturn(new Message%5B%5D%7Bm1, m2%7D);%0A%09%09Message message = target.findMessageById(WANTED_MESSAGE_ID);%0A%09%09assertNull(message);%0A%09%7D
 %0A%0A%7D%0A

Test failure mode too!