You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoCRLFOutputStreamTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2011, 2013 Robin Rosenberg
  3. * Copyright (C) 2013 Robin Stocker
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.util.io;
  45. import java.io.ByteArrayInputStream;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import org.junit.Assert;
  51. import org.junit.Test;
  52. public class AutoCRLFOutputStreamTest {
  53. @Test
  54. public void test() throws IOException {
  55. assertNoCrLf("", "");
  56. assertNoCrLf("\r", "\r");
  57. assertNoCrLf("\r\n", "\n");
  58. assertNoCrLf("\r\n", "\r\n");
  59. assertNoCrLf("\r\r", "\r\r");
  60. assertNoCrLf("\r\n\r", "\n\r");
  61. assertNoCrLf("\r\n\r\r", "\r\n\r\r");
  62. assertNoCrLf("\r\n\r\n", "\r\n\r\n");
  63. assertNoCrLf("\r\n\r\n\r", "\n\r\n\r");
  64. assertNoCrLf("\0\n", "\0\n");
  65. }
  66. @Test
  67. public void testBoundary() throws IOException {
  68. for (int i = AutoCRLFOutputStream.BUFFER_SIZE - 10; i < AutoCRLFOutputStream.BUFFER_SIZE + 10; i++) {
  69. String s1 = repeat("a", i);
  70. assertNoCrLf(s1, s1);
  71. String s2 = repeat("\0", i);
  72. assertNoCrLf(s2, s2);
  73. }
  74. }
  75. public static String repeat(String input, int size) {
  76. StringBuilder sb = new StringBuilder(input.length() * size);
  77. for (int i = 0; i < size; i++)
  78. sb.append(input);
  79. String s = sb.toString();
  80. return s;
  81. }
  82. private void assertNoCrLf(String string, String string2) throws IOException {
  83. assertNoCrLfHelper(string, string2);
  84. // \u00e5 = LATIN SMALL LETTER A WITH RING ABOVE
  85. // the byte value is negative
  86. assertNoCrLfHelper("\u00e5" + string, "\u00e5" + string2);
  87. assertNoCrLfHelper("\u00e5" + string + "\u00e5", "\u00e5" + string2
  88. + "\u00e5");
  89. assertNoCrLfHelper(string + "\u00e5", string2 + "\u00e5");
  90. }
  91. private void assertNoCrLfHelper(String expect, String input)
  92. throws IOException {
  93. byte[] inbytes = input.getBytes();
  94. byte[] expectBytes = expect.getBytes();
  95. for (int i = -4; i < 5; ++i) {
  96. int size = Math.abs(i);
  97. byte[] buf = new byte[size];
  98. InputStream in = new ByteArrayInputStream(inbytes);
  99. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  100. OutputStream out = new AutoCRLFOutputStream(bos);
  101. if (i > 0) {
  102. int n;
  103. while ((n = in.read(buf)) >= 0) {
  104. out.write(buf, 0, n);
  105. }
  106. } else if (i < 0) {
  107. int n;
  108. while ((n = in.read(buf)) >= 0) {
  109. byte[] b = new byte[n];
  110. System.arraycopy(buf, 0, b, 0, n);
  111. out.write(b);
  112. }
  113. } else {
  114. int c;
  115. while ((c = in.read()) != -1)
  116. out.write(c);
  117. }
  118. out.flush();
  119. in.close();
  120. out.close();
  121. byte[] actualBytes = bos.toByteArray();
  122. Assert.assertEquals("bufsize=" + size, encode(expectBytes),
  123. encode(actualBytes));
  124. }
  125. }
  126. String encode(byte[] in) {
  127. StringBuilder str = new StringBuilder();
  128. for (byte b : in) {
  129. if (b < 32)
  130. str.append(0xFF & b);
  131. else {
  132. str.append("'");
  133. str.append((char) b);
  134. str.append("'");
  135. }
  136. str.append(' ');
  137. }
  138. return str.toString();
  139. }
  140. }