KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > AbstractRemoteAddrInNetworkTest


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 package org.apache.james.transport.matchers;
22
23 import java.io.Serializable JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.net.InetAddress JavaDoc;
26 import java.net.UnknownHostException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.mail.MessagingException JavaDoc;
34 import javax.mail.internet.MimeMessage JavaDoc;
35 import javax.mail.internet.ParseException JavaDoc;
36
37 import junit.framework.TestCase;
38
39 import org.apache.james.Constants;
40 import org.apache.james.services.AbstractDNSServer;
41 import org.apache.james.services.DNSServer;
42 import org.apache.james.test.mock.avalon.MockServiceManager;
43 import org.apache.james.test.mock.mailet.MockMailContext;
44 import org.apache.james.test.mock.mailet.MockMatcherConfig;
45 import org.apache.mailet.Mail;
46 import org.apache.mailet.MailAddress;
47
48 public abstract class AbstractRemoteAddrInNetworkTest extends TestCase {
49
50     protected Mail mockedMail;
51
52     protected AbstractNetworkMatcher matcher;
53
54     private String JavaDoc remoteAddr;
55
56     private DNSServer dnsServer;
57
58     private MockServiceManager m_serviceManager;
59
60     public AbstractRemoteAddrInNetworkTest(String JavaDoc arg0)
61             throws UnsupportedEncodingException JavaDoc {
62         super(arg0);
63     }
64
65     protected void setRemoteAddr(String JavaDoc remoteAddr) {
66         this.remoteAddr = remoteAddr;
67     }
68
69     protected void setupMockedMail() {
70         mockedMail = new Mail() {
71
72             private static final long serialVersionUID = 1L;
73
74             public String JavaDoc getName() {
75                 throw new UnsupportedOperationException JavaDoc(
76                         "Unimplemented mock service");
77             }
78
79             public void setName(String JavaDoc newName) {
80                 throw new UnsupportedOperationException JavaDoc(
81                         "Unimplemented mock service");
82             }
83
84             public MimeMessage JavaDoc getMessage() throws MessagingException JavaDoc {
85                 throw new UnsupportedOperationException JavaDoc(
86                         "Unimplemented mock service");
87             }
88
89             public Collection JavaDoc getRecipients() {
90                 ArrayList JavaDoc r = new ArrayList JavaDoc();
91                 try {
92                     r = new ArrayList JavaDoc(Arrays
93                             .asList(new MailAddress[] { new MailAddress(
94                                     "test@james.apache.org") }));
95                 } catch (ParseException JavaDoc e) {
96                 }
97                 return r;
98             }
99
100             public void setRecipients(Collection JavaDoc recipients) {
101                 throw new UnsupportedOperationException JavaDoc(
102                         "Unimplemented mock service");
103             }
104
105             public MailAddress getSender() {
106                 throw new UnsupportedOperationException JavaDoc(
107                         "Unimplemented mock service");
108             }
109
110             public String JavaDoc getState() {
111                 throw new UnsupportedOperationException JavaDoc(
112                         "Unimplemented mock service");
113             }
114
115             public String JavaDoc getRemoteHost() {
116                 throw new UnsupportedOperationException JavaDoc(
117                         "Unimplemented mock service");
118             }
119
120             public String JavaDoc getRemoteAddr() {
121                 return remoteAddr;
122             }
123
124             public String JavaDoc getErrorMessage() {
125                 throw new UnsupportedOperationException JavaDoc(
126                         "Unimplemented mock service");
127             }
128
129             public void setErrorMessage(String JavaDoc msg) {
130                 throw new UnsupportedOperationException JavaDoc(
131                         "Unimplemented mock service");
132             }
133
134             public void setMessage(MimeMessage JavaDoc message) {
135                 throw new UnsupportedOperationException JavaDoc(
136                         "Unimplemented mock service");
137             }
138
139             public void setState(String JavaDoc state) {
140                 throw new UnsupportedOperationException JavaDoc(
141                         "Unimplemented mock service");
142             }
143
144             public Serializable JavaDoc getAttribute(String JavaDoc name) {
145                 throw new UnsupportedOperationException JavaDoc(
146                         "Unimplemented mock service");
147             }
148
149             public Iterator JavaDoc getAttributeNames() {
150                 throw new UnsupportedOperationException JavaDoc(
151                         "Unimplemented mock service");
152             }
153
154             public boolean hasAttributes() {
155                 throw new UnsupportedOperationException JavaDoc(
156                         "Unimplemented mock service");
157             }
158
159             public Serializable JavaDoc removeAttribute(String JavaDoc name) {
160                 throw new UnsupportedOperationException JavaDoc(
161                         "Unimplemented mock service");
162             }
163
164             public void removeAllAttributes() {
165                 throw new UnsupportedOperationException JavaDoc(
166                         "Unimplemented mock service");
167             }
168
169             public Serializable JavaDoc setAttribute(String JavaDoc name, Serializable JavaDoc object) {
170                 throw new UnsupportedOperationException JavaDoc(
171                         "Unimplemented mock service");
172             }
173
174             public long getMessageSize() throws MessagingException JavaDoc {
175                 throw new UnsupportedOperationException JavaDoc(
176                         "Unimplemented mock service");
177             }
178
179             public Date JavaDoc getLastUpdated() {
180                 throw new UnsupportedOperationException JavaDoc(
181                         "Unimplemented mock service");
182             }
183
184             public void setLastUpdated(Date JavaDoc lastUpdated) {
185                 throw new UnsupportedOperationException JavaDoc(
186                         "Unimplemented mock service");
187             }
188
189         };
190
191     }
192
193     protected void setupDNSServer() {
194         dnsServer = new AbstractDNSServer() {
195             public InetAddress JavaDoc getByName(String JavaDoc host)
196                     throws UnknownHostException JavaDoc {
197                 if ("192.168.200.0".equals(host) || "255.255.255.0".equals(host) || "192.168.200.1".equals(host) || "192.168.0.1".equals(host) || "192.168.1.1".equals(host)) {
198                     // called with an IP it only check formal validity
199
return InetAddress.getByName(host);
200                 }
201                 throw new UnsupportedOperationException JavaDoc("getByName("+host+") unimplemented in AbstractRemoteAddrInNetworkTest");
202             }
203
204         };
205     }
206
207     protected void setupMatcher() throws MessagingException JavaDoc {
208         m_serviceManager = new MockServiceManager();
209         m_serviceManager.put(DNSServer.ROLE, dnsServer);
210
211         MockMailContext mmc = new MockMailContext();
212         mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER, m_serviceManager);
213         matcher = createMatcher();
214         MockMatcherConfig mci = new MockMatcherConfig(getConfigOption()
215                 + getAllowedNetworks(), mmc);
216         matcher.init(mci);
217     }
218
219     protected void setupAll() throws MessagingException JavaDoc {
220         setupDNSServer();
221         setupMockedMail();
222         setupMatcher();
223     }
224
225     protected abstract String JavaDoc getConfigOption();
226
227     protected abstract String JavaDoc getAllowedNetworks();
228
229     protected abstract AbstractNetworkMatcher createMatcher();
230 }
231
Popular Tags