NIFI-769: - Add test for and fix bug in ByteCountingInputStream reset method.

Signed-off-by: Mark Payne <markap14@hotmail.com>
This commit is contained in:
Joe Skora 2015-07-17 00:31:21 -04:00 committed by Mark Payne
parent a19390cdec
commit dcd9739bc3
2 changed files with 56 additions and 0 deletions

View File

@ -100,6 +100,7 @@ public class ByteCountingInputStream extends InputStream {
public void reset() throws IOException {
in.reset();
bytesRead -= bytesSinceMark;
bytesSinceMark = 0L;
}
@Override

View File

@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.stream.io;
import junit.framework.TestCase;
public class ByteCountingInputStreamTest extends TestCase {
final ByteArrayInputStream reader = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes());
public void testReset() throws Exception {
final ByteArrayInputStream reader = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes());
final ByteCountingInputStream bcis = new ByteCountingInputStream(reader);
int tmp;
/* verify first 2 bytes */
tmp = bcis.read();
assertEquals(tmp, 97);
tmp = bcis.read();
assertEquals(tmp, 98);
/* save bytes read and place mark */
final long bytesAtMark = bcis.getBytesRead();
bcis.mark(0);
/* verify next 2 bytes */
tmp = bcis.read();
assertEquals(tmp, 99);
tmp = bcis.read();
assertEquals(tmp, 100);
/* verify reset returns to position when mark was placed */
bcis.reset();
assertEquals(bytesAtMark, bcis.getBytesRead());
/* verify that the reset bug has been fixed (bug would reduce bytes read count) */
bcis.reset();
assertEquals(bytesAtMark, bcis.getBytesRead());
}
}