1 19 20 package org.apache.james.mailboxmanager.redundant; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 27 import javax.mail.MessagingException ; 28 import javax.mail.internet.MimeMessage ; 29 30 import org.apache.mailet.Mail; 31 32 public abstract class AbstractMailRepositorySelfTestCase extends AbstractMailRepositoryTestCase { 33 34 38 public void testStoreRetrieve() throws MessagingException , IOException { 39 int addedCount = 0; 40 Collection added=new ArrayList (); 41 for (int i = 1; i < 10; i++) { 42 Mail[] ms = new Mail[i]; 43 44 for (int j = 0; j < i; j++) { 45 ms[j] = generateMail(); 46 added.add(ms[j].getMessage()); 47 mailRepository.store(ms[j]); 48 addedCount++; 49 assertNativeMessageCountEquals(addedCount); 50 } 51 assertNativeMessagesEqual(added); 52 for (int j = 0; j < i; j++) { 53 Mail originalMail = ms[j]; 54 Mail retrievedMail = mailRepository.retrieve(ms[j].getName()); 55 assertNotNull("Mail is null", retrievedMail); 56 assertEquals("keys differs!", originalMail.getName(), 57 retrievedMail.getName()); 58 assertEquals("subject differs", originalMail.getMessage() 59 .getSubject(), retrievedMail.getMessage().getSubject()); 60 assertTrue("content differs!", contentEquals(originalMail 61 .getMessage(), retrievedMail.getMessage())); 62 } 63 } 64 } 65 66 public void testStoreUpdateRetrieve() throws MessagingException , IOException { 67 Mail[] ms = new Mail[15]; 68 MimeMessage [] mms = new MimeMessage [15]; 69 int addedCount = 0; 70 for (int j = 0; j < 15; j++) { 71 ms[j] = generateMail(); 72 mms[j]=ms[j].getMessage(); 73 mailRepository.store(ms[j]); 74 addedCount++; 75 assertNativeMessageCountEquals(addedCount); 76 } 77 assertNativeMessagesEqual(Arrays.asList(mms)); 78 System.out.println(" ####### Test: doing updates #####"); 79 for (int j = 5; j < 10 ; j++) { 80 Mail m= generateMail(); 81 m.setName(ms[j].getName()); 82 mailRepository.store(m); 83 mms[j]=m.getMessage(); 84 assertNativeMessageCountEquals(addedCount); 85 } 86 assertNativeMessagesEqual(Arrays.asList(mms)); 87 for (int j = 0; j < 15; j++) { 88 Mail m=mailRepository.retrieve(ms[j].getName()); 89 assertTrue(contentEquals(mms[j], m.getMessage())); 90 } 91 } 92 93 class RemoveThread extends Thread { 94 95 String key; 96 boolean exception=false; 97 98 RemoveThread(String key) { 99 super("rt"); 100 this.key=key; 101 } 102 103 public void run() { 104 try { 105 mailRepository.remove(key); 106 } catch (MessagingException e) { 107 exception=true; 108 } 109 } 110 public synchronized void doWait(){ 111 try { 112 wait(); 113 } catch (InterruptedException e) { 114 } 115 } 116 } 117 118 122 public void testLock() throws MessagingException { 123 Mail m1=generateMail(); 124 Mail m2=generateMail(); 125 mailRepository.store(m1); 126 mailRepository.store(m2); 127 assertNativeMessageCountEquals(2); 128 129 mailRepository.lock(m1.getName()); 131 RemoveThread rt=new RemoveThread(m1.getName()); 132 rt.start(); 133 rt.doWait(); 134 assertTrue("RemoveThread1 should have thrown exception",rt.exception); 135 assertNativeMessageCountEquals(2); 136 137 mailRepository.unlock(m1.getName()); 139 rt=new RemoveThread(m1.getName()); 140 rt.start(); 141 rt.doWait(); 142 assertFalse("RemoveThread2 should not have thrown an exception",rt.exception); 143 assertNativeMessageCountEquals(1); 144 145 mailRepository.lock(m2.getName()); 147 mailRepository.remove(m2.getName()); 148 assertNativeMessageCountEquals(0); 149 } 150 151 152 } 153 | Popular Tags |