Mercurial > jhg
changeset 352:7b34d24b8f4d
Tests for newline filter (eol extension) functionality
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 30 Nov 2011 05:11:07 +0100 |
parents | 5abba41751e6 |
children | 0f3687e79f5a |
files | src/org/tmatesoft/hg/internal/NewlineFilter.java test/org/tmatesoft/hg/test/TestNewlineFilter.java |
diffstat | 2 files changed, 150 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/NewlineFilter.java Wed Nov 30 04:39:50 2011 +0100 +++ b/src/org/tmatesoft/hg/internal/NewlineFilter.java Wed Nov 30 05:11:07 2011 +0100 @@ -21,8 +21,6 @@ import static org.tmatesoft.hg.internal.KeywordFilter.copySlice; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -44,6 +42,15 @@ private final boolean allowInconsistent; private final boolean winToNix; + // next two factory methods for testsing purposes + public static NewlineFilter createWin2Nix(boolean allowMixed) { + return new NewlineFilter(!allowMixed, 0); + } + + public static NewlineFilter createNix2Win(boolean allowMixed) { + return new NewlineFilter(!allowMixed, 1); + } + private NewlineFilter(boolean failIfInconsistent, int transform) { winToNix = transform == 0; allowInconsistent = !failIfInconsistent; @@ -250,25 +257,4 @@ return null; } } - - public static void main(String[] args) throws Exception { - FileInputStream fis = new FileInputStream(new File("/temp/design.lf.txt")); - FileOutputStream fos = new FileOutputStream(new File("/temp/design.newline.out")); - ByteBuffer b = ByteBuffer.allocate(12); - NewlineFilter nlFilter = new NewlineFilter(true, 1); - while (fis.getChannel().read(b) != -1) { - b.flip(); // get ready to be read - ByteBuffer f = nlFilter.filter(b); - fos.getChannel().write(f); // XXX in fact, f may not be fully consumed - if (b.hasRemaining()) { - b.compact(); - } else { - b.clear(); - } - } - fis.close(); - fos.flush(); - fos.close(); - } - }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/test/TestNewlineFilter.java Wed Nov 30 05:11:07 2011 +0100 @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2011 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@hg4j.com + */ +package org.tmatesoft.hg.test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.nio.ByteBuffer; + +import org.junit.Assert; +import org.junit.Test; +import org.tmatesoft.hg.internal.NewlineFilter; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class TestNewlineFilter { + + public static void main(String[] args) throws Exception { + FileInputStream fis = new FileInputStream(new File("/temp/design.lf.txt")); + FileOutputStream fos = new FileOutputStream(new File("/temp/design.newline.out")); + ByteBuffer b = ByteBuffer.allocate(12); + NewlineFilter nlFilter = NewlineFilter.createNix2Win(true); + while (fis.getChannel().read(b) != -1) { + b.flip(); // get ready to be read + ByteBuffer f = nlFilter.filter(b); + fos.getChannel().write(f); // XXX in fact, f may not be fully consumed + if (b.hasRemaining()) { + b.compact(); + } else { + b.clear(); + } + } + fis.close(); + fos.flush(); + fos.close(); + } + + // pure + private final String crlf_1 = "\r\nA\r\nBC\r\n\r\nDEF\r\n"; + private final String lf_1 = "\nA\nBC\n\nDEF\n"; + // mixed + private final String crlf_2 = "\r\nA\r\nBC\n\nDEF\r\n"; + private final String lf_2 = "\nA\nBC\r\n\r\nDEF\n"; + + @Test + public void testPure_CRLF_2_LF() { + final byte[] input = crlf_1.getBytes(); + byte[] result = apply(NewlineFilter.createWin2Nix(false), input); + Assert.assertArrayEquals(lf_1.getBytes(), result); + } + + @Test + public void testPure_LF_2_CRLF() { + final byte[] input = lf_1.getBytes(); + byte[] result = apply(NewlineFilter.createNix2Win(false), input); + Assert.assertArrayEquals(crlf_1.getBytes(), result); + } + + @Test + public void testRelaxedMixed_CRLF_2_LF() { + // mixed \n and \r\n to uniform \n + byte[] result = apply(NewlineFilter.createWin2Nix(true), crlf_2.getBytes()); + Assert.assertArrayEquals(lf_1.getBytes(), result); + } + + @Test + public void testRelaxedMixed_LF_2_CRLF() { + // mixed \n and \r\n to uniform \r\n + byte[] result = apply(NewlineFilter.createNix2Win(true), lf_2.getBytes()); + Assert.assertArrayEquals(crlf_1.getBytes(), result); + } + + @Test + public void testStrictMixed_CRLF_2_LF() { + try { + byte[] result = apply(NewlineFilter.createWin2Nix(false), crlf_2.getBytes()); + Assert.fail("Shall fail when eol.only-consistent is true:" + new String(result)); + } catch (RuntimeException ex) { + // fine + } + } + + @Test + public void testStrictMixed_LF_2_CRLF() { + try { + byte[] result = apply(NewlineFilter.createNix2Win(false), lf_2.getBytes()); + Assert.fail("Shall fail when eol.only-consistent is true:" + new String(result)); + } catch (RuntimeException ex) { + // fine + } + } + + + @Test + public void testNoConversionNeeded_LF_2_LF() { + final byte[] input = lf_1.getBytes(); + Assert.assertTrue("sanity", indexOf(input, '\r') == -1); + byte[] result = apply(NewlineFilter.createWin2Nix(false), input); + Assert.assertArrayEquals(input, result); + } + + @Test + public void testNoConversionNeeded_CRLF_2_CRLF() { + final byte[] input = crlf_1.getBytes(); + byte[] result = apply(NewlineFilter.createNix2Win(false), input); + Assert.assertArrayEquals(input, result); + } + + private static byte[] apply(NewlineFilter nlFilter, byte[] input) { + ByteBuffer result = nlFilter.filter(ByteBuffer.wrap(input)); + byte[] res = new byte[result.remaining()]; + result.get(res); + return res; + } + + private static int indexOf(byte[] arr, int val) { + for (int i = 0; i < arr.length; i++) { + if (arr[i] == val) { + return i; + } + } + return -1; + } +}