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;
24  import java.io.UnsupportedEncodingException;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Date;
28  import java.util.Iterator;
29  
30  import javax.mail.MessagingException;
31  import javax.mail.internet.MimeMessage;
32  import javax.mail.internet.ParseException;
33  
34  import junit.framework.TestCase;
35  
36  import org.apache.mailet.base.test.FakeMailContext;
37  import org.apache.mailet.base.test.FakeMatcherConfig;
38  import org.apache.mailet.Mail;
39  import org.apache.mailet.MailAddress;
40  import org.apache.mailet.Matcher;
41  
42  public abstract class AbstractSenderIsTest extends TestCase {
43  
44      protected Mail mockedMail;
45  
46      protected Matcher matcher;
47  
48      private MailAddress sender;
49  
50      public AbstractSenderIsTest(String arg0)
51              throws UnsupportedEncodingException {
52          super(arg0);
53      }
54  
55      protected void setSender(MailAddress sender) {
56          this.sender = sender;
57      }
58  
59      protected void setupMockedMail() {
60          mockedMail = new Mail() {
61  
62              private static final long serialVersionUID = 1L;
63  
64              public String getName() {
65                  throw new UnsupportedOperationException(
66                          "Unimplemented mock service");
67              }
68  
69              public void setName(String newName) {
70                  throw new UnsupportedOperationException(
71                          "Unimplemented mock service");
72              }
73  
74              public MimeMessage getMessage() throws MessagingException {
75                  throw new UnsupportedOperationException(
76                          "Unimplemented mock service");
77              }
78  
79              public Collection getRecipients() {
80                  ArrayList r = new ArrayList();
81                  try {
82                      r.add(new MailAddress("test@localhost"));
83                  } catch (ParseException e) {
84                  }
85                  return r;
86              }
87  
88              public void setRecipients(Collection recipients) {
89                  throw new UnsupportedOperationException(
90                          "Unimplemented mock service");
91              }
92  
93              public MailAddress getSender() {
94                  return sender;
95              }
96  
97              public String getState() {
98                  throw new UnsupportedOperationException(
99                          "Unimplemented mock service");
100             }
101 
102             public String getRemoteHost() {
103                 throw new UnsupportedOperationException(
104                         "Unimplemented mock service");
105             }
106 
107             public String getRemoteAddr() {
108                 throw new UnsupportedOperationException(
109                         "Unimplemented mock service");
110             }
111 
112             public String getErrorMessage() {
113                 throw new UnsupportedOperationException(
114                         "Unimplemented mock service");
115             }
116 
117             public void setErrorMessage(String msg) {
118                 throw new UnsupportedOperationException(
119                         "Unimplemented mock service");
120             }
121 
122             public void setMessage(MimeMessage message) {
123                 throw new UnsupportedOperationException(
124                         "Unimplemented mock service");
125             }
126 
127             public void setState(String state) {
128                 throw new UnsupportedOperationException(
129                         "Unimplemented mock service");
130             }
131 
132             public Serializable getAttribute(String name) {
133                 throw new UnsupportedOperationException(
134                         "Unimplemented mock service");
135             }
136 
137             public Iterator getAttributeNames() {
138                 throw new UnsupportedOperationException(
139                         "Unimplemented mock service");
140             }
141 
142             public boolean hasAttributes() {
143                 throw new UnsupportedOperationException(
144                         "Unimplemented mock service");
145             }
146 
147             public Serializable removeAttribute(String name) {
148                 throw new UnsupportedOperationException(
149                         "Unimplemented mock service");
150             }
151 
152             public void removeAllAttributes() {
153                 throw new UnsupportedOperationException(
154                         "Unimplemented mock service");
155             }
156 
157             public Serializable setAttribute(String name, Serializable object) {
158                 throw new UnsupportedOperationException(
159                         "Unimplemented mock service");
160             }
161 
162             public long getMessageSize() throws MessagingException {
163                 throw new UnsupportedOperationException(
164                         "Unimplemented mock service");
165             }
166 
167             public Date getLastUpdated() {
168                 throw new UnsupportedOperationException(
169                         "Unimplemented mock service");
170             }
171 
172             public void setLastUpdated(Date lastUpdated) {
173                 throw new UnsupportedOperationException(
174                         "Unimplemented mock service");
175             }
176 
177         };
178 
179     }
180 
181     protected void setupMatcher() throws MessagingException {
182         matcher = createMatcher();
183         FakeMatcherConfig mci = new FakeMatcherConfig(getConfigOption()
184                 + getConfigValue(), new FakeMailContext());
185         matcher.init(mci);
186     }
187 
188     protected void setupAll() throws MessagingException {
189         setupMockedMail();
190         setupMatcher();
191     }
192 
193     protected abstract String getConfigOption();
194 
195     protected abstract String getConfigValue();
196 
197     protected abstract Matcher createMatcher();
198 }