1+ /**
2+ * Copyright (c) 2011-2012 Optimax Software Ltd.
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * * Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * * Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ * * Neither the name of Optimax Software, ElasticInbox, nor the names
14+ * of its contributors may be used to endorse or promote products derived
15+ * from this software without specific prior written permission.
16+ *
17+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+ */
28+
29+ package com .elasticinbox .itests ;
30+
31+ import static org .ops4j .pax .exam .CoreOptions .*;
32+
33+ import static org .hamcrest .MatcherAssert .assertThat ;
34+ import static org .hamcrest .Matchers .*;
35+
36+ import java .io .IOException ;
37+ import java .util .HashMap ;
38+ import java .util .Map ;
39+ import java .util .UUID ;
40+
41+ import org .apache .commons .net .pop3 .POP3Client ;
42+ import org .apache .commons .net .pop3 .POP3MessageInfo ;
43+ import org .junit .Test ;
44+ import org .junit .runner .RunWith ;
45+ import org .ops4j .pax .exam .Option ;
46+ import org .ops4j .pax .exam .junit .Configuration ;
47+ import org .ops4j .pax .exam .junit .ExamReactorStrategy ;
48+ import org .ops4j .pax .exam .junit .JUnit4TestRunner ;
49+ import org .ops4j .pax .exam .spi .reactors .EagerSingleStagedReactorFactory ;
50+
51+ import com .elasticinbox .core .model .ReservedLabels ;
52+ import com .elasticinbox .core .utils .Base64UUIDUtils ;
53+ import com .google .common .collect .ObjectArrays ;
54+
55+ /**
56+ * Integration test for POP3
57+ *
58+ * @author Rustam Aliyev
59+ */
60+ @ RunWith (JUnit4TestRunner .class )
61+ @ ExamReactorStrategy (EagerSingleStagedReactorFactory .class )
62+ public class Pop3IT extends AbstractIntegrationTest
63+ {
64+ private final static int POP3_PORT = 2110 ;
65+ private final static String POP3_HOST = "localhost" ;
66+
67+ /**
68+ * Append POP3 Specific config options
69+ *
70+ * @return
71+ */
72+ @ Configuration ()
73+ public Option [] config ()
74+ {
75+ return ObjectArrays .concat (super .config (), options (
76+ // POP3 Test Bundles
77+ mavenBundle ().groupId ("commons-net" ).artifactId ("commons-net" ).versionAsInProject (),
78+ mavenBundle ().groupId ("org.apache.james.protocols" ).artifactId ("protocols-netty" ).versionAsInProject (),
79+ mavenBundle ().groupId ("org.apache.james.protocols" ).artifactId ("protocols-api" ).versionAsInProject (),
80+ mavenBundle ().groupId ("org.apache.james.protocols" ).artifactId ("protocols-pop3" ).versionAsInProject (),
81+ mavenBundle ().groupId ("io.netty" ).artifactId ("netty" ).versionAsInProject (),
82+
83+ // ElasticInbox Bundles
84+ scanDir ("../modules/pop3/target/" )
85+ ), Option .class );
86+ }
87+
88+ @ Test
89+ public void testListUidl () throws IOException
90+ {
91+ initAccount ();
92+
93+ // load messages with POP3 label
94+ long mailSizeRegular = getResourceSize (EMAIL_REGULAR );
95+ long mailSizeAttach = getResourceSize (EMAIL_LARGE_ATT );
96+
97+ Map <String , UUID > messages = new HashMap <String , UUID >(2 );
98+ Integer labelId = ReservedLabels .POP3 .getId ();
99+
100+ messages .put ("headers" , RestV2IT .addMessage (EMAIL_REGULAR , labelId ));
101+ messages .put ("attach" , RestV2IT .addMessage (EMAIL_LARGE_ATT , labelId ));
102+
103+ // initialize POP3 client
104+ POP3Client client = new POP3Client ();
105+ client .connect (POP3_HOST , POP3_PORT );
106+
107+ boolean loginSuccess = client .login (TEST_ACCOUNT , "valid" );
108+ assertThat (loginSuccess , is (true ));
109+
110+ // LIST all messages
111+ POP3MessageInfo [] info = client .listMessages ();
112+ assertThat (info .length , equalTo (2 ));
113+ assertThat ((int ) mailSizeAttach , equalTo (info [0 ].size ));
114+ assertThat ((int ) mailSizeRegular , equalTo (info [1 ].size ));
115+ assertThat (info [0 ].number , equalTo (1 ));
116+ assertThat (info [1 ].number , equalTo (2 ));
117+
118+ // LIST one message
119+ POP3MessageInfo msgInfo = client .listMessage (1 );
120+ assertThat ((int ) mailSizeAttach , equalTo (msgInfo .size ));
121+ assertThat (msgInfo .number , equalTo (1 ));
122+
123+ // LIST message that does not exist
124+ msgInfo = client .listMessage (10 );
125+ assertThat (msgInfo , nullValue ());
126+
127+ // UIDL all messages
128+ info = client .listUniqueIdentifiers ();
129+ assertThat (info .length , equalTo (2 ));
130+ assertThat (info [0 ].identifier ,
131+ equalTo (Base64UUIDUtils .encode (messages .get ("attach" ))));
132+ assertThat (info [1 ].identifier ,
133+ equalTo (Base64UUIDUtils .encode (messages .get ("headers" ))));
134+ assertThat (info [0 ].number , equalTo (1 ));
135+ assertThat (info [1 ].number , equalTo (2 ));
136+
137+ // UIDL one message
138+ msgInfo = client .listUniqueIdentifier (1 );
139+ assertThat (msgInfo .identifier ,
140+ equalTo (Base64UUIDUtils .encode (messages .get ("attach" ))));
141+ assertThat (msgInfo .number , equalTo (1 ));
142+
143+ // UIDL message that does not exist
144+ msgInfo = client .listUniqueIdentifier (10 );
145+ assertThat (msgInfo , nullValue ());
146+
147+ boolean logoutSuccess = client .logout ();
148+ assertThat (logoutSuccess , is (true ));
149+ }
150+
151+ @ Test
152+ public void testDele () throws IOException
153+ {
154+ initAccount ();
155+
156+ Map <String , UUID > messages = new HashMap <String , UUID >(2 );
157+ Integer labelId = ReservedLabels .POP3 .getId ();
158+
159+ messages .put ("headers" , RestV2IT .addMessage (EMAIL_REGULAR , labelId ));
160+ messages .put ("attach" , RestV2IT .addMessage (EMAIL_LARGE_ATT , labelId ));
161+
162+ // initialize POP3 client
163+ POP3Client client = new POP3Client ();
164+ client .connect (POP3_HOST , POP3_PORT );
165+
166+ // Login
167+ boolean loginSuccess = client .login (TEST_ACCOUNT , "valid" );
168+ assertThat (loginSuccess , is (true ));
169+
170+ // LIST all messages
171+ POP3MessageInfo [] info = client .listMessages ();
172+ assertThat (info .length , equalTo (2 ));
173+
174+ // DELE message 1
175+ boolean deleteResult = client .deleteMessage (1 );
176+ assertThat (deleteResult , is (true ));
177+
178+ // LIST remaining
179+ info = client .listMessages ();
180+ assertThat (info .length , equalTo (1 ));
181+
182+ // DELE message 1 again
183+ deleteResult = client .deleteMessage (1 );
184+ assertThat (deleteResult , is (false ));
185+
186+ // LIST remaining
187+ info = client .listMessages ();
188+ assertThat (info .length , equalTo (1 ));
189+
190+ // DELE message 2
191+ deleteResult = client .deleteMessage (2 );
192+ assertThat (deleteResult , is (true ));
193+
194+ info = client .listMessages ();
195+ assertThat (info .length , equalTo (0 ));
196+
197+ // QUIT so the messages get expunged
198+ boolean logoutSuccess = client .logout ();
199+ assertThat (logoutSuccess , is (true ));
200+
201+ // reconnect
202+ client .connect (POP3_HOST , POP3_PORT );
203+
204+ // Login
205+ loginSuccess = client .login (TEST_ACCOUNT , "valid" );
206+ assertThat (loginSuccess , is (true ));
207+
208+ info = client .listMessages ();
209+ assertThat (info .length , equalTo (0 ));
210+
211+ logoutSuccess = client .logout ();
212+ assertThat (logoutSuccess , is (true ));
213+ }
214+
215+ @ Test
216+ public void testRset () throws IOException
217+ {
218+ initAccount ();
219+
220+ Integer labelId = ReservedLabels .POP3 .getId ();
221+ RestV2IT .addMessage (EMAIL_REGULAR , labelId );
222+
223+ // initialize POP3 client
224+ POP3Client client = new POP3Client ();
225+ client .connect (POP3_HOST , POP3_PORT );
226+
227+ // Login
228+ boolean loginSuccess = client .login (TEST_ACCOUNT , "valid" );
229+ assertThat (loginSuccess , is (true ));
230+
231+ // LIST all messages
232+ POP3MessageInfo [] info = client .listMessages ();
233+ assertThat (info .length , equalTo (1 ));
234+
235+ // DELE message 1
236+ boolean deleteResult = client .deleteMessage (1 );
237+ assertThat (deleteResult , is (true ));
238+
239+ // LIST remaining
240+ info = client .listMessages ();
241+ assertThat (info .length , equalTo (0 ));
242+
243+ // RSET. After this the deleted mark should be removed again
244+ boolean resetRestult = client .reset ();
245+ assertThat (resetRestult , is (true ));
246+
247+ // LIST all messages
248+ info = client .listMessages ();
249+ assertThat (info .length , equalTo (1 ));
250+
251+ // Logout
252+ boolean logoutSuccess = client .logout ();
253+ assertThat (logoutSuccess , is (true ));
254+ }
255+ }
0 commit comments