jeudi 25 juin 2015

EJB @Schedule Annotation stopped working after JUnit Test Case

I created this class in my J2EE project to send an automated email. After I deploy my application to the server, the EJB @Schedule annotation is supposed to execute the sendAutomatedEmail method at the designated time, such as every Thursday at 10:11am as shown below. This has been working for weeks, however, after I ran a JUnit test case to get code coverage for that method, the @schedule annotation stopped working and none of the code in that method executes now with the Timer. Since I haven't modified any code in the sendautomatedEmail method, I think the JUnit test must have messed up something, maybe something to do with the system properties. I have included my ScheduleEmail class, and my test method from my JUnit class.

package scheduleTimer;

import java.util.Date;
import java.util.Properties;

import javax.ejb.Schedule;
import javax.ejb.Stateless;
//import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
//import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import downloadsupport.ZipFolder;

@Stateless
public class ScheduleEmail {

    // Day of Week Sun, Mon, Tue, Wed, Thu, Fri, Sat
    // hour: 0-23, minute: 0-59, second: 0-59
    @Schedule(second = "0", minute = "11", hour = "10", dayOfWeek = "Thu")
    public static void sendAutomatedEmail() {
        // An array that can contain multiple recipients' email IDs need to be mentioned.
        String[] to = {"email1@email.com","email2@email.com","email3@email.edu"};
        // Sender's email ID needs to be mentioned
        String from = "download_support@asffdsj.com";
        // Assuming you are sending email from smtp
        String host = "smtpmail";
        // Get system properties
        Properties properties = System.getProperties();
        // Setup mail server
        properties.setProperty("mail.smtp.host", host);
        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties);
        System.out.println("Success");
        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));
            // Make email addresses into Internet addresses
            InternetAddress[] addressTo = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++) {
                addressTo[i] = new InternetAddress(to[i]);
                System.out.println("Email Sent to " + addressTo[i]);
            }
            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO, addressTo);     
            // Set Subject: header field
            message.setSubject("Download: Daily Report");
            // Initialize the contents of email message body in HTML
            String header = "<h1>Insurance</h1>";
            String downloadLink= "<a href='http://localhost:9081/downloadsupport/index.jsp'>Continue to Download Support</a>";
            String paragraph = "<p>Daily Log Report: Click Link to Review Log Information</p>";
            String signature = "<p>Contact us at: download_support@email.com</p>";
            message.setContent(header + paragraph + downloadLink + signature, "text/html");

            Transport.send(message);
            System.out.println("All Emails Sent " + new Date());

            // Create Instance of ZipFolder after email is sent successfully
            ZipFolder zip = new ZipFolder();
            zip.createZip();
            zip.deleteZips(7);
        } catch (MessagingException mex) {
              mex.printStackTrace();
        }
    }
}

Here is the JUnit (it was not a successful test case, but since it did invoke sendAutomatedEmail manually and also did some other things related to that method, I'm afraid it altered something not allowing the @Schedule annotation to execute the method. I have since commented out this JUnit test case, but maybe it modified something permanently.

@Test
public void testOther() throws MessagingException, IOException {
    String subject = "Insurance - Download: Daily Report";

    ScheduleEmail.sendAutomatedEmail();
    Session session = Session.getDefaultInstance(new Properties());

    Store store = session.getStore("pop3");

    store.connect("someUrl.com", "someemailName", "password");
    Folder folder = store.getFolder("inbox");
    folder.open(Folder.READ_ONLY);
    Message[] msg = folder.getMessages();

    assertTrue(msg.length == 1);
    assertEquals(subject, msg[0].getSubject());
    //assertEquals(body, msg[0].getContent());
    folder.close(true);
    store.close();
}

Aucun commentaire:

Enregistrer un commentaire