1.8 使用JAVA脚本发送测试报告
测试报告产生了,为了配合CI的实现,可以用JAVA来实现发送测试报告到相关人员的邮件系统中,代码如下。
案例3:利用JAVA发送电子邮件。
package Util.com.jerry; import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.AuthenticationFailedException; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import org.apache.commons.lang.StringUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; /** * 实现邮件发送功能 * @authorAdministrator * */ public class EmailSender { privatestatic final Logger logger = LogManager.getLogger(EmailSender.class); privateString host; // 服务器地址 privateString from; // 发件人 privateString to; // 收件人 多个收件人以,分隔 privateString title; // 主题 privateString content; // 内容 privateList<File> attachmentlist ; //附件集 privateString username; // 用户名 privateString password; // 密码 /**发件人员工编号*/ privateString sendEmployeeId; publicString getSendEmployeeId() { returnsendEmployeeId; } publicvoid setSendEmployeeId(String sendEmployeeId) { this.sendEmployeeId= sendEmployeeId; } publicString getHost() { returnhost; } publicvoid setHost(String host) { this.host= host; } publicString getFrom() { returnfrom; } publicvoid setFrom(String from) { this.from= from; } publicString getTo() { returnto; } publicvoid setTo(String to) { this.to= to; } publicString getTitle() { returntitle; } publicvoid setTitle(String title) { this.title= title; } publicString getContent() { returncontent; } publicvoid setContent(String content) { this.content= content; } publicList<File> getAttachmentlist() { returnattachmentlist; } publicvoid setAttachmentlist(List<File> attachmentlist) { this.attachmentlist= attachmentlist; } publicString getUsername() { returnusername; } publicvoid setUsername(String username) { this.username= username; } publicString getPassword() { returnpassword; } publicvoid setPassword(String password) { this.password= password; } publicString getPort() { returnport; } publicvoid setPort(String port) { this.port= port; } privateString port; publicEmailSender(String host, String from, String to, String title, Stringcontent, List attachmentlist, String username, String password,String port) { this.host= host; this.from= from; this.to= to; this.title= title; this.content= content; this.attachmentlist= attachmentlist; this.username= username; this.password= password; this.port=port; } publicEmailSender(String to, String title, Stringcontent, List attachmentlist) { this.to= to; this.title= title; this.content= content; this.attachmentlist= attachmentlist; } /** * 发送邮件 * @return 发送状态信息index0:状态 0成功 1失败;index1:描述错误信息 */ publicString[] sendMail(){ String[]result=new String[2]; Sessionsession=null; Propertiesprops = System.getProperties(); props.put("mail.smtp.host",host); props.put("mail.smtp.sendpartial","true"); props.put("mail.smtp.port",port); if(StringUtils.isBlank(username)){//不需要验证用户名密码 session= Session.getDefaultInstance(props, null); }else{ props.put("mail.smtp.auth","true"); EmailAuthenticatorauth = new EmailAuthenticator(username, password); session= Session.getInstance(props, auth); } //设置邮件发送信息 try{ //创建邮件 MimeMessagemessage = new MimeMessage(session); //设置发件人地址 message.setFrom(newInternetAddress(from)); //设置收件人地址(多个邮件地址) InternetAddress[]toAddr = InternetAddress.parse(to); message.addRecipients(Message.RecipientType.TO,toAddr); //设置邮件主题 message.setSubject(title); //设置发送时间 message.setSentDate(newDate()); //设置发送内容 Multipartmultipart = new MimeMultipart(); MimeBodyPartcontentPart = new MimeBodyPart(); contentPart.setText(content); multipart.addBodyPart(contentPart); //设置附件 if(attachmentlist!=null&& attachmentlist.size()>0){ for(inti = 0 ; i < attachmentlist.size();i++){ MimeBodyPartattachmentPart = new MimeBodyPart(); FileDataSourcesource = new FileDataSource(attachmentlist.get(i)); attachmentPart.setDataHandler(newDataHandler(source)); attachmentPart.setFileName(MimeUtility.encodeWord(attachmentlist.get(i).getName(),"gb2312", null)); multipart.addBodyPart(attachmentPart); } } message.setContent(multipart); //登录SMTP服务器 if(StringUtils.isBlank(username)) { //不需验证 Transport.send(message); }else { //需要验证 Transporttransport = session.getTransport("smtp"); transport.connect(); transport.sendMessage(message,message.getAllRecipients()); transport.close(); } result[0]="0"; result[1]="发送成功"; logger.info("邮件发送成功!发送人:"+from); }catch(MessagingExceptionmex){ result[0]="1"; result[1]="邮件服务器发生错误"; if(mexinstanceof AuthenticationFailedException){ result[1]="用户名或密码错误"; } }catch (Exception e) { result[0]="1"; result[1]="系统异常"; } returnresult; } publicstatic void main(String[] args){ StringSNMPTServer = "smtp.126.com"; Stringfrom="xianggu625@126.com"; Stringto="hedan@cmss.chinamobile.com"; Stringtitle="发送测试报告"; Stringcontent="附件为测试报告"; Stringusername="xianggu625@126.com"; Stringpassword="123456"; Stringport="25"; Listlist=new ArrayList(); list.add(newFile("C:\\myjava\\web\\junit.rar"));#junit.rar为发送测试报告的目录压缩jar包 EmailSendersender=newEmailSender(SNMPTServer,from,to,title,content,list,username,password,port); String[] result = sender.sendMail(); System.out.println(result[1]+"ffffffffffffffff"); } } /** * classMyAuthenticator用于邮件服务器认证 构造器需要用户名、密码作参数 */ class EmailAuthenticator extends Authenticator { privateString username = null; privateString password = null; publicEmailAuthenticator(String username, String password) { this.username= username; this.password= password; } publicPasswordAuthentication getPasswordAuthentication() { returnnew PasswordAuthentication(username, password); } }
读者只要修改main()方法开始部分的设置就可以了。
星云测试
奇林软件
联合通测
顾翔凡言:
k=(p+m)t
其中:
k为常数。
p:团队人员质量水平,为单位小时内产生的有效质量,单位为/h;
m:团队方法质量水平,为单位小时内产生的有效质量,单位为/h;
t:为单位质量产品的交付时间,单位为h。
在团队方法质量水平不变,团队人员质量水平提高的情况下,交付时间变短;
在团队人员质量水平不变,团队方法质量水平提高的情况下,交付时间变短;
团队人员质量水平与方法质量水平乘积决定了软件的质量水平,如果这个值变小,则t变大;反之t变小。
例如,当k=4时:
当人员质量水平为1/小时、方法水平为1/小时时,交付时间为2小时。
当人员质量水平为2/小时、方法水平为1/小时时,交付时间为4/3小时。
当人员质量水平为1/小时、方法水平为2/小时时,交付时间为4/3小时。
当人员质量水平为0.5/小时、方法水平为0.5/小时时,交付时间为4小时。