Mercurial > hg4j
comparison test/org/tmatesoft/hg/test/TestNewlineFilter.java @ 355:f2c11fe7f3e9
Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 06 Dec 2011 12:57:21 +0100 |
parents | 0f3687e79f5a |
children |
comparison
equal
deleted
inserted
replaced
354:5f9073eabf06 | 355:f2c11fe7f3e9 |
---|---|
87 Assert.assertArrayEquals(crlf_1.getBytes(), result); | 87 Assert.assertArrayEquals(crlf_1.getBytes(), result); |
88 } | 88 } |
89 | 89 |
90 @Test | 90 @Test |
91 public void testStrictMixed_CRLF_2_LF() { | 91 public void testStrictMixed_CRLF_2_LF() { |
92 try { | 92 final byte[] input = crlf_2.getBytes(); |
93 byte[] result = apply(NewlineFilter.createWin2Nix(false), crlf_2.getBytes()); | 93 byte[] result = apply(NewlineFilter.createWin2Nix(false), input); |
94 Assert.fail("Shall fail when eol.only-consistent is true:" + new String(result)); | 94 Assert.assertArrayEquals(input, result); |
95 } catch (RuntimeException ex) { | |
96 // fine | |
97 } | |
98 } | 95 } |
99 | 96 |
100 @Test | 97 @Test |
101 public void testStrictMixed_LF_2_CRLF() { | 98 public void testStrictMixed_LF_2_CRLF() { |
102 try { | 99 final byte[] input = lf_2.getBytes(); |
103 byte[] result = apply(NewlineFilter.createNix2Win(false), lf_2.getBytes()); | 100 byte[] result = apply(NewlineFilter.createNix2Win(false), input); |
104 Assert.fail("Shall fail when eol.only-consistent is true:" + new String(result)); | 101 Assert.assertArrayEquals(input, result); |
105 } catch (RuntimeException ex) { | |
106 // fine | |
107 } | |
108 } | 102 } |
109 | 103 |
110 @Test | 104 @Test |
111 public void testBufferEndInTheMiddle_CRLF_2_LF() { | 105 public void testBufferEndInTheMiddle_CRLF_2_LF() { |
112 // filter works with ByteBuffer that may end with \r, and the next one starting with \n | 106 // filter works with ByteBuffer that may end with \r, and the next one starting with \n |
114 byte[] i1 = "\r\nA\r\nBC\r".getBytes(); | 108 byte[] i1 = "\r\nA\r\nBC\r".getBytes(); |
115 byte[] i2 = "\n\r\nDEF\r\n".getBytes(); | 109 byte[] i2 = "\n\r\nDEF\r\n".getBytes(); |
116 NewlineFilter nlFilter = NewlineFilter.createWin2Nix(false); | 110 NewlineFilter nlFilter = NewlineFilter.createWin2Nix(false); |
117 ByteBuffer input = ByteBuffer.allocate(i1.length + i2.length); | 111 ByteBuffer input = ByteBuffer.allocate(i1.length + i2.length); |
118 ByteBuffer res = ByteBuffer.allocate(i1.length + i2.length); // at most of the original size | 112 ByteBuffer res = ByteBuffer.allocate(i1.length + i2.length); // at most of the original size |
113 nlFilter.preview(ByteBuffer.wrap(i1)); | |
114 nlFilter.preview(ByteBuffer.wrap(i2)); | |
115 // | |
119 input.put(i1).flip(); | 116 input.put(i1).flip(); |
120 res.put(nlFilter.filter(input)); | 117 res.put(nlFilter.filter(input)); |
121 Assert.assertTrue("Unpocessed chars shall be left in input buffer", input.remaining() > 0); | 118 Assert.assertTrue("Unpocessed chars shall be left in input buffer", input.remaining() > 0); |
122 input.compact(); | 119 input.compact(); |
123 input.put(i2); | 120 input.put(i2); |
134 // check the same, with extra \r at the end of first portion | 131 // check the same, with extra \r at the end of first portion |
135 nlFilter = NewlineFilter.createWin2Nix(false); | 132 nlFilter = NewlineFilter.createWin2Nix(false); |
136 res.clear(); | 133 res.clear(); |
137 input.clear(); | 134 input.clear(); |
138 input.put(i1).put("\r\r\r".getBytes()).flip(); | 135 input.put(i1).put("\r\r\r".getBytes()).flip(); |
136 // preview requred | |
137 nlFilter.preview(input); | |
138 nlFilter.preview(ByteBuffer.wrap(i2)); | |
139 // input.position(0); correctly written preview shall not affect buffer position | |
140 // | |
139 res.put(nlFilter.filter(input)); | 141 res.put(nlFilter.filter(input)); |
140 Assert.assertTrue("Unpocessed chars shall be left in input buffer", input.remaining() > 0); | 142 Assert.assertTrue("Unpocessed chars shall be left in input buffer", input.remaining() > 0); |
141 input.compact(); | 143 input.compact(); |
142 input.put(i2); | 144 input.put(i2); |
143 input.flip(); | 145 input.flip(); |
177 byte[] result = apply(NewlineFilter.createNix2Win(false), input); | 179 byte[] result = apply(NewlineFilter.createNix2Win(false), input); |
178 Assert.assertArrayEquals(input, result); | 180 Assert.assertArrayEquals(input, result); |
179 } | 181 } |
180 | 182 |
181 private static byte[] apply(NewlineFilter nlFilter, byte[] input) { | 183 private static byte[] apply(NewlineFilter nlFilter, byte[] input) { |
182 ByteBuffer result = nlFilter.filter(ByteBuffer.wrap(input)); | 184 final ByteBuffer inputBuffer = ByteBuffer.wrap(input); |
185 nlFilter.preview(inputBuffer); | |
186 // inputBuffer.position(0); correctly written filter shall not affect buffer position | |
187 ByteBuffer result = nlFilter.filter(inputBuffer); | |
183 byte[] res = new byte[result.remaining()]; | 188 byte[] res = new byte[result.remaining()]; |
184 result.get(res); | 189 result.get(res); |
185 return res; | 190 return res; |
186 } | 191 } |
187 | 192 |