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.

ObjectIdTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk>
  4. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertTrue;
  49. import java.util.Locale;
  50. import org.eclipse.jgit.errors.InvalidObjectIdException;
  51. import org.junit.Test;
  52. public class ObjectIdTest {
  53. @Test
  54. public void test001_toString() {
  55. final String x = "def4c620bc3713bb1bb26b808ec9312548e73946";
  56. final ObjectId oid = ObjectId.fromString(x);
  57. assertEquals(x, oid.name());
  58. }
  59. @Test
  60. public void test002_toString() {
  61. final String x = "ff00eedd003713bb1bb26b808ec9312548e73946";
  62. final ObjectId oid = ObjectId.fromString(x);
  63. assertEquals(x, oid.name());
  64. }
  65. @Test
  66. public void test003_equals() {
  67. final String x = "def4c620bc3713bb1bb26b808ec9312548e73946";
  68. final ObjectId a = ObjectId.fromString(x);
  69. final ObjectId b = ObjectId.fromString(x);
  70. assertEquals(a.hashCode(), b.hashCode());
  71. assertEquals("a and b are same", b, a);
  72. }
  73. @Test
  74. public void test004_isId() {
  75. assertTrue("valid id", ObjectId
  76. .isId("def4c620bc3713bb1bb26b808ec9312548e73946"));
  77. }
  78. @Test
  79. public void test005_notIsId() {
  80. assertFalse("bob is not an id", ObjectId.isId("bob"));
  81. }
  82. @Test
  83. public void test006_notIsId() {
  84. assertFalse("39 digits is not an id", ObjectId
  85. .isId("def4c620bc3713bb1bb26b808ec9312548e7394"));
  86. }
  87. @Test
  88. public void test007_isId() {
  89. assertTrue("uppercase is accepted", ObjectId
  90. .isId("Def4c620bc3713bb1bb26b808ec9312548e73946"));
  91. }
  92. @Test
  93. public void test008_notIsId() {
  94. assertFalse("g is not a valid hex digit", ObjectId
  95. .isId("gef4c620bc3713bb1bb26b808ec9312548e73946"));
  96. }
  97. @Test
  98. public void test009_toString() {
  99. final String x = "ff00eedd003713bb1bb26b808ec9312548e73946";
  100. final ObjectId oid = ObjectId.fromString(x);
  101. assertEquals(x, ObjectId.toString(oid));
  102. }
  103. @Test
  104. public void test010_toString() {
  105. final String x = "0000000000000000000000000000000000000000";
  106. assertEquals(x, ObjectId.toString(null));
  107. }
  108. @Test
  109. public void test011_toString() {
  110. final String x = "0123456789ABCDEFabcdef1234567890abcdefAB";
  111. final ObjectId oid = ObjectId.fromString(x);
  112. assertEquals(x.toLowerCase(Locale.ROOT), oid.name());
  113. }
  114. @Test(expected = InvalidObjectIdException.class)
  115. public void testFromString_short() {
  116. ObjectId.fromString("cafe1234");
  117. }
  118. @Test(expected = InvalidObjectIdException.class)
  119. public void testFromString_nonHex() {
  120. ObjectId.fromString("0123456789abcdefghij0123456789abcdefghij");
  121. }
  122. @Test(expected = InvalidObjectIdException.class)
  123. public void testFromString_shortNonHex() {
  124. ObjectId.fromString("6789ghij");
  125. }
  126. @Test
  127. public void testGetByte() {
  128. byte[] raw = new byte[20];
  129. for (int i = 0; i < 20; i++)
  130. raw[i] = (byte) (0xa0 + i);
  131. ObjectId id = ObjectId.fromRaw(raw);
  132. assertEquals(raw[0] & 0xff, id.getFirstByte());
  133. assertEquals(raw[0] & 0xff, id.getByte(0));
  134. assertEquals(raw[1] & 0xff, id.getByte(1));
  135. for (int i = 2; i < 20; i++)
  136. assertEquals("index " + i, raw[i] & 0xff, id.getByte(i));
  137. }
  138. @Test
  139. public void testSetByte() {
  140. byte[] exp = new byte[20];
  141. for (int i = 0; i < 20; i++)
  142. exp[i] = (byte) (0xa0 + i);
  143. MutableObjectId id = new MutableObjectId();
  144. id.fromRaw(exp);
  145. assertEquals(ObjectId.fromRaw(exp).name(), id.name());
  146. id.setByte(0, 0x10);
  147. assertEquals(0x10, id.getByte(0));
  148. exp[0] = 0x10;
  149. assertEquals(ObjectId.fromRaw(exp).name(), id.name());
  150. for (int p = 1; p < 20; p++) {
  151. id.setByte(p, 0x10 + p);
  152. assertEquals(0x10 + p, id.getByte(p));
  153. exp[p] = (byte) (0x10 + p);
  154. assertEquals(ObjectId.fromRaw(exp).name(), id.name());
  155. }
  156. for (int p = 0; p < 20; p++) {
  157. id.setByte(p, 0x80 + p);
  158. assertEquals(0x80 + p, id.getByte(p));
  159. exp[p] = (byte) (0x80 + p);
  160. assertEquals(ObjectId.fromRaw(exp).name(), id.name());
  161. }
  162. }
  163. }