Improved test coverage
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1300516 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
637426c50d
commit
d2c8f90800
|
@ -50,12 +50,7 @@ class UnicodeUnescapeReader extends Reader {
|
|||
|
||||
if (c == '\\') {
|
||||
int l = reader.read(sequence);
|
||||
if (l == sequence.length
|
||||
&& 'u' == sequence[0]
|
||||
&& isHexadecimal(sequence[1])
|
||||
&& isHexadecimal(sequence[2])
|
||||
&& isHexadecimal(sequence[3])
|
||||
&& isHexadecimal(sequence[4])) {
|
||||
if (l == sequence.length && isUnicodeSequence(sequence)) {
|
||||
// unicode escape found
|
||||
c = Integer.parseInt(new String(sequence, 1, 4), 16);
|
||||
|
||||
|
@ -71,7 +66,15 @@ class UnicodeUnescapeReader extends Reader {
|
|||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
private boolean isUnicodeSequence(char[] sequence) {
|
||||
return 'u' == sequence[0]
|
||||
&& isHexadecimal(sequence[1])
|
||||
&& isHexadecimal(sequence[2])
|
||||
&& isHexadecimal(sequence[3])
|
||||
&& isHexadecimal(sequence[4]);
|
||||
}
|
||||
|
||||
private boolean isHexadecimal(char c) {
|
||||
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
|
||||
}
|
||||
|
|
|
@ -164,4 +164,16 @@ public class CharBufferTest extends TestCase {
|
|||
buffer.ensureCapacity(8);
|
||||
assertTrue(buffer.capacity() >= 8);
|
||||
}
|
||||
|
||||
public void testTrim() throws Exception {
|
||||
CharBuffer buffer = new CharBuffer();
|
||||
buffer.append(" ");
|
||||
buffer.trimTrailingWhitespace();
|
||||
assertEquals(0, buffer.length());
|
||||
|
||||
buffer = new CharBuffer();
|
||||
buffer.append("Hello World! ");
|
||||
buffer.trimTrailingWhitespace();
|
||||
assertEquals("Hello World!", buffer.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
@ -21,15 +22,19 @@ import java.util.Arrays;
|
|||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* ExtendedBufferedReaderTest
|
||||
*/
|
||||
public class ExtendedBufferedReaderTest extends TestCase {
|
||||
|
||||
public void testReadLookahead1() throws Exception {
|
||||
public void testEmptyInput() throws Exception {
|
||||
ExtendedBufferedReader br = getBufferedReader("");
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
|
||||
assertNull(br.readLine());
|
||||
assertEquals(0, br.read(new char[10], 0, 0));
|
||||
}
|
||||
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, getEBR("").read());
|
||||
ExtendedBufferedReader br = getEBR("1\n2\r3\n");
|
||||
public void testReadLookahead1() throws Exception {
|
||||
ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n");
|
||||
assertEquals('1', br.lookAhead());
|
||||
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
|
||||
assertEquals('1', br.read());
|
||||
|
@ -82,12 +87,8 @@ public class ExtendedBufferedReaderTest extends TestCase {
|
|||
public void testReadLookahead2() throws Exception {
|
||||
char[] ref = new char[5];
|
||||
char[] res = new char[5];
|
||||
|
||||
ExtendedBufferedReader br = getEBR("");
|
||||
assertEquals(0, br.read(res, 0, 0));
|
||||
assertTrue(Arrays.equals(res, ref));
|
||||
|
||||
br = getEBR("abcdefg");
|
||||
|
||||
ExtendedBufferedReader br = getBufferedReader("abcdefg");
|
||||
ref[0] = 'a';
|
||||
ref[1] = 'b';
|
||||
ref[2] = 'c';
|
||||
|
@ -100,18 +101,17 @@ public class ExtendedBufferedReaderTest extends TestCase {
|
|||
assertEquals(1, br.read(res, 4, 1));
|
||||
assertTrue(Arrays.equals(res, ref));
|
||||
assertEquals('d', br.readAgain());
|
||||
|
||||
}
|
||||
|
||||
public void testReadLine() throws Exception {
|
||||
ExtendedBufferedReader br = getEBR("");
|
||||
ExtendedBufferedReader br = getBufferedReader("");
|
||||
assertTrue(br.readLine() == null);
|
||||
|
||||
br = getEBR("\n");
|
||||
br = getBufferedReader("\n");
|
||||
assertTrue(br.readLine().equals(""));
|
||||
assertTrue(br.readLine() == null);
|
||||
|
||||
br = getEBR("foo\n\nhello");
|
||||
br = getBufferedReader("foo\n\nhello");
|
||||
assertEquals(0, br.getLineNumber());
|
||||
assertTrue(br.readLine().equals("foo"));
|
||||
assertEquals(1, br.getLineNumber());
|
||||
|
@ -122,7 +122,7 @@ public class ExtendedBufferedReaderTest extends TestCase {
|
|||
assertTrue(br.readLine() == null);
|
||||
assertEquals(3, br.getLineNumber());
|
||||
|
||||
br = getEBR("foo\n\nhello");
|
||||
br = getBufferedReader("foo\n\nhello");
|
||||
assertEquals('f', br.read());
|
||||
assertEquals('o', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("oo"));
|
||||
|
@ -136,7 +136,7 @@ public class ExtendedBufferedReaderTest extends TestCase {
|
|||
assertEquals(3, br.getLineNumber());
|
||||
|
||||
|
||||
br = getEBR("foo\rbaar\r\nfoo");
|
||||
br = getBufferedReader("foo\rbaar\r\nfoo");
|
||||
assertTrue(br.readLine().equals("foo"));
|
||||
assertEquals('b', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("baar"));
|
||||
|
@ -145,7 +145,7 @@ public class ExtendedBufferedReaderTest extends TestCase {
|
|||
assertTrue(br.readLine() == null);
|
||||
}
|
||||
|
||||
private ExtendedBufferedReader getEBR(String s) {
|
||||
private ExtendedBufferedReader getBufferedReader(String s) {
|
||||
return new ExtendedBufferedReader(new StringReader(s));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue