awa_2.4.0_59135a52/awa/regtests/awa-mail-clients-tests.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
-----------------------------------------------------------------------
--  awa-mail-clients-tests -- Unit tests for Mail clients
--  Copyright (C) 2012, 2013, 2020 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Unchecked_Deallocation;

with Util.Test_Caller;
with Util.Properties;

package body AWA.Mail.Clients.Tests is

   procedure Free is
      new Ada.Unchecked_Deallocation (Object => AWA.Mail.Clients.Mail_Manager'Class,
                                      Name   => AWA.Mail.Clients.Mail_Manager_Access);

   procedure Free is
     new Ada.Unchecked_Deallocation (Object => AWA.Mail.Clients.Mail_Message'Class,
                                     Name   => AWA.Mail.Clients.Mail_Message_Access);

   package Caller is new Util.Test_Caller (Test, "Mail.Clients");

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test AWA.Mail.Clients.Factory",
                       Test_Factory'Access);
      Caller.Add_Test (Suite, "Test AWA.Mail.Clients.Create_Message",
                       Test_Create_Message'Access);
   end Add_Tests;

   --  ------------------------------
   --  Test the mail manager factory.
   --  ------------------------------
   procedure Test_Factory (T : in out Test) is
      M : AWA.Mail.Clients.Mail_Manager_Access;
      P : Util.Properties.Manager;
   begin
      M := AWA.Mail.Clients.Factory ("file", P);
      T.Assert (M /= null, "Factory returned a null mail manager");

      Free (M);
      M := AWA.Mail.Clients.Factory ("something", P);
      T.Assert (M = null, "Factory returned a non null mail manager");
   end Test_Factory;

   --  ------------------------------
   --  Create an email message and verify its content.
   --  ------------------------------
   procedure Test_Create_Message (T : in out Test) is
      procedure Send;

      M   : AWA.Mail.Clients.Mail_Manager_Access;

      procedure Send is
         Msg : AWA.Mail.Clients.Mail_Message_Access := M.Create_Message;
         C   : Ada.Strings.Unbounded.Unbounded_String;
      begin
         Append (C, "Le palais doit etre debarasse des fantaisies humaines.");
         Msg.Set_From (Name    => "Iorek Byrnison", Address => "Iorek.Byrnison@svalbard.com");
         Msg.Add_Recipient (Kind => TO, Name => "Tous les ours", Address => "all@svalbard.com");
         Msg.Set_Subject (Subject => "Decret");
         Msg.Set_Body (Content => C, Alternative => C);
         Msg.Send;
         Free (Msg);
      end Send;

   begin
      M := AWA.Mail.Clients.Factory ("file", Util.Tests.Get_Properties);
      T.Assert (M /= null, "Factory returned a null mail manager");

      for I in 1 .. 10 loop
         Send;
      end loop;
      Free (M);

      --  The SMTP mailer could be disabled from the configuration.
      M := AWA.Mail.Clients.Factory ("smtp", Util.Tests.Get_Properties);
      if M /= null then
         for I in 1 .. 10 loop
            Send;
         end loop;
         Free (M);
      end if;
   end Test_Create_Message;

end AWA.Mail.Clients.Tests;