001 /*********************************************************************************************************************** 002 * 003 * JavaMail Mock2 Provider - open source mock classes for mock up JavaMail 004 * ======================================================================= 005 * 006 * Copyright (C) 2014 by Hendrik Saly (http://saly.de) 007 * 008 * Based on ideas from Kohsuke Kawaguchi's Mock-javamail (https://java.net/projects/mock-javamail) 009 * 010 *********************************************************************************************************************** 011 * 012 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 013 * the License. You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 018 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations under the License. 020 * 021 *********************************************************************************************************************** 022 * 023 * $Id:$ 024 * 025 **********************************************************************************************************************/ 026 package de.saly.javamail.mock2; 027 028 import java.util.Date; 029 030 import javax.activation.DataHandler; 031 import javax.mail.Address; 032 import javax.mail.Flags; 033 import javax.mail.Folder; 034 import javax.mail.IllegalWriteException; 035 import javax.mail.Message; 036 import javax.mail.MessagingException; 037 import javax.mail.Multipart; 038 import javax.mail.internet.MimeMessage; 039 040 public class MockMessage extends MimeMessage implements Comparable<MockMessage> { 041 042 public static interface FlagChangeListener { 043 void onFlagChange(MockMessage msg, Flags flags, boolean set); 044 } 045 046 private final FlagChangeListener flagChangeListener; 047 private Folder folder; 048 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass()); 049 private final MailboxFolder mbf; 050 051 private final long mockid; 052 053 protected MockMessage(final Message source, final Folder folder) throws MessagingException { 054 this((MimeMessage) source, ((MockMessage) source).mockid, ((MockMessage) source).mbf, ((MockMessage) source).flagChangeListener); 055 this.folder = folder; 056 this.setMessageNumber(source.getMessageNumber()); 057 } 058 059 protected MockMessage(final MimeMessage source, final long mockid, final MailboxFolder mbf, final FlagChangeListener flagChangeListener) 060 throws MessagingException { 061 super(source); 062 this.mockid = mockid; 063 this.flagChangeListener = flagChangeListener; 064 this.mbf = mbf; 065 066 } 067 068 /* (non-Javadoc) 069 * @see javax.mail.internet.MimeMessage#addFrom(javax.mail.Address[]) 070 */ 071 @Override 072 public void addFrom(final Address[] addresses) throws MessagingException { 073 074 throw new IllegalWriteException("Mock messages are read-only"); 075 } 076 077 /* (non-Javadoc) 078 * @see javax.mail.internet.MimeMessage#addHeader(java.lang.String, java.lang.String) 079 */ 080 @Override 081 public void addHeader(final String name, final String value) throws MessagingException { 082 throw new IllegalWriteException("Mock messages are read-only"); 083 } 084 085 /* (non-Javadoc) 086 * @see javax.mail.internet.MimeMessage#addHeaderLine(java.lang.String) 087 */ 088 @Override 089 public void addHeaderLine(final String line) throws MessagingException { 090 throw new IllegalWriteException("Mock messages are read-only"); 091 } 092 093 /* (non-Javadoc) 094 * @see javax.mail.Message#addRecipient(javax.mail.Message.RecipientType, javax.mail.Address) 095 */ 096 @Override 097 public void addRecipient(final javax.mail.Message.RecipientType type, final Address address) throws MessagingException { 098 throw new IllegalWriteException("Mock messages are read-only"); 099 } 100 101 /* (non-Javadoc) 102 * @see javax.mail.internet.MimeMessage#addRecipients(javax.mail.Message.RecipientType, javax.mail.Address[]) 103 */ 104 @Override 105 public void addRecipients(final javax.mail.Message.RecipientType type, final Address[] addresses) throws MessagingException { 106 throw new IllegalWriteException("Mock messages are read-only"); 107 } 108 109 /* (non-Javadoc) 110 * @see javax.mail.internet.MimeMessage#addRecipients(javax.mail.Message.RecipientType, java.lang.String) 111 */ 112 @Override 113 public void addRecipients(final javax.mail.Message.RecipientType type, final String addresses) throws MessagingException { 114 throw new IllegalWriteException("Mock messages are read-only"); 115 } 116 117 @Override 118 public int compareTo(final MockMessage o) { 119 120 return new Long(this.getMockid()).compareTo(new Long(o.getMockid())); 121 } 122 123 @Override 124 public synchronized Folder getFolder() { 125 if (folder == null) { 126 throw new RuntimeException("wrong/unshaded mock message"); 127 } else { 128 return folder; 129 } 130 } 131 132 // IllegalWriteException("Mock messages are read-only"); 133 134 /** 135 * @return the mockid 136 */ 137 public long getMockid() { 138 return mockid; 139 } 140 141 /* (non-Javadoc) 142 * @see javax.mail.internet.MimeMessage#removeHeader(java.lang.String) 143 */ 144 @Override 145 public void removeHeader(final String name) throws MessagingException { 146 throw new IllegalWriteException("Mock messages are read-only"); 147 } 148 149 /* (non-Javadoc) 150 * @see javax.mail.internet.MimeMessage#saveChanges() 151 */ 152 @Override 153 public void saveChanges() throws MessagingException { 154 throw new IllegalWriteException("Mock messages are read-only"); 155 } 156 157 /* (non-Javadoc) 158 * @see javax.mail.internet.MimeMessage#setContent(javax.mail.Multipart) 159 */ 160 @Override 161 public void setContent(final Multipart mp) throws MessagingException { 162 throw new IllegalWriteException("Mock messages are read-only"); 163 } 164 165 /* (non-Javadoc) 166 * @see javax.mail.internet.MimeMessage#setContent(java.lang.Object, java.lang.String) 167 */ 168 @Override 169 public void setContent(final Object o, final String type) throws MessagingException { 170 throw new IllegalWriteException("Mock messages are read-only"); 171 } 172 173 /* (non-Javadoc) 174 * @see javax.mail.internet.MimeMessage#setContentID(java.lang.String) 175 */ 176 @Override 177 public void setContentID(final String cid) throws MessagingException { 178 throw new IllegalWriteException("Mock messages are read-only"); 179 } 180 181 /* (non-Javadoc) 182 * @see javax.mail.internet.MimeMessage#setContentLanguage(java.lang.String[]) 183 */ 184 @Override 185 public void setContentLanguage(final String[] languages) throws MessagingException { 186 throw new IllegalWriteException("Mock messages are read-only"); 187 } 188 189 /* (non-Javadoc) 190 * @see javax.mail.internet.MimeMessage#setContentMD5(java.lang.String) 191 */ 192 @Override 193 public void setContentMD5(final String md5) throws MessagingException { 194 throw new IllegalWriteException("Mock messages are read-only"); 195 } 196 197 @Override 198 public void setDataHandler(final DataHandler content) throws MessagingException { 199 throw new IllegalWriteException("Mock messages are read-only"); 200 } 201 202 /* (non-Javadoc) 203 * @see javax.mail.internet.MimeMessage#setDescription(java.lang.String) 204 */ 205 @Override 206 public void setDescription(final String description) throws MessagingException { 207 throw new IllegalWriteException("Mock messages are read-only"); 208 } 209 210 /* (non-Javadoc) 211 * @see javax.mail.internet.MimeMessage#setDescription(java.lang.String, java.lang.String) 212 */ 213 @Override 214 public void setDescription(final String description, final String charset) throws MessagingException { 215 throw new IllegalWriteException("Mock messages are read-only"); 216 } 217 218 /* (non-Javadoc) 219 * @see javax.mail.internet.MimeMessage#setDisposition(java.lang.String) 220 */ 221 @Override 222 public void setDisposition(final String disposition) throws MessagingException { 223 throw new IllegalWriteException("Mock messages are read-only"); 224 } 225 226 @Override 227 protected void setExpunged(final boolean expunged) { 228 229 super.setExpunged(expunged); 230 } 231 232 /* (non-Javadoc) 233 * @see javax.mail.internet.MimeMessage#setFileName(java.lang.String) 234 */ 235 @Override 236 public void setFileName(final String filename) throws MessagingException { 237 throw new IllegalWriteException("Mock messages are read-only"); 238 } 239 240 /* (non-Javadoc) 241 * @see javax.mail.internet.MimeMessage#setFlags(javax.mail.Flags, boolean) 242 */ 243 @Override 244 public synchronized void setFlags(final Flags flag, final boolean set) throws MessagingException { 245 246 super.setFlags(flag, set); 247 248 if (flagChangeListener != null) { 249 flagChangeListener.onFlagChange(this, flag, set); 250 } 251 252 } 253 254 /* (non-Javadoc) 255 * @see javax.mail.internet.MimeMessage#setFrom() 256 */ 257 @Override 258 public void setFrom() throws MessagingException { 259 throw new IllegalWriteException("Mock messages are read-only"); 260 } 261 262 /* (non-Javadoc) 263 * @see javax.mail.internet.MimeMessage#setFrom(javax.mail.Address) 264 */ 265 @Override 266 public void setFrom(final Address address) throws MessagingException { 267 throw new IllegalWriteException("Mock messages are read-only"); 268 } 269 270 /* (non-Javadoc) 271 * @see javax.mail.internet.MimeMessage#setFrom(java.lang.String) 272 */ 273 @Override 274 public void setFrom(final String address) throws MessagingException { 275 throw new IllegalWriteException("Mock messages are read-only"); 276 } 277 278 /* (non-Javadoc) 279 * @see javax.mail.internet.MimeMessage#setHeader(java.lang.String, java.lang.String) 280 */ 281 @Override 282 public void setHeader(final String name, final String value) throws MessagingException { 283 throw new IllegalWriteException("Mock messages are read-only"); 284 } 285 286 @Override 287 protected void setMessageNumber(final int msgnum) { 288 289 super.setMessageNumber(msgnum); 290 } 291 292 /* (non-Javadoc) 293 * @see javax.mail.Message#setRecipient(javax.mail.Message.RecipientType, javax.mail.Address) 294 */ 295 @Override 296 public void setRecipient(final javax.mail.Message.RecipientType type, final Address address) throws MessagingException { 297 throw new IllegalWriteException("Mock messages are read-only"); 298 } 299 300 /* (non-Javadoc) 301 * @see javax.mail.internet.MimeMessage#setRecipients(javax.mail.Message.RecipientType, javax.mail.Address[]) 302 */ 303 @Override 304 public void setRecipients(final javax.mail.Message.RecipientType type, final Address[] addresses) throws MessagingException { 305 throw new IllegalWriteException("Mock messages are read-only"); 306 } 307 308 /* (non-Javadoc) 309 * @see javax.mail.internet.MimeMessage#setRecipients(javax.mail.Message.RecipientType, java.lang.String) 310 */ 311 @Override 312 public void setRecipients(final javax.mail.Message.RecipientType type, final String addresses) throws MessagingException { 313 throw new IllegalWriteException("Mock messages are read-only"); 314 } 315 316 /* (non-Javadoc) 317 * @see javax.mail.internet.MimeMessage#setReplyTo(javax.mail.Address[]) 318 */ 319 @Override 320 public void setReplyTo(final Address[] addresses) throws MessagingException { 321 throw new IllegalWriteException("Mock messages are read-only"); 322 } 323 324 /* (non-Javadoc) 325 * @see javax.mail.internet.MimeMessage#setSender(javax.mail.Address) 326 */ 327 @Override 328 public void setSender(final Address address) throws MessagingException { 329 throw new IllegalWriteException("Mock messages are read-only"); 330 } 331 332 /* (non-Javadoc) 333 * @see javax.mail.internet.MimeMessage#setSentDate(java.util.Date) 334 */ 335 @Override 336 public void setSentDate(final Date d) throws MessagingException { 337 throw new IllegalWriteException("Mock messages are read-only"); 338 } 339 340 void setSpecialHeader(final String name, final String value) throws MessagingException { 341 super.addHeader(name, value); 342 } 343 344 /* (non-Javadoc) 345 * @see javax.mail.internet.MimeMessage#setSubject(java.lang.String) 346 */ 347 @Override 348 public void setSubject(final String subject) throws MessagingException { 349 throw new IllegalWriteException("Mock messages are read-only"); 350 } 351 352 /* (non-Javadoc) 353 * @see javax.mail.internet.MimeMessage#setSubject(java.lang.String, java.lang.String) 354 */ 355 @Override 356 public void setSubject(final String subject, final String charset) throws MessagingException { 357 throw new IllegalWriteException("Mock messages are read-only"); 358 } 359 360 /* (non-Javadoc) 361 * @see javax.mail.internet.MimeMessage#setText(java.lang.String) 362 */ 363 @Override 364 public void setText(final String text) throws MessagingException { 365 throw new IllegalWriteException("Mock messages are read-only"); 366 } 367 368 /* (non-Javadoc) 369 * @see javax.mail.internet.MimeMessage#setText(java.lang.String, java.lang.String) 370 */ 371 @Override 372 public void setText(final String text, final String charset) throws MessagingException { 373 throw new IllegalWriteException("Mock messages are read-only"); 374 } 375 376 /* (non-Javadoc) 377 * @see javax.mail.internet.MimeMessage#setText(java.lang.String, java.lang.String, java.lang.String) 378 */ 379 @Override 380 public void setText(final String text, final String charset, final String subtype) throws MessagingException { 381 throw new IllegalWriteException("Mock messages are read-only"); 382 } 383 384 }