2010-05-06 15 views
5

Sto provando a testare un metodo che elabora istanze javax.mail.Message.Caricamento di file .eml in javax.mail.Messages

Sto scrivendo un convertitore per modificare le email che arrivano in diversi formati e vengono quindi convertiti in un formato interno coerente (MyMessage). Questa conversione dipenderà in genere dall'indirizzo o dall'indirizzo di risposta dell'email, e per la creazione del nuovo MyMessage saranno necessarie le parti dell'email, l'oggetto e gli indirizzi di risposta e di risposta.

Ho una collezione di messaggi di posta elettronica prime che vengono salvati localmente come .eml file, e mi piacerebbe fare una prova di unità che carica i file .eml dal percorso di classe e li converte in javax.mail.Message istanze. È possibile, e se sì, come si farebbe?

risposta

0

Il mio problema deriva dall'usare Mockito per simulare il javax.mail.Folder richiesto dal costruttore javax.mail.internet.MimeMessageMimeMessage(Folder, InputStream, int). Ciò chiama il costruttore per javax.mail.MessageMessage(Folder, int) che quindi accede a folder.store.session. Ciò ha comportato l'emissione di NullPointerException da parte del costruttore per MimeMessage.

Soluzione:

class ClasspathMimeMessage extends MimeMessage { 
    private ClasspathMimeMessage(Folder folder, InputStream is, int msgnum) throws MessagingException { 
     super(folder, is, 0); 
    } 

    public static MimeMessage create(String resourceName) { 
     Class<PopEmailMmsReceiverTest> loaderClass = PopEmailMmsReceiverTest.class; 
     InputStream is = loaderClass.getResourceAsStream(resourceName); 

     Folder inbox = new MyFolder(); 

     try { 
      return new ClasspathMimeMessage(inbox, is, 0); 
     } catch (MessagingException ex) { 
      throw new RuntimeException("Unable to load email from classpath at " + loaderClass.getResource(resourceName).toString()); 
     } 
    } 
} 

class MyFolder extends Folder { 
    MyFolder() { 
     super(createMockStore()); 
    } 
    private static Store createMockStore() { 
     return mock(Store.class); 
    } 
    public void appendMessages(Message[] msgs) throws MessagingException { 
    } 
    public void close(boolean expunge) throws MessagingException { 
    } 
    public boolean create(int type) throws MessagingException { 
     return false; 
    } 
    public boolean delete(boolean recurse) throws MessagingException { 
     return false; 
    } 
    public boolean exists() throws MessagingException { 
     return false; 
    } 
    public Message[] expunge() throws MessagingException { 
     return null; 
    } 
    public Folder getFolder(String name) throws MessagingException { 
     return null; 
    } 
    public String getFullName() { 
     return null; 
    } 
    public Message getMessage(int msgnum) throws MessagingException { 
     return null; 
    } 
    public int getMessageCount() throws MessagingException { 
     return 0; 
    } 
    public String getName() { 
     return null; 
    } 
    public Folder getParent() throws MessagingException { 
     return null; 
    } 
    public Flags getPermanentFlags() { 
     return null; 
    } 
    public char getSeparator() throws MessagingException { 
     return 0; 
    } 
    public int getType() throws MessagingException { 
     return 0; 
    } 
    public boolean hasNewMessages() throws MessagingException { 
     return false; 
    } 
    public boolean isOpen() { 
     return false; 
    } 
    public Folder[] list(String pattern) throws MessagingException { 
     return null; 
    } 
    public void open(int mode) throws MessagingException { 
    } 
    public boolean renameTo(Folder f) throws MessagingException { 
     return false; 
    } 
} 

Questo sembra molto brutto per me, quindi se qualcuno ha un suggerimento migliore, sarei felice di sentirlo.

9

Dopo alcuni test, ho finalmente caricato con successo un messaggio utilizzando il costruttore pubblico MimeMessage(Session, InputStream) (in contrapposizione a quello protetto basato su cartella citato nell'altra risposta).

import java.io.FileInputStream; 
import java.io.InputStream; 

import javax.mail.internet.MimeMessage; 

public class LoadEML { 

    public static void main(String[] args) throws Exception { 
     InputStream is = new FileInputStream(args[0]); 
     MimeMessage mime = new MimeMessage(null, is); 
     System.out.println("Subject: " + mime.getSubject()); 
    } 

} 
+0

ha funzionato anche per me – grasshopper