Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PacketLineOutTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.IOException;
  46. import java.io.OutputStream;
  47. import junit.framework.TestCase;
  48. import org.eclipse.jgit.lib.Constants;
  49. // Note, test vectors created with:
  50. //
  51. // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
  52. public class PacketLineOutTest extends TestCase {
  53. private ByteArrayOutputStream rawOut;
  54. private PacketLineOut out;
  55. protected void setUp() throws Exception {
  56. super.setUp();
  57. rawOut = new ByteArrayOutputStream();
  58. out = new PacketLineOut(rawOut);
  59. }
  60. // writeString
  61. public void testWriteString1() throws IOException {
  62. out.writeString("a");
  63. out.writeString("bc");
  64. assertBuffer("0005a0006bc");
  65. }
  66. public void testWriteString2() throws IOException {
  67. out.writeString("a\n");
  68. out.writeString("bc\n");
  69. assertBuffer("0006a\n0007bc\n");
  70. }
  71. public void testWriteString3() throws IOException {
  72. out.writeString("");
  73. assertBuffer("0004");
  74. }
  75. // end
  76. public void testWriteEnd() throws IOException {
  77. final int[] flushCnt = new int[1];
  78. final OutputStream mockout = new OutputStream() {
  79. @Override
  80. public void write(int arg0) throws IOException {
  81. rawOut.write(arg0);
  82. }
  83. @Override
  84. public void flush() throws IOException {
  85. flushCnt[0]++;
  86. }
  87. };
  88. new PacketLineOut(mockout).end();
  89. assertBuffer("0000");
  90. assertEquals(1, flushCnt[0]);
  91. }
  92. // writePacket
  93. public void testWritePacket1() throws IOException {
  94. out.writePacket(new byte[] { 'a' });
  95. assertBuffer("0005a");
  96. }
  97. public void testWritePacket2() throws IOException {
  98. out.writePacket(new byte[] { 'a', 'b', 'c', 'd' });
  99. assertBuffer("0008abcd");
  100. }
  101. public void testWritePacket3() throws IOException {
  102. final int buflen = SideBandOutputStream.MAX_BUF - 5;
  103. final byte[] buf = new byte[buflen];
  104. for (int i = 0; i < buf.length; i++) {
  105. buf[i] = (byte) i;
  106. }
  107. out.writePacket(buf);
  108. out.flush();
  109. final byte[] act = rawOut.toByteArray();
  110. final String explen = Integer.toString(buf.length + 4, 16);
  111. assertEquals(4 + buf.length, act.length);
  112. assertEquals(new String(act, 0, 4, "UTF-8"), explen);
  113. for (int i = 0, j = 4; i < buf.length; i++, j++) {
  114. assertEquals(buf[i], act[j]);
  115. }
  116. }
  117. // flush
  118. public void testFlush() throws IOException {
  119. final int[] flushCnt = new int[1];
  120. final OutputStream mockout = new OutputStream() {
  121. @Override
  122. public void write(int arg0) throws IOException {
  123. fail("should not write");
  124. }
  125. @Override
  126. public void flush() throws IOException {
  127. flushCnt[0]++;
  128. }
  129. };
  130. new PacketLineOut(mockout).flush();
  131. assertEquals(1, flushCnt[0]);
  132. }
  133. private void assertBuffer(final String exp) throws IOException {
  134. assertEquals(exp, new String(rawOut.toByteArray(),
  135. Constants.CHARACTER_ENCODING));
  136. }
  137. }