1   package test.mock.mail;
2   
3   import alt.javax.mail.Session;
4   import alt.javax.mail.Transport;
5   import com.mockobjects.ExpectationValue;
6   import com.mockobjects.MockObject;
7   
8   import javax.mail.*;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.Properties;
12  
13  public class MockSession extends MockObject implements Session
14  {
15      private Map urlAuthenticators = new HashMap();
16      private final ExpectationValue myDebug = new ExpectationValue("debug");
17      private Transport myTransport;
18      private Properties props;
19      private javax.mail.Session wrapped;
20  
21      public Session getInstance(Properties props, Authenticator authenticator)
22      {
23          getInstance(props);
24          if (authenticator == null)
25              setPasswordAuthentication(new URLName((String) props.get("mail.smtp.host")), null);
26          else
27              setPasswordAuthentication(new URLName((String) props.get("mail.smtp.host")), ((MockAuthenticator) authenticator).getPasswordAuthentication());
28          wrapped = javax.mail.Session.getInstance(props, authenticator);
29          return this;
30      }
31  
32      public Session getInstance(Properties props)
33      {
34          this.props = props;
35          wrapped = javax.mail.Session.getInstance(props);
36          return this;
37      }
38  
39      public Session getDefaultInstance(Properties props, Authenticator authenticator)
40      {
41          wrapped = javax.mail.Session.getDefaultInstance(props, authenticator);
42          return getInstance(props, authenticator);
43      }
44  
45      public Session getDefaultInstance(Properties props)
46      {
47          wrapped = javax.mail.Session.getDefaultInstance(props);
48          return getInstance(props);
49      }
50  
51      public void setExpectedDebug(boolean aDebug)
52      {
53          myDebug.setActual(aDebug);
54      }
55  
56      public void setDebug(boolean aDebug)
57      {
58          myDebug.setActual(aDebug);
59      }
60  
61      public boolean getDebug()
62      {
63          notImplemented();
64          return false;
65      }
66  
67      public Provider getProviders()[]
68      {
69          notImplemented();
70          return null;
71      }
72  
73      public Provider getProvider(String name)
74      {
75          notImplemented();
76          return null;
77      }
78  
79      public void setProvider(Provider provider)
80      {
81          notImplemented();
82      }
83  
84      public Transport getTransport()
85      {
86          if (myTransport == null)
87              myTransport = new MockTransport();
88          return myTransport;
89      }
90  
91      public void setupGetTransport(Transport aTransport)
92      {
93          myTransport = aTransport;
94      }
95  
96      public Transport getTransport(String aTransportName)
97      {
98          if ("smtp".equals(aTransportName))
99              return getTransport();
100         else
101             return null;
102     }
103 
104     public Transport getTransport(Address address)
105     {
106         notImplemented();
107         return null;
108     }
109 
110     public Transport getTransport(Provider provider)
111     {
112         notImplemented();
113         return null;
114     }
115 
116     public Transport getTransport(URLName url)
117     {
118         notImplemented();
119         return null;
120     }
121 
122     public Store getStore()
123     {
124         notImplemented();
125         return null;
126     }
127 
128     public Store getStore(String name)
129     {
130         notImplemented();
131         return null;
132     }
133 
134     public Store getStore(URLName url)
135     {
136         notImplemented();
137         return null;
138     }
139 
140     public Store getStore(Provider provider)
141     {
142         notImplemented();
143         return null;
144     }
145 
146     public Folder getFolder()
147     {
148         notImplemented();
149         return null;
150     }
151 
152     public Folder getFolder(Store store)
153     {
154         notImplemented();
155         return null;
156     }
157 
158     public Folder getFolder(URLName url)
159     {
160         notImplemented();
161         return null;
162     }
163 
164     public void setPasswordAuthentication(URLName url, PasswordAuthentication
165             passwordAuthentication)
166     {
167         urlAuthenticators.put(url.getHost(), passwordAuthentication);
168     }
169 
170     public PasswordAuthentication getPasswordAuthentication(URLName url)
171     {
172         return (PasswordAuthentication) urlAuthenticators.get(url.getHost());
173     }
174 
175     public PasswordAuthentication requestPasswordAuthentication(java.net.InetAddress address, int port, String protocol, String prompt, String defaultUserName)
176     {
177         notImplemented();
178         return null;
179     }
180 
181     public Properties getProperties()
182     {
183         notImplemented();
184         return null;
185     }
186 
187     public String getProperty(String name)
188     {
189         notImplemented();
190         return null;
191     }
192 
193     public javax.mail.Session getWrappedSession()
194     {
195         return wrapped;
196     }
197 
198 }