1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mailboxmanager.torque;
21  
22  import junit.framework.TestCase;
23  
24  public class MessageUtilsNormalisedWriteToTest extends TestCase {
25  
26      StringBuffer buffer;
27  
28      protected void setUp() throws Exception {
29          super.setUp();
30          buffer = new StringBuffer();
31      }
32  
33      protected void tearDown() throws Exception {
34          super.tearDown();
35      }
36  
37      public void testEmpty() throws Exception {
38          MessageUtils.normalisedWriteTo("".getBytes(), buffer);
39          assertEquals("Check processing of empty array", "", buffer.toString());
40      }
41  
42      public void testNormal() throws Exception {
43          MessageUtils.normalisedWriteTo("One\r\nTwo\r\nThree\r\n".getBytes(),
44                  buffer);
45          assertEquals("Check processing of normal data",
46                  "One\r\nTwo\r\nThree\r\n", buffer.toString());
47      }
48  
49      public void testMissing() throws Exception {
50          MessageUtils
51                  .normalisedWriteTo("One\rTwo\nThree\r\n".getBytes(), buffer);
52          assertEquals("Check processing simple data containing unnormal lines",
53                  "One\r\nTwo\r\nThree\r\n", buffer.toString());
54      }
55  
56      public void testCRAtEnd() throws Exception {
57          MessageUtils.normalisedWriteTo("One\r\nTwo\r\nThree\r".getBytes(),
58                  buffer);
59          assertEquals("CR at end", "One\r\nTwo\r\nThree\r\n", buffer.toString());
60      }
61  
62      public void testLFAtEnd() throws Exception {
63          MessageUtils.normalisedWriteTo("One\r\nTwo\r\nThree\n".getBytes(),
64                  buffer);
65          assertEquals("LF at end", "One\r\nTwo\r\nThree\r\n", buffer.toString());
66      }
67  
68      public void testCRAtStart() throws Exception {
69          MessageUtils.normalisedWriteTo("\rOne\r\nTwo\r\nThree\r".getBytes(),
70                  buffer);
71          assertEquals("CR at start", "\r\nOne\r\nTwo\r\nThree\r\n", buffer
72                  .toString());
73      }
74  
75      public void testLFAtStart() throws Exception {
76          MessageUtils.normalisedWriteTo("\nOne\r\nTwo\r\nThree".getBytes(),
77                  buffer);
78          assertEquals("CR at start", "\r\nOne\r\nTwo\r\nThree", buffer
79                  .toString());
80      }
81  
82      public void testSwitchOrder() throws Exception {
83          MessageUtils.normalisedWriteTo(
84                  "\n\rOne\n\rTwo\n\rThree\n\r".getBytes(), buffer);
85          assertEquals("Check processing simple data containing unnormal lines",
86                  "\r\n\r\nOne\r\n\r\nTwo\r\n\r\nThree\r\n\r\n", buffer
87                          .toString());
88      }
89  }