File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\maven\reporters\MavenMailerTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, Dominik Bartholdi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.maven.reporters;
25
26 import hudson.maven.MavenModuleSet;
27 import hudson.model.Result;
28 import hudson.tasks.Mailer;
29 import hudson.tasks.Mailer.DescriptorImpl;
30
31 import javax.mail.Address;
32 import javax.mail.internet.InternetAddress;
33
34 import jenkins.model.Jenkins;
35
36 import org.jvnet.hudson.test.Bug;
37 import org.jvnet.hudson.test.ExtractResourceSCM;
38 import org.jvnet.hudson.test.HudsonTestCase;
39 import org.jvnet.mock_javamail.Mailbox;
40
41 /**
42 *
43 * @author imod (Dominik Bartholdi)
44 *
45 */
46 public class MavenMailerTest extends HudsonTestCase {
47
48 @Bug(5695)
49 public void testMulipleMails() throws Exception {
50
51 // there is one module failing in the build, therefore we expect one mail for the failed module and one for the over all build status
52 final Mailbox inbox = runMailTest(true);
53 assertEquals(2, inbox.size());
54
55 }
56
57 @Bug(5695)
58 public void testSingleMails() throws Exception {
59
60 final Mailbox inbox = runMailTest(false);
61 assertEquals(1, inbox.size());
62
63 }
64
65 public Mailbox runMailTest(boolean perModuleEamil) throws Exception {
66
67 final DescriptorImpl mailDesc = Jenkins.getInstance().getDescriptorByType(Mailer.DescriptorImpl.class);
68
69 // intentionally give the whole thin in a double quote
70 Mailer.descriptor().setAdminAddress("\"me <me@sun.com>\"");
71
72 String recipient = "you <you@sun.com>";
73 Mailbox yourInbox = Mailbox.get(new InternetAddress(recipient));
74 yourInbox.clear();
75
76 configureDefaultMaven();
77 MavenModuleSet mms = createMavenProject();
78 mms.setGoals("test");
79 mms.setScm(new ExtractResourceSCM(getClass().getResource("/hudson/maven/maven-multimodule-unit-failure.zip")));
80 assertBuildStatus(Result.UNSTABLE, mms.scheduleBuild2(0).get());
81
82 MavenMailer m = new MavenMailer();
83 m.recipients = recipient;
84 m.perModuleEmail = perModuleEamil;
85 mms.getReporters().add(m);
86
87 mms.scheduleBuild2(0).get();
88
89 Address[] senders = yourInbox.get(0).getFrom();
90 assertEquals(1, senders.length);
91 assertEquals("me <me@sun.com>", senders[0].toString());
92
93 return yourInbox;
94 }
95
96 }
97