MatrixBerryCore
mlunit_test_emaillogger.m
Go to the documentation of this file.
2  properties (Access=private)
3  eMail
4  smtpServer
5  end
6  methods
7  function self = mlunit_test_emaillogger(varargin)
8  self = self@mxberry.unittest.TestCase(varargin{:});
9  % Save current Internet preferences
10  if ispref('Internet','SMTP_Server')
11  self.smtpServer = getpref('Internet','SMTP_Server');
12  else
13  self.smtpServer = '';
14  end
15  if ispref('Internet','E_mail')
16  self.eMail = getpref('Internet','E_mail');
17  else
18  self.eMail = '';
19  end
20  end
21  end
22  methods (Test)
23  %
24  function self = test_emaillogger_fail(self)
25  obj=mxberry.log.EmailLogger(...
26  'emailDistributionList',{'billy@microsoft.com'},...
27  'emailAttachmentNameList',{},...
28  'smtpServer','invalid.server',...
29  'subjectSuffix','for mydatabase on mypc',...
30  'loggerName','MyApplication',...
31  'isThrowExceptions',true); %#ok<NASGU>
32  commandStr=['obj.sendMessage(''calculation started'',',...
33  '''calculation started'')']; %#ok<NASGU>
34  self.runAndCheckError('evalc(commandStr)','sendEmailFailed');
35  end
36  %
37  function self = test_emaillogger_preferences(self)
38  testSmtpServer = 'some.server';
39  [userName,hostName]=mxberry.system.getuserhost();
40  if isempty(userName)
41  userName = 'unknown';
42  end
43  if isempty(hostName)
44  hostName = 'unknown';
45  end
46  %
47  testEmail = [userName, '@', hostName];
48  % EmailLogger sets preferences Internet.SMTP_Server and
49  % Internet.E_mail
50  logger=mxberry.log.EmailLogger(...
51  'emailDistributionList',{'billy@microsoft.com'},...
52  'smtpServer',testSmtpServer,...
53  'subjectSuffix','for mydatabase on mypc',...
54  'loggerName','MyApplication');
55  self.verifyEqual(testSmtpServer,...
56  logger.getSMTPServer());
57  self.verifyEqual(testEmail,...
58  logger.getFromEmailAddress());
59  % EmailLogger should warn about changed preferences and reset
60  % them to their original values
61  setpref('Internet','SMTP_Server','some.other.server');
62  setpref('Internet','E_mail','some.other@email');
63  outputText = evalc(...
64  ['logger.sendMessage(''calculation started'',',...
65  '''calculation started'')']);
66  nWarnings = length(strfind('Warning:',outputText));
67  self.verifyEqual(0,nWarnings)
68  self.verifyEqual(testSmtpServer,...
69  logger.getSMTPServer());
70  self.verifyEqual(testEmail,...
71  logger.getFromEmailAddress());
72  % If 'dryRun' property is set, EmailLogger should also change
73  % e-mail preferences
74  FROM_EMAIL_ADDRESS='test@mydomain.com';
75  logger=mxberry.log.EmailLogger(...
76  'emailDistributionList',{'billy@microsoft.com'},...
77  'smtpServer','some.other.server',...
78  'subjectSuffix','for mydatabase on mypc',...
79  'loggerName','MyApplication',...
80  'dryRun',true,'fromEmailAddress',FROM_EMAIL_ADDRESS);
81  self.verifyEqual('some.other.server',...
82  logger.getSMTPServer());
83  self.verifyEqual(FROM_EMAIL_ADDRESS,...
84  logger.getFromEmailAddress());
85  end
86  %
87  function self = tear_down(self)
88  if ~isempty(self.smtpServer)
89  setpref('Internet','SMTP_Server',self.smtpServer);
90  elseif ispref('Internet','SMTP_Server')
91  rmpref('Internet','SMTP_Server');
92  end
93  if ~isempty(self.eMail)
94  setpref('Internet','E_mail',self.eMail);
95  elseif ispref('Internet','E_mail')
96  rmpref('Internet','E_mail');
97  end
98  end
99  end
100 end
function sendMessage(in self, in subjectMessage, in varargin)