EqualityCheck xref

View Javadoc

    1   package com.ozacc.mail.mock;
    2
    3   import javax.mail.internet.InternetAddress;
    4
    5   import com.ozacc.mail.Mail;
    6
    7   /***
    8    * ¥á¡¼¥?¤¬Æ±ÃÍ¤Ç¤¢¤?¤³¤È¤òÄ´¤Ù¤?¥á¥½¥Ã¥É¤òÄó¶¡¡£
    9    * <p>
    10   * <strong>Ã?:</strong> ÅºÉÕ¥Õ¥¡¥¤¥?¤ÏÈæ³ÓÂÐ¾Ý¤Ë¤Ê¤ê¤Þ¤»¤ó¡£
    11   *
    12   * @since 1.1
    13   *
    14   * @author Tomohiro Otsuka
    15   * @version $Id: EqualityCheck.java,v 1.5 2004/10/31 04:33:33 otsuka Exp $
    16   */
    17  public class EqualityCheck {
    18
    19  	private EqualityCheck() {}
    20
    21  	/***
    22  	 * expected¤Èsent¤Î¥á¡¼¥?ÆâÍÆ¤¬Æ±°?¤Ç¤¢¤?¤«¤É¤¦¤«¤òÈ½Äê¤·¤Þ¤¹¡£<br>
    23  	 * MultipartMail¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤â»ØÄê¤Ç¤¤Þ¤¹¡£Ã¢¤·¡¢ÅºÉÕ¥Õ¥¡¥¤¥?¤Ï¥Á¥§¥Ã¥¯¤µ¤?¤Þ¤»¤ó¡£
    24  	 *
    25  	 * @param expected
    26  	 * @param sent
    27  	 * @return expected¤Èsent¤Î¥á¡¼¥?ÆâÍÆ¤¬Æ±°?¤Ç¤¢¤?¾?¹? true
    28  	 */
    29  	public static boolean equals(Mail expected, Mail sent) {
    30  		boolean mockMode = (expected instanceof MockMail);
    31
    32  		// ¥Þ¥?¥Á¥Ñ¡¼¥È¥á¡¼¥?¤Î¾?¹?
    33  		if (expected.isMultipartMail()) {
    34
    35  			// HTML
    36  			if (!mockMode) {
    37  				if ((expected.getHtmlText() == null && sent.getHtmlText() != null)
    38  						|| (expected.getHtmlText() != null && sent.getHtmlText() == null)
    39  						|| (!expected.getHtmlText().equals(sent.getHtmlText()))) {
    40  					return false;
    41  				}
    42  			} else if (mockMode && expected.getHtmlText() != null) {
    43  				if (!expected.getHtmlText().equals(sent.getHtmlText())) {
    44  					return false;
    45  				}
    46  			}
    47  		}
    48
    49  		// Return-Path
    50  		if (!mockMode || (mockMode && expected.getReturnPath() != null)) {
    51  			if (expected.getReturnPath() != null && sent.getReturnPath() != null) {
    52  				if (!expected.getReturnPath().equals(sent.getReturnPath())) {
    53  					return false;
    54  				}
    55  			} else if ((expected.getReturnPath() != null && sent.getReturnPath() == null)
    56  					|| (expected.getReturnPath() == null && sent.getReturnPath() != null)) {
    57  				return false;
    58  			}
    59  		}
    60
    61  		//	From
    62  		if (!mockMode || (mockMode && expected.getFrom() != null)) {
    63  			if (expected.getFrom() != null && sent.getFrom() != null) {
    64  				if (!equals(expected.getFrom(), sent.getFrom())) {
    65  					return false;
    66  				}
    67  			} else if ((expected.getFrom() != null && sent.getFrom() == null)
    68  					|| (expected.getFrom() == null && sent.getFrom() != null)) {
    69  				return false;
    70  			}
    71  		}
    72
    73  		// to
    74  		InternetAddress[] expectedAddresses = expected.getTo();
    75  		InternetAddress[] sentAddresses = sent.getTo();
    76  		if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
    77  			if (expectedAddresses.length != sentAddresses.length) {
    78  				return false;
    79  			}
    80  			for (int i = 0; i < expectedAddresses.length; i++) {
    81  				if (!equals(expectedAddresses[i], sentAddresses[i])) {
    82  					return false;
    83  				}
    84  			}
    85  		}
    86
    87  		// cc
    88  		expectedAddresses = expected.getCc();
    89  		sentAddresses = sent.getCc();
    90  		if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
    91  			if (expectedAddresses.length != sentAddresses.length) {
    92  				return false;
    93  			}
    94  			for (int i = 0; i < expectedAddresses.length; i++) {
    95  				if (!equals(expectedAddresses[i], sentAddresses[i])) {
    96  					return false;
    97  				}
    98  			}
    99  		}
    100
    101 		// bcc
    102 		expectedAddresses = expected.getBcc();
    103 		sentAddresses = sent.getBcc();
    104 		if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
    105 			if (expectedAddresses.length != sentAddresses.length) {
    106 				return false;
    107 			}
    108 			for (int i = 0; i < expectedAddresses.length; i++) {
    109 				if (!equals(expectedAddresses[i], sentAddresses[i])) {
    110 					return false;
    111 				}
    112 			}
    113 		}
    114
    115 		// Reply-To
    116 		if (!mockMode || (mockMode && expected.getReplyTo() != null)) {
    117 			if (expected.getReplyTo() != null && sent.getReplyTo() != null) {
    118 				if (!equals(expected.getReplyTo(), sent.getReplyTo())) {
    119 					return false;
    120 				}
    121 			} else if ((expected.getReplyTo() != null && sent.getReplyTo() == null)
    122 					|| (expected.getReplyTo() == null && sent.getReplyTo() != null)) {
    123 				return false;
    124 			}
    125 		}
    126
    127 		// ·?Ì¾
    128 		if (!mockMode || (mockMode && expected.getSubject().length() > 0)) {
    129 			if (!expected.getSubject().equals(sent.getSubject())) {
    130 				return false;
    131 			}
    132 		}
    133
    134 		// ËÜÊ¸
    135 		if (!mockMode || (mockMode && expected.getText().length() > 0)) {
    136 			if (!expected.getText().equals(sent.getText())) {
    137 				return false;
    138 			}
    139 		}
    140
    141 		return true;
    142 	}
    143
    144 	/***
    145 	 * »ØÄê¤µ¤?¤¿Æó¤Ä¤ÎInternetAddress¥¤¥ó¥¹¥¿¥ó¥¹¤¬Åù¤·¤¤¤«¤É¤¦¤«È½Äê¤·¤Þ¤¹¡£
    146 	 * <p>
    147 	 * InternetAddress#equals()¥á¥½¥Ã¥É¤Ç¤Ï¡¢¥á¡¼¥?¥¢¥É¥?¥¹¤·¤«¸¡ºº¤·¤Ê¤¤¤¿¤á¡¢
    148 	 * ¤³¤Î¥á¥½¥Ã¥É¤Ç¤ÏInternetAddress¤ËÌ¾Á°¤¬´Þ¤Þ¤?¤Æ¤¤¤?¾?¹ç¡¢¤½¤ÎÌ¾Á°¤?
    149 	 * Åù¤·¤¤¤«¤É¤¦¤«¸¡ºº¤·¤Þ¤¹¡£
    150 	 *
    151 	 * @since 1.1.3
    152 	 * @param a Èæ³Ó¤¹¤?InternetAddress¥¤¥ó¥¹¥¿¥ó¥¹
    153 	 * @param b Èæ³Ó¤¹¤?InternetAddress¥¤¥ó¥¹¥¿¥ó¥¹
    154 	 * @return Æó¤Ä¤ÎInternetAddress¥¤¥ó¥¹¥¿¥ó¥¹¤¬Åù¤·¤¤¾?¹? true
    155 	 */
    156 	public static boolean equals(InternetAddress a, InternetAddress b) {
    157 		if (a.equals(b)) {
    158 			if (a.getPersonal() != null || b.getPersonal() != null) {
    159 				return a.getPersonal().equals(b.getPersonal());
    160 			} else {
    161 				return true;
    162 			}
    163 		}
    164 		return false;
    165 	}
    166 }