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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.lib;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertFalse;
  15. import static org.junit.Assert.assertTrue;
  16. import java.util.Locale;
  17. import org.eclipse.jgit.errors.InvalidObjectIdException;
  18. import org.junit.Test;
  19. public class ObjectIdTest {
  20. @Test
  21. public void test001_toString() {
  22. final String x = "def4c620bc3713bb1bb26b808ec9312548e73946";
  23. final ObjectId oid = ObjectId.fromString(x);
  24. assertEquals(x, oid.name());
  25. }
  26. @Test
  27. public void test002_toString() {
  28. final String x = "ff00eedd003713bb1bb26b808ec9312548e73946";
  29. final ObjectId oid = ObjectId.fromString(x);
  30. assertEquals(x, oid.name());
  31. }
  32. @Test
  33. public void test003_equals() {
  34. final String x = "def4c620bc3713bb1bb26b808ec9312548e73946";
  35. final ObjectId a = ObjectId.fromString(x);
  36. final ObjectId b = ObjectId.fromString(x);
  37. assertEquals(a.hashCode(), b.hashCode());
  38. assertEquals("a and b are same", b, a);
  39. }
  40. @Test
  41. public void test004_isId() {
  42. assertTrue("valid id", ObjectId
  43. .isId("def4c620bc3713bb1bb26b808ec9312548e73946"));
  44. }
  45. @Test
  46. public void test005_notIsId() {
  47. assertFalse("bob is not an id", ObjectId.isId("bob"));
  48. }
  49. @Test
  50. public void test006_notIsId() {
  51. assertFalse("39 digits is not an id", ObjectId
  52. .isId("def4c620bc3713bb1bb26b808ec9312548e7394"));
  53. }
  54. @Test
  55. public void test007_isId() {
  56. assertTrue("uppercase is accepted", ObjectId
  57. .isId("Def4c620bc3713bb1bb26b808ec9312548e73946"));
  58. }
  59. @Test
  60. public void test008_notIsId() {
  61. assertFalse("g is not a valid hex digit", ObjectId
  62. .isId("gef4c620bc3713bb1bb26b808ec9312548e73946"));
  63. }
  64. @Test
  65. public void test009_toString() {
  66. final String x = "ff00eedd003713bb1bb26b808ec9312548e73946";
  67. final ObjectId oid = ObjectId.fromString(x);
  68. assertEquals(x, ObjectId.toString(oid));
  69. }
  70. @Test
  71. public void test010_toString() {
  72. final String x = "0000000000000000000000000000000000000000";
  73. assertEquals(x, ObjectId.toString(null));
  74. }
  75. @Test
  76. public void test011_toString() {
  77. final String x = "0123456789ABCDEFabcdef1234567890abcdefAB";
  78. final ObjectId oid = ObjectId.fromString(x);
  79. assertEquals(x.toLowerCase(Locale.ROOT), oid.name());
  80. }
  81. @Test(expected = InvalidObjectIdException.class)
  82. public void testFromString_short() {
  83. ObjectId.fromString("cafe1234");
  84. }
  85. @Test(expected = InvalidObjectIdException.class)
  86. public void testFromString_nonHex() {
  87. ObjectId.fromString("0123456789abcdefghij0123456789abcdefghij");
  88. }
  89. @Test(expected = InvalidObjectIdException.class)
  90. public void testFromString_shortNonHex() {
  91. ObjectId.fromString("6789ghij");
  92. }
  93. @Test
  94. public void testGetByte() {
  95. byte[] raw = new byte[20];
  96. for (int i = 0; i < 20; i++)
  97. raw[i] = (byte) (0xa0 + i);
  98. ObjectId id = ObjectId.fromRaw(raw);
  99. assertEquals(raw[0] & 0xff, id.getFirstByte());
  100. assertEquals(raw[0] & 0xff, id.getByte(0));
  101. assertEquals(raw[1] & 0xff, id.getByte(1));
  102. for (int i = 2; i < 20; i++)
  103. assertEquals("index " + i, raw[i] & 0xff, id.getByte(i));
  104. }
  105. @Test
  106. public void testSetByte() {
  107. byte[] exp = new byte[20];
  108. for (int i = 0; i < 20; i++)
  109. exp[i] = (byte) (0xa0 + i);
  110. MutableObjectId id = new MutableObjectId();
  111. id.fromRaw(exp);
  112. assertEquals(ObjectId.fromRaw(exp).name(), id.name());
  113. id.setByte(0, 0x10);
  114. assertEquals(0x10, id.getByte(0));
  115. exp[0] = 0x10;
  116. assertEquals(ObjectId.fromRaw(exp).name(), id.name());
  117. for (int p = 1; p < 20; p++) {
  118. id.setByte(p, 0x10 + p);
  119. assertEquals(0x10 + p, id.getByte(p));
  120. exp[p] = (byte) (0x10 + p);
  121. assertEquals(ObjectId.fromRaw(exp).name(), id.name());
  122. }
  123. for (int p = 0; p < 20; p++) {
  124. id.setByte(p, 0x80 + p);
  125. assertEquals(0x80 + p, id.getByte(p));
  126. exp[p] = (byte) (0x80 + p);
  127. assertEquals(ObjectId.fromRaw(exp).name(), id.name());
  128. }
  129. }
  130. }