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    *   http://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.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          // This test assumes:
49          // - 26 letters in the generator alphabet
50          // - the generator uses a fixed size tag
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; // letters in alphabet
56          }
57          int i = 0;
58          boolean matched = false;
59          while (i <= expected + 10) { // don't loop forever, but allow it to pass go!
60              i++;
61              final String s = imap.generateCommandID();
62              matched = initial.equals(s);
63              if (matched) { // we've wrapped around completely
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 }