1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.imap;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.stream.Stream;
25
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.MethodSource;
29
30 public class IMAPTest {
31
32 private static Stream<String> mailboxNamesToBeQuoted() {
33 return Stream.of(
34 "",
35 " ",
36 "\"",
37 "\"\"",
38 "\\/ ",
39 "Hello\", ",
40 "\" World!",
41 "Hello\",\" World!"
42 );
43 }
44
45 @ParameterizedTest(name = "String `{0}` should be quoted")
46 @MethodSource("mailboxNamesToBeQuoted")
47 public void quoteMailboxName(final String input) {
48 final String quotedMailboxName = IMAP.quoteMailboxName(input);
49 assertTrue(quotedMailboxName.startsWith("\""), "quoted string should start with quotation mark");
50 assertTrue(quotedMailboxName.endsWith("\""), "quoted string should end with quotation mark");
51 }
52
53 @Test
54 public void testCheckGenerator() {
55
56
57
58 final IMAP imap = new IMAP();
59 final String initial = imap.generateCommandID();
60 int expected = 1;
61 for (int j = 0; j < initial.length(); j++) {
62 expected *= 26;
63 }
64 int i = 0;
65 boolean matched = false;
66 while (i <= expected + 10) {
67 i++;
68 final String s = imap.generateCommandID();
69 matched = initial.equals(s);
70 if (matched) {
71 break;
72 }
73 }
74 assertEquals(expected, i);
75 assertTrue(matched, "Expected to see the original value again");
76 }
77
78 @Test
79 public void testConstructDefaultIMAP() {
80 final IMAP imap = new IMAP();
81 assertEquals(IMAP.DEFAULT_PORT, imap.getDefaultPort());
82 assertEquals(IMAP.IMAPState.DISCONNECTED_STATE, imap.getState());
83 assertEquals(0, imap.getReplyStrings().length);
84 }
85
86 @Test
87 public void testQuoteMailboxNameNullInput() {
88 assertNull(IMAP.quoteMailboxName(null));
89 }
90
91 @Test
92 public void testQuoteMailboxNoQuotingIfNoSpacePresent() {
93 final String stringToQuote = "Foobar\"";
94 assertEquals(stringToQuote, IMAP.quoteMailboxName(stringToQuote));
95 }
96
97 @Test
98 public void testTrueChunkListener() {
99 assertTrue(IMAP.TRUE_CHUNK_LISTENER.chunkReceived(new IMAP()));
100 }
101
102 }