KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > vut > AbstractVirtualUserTableTest


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
21
22 package org.apache.james.vut;
23
24 import java.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.avalon.framework.configuration.ConfigurationException;
31 import org.apache.avalon.framework.container.ContainerUtil;
32 import org.apache.avalon.framework.service.ServiceException;
33 import org.apache.james.services.AbstractDNSServer;
34 import org.apache.james.services.DNSServer;
35 import org.apache.james.services.VirtualUserTable;
36
37 import junit.framework.TestCase;
38
39 public abstract class AbstractVirtualUserTableTest extends TestCase {
40
41     protected AbstractVirtualUserTable virtualUserTable;
42     protected final static int REGEX_TYPE = 0;
43     protected final static int ERROR_TYPE = 1;
44     protected final static int ADDRESS_TYPE = 2;
45     protected final static int ALIASDOMAIN_TYPE = 3;
46     
47     protected void setUp() throws Exception JavaDoc {
48         virtualUserTable = getVirtalUserTable();
49     }
50     
51     protected void tearDown() throws Exception JavaDoc {
52         Map JavaDoc mappings = virtualUserTable.getAllMappings();
53         
54         if (mappings != null) {
55             Iterator JavaDoc mappingsIt = virtualUserTable.getAllMappings().keySet().iterator();
56     
57     
58             while(mappingsIt.hasNext()) {
59                 String JavaDoc key = mappingsIt.next().toString();
60                 String JavaDoc args[] = key.split("@");
61         
62                 Collection JavaDoc map = (Collection JavaDoc) mappings.get(key);
63         
64                 Iterator JavaDoc mapIt = map.iterator();
65         
66                 while (mapIt.hasNext()) {
67                     try {
68                         removeMapping(args[0], args[1], mapIt.next().toString());
69                     } catch (InvalidMappingException e) {
70                         e.printStackTrace();
71                     }
72                 }
73             }
74         }
75         ContainerUtil.dispose(virtualUserTable);
76     }
77     
78     private void removeMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc rawMapping) throws InvalidMappingException {
79         if (rawMapping.startsWith(VirtualUserTable.ERROR_PREFIX)) {
80             removeMapping(user, domain, rawMapping.substring(VirtualUserTable.ERROR_PREFIX.length()), ERROR_TYPE);
81         } else if (rawMapping.startsWith(VirtualUserTable.REGEX_PREFIX)) {
82             removeMapping(user, domain, rawMapping.substring(VirtualUserTable.REGEX_PREFIX.length()), REGEX_TYPE);
83         } else if (rawMapping.startsWith(VirtualUserTable.ALIASDOMAIN_PREFIX)) {
84             removeMapping(user, domain, rawMapping.substring(VirtualUserTable.ALIASDOMAIN_PREFIX.length()), ALIASDOMAIN_TYPE);
85         } else {
86             removeMapping(user, domain, rawMapping, ADDRESS_TYPE);
87         }
88     }
89     
90     protected abstract AbstractVirtualUserTable getVirtalUserTable() throws ServiceException, ConfigurationException, Exception JavaDoc;
91     
92     protected abstract boolean addMapping(String JavaDoc user , String JavaDoc domain, String JavaDoc mapping,int type)throws InvalidMappingException;
93     
94     protected abstract boolean removeMapping(String JavaDoc user, String JavaDoc domain, String JavaDoc mapping, int type) throws InvalidMappingException;
95     
96  
97     protected DNSServer setUpDNSServer() {
98         DNSServer dns = new AbstractDNSServer() {
99             public String JavaDoc getHostName(InetAddress JavaDoc inet) {
100                 return "test";
101             }
102             
103             public InetAddress JavaDoc[] getAllByName(String JavaDoc name) throws UnknownHostException JavaDoc {
104                 return new InetAddress JavaDoc[] { InetAddress.getByName("127.0.0.1")};
105             }
106             
107             public InetAddress JavaDoc getLocalHost() throws UnknownHostException JavaDoc {
108                 return InetAddress.getLocalHost();
109             }
110         };
111         return dns;
112     }
113     
114     public void testStoreAndRetrieveRegexMapping() throws ErrorMappingException {
115         String JavaDoc user = "test";
116         String JavaDoc domain = "localhost";
117         String JavaDoc regex = "(.*):{$1}@localhost";
118         String JavaDoc regex2 = "(.+):{$1}@test";
119         String JavaDoc invalidRegex = ".*):";
120         boolean catched = false;
121         try {
122         
123             assertNull("No mapping",virtualUserTable.getMappings(user, domain));
124         
125             assertTrue("Added virtual mapping", addMapping(user, domain, regex, REGEX_TYPE));
126             assertTrue("Added virtual mapping", addMapping(user, domain, regex2, REGEX_TYPE));
127
128             assertEquals("Two mappings",virtualUserTable.getMappings(user, domain).size(), 2);
129             assertEquals("One mappingline",virtualUserTable.getAllMappings().size(),1);
130            
131             // Test DomainList implementations!
132
assertEquals("Three domains",virtualUserTable.getDomains().size(), 3);
133             assertTrue("Contains Domain",virtualUserTable.containsDomain(domain));
134             
135             assertTrue("remove virtual mapping", removeMapping(user, domain, regex, REGEX_TYPE));
136         
137             try {
138                 assertTrue("Added virtual mapping", virtualUserTable.addRegexMapping(user, domain, invalidRegex));
139             } catch (InvalidMappingException e) {
140                 catched = true;
141             }
142             assertTrue("Invalid Mapping throw exception" , catched);
143
144             assertTrue("remove virtual mapping", removeMapping(user, domain, regex2, REGEX_TYPE));
145             assertNull("No mapping",virtualUserTable.getMappings(user, domain));
146             
147             assertNull("No mappings",virtualUserTable.getAllMappings());
148             
149         } catch (InvalidMappingException e) {
150             fail("Storing failed");
151         }
152     
153     }
154     
155
156     public void testStoreAndRetrieveAddressMapping() throws ErrorMappingException {
157         String JavaDoc user = "test";
158         String JavaDoc domain = "localhost";
159         String JavaDoc address = "test@localhost2";
160         String JavaDoc address2 = "test@james";
161         String JavaDoc invalidAddress= ".*@localhost2:";
162         boolean catched = false;
163         
164         try {
165         
166             assertNull("No mapping",virtualUserTable.getMappings(user, domain));
167         
168             assertTrue("Added virtual mapping", addMapping(user, domain, address, ADDRESS_TYPE));
169             assertTrue("Added virtual mapping", addMapping(user, domain, address2, ADDRESS_TYPE));
170
171             assertEquals("Two mappings",virtualUserTable.getMappings(user, domain).size(),2);
172             assertEquals("One mappingline",virtualUserTable.getAllMappings().size(),1);
173         
174             assertTrue("remove virtual mapping", removeMapping(user, domain, address, ADDRESS_TYPE));
175         
176             if (virtualUserTable instanceof JDBCVirtualUserTable) {
177                 try {
178                     assertTrue("Added virtual mapping", addMapping(user, domain, invalidAddress, ADDRESS_TYPE));
179                 } catch (InvalidMappingException e) {
180                     catched = true;
181                 }
182                 assertTrue("Invalid Mapping throw exception" , catched);
183             }
184             
185             assertTrue("remove virtual mapping", removeMapping(user, domain, address2, ADDRESS_TYPE));
186             
187             assertNull("No mapping",virtualUserTable.getMappings(user, domain));
188             assertNull("No mappings",virtualUserTable.getAllMappings());
189             
190         } catch (InvalidMappingException e) {
191             fail("Storing failed");
192         }
193         
194     
195     }
196
197     public void testStoreAndRetrieveErrorMapping() throws ErrorMappingException {
198         String JavaDoc user = "test";
199         String JavaDoc domain = "localhost";
200         String JavaDoc error = "bounce!";
201         boolean catched = false;
202         
203         try {
204         
205             assertNull("No mapping",virtualUserTable.getMappings(user, domain));
206         
207             assertTrue("Added virtual mapping", addMapping(user, domain, error, ERROR_TYPE));
208             assertEquals("One mappingline",virtualUserTable.getAllMappings().size(),1);
209
210             try {
211                 virtualUserTable.getMappings(user, domain);
212             } catch (ErrorMappingException e) {
213                 catched = true;
214             }
215             assertTrue("Error Mapping throw exception" , catched);
216
217             assertTrue("remove virtual mapping", removeMapping(user, domain, error, ERROR_TYPE));
218             assertNull("No mapping",virtualUserTable.getMappings(user, domain));
219             assertNull("No mappings",virtualUserTable.getAllMappings());
220             
221         } catch (InvalidMappingException e) {
222              fail("Storing failed");
223         }
224
225     }
226     
227     public void testStoreAndRetrieveWildCardAddressMapping() throws ErrorMappingException {
228         String JavaDoc user = "test";
229         String JavaDoc user2 = "test2";
230         String JavaDoc domain = "localhost";
231         String JavaDoc address = "test@localhost2";
232         String JavaDoc address2 = "test@james";
233
234
235        try {
236                  
237             assertNull("No mapping",virtualUserTable.getMappings(user, domain));
238         
239             assertTrue("Added virtual mapping", addMapping(null, domain, address, ADDRESS_TYPE));
240             assertTrue("Added virtual mapping", addMapping(user, domain, address2, ADDRESS_TYPE));
241
242           
243             assertTrue("One mappings",virtualUserTable.getMappings(user, domain).size() == 1);
244             assertTrue("One mappings",virtualUserTable.getMappings(user2, domain).size() == 1);
245            
246             assertTrue("remove virtual mapping", removeMapping(user, domain, address2, ADDRESS_TYPE));
247             assertTrue("remove virtual mapping", removeMapping(null, domain, address, ADDRESS_TYPE));
248             assertNull("No mapping",virtualUserTable.getMappings(user, domain));
249             assertNull("No mapping",virtualUserTable.getMappings(user2, domain));
250       
251         } catch (InvalidMappingException e) {
252            fail("Storing failed");
253         }
254     
255     }
256     
257     public void testRecursiveMapping() throws ErrorMappingException {
258         String JavaDoc user1 = "user1";
259         String JavaDoc user2 = "user2";
260         String JavaDoc user3 = "user3";
261         String JavaDoc domain1 = "domain1";
262         String JavaDoc domain2 = "domain2";
263         String JavaDoc domain3 = "domain3";
264         boolean exception1 = false;
265     
266         virtualUserTable.setRecursiveMapping(true);
267        
268         try {
269             assertNull("No mappings",virtualUserTable.getAllMappings());
270      
271             assertTrue("Add mapping", addMapping(user1, domain1, user2 + "@" + domain2, ADDRESS_TYPE));
272             assertTrue("Add mapping", addMapping(user2, domain2, user3 + "@" + domain3, ADDRESS_TYPE));
273             assertEquals("Recursive mapped", virtualUserTable.getMappings(user1, domain1).iterator().next(),user3 + "@" + domain3);
274
275             assertTrue("Add mapping", addMapping(user3, domain3, user1 + "@" + domain1, ADDRESS_TYPE));
276             try {
277                 virtualUserTable.getMappings(user1, domain1);
278             } catch (ErrorMappingException e) {
279                 exception1 = true;
280             }
281             assertTrue("Exception thrown on to many mappings", exception1);
282             
283             // disable recursive mapping
284
virtualUserTable.setRecursiveMapping(false);
285             assertEquals("Not recursive mapped", virtualUserTable.getMappings(user1, domain1).iterator().next(),user2 + "@" + domain2);
286             
287         } catch (InvalidMappingException e) {
288             fail("Storing failed");
289         }
290     }
291    
292     
293     public void testAliasDomainMapping() throws ErrorMappingException {
294         String JavaDoc domain = "realdomain";
295         String JavaDoc aliasDomain = "aliasdomain";
296         String JavaDoc user = "user";
297         String JavaDoc user2 = "user2";
298     
299         assertNull("No mappings",virtualUserTable.getAllMappings());
300         try {
301             assertTrue("Add mapping",addMapping(null, aliasDomain, user2 + "@" + domain, ADDRESS_TYPE));
302             assertTrue("Add aliasDomain mapping", addMapping(null, aliasDomain, domain, ALIASDOMAIN_TYPE));
303         
304             Iterator JavaDoc mappings = virtualUserTable.getMappings(user, aliasDomain).iterator();
305             assertEquals("Domain mapped as first ", mappings.next(), user + "@" + domain);
306             assertEquals("Address mapped as second ", mappings.next(), user2 + "@" + domain);
307             
308             assertTrue("Remove mapping", removeMapping(null, aliasDomain, user2 + "@" + domain, ADDRESS_TYPE));
309             assertTrue("Remove aliasDomain mapping", removeMapping(null, aliasDomain, domain, ALIASDOMAIN_TYPE));
310         
311         } catch (InvalidMappingException e) {
312             fail("Storing failed");
313         }
314     }
315     
316 }
317
Popular Tags