KickJava   Java API By Example, From Geeks To Geeks.

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


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.DefaultConfiguration;
23 import org.apache.avalon.framework.container.ContainerUtil;
24 import org.apache.avalon.framework.logger.ConsoleLogger;
25 import org.apache.avalon.framework.service.DefaultServiceManager;
26 import org.apache.james.mailrepository.filepair.File_Persistent_Object_Repository;
27 import org.apache.james.services.FileSystem;
28 import org.apache.james.services.JamesUser;
29 import org.apache.james.services.UsersRepository;
30 import org.apache.james.services.VirtualUserTable;
31 import org.apache.james.test.mock.avalon.MockLogger;
32 import org.apache.james.test.mock.avalon.MockStore;
33 import org.apache.mailet.MailAddress;
34
35 import java.io.File JavaDoc;
36 import java.io.FileNotFoundException JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 /**
41  * Test basic behaviours of UsersFileRepository
42  */

43 public class UsersFileRepositoryTest extends MockUsersRepositoryTest {
44
45     /**
46      * Create the repository to be tested.
47      *
48      * @return the user repository
49      * @throws Exception
50      */

51     protected UsersRepository getUsersRepository() throws Exception JavaDoc {
52         UsersFileRepository res = new UsersFileRepository();
53         DefaultServiceManager serviceManager = new DefaultServiceManager();
54         serviceManager.put(FileSystem.ROLE, new FileSystem() {
55
56             public File JavaDoc getBasedir() throws FileNotFoundException JavaDoc {
57                 return new File JavaDoc(".");
58             }
59
60             public File JavaDoc getFile(String JavaDoc fileURL) throws FileNotFoundException JavaDoc {
61                 throw new UnsupportedOperationException JavaDoc();
62             }
63             
64         });
65         MockStore mockStore = new MockStore();
66         File_Persistent_Object_Repository file_Persistent_Object_Repository = new File_Persistent_Object_Repository();
67         file_Persistent_Object_Repository.service(serviceManager);
68         file_Persistent_Object_Repository.enableLogging(new MockLogger());
69         DefaultConfiguration defaultConfiguration22 = new DefaultConfiguration("conf");
70         defaultConfiguration22.setAttribute("destinationURL", "file://target/var/users");
71         file_Persistent_Object_Repository.configure(defaultConfiguration22);
72         file_Persistent_Object_Repository.initialize();
73         mockStore.add("OBJECT.users", file_Persistent_Object_Repository);
74         res.setStore(mockStore);
75         DefaultConfiguration configuration = new DefaultConfiguration("test");
76         DefaultConfiguration destinationConf = new DefaultConfiguration("destination");
77         destinationConf.setAttribute("URL", "file://target/var/users");
78         configuration.addChild(destinationConf);
79         res.enableLogging(new ConsoleLogger());
80         res.configure(configuration );
81         res.initialize();
82         return res;
83     }
84
85     protected void disposeUsersRepository() {
86         Iterator JavaDoc i = this.usersRepository.list();
87         while (i.hasNext()) {
88             this.usersRepository.removeUser((String JavaDoc) i.next());
89         }
90         ContainerUtil.dispose(this.usersRepository);
91     }
92     
93     public void testVirtualUserTableImpl() throws Exception JavaDoc {
94         String JavaDoc username = "test";
95         String JavaDoc password = "pass";
96         String JavaDoc alias = "alias";
97         String JavaDoc domain = "localhost";
98         String JavaDoc forward = "forward@somewhere";
99         
100         UsersFileRepository repos = (UsersFileRepository) getUsersRepository();
101         repos.setEnableAliases(true);
102         repos.setEnableForwarding(true);
103         repos.addUser(username,password);
104         
105         JamesUser user = (JamesUser)repos.getUserByName(username);
106         user.setAlias(alias);
107         repos.updateUser(user);
108         
109         Collection JavaDoc map = ((VirtualUserTable) repos).getMappings(username, domain);
110         assertNull("No mapping", map);
111         
112         user.setAliasing(true);
113         repos.updateUser(user);
114         map = ((VirtualUserTable) repos).getMappings(username, domain);
115         assertEquals("One mapping", 1, map.size());
116         assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain);
117         
118         
119         user.setForwardingDestination(new MailAddress(forward));
120         repos.updateUser(user);
121         map = ((VirtualUserTable) repos).getMappings(username, domain);
122         assertTrue("One mapping", map.size() == 1);
123         assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain);
124         
125         
126         user.setForwarding(true);
127         repos.updateUser(user);
128         map = ((VirtualUserTable) repos).getMappings(username, domain);
129         Iterator JavaDoc mappings = map.iterator();
130         assertTrue("Two mapping",map.size() == 2);
131         assertEquals("Alias found", mappings.next().toString(), alias + "@" + domain);
132         assertEquals("Forward found", mappings.next().toString(), forward);
133     }
134
135
136 }
137
Popular Tags