KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > userrepository > JamesUsersJdbcRepositoryTest


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.userrepository;
21
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.configuration.DefaultConfiguration;
24 import org.apache.avalon.framework.container.ContainerUtil;
25 import org.apache.avalon.framework.logger.ConsoleLogger;
26 import org.apache.james.services.JamesUser;
27 import org.apache.james.services.UsersRepository;
28 import org.apache.james.services.VirtualUserTable;
29 import org.apache.james.test.mock.james.MockFileSystem;
30 import org.apache.james.test.mock.util.AttrValConfiguration;
31 import org.apache.james.test.util.Util;
32 import org.apache.mailet.MailAddress;
33
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * Test basic behaviours of UsersFileRepository
39  */

40 public class JamesUsersJdbcRepositoryTest extends MockUsersRepositoryTest {
41
42     /**
43      * Create the repository to be tested.
44      *
45      * @return the user repository
46      * @throws Exception
47      */

48     protected UsersRepository getUsersRepository() throws Exception JavaDoc {
49         JamesUsersJdbcRepository res = new JamesUsersJdbcRepository();
50         String JavaDoc tableString = "jamesusers";
51         configureAbstractJdbcUsersRepository(res, tableString);
52         return res;
53     }
54
55     /**
56      * @param res
57      * @param tableString
58      * @throws Exception
59      * @throws ConfigurationException
60      */

61     protected void configureAbstractJdbcUsersRepository(AbstractJdbcUsersRepository res, String JavaDoc tableString) throws Exception JavaDoc, ConfigurationException {
62         res.setFileSystem(new MockFileSystem());
63         res.setDatasources(Util.getDataSourceSelector());
64         
65         DefaultConfiguration configuration = new DefaultConfiguration("test");
66         configuration.setAttribute("destinationURL", "db://maildb/"+tableString);
67         configuration.addChild(new AttrValConfiguration("sqlFile","file://conf/sqlResources.xml"));
68         res.enableLogging(new ConsoleLogger());
69         res.configure(configuration );
70         res.initialize();
71     }
72
73     /**
74      * @return
75      */

76     protected boolean getCheckCase() {
77         return true;
78     }
79
80     protected void disposeUsersRepository() {
81         Iterator JavaDoc i = this.usersRepository.list();
82         while (i.hasNext()) {
83             this.usersRepository.removeUser((String JavaDoc) i.next());
84         }
85         ContainerUtil.dispose(this.usersRepository);
86     }
87     
88     
89     public void testVirtualUserTableImpl() throws Exception JavaDoc {
90         String JavaDoc username = "test";
91         String JavaDoc password = "pass";
92         String JavaDoc alias = "alias";
93         String JavaDoc domain = "localhost";
94         String JavaDoc forward = "forward@somewhere";
95         
96         JamesUsersJdbcRepository repos = (JamesUsersJdbcRepository) getUsersRepository();
97         repos.setEnableAliases(true);
98         repos.setEnableForwarding(true);
99         repos.addUser(username,password);
100         
101         JamesUser user = (JamesUser)repos.getUserByName(username);
102         user.setAlias(alias);
103         repos.updateUser(user);
104         
105         Collection JavaDoc map = ((VirtualUserTable) repos).getMappings(username, domain);
106         assertNull("No mapping", map);
107         
108         user.setAliasing(true);
109         repos.updateUser(user);
110         map = ((VirtualUserTable) repos).getMappings(username, domain);
111         assertEquals("One mapping", 1, map.size());
112         assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain);
113         
114         
115         user.setForwardingDestination(new MailAddress(forward));
116         repos.updateUser(user);
117         map = ((VirtualUserTable) repos).getMappings(username, domain);
118         assertTrue("One mapping", map.size() == 1);
119         assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain);
120         
121         
122         user.setForwarding(true);
123         repos.updateUser(user);
124         map = ((VirtualUserTable) repos).getMappings(username, domain);
125         Iterator JavaDoc mappings = map.iterator();
126         assertTrue("Two mapping",map.size() == 2);
127         assertEquals("Alias found", mappings.next().toString(), alias + "@" + domain);
128         assertEquals("Forward found", mappings.next().toString(), forward);
129     }
130
131
132 }
133
Popular Tags