[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 java.io.ByteArrayInputStream; |
---|
| 23 | import java.io.IOException; |
---|
| 24 | |
---|
| 25 | import org.apache.james.mime4j.io.LineNumberInputStream; |
---|
| 26 | |
---|
| 27 | import junit.framework.TestCase; |
---|
| 28 | |
---|
| 29 | public class LineNumberInputStreamTest extends TestCase { |
---|
| 30 | /** |
---|
| 31 | * Tests that reading single bytes updates the line number appropriately. |
---|
| 32 | */ |
---|
| 33 | public void testReadSingleByte() throws IOException { |
---|
| 34 | String s = "Yada\r\nyada\r\nyada\r\n"; |
---|
| 35 | LineNumberInputStream is = new LineNumberInputStream( |
---|
| 36 | new ByteArrayInputStream(s.getBytes())); |
---|
| 37 | |
---|
| 38 | for (int i = 0; i < 6; i++) { |
---|
| 39 | assertEquals(1, is.getLineNumber()); |
---|
| 40 | is.read(); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | for (int i = 6; i < 12; i++) { |
---|
| 44 | assertEquals(2, is.getLineNumber()); |
---|
| 45 | is.read(); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | for (int i = 12; i < 18; i++) { |
---|
| 49 | assertEquals(3, is.getLineNumber()); |
---|
| 50 | is.read(); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | assertEquals(4, is.getLineNumber()); |
---|
| 54 | assertEquals(-1, is.read()); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * Tests that reading multiple bytes at once updates the line number |
---|
| 59 | * appropriately. |
---|
| 60 | */ |
---|
| 61 | public void testReadManyBytes() throws IOException { |
---|
| 62 | String s = "Yada\r\nyada\r\nyada\r\n"; |
---|
| 63 | LineNumberInputStream is = new LineNumberInputStream( |
---|
| 64 | new ByteArrayInputStream(s.getBytes())); |
---|
| 65 | |
---|
| 66 | byte[] buf = new byte[4]; |
---|
| 67 | assertEquals(1, is.getLineNumber()); |
---|
| 68 | is.read(buf); |
---|
| 69 | assertEquals(1, is.getLineNumber()); |
---|
| 70 | is.read(buf); |
---|
| 71 | assertEquals(2, is.getLineNumber()); |
---|
| 72 | is.read(buf); |
---|
| 73 | assertEquals(3, is.getLineNumber()); |
---|
| 74 | is.read(buf); |
---|
| 75 | assertEquals(3, is.getLineNumber()); |
---|
| 76 | is.read(buf); |
---|
| 77 | assertEquals(4, is.getLineNumber()); |
---|
| 78 | |
---|
| 79 | assertEquals(-1, is.read()); |
---|
| 80 | } |
---|
| 81 | } |
---|