KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.mail.MessagingException JavaDoc;
28
29 import org.apache.mailet.Mail;
30
31 public abstract class AbstractMailRepositoryNativeTestCase extends
32         AbstractMailRepositorySelfTestCase {
33
34     public void testStore() throws MessagingException JavaDoc, IOException JavaDoc {
35         Collection JavaDoc added = new ArrayList JavaDoc();
36         for (int i = 0; i < 10; i++) {
37             Mail m = generateMail();
38             mailRepository.store(m);
39             assertNativeMessageCountEquals(i + 1);
40             added.add(m.getMessage());
41             assertNativeMessagesEqual(added);
42         }
43     }
44
45     public void testStoreRemoveByMailOrKey() throws MessagingException JavaDoc,
46             IOException JavaDoc {
47         Collection JavaDoc added = new ArrayList JavaDoc();
48         for (int i = 0; i < 10; i++) {
49             Mail m1 = generateMail();
50             Mail m2 = generateMail();
51             mailRepository.store(m1);
52             assertNativeMessageCountEquals(i + 1);
53             mailRepository.store(m2);
54             assertNativeMessageCountEquals(i + 2);
55             if (i % 2 == 0) {
56                 mailRepository.remove(m1);
57             } else {
58                 mailRepository.remove(m1.getName());
59             }
60             assertNativeMessageCountEquals(i + 1);
61             added.add(m2.getMessage());
62         }
63         assertNativeMessagesEqual(added);
64     }
65
66     public void testStoreRemoveByCollection() throws MessagingException JavaDoc,
67             IOException JavaDoc {
68         Collection JavaDoc retain = new ArrayList JavaDoc();
69         Collection JavaDoc delete = new ArrayList JavaDoc();
70
71         for (int i = 0; i < 10; i++) {
72             Mail m1 = generateMail();
73             mailRepository.store(m1);
74             assertNativeMessageCountEquals(i + 1);
75             if (i % 2 == 0) {
76                 delete.add(m1);
77             } else {
78                 retain.add(m1.getMessage());
79             }
80         }
81         mailRepository.remove(delete);
82         assertNativeMessageCountEquals(5);
83         assertNativeMessagesEqual(retain);
84     }
85     
86     /*
87      * Test method for
88      * 'org.apache.james.mailrepository.UIDPlusFolderMailRepository.list()'
89      */

90     public void testListRetrieve() throws MessagingException JavaDoc, IOException JavaDoc {
91         try {
92             Collection JavaDoc added = new ArrayList JavaDoc();
93             for (int i = 0; i < 10; i++) {
94                 Mail m = generateMail();
95                 nativeStoreMessage(m.getMessage());
96                 added.add(m.getMessage());
97             }
98             assertNativeMessageCountEquals(10);
99             assertNativeMessagesEqual(added);
100             
101             Collection JavaDoc retrieved = new ArrayList JavaDoc();
102             for (Iterator JavaDoc it= mailRepository.list(); it.hasNext();) {
103                 String JavaDoc key = (String JavaDoc) it.next();
104                 assertNotNull("key is null",key);
105                 Mail m=mailRepository.retrieve(key);
106                 assertNotNull("Mail is null",m);
107                 assertNotNull("key of Mail is null",key);
108                 assertEquals("key differs",key,m.getName());
109                 retrieved.add(m.getMessage());
110             }
111             assertEquals("number of retrieved messages differs",10,retrieved.size());
112             assertTrue("messages differ",messageSetsEqual(added,retrieved));
113             
114             
115         } catch (NativeMethodNotSupportetException e) {
116
117         }
118     }
119     public void testListRemove() throws MessagingException JavaDoc, IOException JavaDoc {
120         try {
121             Collection JavaDoc added = new ArrayList JavaDoc();
122             for (int i = 0; i < 10; i++) {
123                 Mail m = generateMail();
124                 nativeStoreMessage(m.getMessage());
125                 added.add(m.getMessage());
126             }
127             assertNativeMessageCountEquals(10);
128             int count=10;
129             for (Iterator JavaDoc it= mailRepository.list(); it.hasNext();) {
130                 String JavaDoc key = (String JavaDoc) it.next();
131                 assertNotNull("key is null",key);
132                 mailRepository.remove(key);
133                 count--;
134                 assertNativeMessageCountEquals(count);
135             }
136         } catch (NativeMethodNotSupportetException e) {
137
138         }
139     }
140
141
142 }
143
Popular Tags