comparison test/org/tmatesoft/hg/test/TestNewlineFilter.java @ 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
children 0f3687e79f5a
comparison
equal deleted inserted replaced
351:5abba41751e6 352:7b34d24b8f4d
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.test;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.nio.ByteBuffer;
23
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.tmatesoft.hg.internal.NewlineFilter;
27
28 /**
29 *
30 * @author Artem Tikhomirov
31 * @author TMate Software Ltd.
32 */
33 public class TestNewlineFilter {
34
35 public static void main(String[] args) throws Exception {
36 FileInputStream fis = new FileInputStream(new File("/temp/design.lf.txt"));
37 FileOutputStream fos = new FileOutputStream(new File("/temp/design.newline.out"));
38 ByteBuffer b = ByteBuffer.allocate(12);
39 NewlineFilter nlFilter = NewlineFilter.createNix2Win(true);
40 while (fis.getChannel().read(b) != -1) {
41 b.flip(); // get ready to be read
42 ByteBuffer f = nlFilter.filter(b);
43 fos.getChannel().write(f); // XXX in fact, f may not be fully consumed
44 if (b.hasRemaining()) {
45 b.compact();
46 } else {
47 b.clear();
48 }
49 }
50 fis.close();
51 fos.flush();
52 fos.close();
53 }
54
55 // pure
56 private final String crlf_1 = "\r\nA\r\nBC\r\n\r\nDEF\r\n";
57 private final String lf_1 = "\nA\nBC\n\nDEF\n";
58 // mixed
59 private final String crlf_2 = "\r\nA\r\nBC\n\nDEF\r\n";
60 private final String lf_2 = "\nA\nBC\r\n\r\nDEF\n";
61
62 @Test
63 public void testPure_CRLF_2_LF() {
64 final byte[] input = crlf_1.getBytes();
65 byte[] result = apply(NewlineFilter.createWin2Nix(false), input);
66 Assert.assertArrayEquals(lf_1.getBytes(), result);
67 }
68
69 @Test
70 public void testPure_LF_2_CRLF() {
71 final byte[] input = lf_1.getBytes();
72 byte[] result = apply(NewlineFilter.createNix2Win(false), input);
73 Assert.assertArrayEquals(crlf_1.getBytes(), result);
74 }
75
76 @Test
77 public void testRelaxedMixed_CRLF_2_LF() {
78 // mixed \n and \r\n to uniform \n
79 byte[] result = apply(NewlineFilter.createWin2Nix(true), crlf_2.getBytes());
80 Assert.assertArrayEquals(lf_1.getBytes(), result);
81 }
82
83 @Test
84 public void testRelaxedMixed_LF_2_CRLF() {
85 // mixed \n and \r\n to uniform \r\n
86 byte[] result = apply(NewlineFilter.createNix2Win(true), lf_2.getBytes());
87 Assert.assertArrayEquals(crlf_1.getBytes(), result);
88 }
89
90 @Test
91 public void testStrictMixed_CRLF_2_LF() {
92 try {
93 byte[] result = apply(NewlineFilter.createWin2Nix(false), crlf_2.getBytes());
94 Assert.fail("Shall fail when eol.only-consistent is true:" + new String(result));
95 } catch (RuntimeException ex) {
96 // fine
97 }
98 }
99
100 @Test
101 public void testStrictMixed_LF_2_CRLF() {
102 try {
103 byte[] result = apply(NewlineFilter.createNix2Win(false), lf_2.getBytes());
104 Assert.fail("Shall fail when eol.only-consistent is true:" + new String(result));
105 } catch (RuntimeException ex) {
106 // fine
107 }
108 }
109
110
111 @Test
112 public void testNoConversionNeeded_LF_2_LF() {
113 final byte[] input = lf_1.getBytes();
114 Assert.assertTrue("sanity", indexOf(input, '\r') == -1);
115 byte[] result = apply(NewlineFilter.createWin2Nix(false), input);
116 Assert.assertArrayEquals(input, result);
117 }
118
119 @Test
120 public void testNoConversionNeeded_CRLF_2_CRLF() {
121 final byte[] input = crlf_1.getBytes();
122 byte[] result = apply(NewlineFilter.createNix2Win(false), input);
123 Assert.assertArrayEquals(input, result);
124 }
125
126 private static byte[] apply(NewlineFilter nlFilter, byte[] input) {
127 ByteBuffer result = nlFilter.filter(ByteBuffer.wrap(input));
128 byte[] res = new byte[result.remaining()];
129 result.get(res);
130 return res;
131 }
132
133 private static int indexOf(byte[] arr, int val) {
134 for (int i = 0; i < arr.length; i++) {
135 if (arr[i] == val) {
136 return i;
137 }
138 }
139 return -1;
140 }
141 }