[6785] | 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.mime4j.io; |
---|
| 21 | |
---|
| 22 | import org.apache.james.mime4j.io.LineReaderInputStreamAdaptor; |
---|
| 23 | import org.apache.james.mime4j.io.MaxLineLimitException; |
---|
| 24 | import org.apache.james.mime4j.util.ByteArrayBuffer; |
---|
| 25 | |
---|
| 26 | import java.io.ByteArrayInputStream; |
---|
| 27 | import java.io.ByteArrayOutputStream; |
---|
| 28 | |
---|
| 29 | import junit.framework.TestCase; |
---|
| 30 | |
---|
| 31 | public class LineReaderInputStreamAdaptorTest extends TestCase { |
---|
| 32 | |
---|
| 33 | public void testBasicOperations() throws Exception { |
---|
| 34 | String text = "ah blahblah"; |
---|
| 35 | byte[] b1 = text.getBytes("US-ASCII"); |
---|
| 36 | |
---|
| 37 | LineReaderInputStreamAdaptor instream = new LineReaderInputStreamAdaptor( |
---|
| 38 | new ByteArrayInputStream(b1)); |
---|
| 39 | |
---|
| 40 | assertEquals((byte)'a', instream.read()); |
---|
| 41 | assertEquals((byte)'h', instream.read()); |
---|
| 42 | assertEquals((byte)' ', instream.read()); |
---|
| 43 | |
---|
| 44 | byte[] tmp1 = new byte[4]; |
---|
| 45 | assertEquals(4, instream.read(tmp1)); |
---|
| 46 | assertEquals(4, instream.read(tmp1)); |
---|
| 47 | |
---|
| 48 | assertEquals(-1, instream.read(tmp1)); |
---|
| 49 | assertEquals(-1, instream.read(tmp1)); |
---|
| 50 | assertEquals(-1, instream.read()); |
---|
| 51 | assertEquals(-1, instream.read()); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public void testBasicReadLine() throws Exception { |
---|
| 55 | |
---|
| 56 | String[] teststrs = new String[5]; |
---|
| 57 | teststrs[0] = "Hello\r\n"; |
---|
| 58 | teststrs[1] = "This string should be much longer than the size of the input buffer " + |
---|
| 59 | "which is only 16 bytes for this test\r\n"; |
---|
| 60 | StringBuilder sb = new StringBuilder(); |
---|
| 61 | for (int i = 0; i < 15; i++) { |
---|
| 62 | sb.append("123456789 "); |
---|
| 63 | } |
---|
| 64 | sb.append("and stuff like that\r\n"); |
---|
| 65 | teststrs[2] = sb.toString(); |
---|
| 66 | teststrs[3] = "\r\n"; |
---|
| 67 | teststrs[4] = "And goodbye\r\n"; |
---|
| 68 | |
---|
| 69 | ByteArrayOutputStream outstream = new ByteArrayOutputStream(); |
---|
| 70 | |
---|
| 71 | for (String teststr : teststrs) { |
---|
| 72 | outstream.write(teststr.getBytes("US-ASCII")); |
---|
| 73 | } |
---|
| 74 | byte[] raw = outstream.toByteArray(); |
---|
| 75 | |
---|
| 76 | LineReaderInputStreamAdaptor instream = new LineReaderInputStreamAdaptor( |
---|
| 77 | new ByteArrayInputStream(raw)); |
---|
| 78 | |
---|
| 79 | ByteArrayBuffer linebuf = new ByteArrayBuffer(8); |
---|
| 80 | for (String teststr : teststrs) { |
---|
| 81 | linebuf.clear(); |
---|
| 82 | instream.readLine(linebuf); |
---|
| 83 | String s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 84 | assertEquals(teststr, s); |
---|
| 85 | } |
---|
| 86 | assertEquals(-1, instream.readLine(linebuf)); |
---|
| 87 | assertEquals(-1, instream.readLine(linebuf)); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | public void testReadEmptyLine() throws Exception { |
---|
| 91 | |
---|
| 92 | String teststr = "\n\n\r\n\r\r\n\n\n\n\n\n"; |
---|
| 93 | byte[] raw = teststr.getBytes("US-ASCII"); |
---|
| 94 | |
---|
| 95 | LineReaderInputStreamAdaptor instream = new LineReaderInputStreamAdaptor( |
---|
| 96 | new ByteArrayInputStream(raw)); |
---|
| 97 | |
---|
| 98 | ByteArrayBuffer linebuf = new ByteArrayBuffer(8); |
---|
| 99 | linebuf.clear(); |
---|
| 100 | instream.readLine(linebuf); |
---|
| 101 | String s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 102 | assertEquals("\n", s); |
---|
| 103 | |
---|
| 104 | linebuf.clear(); |
---|
| 105 | instream.readLine(linebuf); |
---|
| 106 | s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 107 | assertEquals("\n", s); |
---|
| 108 | |
---|
| 109 | linebuf.clear(); |
---|
| 110 | instream.readLine(linebuf); |
---|
| 111 | s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 112 | assertEquals("\r\n", s); |
---|
| 113 | |
---|
| 114 | linebuf.clear(); |
---|
| 115 | instream.readLine(linebuf); |
---|
| 116 | s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 117 | assertEquals("\r\r\n", s); |
---|
| 118 | |
---|
| 119 | linebuf.clear(); |
---|
| 120 | instream.readLine(linebuf); |
---|
| 121 | s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 122 | assertEquals("\n", s); |
---|
| 123 | |
---|
| 124 | linebuf.clear(); |
---|
| 125 | instream.readLine(linebuf); |
---|
| 126 | s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 127 | assertEquals("\n", s); |
---|
| 128 | |
---|
| 129 | linebuf.clear(); |
---|
| 130 | instream.readLine(linebuf); |
---|
| 131 | s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 132 | assertEquals("\n", s); |
---|
| 133 | |
---|
| 134 | linebuf.clear(); |
---|
| 135 | instream.readLine(linebuf); |
---|
| 136 | s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 137 | assertEquals("\n", s); |
---|
| 138 | |
---|
| 139 | linebuf.clear(); |
---|
| 140 | instream.readLine(linebuf); |
---|
| 141 | s = new String(linebuf.toByteArray(), "US-ASCII"); |
---|
| 142 | assertEquals("\n", s); |
---|
| 143 | |
---|
| 144 | assertEquals(-1, instream.readLine(linebuf)); |
---|
| 145 | assertEquals(-1, instream.readLine(linebuf)); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | public void testReadEmptyLineMaxLimit() throws Exception { |
---|
| 149 | |
---|
| 150 | String teststr = "1234567890\r\n"; |
---|
| 151 | byte[] raw = teststr.getBytes("US-ASCII"); |
---|
| 152 | |
---|
| 153 | LineReaderInputStreamAdaptor instream1 = new LineReaderInputStreamAdaptor( |
---|
| 154 | new ByteArrayInputStream(raw), 13); |
---|
| 155 | ByteArrayBuffer linebuf = new ByteArrayBuffer(8); |
---|
| 156 | linebuf.clear(); |
---|
| 157 | instream1.readLine(linebuf); |
---|
| 158 | |
---|
| 159 | LineReaderInputStreamAdaptor instream2 = new LineReaderInputStreamAdaptor( |
---|
| 160 | new ByteArrayInputStream(raw), 12); |
---|
| 161 | linebuf.clear(); |
---|
| 162 | try { |
---|
| 163 | instream2.readLine(linebuf); |
---|
| 164 | fail("MaxLineLimitException should have been thrown"); |
---|
| 165 | } catch (MaxLineLimitException ex) { |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | } |
---|