KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailboxmanager > redundant > AbstractMailRepositorySelfTestCase


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.mailboxmanager.redundant;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26
27 import javax.mail.MessagingException JavaDoc;
28 import javax.mail.internet.MimeMessage JavaDoc;
29
30 import org.apache.mailet.Mail;
31
32 public abstract class AbstractMailRepositorySelfTestCase extends AbstractMailRepositoryTestCase {
33
34     /*
35      * Test method for
36      * 'org.apache.james.mailrepository.UIDPlusFolderMailRepository.retrieve(String)'
37      */

38     public void testStoreRetrieve() throws MessagingException JavaDoc, IOException JavaDoc {
39         int addedCount = 0;
40         Collection JavaDoc added=new ArrayList JavaDoc();
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 JavaDoc, IOException JavaDoc {
67         Mail[] ms = new Mail[15];
68         MimeMessage JavaDoc[] mms = new MimeMessage JavaDoc[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 JavaDoc {
94         
95         String JavaDoc key;
96         boolean exception=false;
97         
98         RemoveThread(String JavaDoc key) {
99             super("rt");
100             this.key=key;
101         }
102         
103         public void run() {
104             try {
105                 mailRepository.remove(key);
106             } catch (MessagingException JavaDoc e) {
107                 exception=true;
108             }
109         }
110         public synchronized void doWait(){
111             try {
112                 wait();
113             } catch (InterruptedException JavaDoc e) {
114             }
115         }
116     }
117     
118     /*
119      * Test method for
120      * 'org.apache.james.mailrepository.UIDPlusFolderMailRepository.lock(String)'
121      */

122     public void testLock() throws MessagingException JavaDoc {
123         Mail m1=generateMail();
124         Mail m2=generateMail();
125         mailRepository.store(m1);
126         mailRepository.store(m2);
127         assertNativeMessageCountEquals(2);
128         
129         // Try to remove locked message by different Thread
130
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         // Try to remove unlocked message by different Thread
138
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         // try to remove locked message by same Thread
146
mailRepository.lock(m2.getName());
147         mailRepository.remove(m2.getName());
148         assertNativeMessageCountEquals(0);
149     }
150
151
152 }
153
Popular Tags