View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // This test assumes:
56          // - 26 letters in the generator alphabet
57          // - the generator uses a fixed size tag
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; // letters in alphabet
63          }
64          int i = 0;
65          boolean matched = false;
66          while (i <= expected + 10) { // don't loop forever, but allow it to pass go!
67              i++;
68              final String s = imap.generateCommandID();
69              matched = initial.equals(s);
70              if (matched) { // we've wrapped around completely
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 }