KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > JamesTest


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;
23
24 import org.apache.avalon.cornerstone.services.store.Store;
25 import org.apache.avalon.framework.container.ContainerUtil;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.james.services.AbstractDNSServer;
28 import org.apache.james.services.DNSServer;
29 import org.apache.james.services.DomainList;
30 import org.apache.james.services.FileSystem;
31 import org.apache.james.services.MailServer;
32 import org.apache.james.services.MailServerTestAllImplementations;
33 import org.apache.james.services.UsersRepository;
34 import org.apache.james.services.UsersStore;
35 import org.apache.james.test.mock.avalon.MockLogger;
36 import org.apache.james.test.mock.avalon.MockServiceManager;
37 import org.apache.james.test.mock.avalon.MockStore;
38 import org.apache.james.test.mock.james.InMemorySpoolRepository;
39 import org.apache.james.test.mock.james.MockUsersStore;
40 import org.apache.james.userrepository.MockUsersRepository;
41
42 import java.io.File JavaDoc;
43 import java.io.FileNotFoundException JavaDoc;
44 import java.net.InetAddress JavaDoc;
45 import java.net.UnknownHostException JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.List JavaDoc;
48
49 public class JamesTest extends MailServerTestAllImplementations {
50     
51     private File JavaDoc tempContextFile = null;
52     private InMemorySpoolRepository mockMailRepository;
53
54     public MailServer createMailServer() throws ServiceException {
55         James james = new James();
56         james.service(setUpServiceManager());
57         MockLogger mockLogger = new MockLogger();
58         mockLogger.disableDebug();
59         ContainerUtil.enableLogging(james, mockLogger);
60         try {
61             JamesTestConfiguration conf = new JamesTestConfiguration();
62             conf.init();
63             ContainerUtil.configure(james, conf);
64             ContainerUtil.initialize(james);
65         } catch (Exception JavaDoc e) {
66             e.printStackTrace();
67             fail("James.initialize() failed");
68         }
69         return james;
70     }
71     
72     
73
74     protected void tearDown() throws Exception JavaDoc {
75         if (tempContextFile != null) {
76             tempContextFile.delete();
77         }
78         if (mockMailRepository != null) {
79             ContainerUtil.dispose(mockMailRepository);
80         }
81         super.tearDown();
82     }
83
84
85
86     private MockServiceManager setUpServiceManager() {
87         MockServiceManager serviceManager = new MockServiceManager();
88         MockUsersRepository mockUsersRepository = new MockUsersRepository();
89         serviceManager.put(UsersRepository.ROLE, mockUsersRepository);
90         serviceManager.put(UsersStore.ROLE, new MockUsersStore(mockUsersRepository));
91         serviceManager.put(FileSystem.ROLE, new FileSystem() {
92
93             public File JavaDoc getBasedir() throws FileNotFoundException JavaDoc {
94                 return new File JavaDoc(".");
95             }
96
97             public File JavaDoc getFile(String JavaDoc fileURL) throws FileNotFoundException JavaDoc {
98                 return new File JavaDoc("./conf/");
99             }
100             
101         });
102         serviceManager.put(DomainList.ROLE, new DomainList() {
103
104             public List getDomains() {
105                 ArrayList JavaDoc d = new ArrayList JavaDoc();
106                 d.add("localhost");
107                 return d;
108             }
109
110             public boolean containsDomain(String JavaDoc domain) {
111                 return getDomains().contains(domain);
112             }
113
114         public void setAutoDetect(boolean autodetect) {}
115
116         public void setAutoDetectIP(boolean autodetectIP) {}
117         });
118         MockStore mockStore = new MockStore();
119         mockMailRepository = new InMemorySpoolRepository();
120         mockStore.add(EXISTING_USER_NAME, mockMailRepository);
121         serviceManager.put(Store.ROLE, mockStore);
122         serviceManager.put(DNSServer.ROLE, setUpDNSServer());
123         return serviceManager;
124     }
125     
126     private DNSServer setUpDNSServer() {
127         DNSServer dns = new AbstractDNSServer() {
128             public String JavaDoc getHostName(InetAddress JavaDoc addr) {
129                 return "localhost";
130             }
131
132             public InetAddress JavaDoc getLocalHost() throws UnknownHostException JavaDoc {
133                 throw new UnknownHostException JavaDoc("Unknown");
134             }
135         };
136         return dns;
137     }
138
139     public boolean allowsPasswordlessUser() {
140         return false;
141     }
142
143     public boolean canTestUserExists() {
144         return true;
145     }
146
147     public boolean isUserExisting(MailServer mailServerImpl, String JavaDoc username) {
148         return ((James)mailServerImpl).isLocalUser(username);
149     }
150 }
151
Popular Tags