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