View Javadoc

1   package org.apache.maven.continuum.notification.mail;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.mail.Address;
28  import javax.mail.internet.InternetAddress;
29  import javax.mail.internet.MimeMessage;
30  import javax.mail.internet.MimeMessage.RecipientType;
31  
32  import org.apache.continuum.dao.BuildDefinitionDao;
33  import org.apache.continuum.dao.BuildResultDao;
34  import org.apache.continuum.notification.mail.MockJavaMailSender;
35  import org.apache.maven.continuum.AbstractContinuumTest;
36  import org.apache.maven.continuum.model.project.BuildDefinition;
37  import org.apache.maven.continuum.model.project.BuildResult;
38  import org.apache.maven.continuum.model.project.Project;
39  import org.apache.maven.continuum.model.project.ProjectGroup;
40  import org.apache.maven.continuum.model.project.ProjectNotifier;
41  import org.apache.maven.continuum.notification.ContinuumNotificationDispatcher;
42  import org.apache.maven.continuum.notification.MessageContext;
43  import org.apache.maven.continuum.notification.Notifier;
44  import org.apache.maven.continuum.project.ContinuumProjectState;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  import org.springframework.mail.javamail.JavaMailSender;
48  
49  /**
50   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
51   * @version $Id: MailContinuumNotifierTest.java 790386 2009-07-01 21:27:25Z evenisse $
52   */
53  public class MailContinuumNotifierTest
54      extends AbstractContinuumTest
55  {
56      protected static final Logger logger = LoggerFactory.getLogger( MailContinuumNotifierTest.class );
57  
58      public void testSuccessfulBuild()
59          throws Exception
60      {
61          MailContinuumNotifier notifier = (MailContinuumNotifier) lookup( Notifier.class.getName(), "mail" );
62          String toOverride = "recipient@host.com";
63          notifier.setToOverride( toOverride );
64  
65          ProjectGroup group = createStubProjectGroup( "foo.bar", "" );
66  
67          BuildResultDao brDao = (BuildResultDao) lookup( BuildResultDao.class );
68          Project project = addProject( "Test Project", group );
69          BuildResult br = makeBuild( ContinuumProjectState.FAILED );
70          brDao.addBuildResult( project, br );
71  
72          br = makeBuild( ContinuumProjectState.OK );
73          brDao.addBuildResult( project, br );
74  
75          br = makeBuild( ContinuumProjectState.FAILED );
76          brDao.addBuildResult( project, br );
77  
78          BuildResult build = makeBuild( ContinuumProjectState.OK );
79          assertEquals( ContinuumProjectState.OK, build.getState() );
80  
81          project.setState( build.getState() );
82          getProjectDao().updateProject( project );
83  
84          BuildDefinition buildDef = new BuildDefinition();
85          buildDef.setType( "maven2" );
86          buildDef.setBuildFile( "pom.xml" );
87          buildDef.setGoals( "clean install" );
88          buildDef.setArguments( "" );
89          BuildDefinitionDao buildDefDao = (BuildDefinitionDao) lookup( BuildDefinitionDao.class );
90          buildDef = buildDefDao.addBuildDefinition( buildDef );
91          build.setBuildDefinition( buildDef );
92          assertEquals( ContinuumProjectState.OK, build.getState() );
93  
94          brDao.addBuildResult( project, build );
95          build = brDao.getLatestBuildResultForProjectWithDetails( project.getId() );
96          assertEquals( ContinuumProjectState.OK, build.getState() );
97  
98          MimeMessage mailMessage =
99              sendNotificationAndGetMessage( project, build, buildDef, "lots out build output", toOverride );
100 
101         assertEquals( "[continuum] BUILD SUCCESSFUL: foo.bar Test Project", mailMessage.getSubject() );
102 
103         String mailContent = dumpContent( mailMessage, "recipient@host.com" );
104 
105         //CONTINUUM-1946
106         assertTrue( mailContent.indexOf( "Goals: clean install" ) > 0 );
107     }
108 
109     public void testFailedBuild()
110         throws Exception
111     {
112         ProjectGroup group = createStubProjectGroup( "foo.bar", "" );
113 
114         Project project = addProject( "Test Project", group );
115 
116         BuildResult build = makeBuild( ContinuumProjectState.FAILED );
117 
118         MimeMessage mailMessage = sendNotificationAndGetMessage( project, build, null, "output", null );
119 
120         assertEquals( "[continuum] BUILD FAILURE: foo.bar Test Project", mailMessage.getSubject() );
121 
122         dumpContent( mailMessage );
123     }
124 
125     public void testErrorenousBuild()
126         throws Exception
127     {
128         ProjectGroup group = createStubProjectGroup( "foo.bar", "" );
129 
130         Project project = addProject( "Test Project", group );
131 
132         BuildResult build = makeBuild( ContinuumProjectState.ERROR );
133 
134         build.setError( "Big long error message" );
135 
136         MimeMessage mailMessage = sendNotificationAndGetMessage( project, build, null, "lots of stack traces", null );
137 
138         assertEquals( "[continuum] BUILD ERROR: foo.bar Test Project", mailMessage.getSubject() );
139 
140         dumpContent( mailMessage );
141     }
142 
143     private String dumpContent( MimeMessage mailMessage )
144         throws Exception
145     {
146         return dumpContent( mailMessage, null );
147     }
148 
149     private String dumpContent( MimeMessage mailMessage, String toOverride )
150         throws Exception
151     {
152         Address[] tos = mailMessage.getRecipients( RecipientType.TO );
153         if ( toOverride != null )
154         {
155             assertEquals( toOverride, ( (InternetAddress) tos[0] ).getAddress() );
156         }
157         else
158         {
159             assertEquals( "foo@bar", ( (InternetAddress) tos[0] ).getAddress() );
160         }
161         assertTrue( "The template isn't loaded correctly.",
162                     ( (String) mailMessage.getContent() ).indexOf( "#shellBuildResult()" ) < 0 );
163         assertTrue( "The template isn't loaded correctly.",
164                     ( (String) mailMessage.getContent() ).indexOf( "Build statistics" ) > 0 );
165 
166         String mailContent = (String) mailMessage.getContent();
167 
168         logger.info( mailContent );
169 
170         return mailContent;
171     }
172 
173     // ----------------------------------------------------------------------
174     //
175     // ----------------------------------------------------------------------
176 
177     private MimeMessage sendNotificationAndGetMessage( Project project, BuildResult build, BuildDefinition buildDef,
178                                                        String buildOutput, String toOverride )
179         throws Exception
180     {
181         MessageContext context = new MessageContext();
182 
183         context.setProject( project );
184 
185         context.setBuildResult( build );
186 
187         context.setBuildDefinition( buildDef );
188 
189         ProjectNotifier projectNotifier = new ProjectNotifier();
190         projectNotifier.setType( "mail" );
191         Map<String, String> config = new HashMap<String, String>();
192         config.put( MailContinuumNotifier.ADDRESS_FIELD, "foo@bar" );
193         projectNotifier.setConfiguration( config );
194         List<ProjectNotifier> projectNotifiers = new ArrayList<ProjectNotifier>();
195         projectNotifiers.add( projectNotifier );
196         context.setNotifier( projectNotifiers );
197 
198         //context.put( ContinuumNotificationDispatcher.CONTEXT_BUILD_OUTPUT, buildOutput );
199 
200         //context.put( "buildHost", "foo.bar.com" );
201 
202         // ----------------------------------------------------------------------
203         //
204         // ----------------------------------------------------------------------
205 
206         Notifier notifier = (Notifier) lookup( Notifier.class.getName(), "mail" );
207 
208         ( (MailContinuumNotifier) notifier ).setBuildHost( "foo.bar.com" );
209 
210         notifier.sendMessage( ContinuumNotificationDispatcher.MESSAGE_ID_BUILD_COMPLETE, context );
211 
212         // ----------------------------------------------------------------------
213         //
214         // ----------------------------------------------------------------------
215 
216         MockJavaMailSender mailSender = (MockJavaMailSender) lookup( JavaMailSender.class, "continuum" );
217 
218         assertEquals( 1, mailSender.getReceivedEmails().size() );
219 
220         List<MimeMessage> mails = mailSender.getReceivedEmails();
221 
222         MimeMessage mailMessage = mails.get( 0 );
223 
224         // ----------------------------------------------------------------------
225         //
226         // ----------------------------------------------------------------------
227 
228         assertEquals( "continuum@localhost", ( (InternetAddress) mailMessage.getFrom()[0] ).getAddress() );
229 
230         assertEquals( "Continuum", ( (InternetAddress) mailMessage.getFrom()[0] ).getPersonal() );
231 
232         Address[] tos = mailMessage.getRecipients( RecipientType.TO );
233 
234         assertEquals( 1, tos.length );
235 
236         assertEquals( toOverride == null ? "foo@bar" : toOverride, ( (InternetAddress) tos[0] ).getAddress() );
237 
238         return mailMessage;
239     }
240 
241     private BuildResult makeBuild( int state )
242     {
243         BuildResult build = new BuildResult();
244 
245         build.setStartTime( System.currentTimeMillis() );
246 
247         build.setEndTime( System.currentTimeMillis() + 1234567 );
248 
249         build.setState( state );
250 
251         build.setTrigger( ContinuumProjectState.TRIGGER_FORCED );
252 
253         build.setExitCode( 10 );
254 
255         return build;
256     }
257 }